2020-02-15 10:01:15 +01:00
|
|
|
<?php
|
2020-07-01 03:06:40 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
|
|
|
* @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)
|
2020-07-01 03:06:40 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-07-01 03:06:40 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
namespace App\Services\Credit;
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
use App\Models\Client;
|
2020-09-06 11:38:10 +02:00
|
|
|
use App\Models\Credit;
|
2020-02-17 10:37:44 +01:00
|
|
|
use App\Services\AbstractService;
|
2020-02-17 20:07:31 +01:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2022-04-07 09:40:59 +02:00
|
|
|
use Illuminate\Database\QueryException;
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
class ApplyNumber extends AbstractService
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
|
|
|
|
2020-02-18 21:56:21 +01:00
|
|
|
private $client;
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2020-02-18 21:56:21 +01:00
|
|
|
private $credit;
|
|
|
|
|
2022-04-07 09:40:59 +02:00
|
|
|
private bool $completed = true;
|
|
|
|
|
2020-02-18 21:56:21 +01:00
|
|
|
public function __construct(Client $client, Credit $credit)
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
2020-02-17 10:37:44 +01:00
|
|
|
$this->client = $client;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-18 21:56:21 +01:00
|
|
|
$this->credit = $credit;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
|
2020-02-18 21:56:21 +01:00
|
|
|
public function run()
|
2020-02-15 10:01:15 +01:00
|
|
|
{
|
2020-02-18 21:56:21 +01:00
|
|
|
if ($this->credit->number != '') {
|
|
|
|
return $this->credit;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
|
|
|
|
2022-04-07 09:40:59 +02:00
|
|
|
$this->trySaving();
|
|
|
|
// $this->credit->number = $this->getNextCreditNumber($this->client, $this->credit);
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2020-02-18 21:56:21 +01:00
|
|
|
return $this->credit;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
2022-04-07 09:40:59 +02:00
|
|
|
|
|
|
|
private function trySaving()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$x = 1;
|
2022-04-07 09:40:59 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
do {
|
|
|
|
try {
|
2022-04-07 09:40:59 +02:00
|
|
|
$this->credit->number = $this->getNextCreditNumber($this->client, $this->credit);
|
|
|
|
$this->credit->saveQuietly();
|
|
|
|
|
|
|
|
$this->completed = false;
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (QueryException $e) {
|
2022-04-07 09:40:59 +02:00
|
|
|
$x++;
|
|
|
|
|
2022-09-07 06:15:27 +02:00
|
|
|
if ($x > 50) {
|
2022-04-07 09:40:59 +02:00
|
|
|
$this->completed = false;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-04-07 09:40:59 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
} while ($this->completed);
|
2022-04-07 09:40:59 +02:00
|
|
|
}
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|