2020-02-11 21:57:25 +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
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. 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-11 21:57:25 +01:00
|
|
|
namespace App\Services\Quote;
|
|
|
|
|
|
|
|
use App\Models\Quote;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2022-04-07 09:40:59 +02:00
|
|
|
use Illuminate\Database\QueryException;
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
class ApplyNumber
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
|
|
|
|
|
|
|
private $client;
|
|
|
|
|
2022-04-07 09:40:59 +02:00
|
|
|
private bool $completed = true;
|
|
|
|
|
2020-02-11 21:57:25 +01:00
|
|
|
public function __construct($client)
|
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
}
|
|
|
|
|
2020-02-14 04:32:22 +01:00
|
|
|
public function run($quote)
|
2020-02-11 21:57:25 +01:00
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($quote->number != '') {
|
2020-02-11 21:57:25 +01:00
|
|
|
return $quote;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-11 21:57:25 +01:00
|
|
|
|
|
|
|
switch ($this->client->getSetting('counter_number_applied')) {
|
|
|
|
case 'when_saved':
|
2022-04-07 09:40:59 +02:00
|
|
|
$quote = $this->trySaving($quote);
|
2022-05-12 05:57:41 +02:00
|
|
|
// $quote->number = $this->getNextQuoteNumber($this->client, $quote);
|
2020-02-11 21:57:25 +01:00
|
|
|
break;
|
|
|
|
case 'when_sent':
|
|
|
|
if ($quote->status_id == Quote::STATUS_SENT) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$quote = $this->trySaving($quote);
|
2022-04-07 09:40:59 +02:00
|
|
|
// $quote->number = $this->getNextQuoteNumber($this->client, $quote);
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-09-06 11:38:10 +02:00
|
|
|
// code...
|
2020-02-11 21:57:25 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $quote;
|
|
|
|
}
|
2022-04-07 09:40:59 +02:00
|
|
|
|
|
|
|
private function trySaving($quote)
|
|
|
|
{
|
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
|
|
|
$quote->number = $this->getNextQuoteNumber($this->client, $quote);
|
|
|
|
$quote->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-06-21 11:57:17 +02:00
|
|
|
if ($x > 10) {
|
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
|
|
|
|
|
|
|
return $quote;
|
|
|
|
}
|
2020-02-11 21:57:25 +01:00
|
|
|
}
|