2022-06-05 05:56:08 +02:00
|
|
|
<?php
|
2022-06-05 11:41:19 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2022-06-05 11:41:19 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
2022-06-05 05:56:08 +02:00
|
|
|
|
|
|
|
namespace App\Services\PurchaseOrder;
|
|
|
|
|
|
|
|
use App\Models\PurchaseOrder;
|
2022-06-21 11:57:17 +02:00
|
|
|
use App\Models\Vendor;
|
2022-06-05 05:56:08 +02:00
|
|
|
use App\Services\AbstractService;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
|
|
|
use Illuminate\Database\QueryException;
|
|
|
|
|
|
|
|
class ApplyNumber extends AbstractService
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
|
|
|
|
2022-06-05 11:41:19 +02:00
|
|
|
public Vendor $vendor;
|
2022-06-05 05:56:08 +02:00
|
|
|
|
|
|
|
private PurchaseOrder $purchase_order;
|
|
|
|
|
|
|
|
private bool $completed = true;
|
|
|
|
|
2022-06-05 11:41:19 +02:00
|
|
|
public function __construct(Vendor $vendor, PurchaseOrder $purchase_order)
|
2022-06-05 05:56:08 +02:00
|
|
|
{
|
2022-06-05 11:41:19 +02:00
|
|
|
$this->vendor = $vendor;
|
2022-06-05 05:56:08 +02:00
|
|
|
|
|
|
|
$this->purchase_order = $purchase_order;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if ($this->purchase_order->number != '') {
|
|
|
|
return $this->purchase_order;
|
|
|
|
}
|
|
|
|
|
2022-07-02 03:40:51 +02:00
|
|
|
switch ($this->vendor->company->getSetting('counter_number_applied')) {
|
|
|
|
case 'when_saved':
|
|
|
|
$this->trySaving();
|
|
|
|
break;
|
|
|
|
case 'when_sent':
|
2022-07-17 23:15:37 +02:00
|
|
|
if ($this->purchase_order->status_id == PurchaseOrder::STATUS_SENT) {
|
2022-07-02 03:40:51 +02:00
|
|
|
$this->trySaving();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-05 05:56:08 +02:00
|
|
|
|
|
|
|
return $this->purchase_order;
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-06-05 05:56:08 +02:00
|
|
|
private function trySaving()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$x = 1;
|
|
|
|
do {
|
|
|
|
try {
|
2022-06-05 11:41:19 +02:00
|
|
|
$this->purchase_order->number = $this->getNextPurchaseOrderNumber($this->purchase_order);
|
2022-06-05 05:56:08 +02:00
|
|
|
$this->purchase_order->saveQuietly();
|
|
|
|
$this->completed = false;
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (QueryException $e) {
|
2022-06-05 05:56:08 +02:00
|
|
|
$x++;
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($x > 10) {
|
2022-06-05 05:56:08 +02:00
|
|
|
$this->completed = false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-06-05 05:56:08 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
} while ($this->completed);
|
2022-06-05 05:56:08 +02:00
|
|
|
}
|
|
|
|
}
|