1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Services/Invoice/ApplyNumber.php

90 lines
2.2 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @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)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Invoice;
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
{
use GeneratesCounter;
private $client;
private $invoice;
private $completed = true;
public function __construct(Client $client, Invoice $invoice)
{
$this->client = $client;
$this->invoice = $invoice;
}
public function run()
{
if ($this->invoice->number != '') {
return $this->invoice;
2023-03-17 02:49:08 +01:00
}
2023-03-19 10:10:20 +01:00
/** Do not give a pro forma invoice a proper invoice number */
if ($this->invoice->is_proforma && $this->invoice->recurring_id) {
2023-03-17 02:49:08 +01:00
$this->invoice->number = ctrans('texts.pre_payment') . " " . now()->format('Y-m-d : H:i:s');
$this->invoice->saveQuietly();
return $this->invoice;
}
switch ($this->client->getSetting('counter_number_applied')) {
case 'when_saved':
$this->trySaving();
break;
case 'when_sent':
if ($this->invoice->status_id >= Invoice::STATUS_SENT) {
$this->trySaving();
}
break;
default:
break;
}
return $this->invoice;
}
private function trySaving()
{
2024-01-14 05:05:00 +01:00
$x = 1;
2023-02-16 02:36:09 +01:00
do {
try {
$this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice, $this->invoice->recurring_id);
$this->invoice->saveQuietly();
$this->completed = false;
2023-02-16 02:36:09 +01:00
} catch(QueryException $e) {
$x++;
2024-01-14 05:05:00 +01:00
if ($x > 50) {
$this->completed = false;
2023-02-16 02:36:09 +01:00
}
}
2023-02-16 02:36:09 +01:00
} while ($this->completed);
2022-09-12 13:08:50 +02:00
return $this;
}
}