2020-08-12 00:17:32 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Payment Ninja (https://paymentninja.com).
|
2020-08-12 00:17:32 +02:00
|
|
|
*
|
2023-08-20 06:36:22 +02:00
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
2020-08-12 00:17:32 +02:00
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Payment Ninja LLC (https://paymentninja.com)
|
2020-08-12 00:17:32 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-08-12 00:17:32 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Payment;
|
|
|
|
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Services\AbstractService;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2022-04-07 09:40:59 +02:00
|
|
|
use Illuminate\Database\QueryException;
|
2020-08-12 00:17:32 +02:00
|
|
|
|
|
|
|
class ApplyNumber extends AbstractService
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
|
|
|
|
2022-04-07 09:40:59 +02:00
|
|
|
private bool $completed = true;
|
|
|
|
|
2023-06-08 09:11:19 +02:00
|
|
|
public function __construct(private Payment $payment)
|
2020-08-12 00:17:32 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if ($this->payment->number != '') {
|
|
|
|
return $this->payment;
|
|
|
|
}
|
|
|
|
|
2022-04-07 09:40:59 +02:00
|
|
|
$this->trySaving();
|
2020-08-12 00:17:32 +02:00
|
|
|
|
|
|
|
return $this->payment;
|
|
|
|
}
|
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 {
|
2023-06-08 09:11:19 +02:00
|
|
|
$this->payment->number = $this->getNextPaymentNumber($this->payment->client, $this->payment);
|
2022-04-07 09:40:59 +02:00
|
|
|
$this->payment->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-08-12 00:17:32 +02:00
|
|
|
}
|