1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Services/Credit/ApplyNumber.php
2022-09-07 14:15:27 +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) 2022. 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);
}
}