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
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. 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;
|
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();
|
|
|
|
// $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice, $this->invoice->recurring_id);
|
2020-02-03 11:33:07 +01:00
|
|
|
break;
|
|
|
|
case 'when_sent':
|
2020-02-17 10:37:44 +01:00
|
|
|
if ($this->invoice->status_id == Invoice::STATUS_SENT) {
|
2022-04-07 09:40:59 +02:00
|
|
|
$this->trySaving();
|
|
|
|
// $this->invoice->number = $this->getNextInvoiceNumber($this->client, $this->invoice, $this->invoice->recurring_id);
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
2020-02-14 04:32:22 +01:00
|
|
|
}
|