2020-02-03 11:33:07 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-02-03 11:33:07 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-02-03 11:33:07 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-02-03 11:33:07 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
use App\Models\Client;
|
2020-02-03 11:33:07 +01:00
|
|
|
use App\Models\Invoice;
|
2020-02-17 10:37:44 +01:00
|
|
|
use App\Services\AbstractService;
|
2020-02-03 11:33:07 +01:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2022-04-07 09:40:59 +02:00
|
|
|
use Illuminate\Database\QueryException;
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
class ApplyNumber extends AbstractService
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
use GeneratesCounter;
|
2020-02-03 11:33:07 +01:00
|
|
|
|
|
|
|
private $client;
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
private $invoice;
|
|
|
|
|
2022-04-07 09:40:59 +02:00
|
|
|
private $completed = true;
|
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
public function __construct(Client $client, Invoice $invoice)
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
$this->client = $client;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
$this->invoice = $invoice;
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function run()
|
2020-02-14 04:32:22 +01:00
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($this->invoice->number != '') {
|
2020-02-17 10:37:44 +01:00
|
|
|
return $this->invoice;
|
2023-03-17 02:49:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Do no give pro forma invoices a proper invoice number */
|
|
|
|
if($this->invoice->is_proforma) {
|
|
|
|
$this->invoice->number = ctrans('texts.pre_payment') . " " . now()->format('Y-m-d : H:i:s');
|
|
|
|
$this->invoice->saveQuietly();
|
|
|
|
return $this->invoice;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-03 11:33:07 +01:00
|
|
|
|
|
|
|
switch ($this->client->getSetting('counter_number_applied')) {
|
|
|
|
case 'when_saved':
|
2022-04-07 09:40:59 +02:00
|
|
|
$this->trySaving();
|
2020-02-03 11:33:07 +01:00
|
|
|
break;
|
|
|
|
case 'when_sent':
|
2023-02-07 12:25:40 +01:00
|
|
|
if ($this->invoice->status_id >= Invoice::STATUS_SENT) {
|
2022-04-07 09:40:59 +02:00
|
|
|
$this->trySaving();
|
2020-02-03 11:33:07 +01:00
|
|
|
}
|
|
|
|
break;
|
2020-02-14 04:32:22 +01:00
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-02-14 04:32:22 +01:00
|
|
|
|
2020-02-17 10:37:44 +01:00
|
|
|
return $this->invoice;
|
2020-02-14 04:32:22 +01:00
|
|
|
}
|
2022-04-07 09:40:59 +02:00
|
|
|
|
|
|
|
private function trySaving()
|
|
|
|
{
|
|
|
|
$x=1;
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
do {
|
|
|
|
try {
|
2022-04-07 09:40:59 +02:00
|
|
|
$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) {
|
2022-04-07 09:40:59 +02:00
|
|
|
$x++;
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($x>50) {
|
2022-04-07 09:40:59 +02:00
|
|
|
$this->completed = false;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-04-07 09:40:59 +02:00
|
|
|
}
|
2023-02-16 02:36:09 +01:00
|
|
|
} while ($this->completed);
|
2022-04-07 09:40:59 +02:00
|
|
|
|
2022-09-12 13:08:50 +02:00
|
|
|
|
|
|
|
return $this;
|
2022-04-07 09:40:59 +02:00
|
|
|
}
|
2020-02-14 04:32:22 +01:00
|
|
|
}
|