diff --git a/app/Jobs/Quote/ApplyQuoteNumber.php b/app/Jobs/Quote/ApplyQuoteNumber.php deleted file mode 100644 index a046e5f62315..000000000000 --- a/app/Jobs/Quote/ApplyQuoteNumber.php +++ /dev/null @@ -1,85 +0,0 @@ -quote = $quote; - - $this->settings = $settings; - - $this->company = $company; - } - - /** - * Execute the job. - * - * - * @return Quote - */ - public function handle() - { - MultiDB::setDB($this->company->db); - - //return early - if ($this->quote->number != '') { - return $this->quote; - } - - switch ($this->settings->quote_number_applied) { - case 'when_saved': - $this->quote->number = $this->getNextQuoteNumber($this->quote->client, $this->quote); - break; - case 'when_sent': - if ($this->quote->status_id == Quote::STATUS_SENT) { - $this->quote->number = $this->getNextQuoteNumber($this->quote->client, $this->quote); - } - break; - - default: - // code... - break; - } - - $this->quote->save(); - - return $this->quote; - } -} diff --git a/app/Services/Credit/ApplyNumber.php b/app/Services/Credit/ApplyNumber.php index 8bb190952118..bfc474f7cd22 100644 --- a/app/Services/Credit/ApplyNumber.php +++ b/app/Services/Credit/ApplyNumber.php @@ -15,6 +15,7 @@ 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 { @@ -24,6 +25,8 @@ class ApplyNumber extends AbstractService private $credit; + private bool $completed = true; + public function __construct(Client $client, Credit $credit) { $this->client = $client; @@ -37,8 +40,40 @@ class ApplyNumber extends AbstractService return $this->credit; } - $this->credit->number = $this->getNextCreditNumber($this->client, $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>10) + $this->completed = false; + } + + } + while($this->completed); + + } + } diff --git a/app/Services/Invoice/ApplyNumber.php b/app/Services/Invoice/ApplyNumber.php index 786051fb18c8..333566a82dbb 100644 --- a/app/Services/Invoice/ApplyNumber.php +++ b/app/Services/Invoice/ApplyNumber.php @@ -15,6 +15,7 @@ use App\Models\Client; use App\Models\Invoice; use App\Services\AbstractService; use App\Utils\Traits\GeneratesCounter; +use Illuminate\Database\QueryException; class ApplyNumber extends AbstractService { @@ -24,6 +25,8 @@ class ApplyNumber extends AbstractService private $invoice; + private $completed = true; + public function __construct(Client $client, Invoice $invoice) { $this->client = $client; @@ -39,11 +42,13 @@ class ApplyNumber extends AbstractService switch ($this->client->getSetting('counter_number_applied')) { case 'when_saved': - $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice, $this->invoice->recurring_id); + $this->trySaving(); + // $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice, $this->invoice->recurring_id); break; case 'when_sent': if ($this->invoice->status_id == Invoice::STATUS_SENT) { - $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice, $this->invoice->recurring_id); + $this->trySaving(); + // $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice, $this->invoice->recurring_id); } break; @@ -53,4 +58,33 @@ class ApplyNumber extends AbstractService return $this->invoice; } + + private function trySaving() + { + + $x=1; + + do{ + + try{ + + $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice, $this->invoice->recurring_id); + $this->invoice->saveQuietly(); + + $this->completed = false; + + + } + catch(QueryException $e){ + + $x++; + + if($x>10) + $this->completed = false; + } + + } + while($this->completed); + + } } diff --git a/app/Services/Invoice/ApplyRecurringNumber.php b/app/Services/Invoice/ApplyRecurringNumber.php index 1c26e7f60ba1..c93817e6f3c3 100644 --- a/app/Services/Invoice/ApplyRecurringNumber.php +++ b/app/Services/Invoice/ApplyRecurringNumber.php @@ -15,6 +15,7 @@ use App\Models\Client; use App\Models\Invoice; use App\Services\AbstractService; use App\Utils\Traits\GeneratesCounter; +use Illuminate\Database\QueryException; class ApplyRecurringNumber extends AbstractService { @@ -24,6 +25,8 @@ class ApplyRecurringNumber extends AbstractService private $invoice; + private bool $completed = true; + public function __construct(Client $client, Invoice $invoice) { $this->client = $client; @@ -39,11 +42,13 @@ class ApplyRecurringNumber extends AbstractService switch ($this->client->getSetting('counter_number_applied')) { case 'when_saved': - $this->invoice->number = $this->getNextRecurringInvoiceNumber($this->client, $this->invoice); + $this->trySaving(); + //$this->invoice->number = $this->getNextRecurringInvoiceNumber($this->client, $this->invoice); break; case 'when_sent': if ($this->invoice->status_id == Invoice::STATUS_SENT) { - $this->invoice->number = $this->getNextRecurringInvoiceNumber($this->client, $this->invoice); + $this->trySaving(); + // $this->invoice->number = $this->getNextRecurringInvoiceNumber($this->client, $this->invoice); } break; @@ -54,4 +59,33 @@ class ApplyRecurringNumber extends AbstractService return $this->invoice; } -} + + private function trySaving() + { + + $x=1; + + do{ + + try{ + + $this->invoice->number = $this->getNextRecurringInvoiceNumber($this->client, $this->invoice); + $this->invoice->saveQuietly(); + + $this->completed = false; + + + } + catch(QueryException $e){ + + $x++; + + if($x>10) + $this->completed = false; + } + + } + while($this->completed); + + } +} \ No newline at end of file diff --git a/app/Services/Payment/ApplyNumber.php b/app/Services/Payment/ApplyNumber.php index 2f773dcb02fe..34604843bd57 100644 --- a/app/Services/Payment/ApplyNumber.php +++ b/app/Services/Payment/ApplyNumber.php @@ -14,6 +14,7 @@ namespace App\Services\Payment; use App\Models\Payment; use App\Services\AbstractService; use App\Utils\Traits\GeneratesCounter; +use Illuminate\Database\QueryException; class ApplyNumber extends AbstractService { @@ -21,6 +22,8 @@ class ApplyNumber extends AbstractService private $payment; + private bool $completed = true; + public function __construct(Payment $payment) { $this->client = $payment->client; @@ -34,8 +37,38 @@ class ApplyNumber extends AbstractService return $this->payment; } - $this->payment->number = $this->getNextPaymentNumber($this->client, $this->payment); + $this->trySaving(); + // $this->payment->number = $this->getNextPaymentNumber($this->client, $this->payment); return $this->payment; } + + private function trySaving() + { + + $x=1; + + do{ + + try{ + + $this->payment->number = $this->getNextPaymentNumber($this->client, $this->payment); + $this->payment->saveQuietly(); + + $this->completed = false; + + + } + catch(QueryException $e){ + + $x++; + + if($x>10) + $this->completed = false; + } + + } + while($this->completed); + + } } diff --git a/app/Services/Quote/ApplyNumber.php b/app/Services/Quote/ApplyNumber.php index d7a8a0055a6c..1b9b77b98a28 100644 --- a/app/Services/Quote/ApplyNumber.php +++ b/app/Services/Quote/ApplyNumber.php @@ -13,6 +13,7 @@ namespace App\Services\Quote; use App\Models\Quote; use App\Utils\Traits\GeneratesCounter; +use Illuminate\Database\QueryException; class ApplyNumber { @@ -20,6 +21,8 @@ class ApplyNumber private $client; + private bool $completed = true; + public function __construct($client) { $this->client = $client; @@ -33,11 +36,13 @@ class ApplyNumber switch ($this->client->getSetting('counter_number_applied')) { case 'when_saved': - $quote->number = $this->getNextQuoteNumber($this->client, $quote); + $quote = $this->trySaving($quote); + // $quote->number = $this->getNextQuoteNumber($this->client, $quote); break; case 'when_sent': if ($quote->status_id == Quote::STATUS_SENT) { - $quote->number = $this->getNextQuoteNumber($this->client, $quote); + $quote = $this->trySaving($quote); + // $quote->number = $this->getNextQuoteNumber($this->client, $quote); } break; @@ -48,4 +53,34 @@ class ApplyNumber return $quote; } + + private function trySaving($quote) + { + + $x=1; + + do{ + + try{ + + $quote->number = $this->getNextQuoteNumber($this->client, $quote); + $quote->saveQuietly(); + + $this->completed = false; + + + } + catch(QueryException $e){ + + $x++; + + if($x>10) + $this->completed = false; + } + + } + while($this->completed); + + return $quote; + } } diff --git a/app/Services/Recurring/ApplyNumber.php b/app/Services/Recurring/ApplyNumber.php index 1e95c6d47a22..4fab238f8b76 100644 --- a/app/Services/Recurring/ApplyNumber.php +++ b/app/Services/Recurring/ApplyNumber.php @@ -14,6 +14,7 @@ namespace App\Services\Recurring; use App\Models\Client; use App\Services\AbstractService; use App\Utils\Traits\GeneratesCounter; +use Illuminate\Database\QueryException; class ApplyNumber extends AbstractService { @@ -23,6 +24,8 @@ class ApplyNumber extends AbstractService private $recurring_entity; + private bool $completed = true; + public function __construct(Client $client, $recurring_entity) { $this->client = $client; @@ -36,8 +39,38 @@ class ApplyNumber extends AbstractService if ($this->recurring_entity->number != '') return $this->recurring_entity; - $this->recurring_entity->number = $this->getNextRecurringInvoiceNumber($this->client, $this->recurring_entity); + $this->trySaving(); + //$this->recurring_entity->number = $this->getNextRecurringInvoiceNumber($this->client, $this->recurring_entity); return $this->recurring_entity; } + + private function trySaving() + { + + $x=1; + + do{ + + try{ + + $this->recurring_entity->number = $this->getNextRecurringInvoiceNumber($this->client, $this->recurring_entity); + $this->recurring_entity->saveQuietly(); + + $this->completed = false; + + + } + catch(QueryException $e){ + + $x++; + + if($x>10) + $this->completed = false; + } + + } + while($this->completed); + + } }