2023-11-24 06:10:10 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2023-11-24 06:10:10 +01:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Vendor;
|
|
|
|
|
|
|
|
use App\Models\Vendor;
|
2024-07-08 23:32:13 +02:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
|
|
|
use Illuminate\Database\QueryException;
|
2023-11-24 06:10:10 +01:00
|
|
|
|
|
|
|
class VendorService
|
|
|
|
{
|
2024-07-08 23:32:13 +02:00
|
|
|
use GeneratesCounter;
|
|
|
|
|
|
|
|
private bool $completed = true;
|
|
|
|
|
2023-11-24 06:10:10 +01:00
|
|
|
public function __construct(public Vendor $vendor)
|
|
|
|
{
|
|
|
|
}
|
2024-07-08 23:32:13 +02:00
|
|
|
|
|
|
|
public function applyNumber(): self
|
|
|
|
{
|
|
|
|
$x = 1;
|
|
|
|
|
|
|
|
if(isset($this->vendor->number)) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
try {
|
|
|
|
$this->vendor->number = $this->getNextVendorNumber($this->vendor);
|
|
|
|
$this->vendor->saveQuietly();
|
|
|
|
|
|
|
|
$this->completed = false;
|
|
|
|
} catch (QueryException $e) {
|
|
|
|
$x++;
|
|
|
|
|
|
|
|
if ($x > 50) {
|
|
|
|
$this->completed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while ($this->completed);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2024-10-04 07:40:55 +02:00
|
|
|
public function merge(Vendor $mergable_vendor)
|
|
|
|
{
|
|
|
|
$this->vendor = (new Merge($this->vendor, $mergable_vendor))->run();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2024-07-08 23:32:13 +02:00
|
|
|
/**
|
|
|
|
* Saves the vendor instance
|
|
|
|
*
|
|
|
|
* @return Vendor The Vendor Model
|
|
|
|
*/
|
|
|
|
public function save(): Vendor
|
|
|
|
{
|
|
|
|
$this->vendor->saveQuietly();
|
|
|
|
|
|
|
|
return $this->vendor->fresh();
|
|
|
|
}
|
2023-11-26 08:41:42 +01:00
|
|
|
}
|