1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Services/Credit/ApplyNumber.php
2024-04-12 14:15:41 +10:00

69 lines
1.5 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Credit;
use App\Models\Client;
use App\Models\Credit;
use App\Services\AbstractService;
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Database\QueryException;
class ApplyNumber extends AbstractService
{
use GeneratesCounter;
private $client;
private $credit;
private bool $completed = true;
public function __construct(Client $client, Credit $credit)
{
$this->client = $client;
$this->credit = $credit;
}
public function run()
{
if ($this->credit->number != '') {
return $this->credit;
}
$this->trySaving();
// $this->credit->number = $this->getNextCreditNumber($this->client, $this->credit);
return $this->credit;
}
private function trySaving()
{
$x = 1;
do {
try {
$this->credit->number = $this->getNextCreditNumber($this->client, $this->credit);
$this->credit->saveQuietly();
$this->completed = false;
} catch (QueryException $e) {
$x++;
if ($x > 50) {
$this->completed = false;
}
}
} while ($this->completed);
}
}