2020-04-08 15:31:22 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-04-08 15:31:22 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
2020-04-09 01:35:40 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasCancelled;
|
2020-04-08 15:31:22 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Services\AbstractService;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-04-08 15:31:22 +02:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2020-10-28 11:10:49 +01:00
|
|
|
use stdClass;
|
2020-04-08 15:31:22 +02:00
|
|
|
|
|
|
|
class HandleCancellation extends AbstractService
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
|
|
|
|
|
|
|
private $invoice;
|
|
|
|
|
|
|
|
public function __construct(Invoice $invoice)
|
2020-04-15 02:30:52 +02:00
|
|
|
{
|
2020-04-08 15:31:22 +02:00
|
|
|
$this->invoice = $invoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
/* Check again!! */
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $this->invoice->invoiceCancellable($this->invoice)) {
|
2020-04-08 15:31:22 +02:00
|
|
|
return $this->invoice;
|
2020-04-15 02:30:52 +02:00
|
|
|
}
|
2020-04-08 15:31:22 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$adjustment = $this->invoice->balance * -1;
|
2020-06-15 01:34:18 +02:00
|
|
|
|
|
|
|
$this->backupCancellation($adjustment);
|
|
|
|
|
2020-04-08 15:31:22 +02:00
|
|
|
//set invoice balance to 0
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice->ledger()->updateInvoiceBalance($adjustment, 'Invoice cancellation');
|
2020-04-08 15:31:22 +02:00
|
|
|
|
|
|
|
$this->invoice->balance = 0;
|
|
|
|
$this->invoice = $this->invoice->service()->setStatus(Invoice::STATUS_CANCELLED)->save();
|
|
|
|
|
|
|
|
//adjust client balance
|
|
|
|
$this->invoice->client->service()->updateBalance($adjustment)->save();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new InvoiceWasCancelled($this->invoice, $this->invoice->company, Ninja::eventVars()));
|
2020-06-15 01:34:18 +02:00
|
|
|
|
2020-04-08 15:31:22 +02:00
|
|
|
return $this->invoice;
|
|
|
|
}
|
2020-06-15 01:34:18 +02:00
|
|
|
|
|
|
|
public function reverse()
|
|
|
|
{
|
2020-06-15 01:53:57 +02:00
|
|
|
$cancellation = $this->invoice->backup->cancellation;
|
2020-06-15 01:34:18 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$adjustment = $cancellation->adjustment * -1;
|
2020-06-15 01:34:18 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$this->invoice->ledger()->updateInvoiceBalance($adjustment, 'Invoice cancellation REVERSAL');
|
2020-06-15 01:34:18 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
/* Reverse the invoice status and balance */
|
2020-06-15 01:34:18 +02:00
|
|
|
$this->invoice->balance += $adjustment;
|
|
|
|
$this->invoice->status_id = $cancellation->status_id;
|
|
|
|
|
|
|
|
$this->invoice->client->service()->updateBalance($adjustment)->save();
|
|
|
|
|
|
|
|
/* Pop the cancellation out of the backup*/
|
|
|
|
$backup = $this->invoice->backup;
|
|
|
|
unset($backup->cancellation);
|
|
|
|
$this->invoice->backup = $backup;
|
|
|
|
$this->invoice->save();
|
|
|
|
|
|
|
|
return $this->invoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backup the cancellation in case we ever need to reverse it.
|
2020-09-06 11:38:10 +02:00
|
|
|
*
|
2020-06-15 01:34:18 +02:00
|
|
|
* @param float $adjustment The amount the balance has been reduced by to cancel the invoice
|
2020-09-06 11:38:10 +02:00
|
|
|
* @return void
|
2020-06-15 01:34:18 +02:00
|
|
|
*/
|
|
|
|
private function backupCancellation($adjustment)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! is_object($this->invoice->backup)) {
|
2020-10-28 11:10:49 +01:00
|
|
|
$backup = new stdClass;
|
2020-06-15 01:34:18 +02:00
|
|
|
$this->invoice->backup = $backup;
|
|
|
|
}
|
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
$cancellation = new stdClass;
|
2020-06-15 01:34:18 +02:00
|
|
|
$cancellation->adjustment = $adjustment;
|
|
|
|
$cancellation->status_id = $this->invoice->status_id;
|
|
|
|
|
|
|
|
$invoice_backup = $this->invoice->backup;
|
|
|
|
$invoice_backup->cancellation = $cancellation;
|
|
|
|
|
|
|
|
$this->invoice->backup = $invoice_backup;
|
|
|
|
$this->invoice->save();
|
|
|
|
}
|
2020-04-08 15:31:22 +02:00
|
|
|
}
|