1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 08:02:40 +01:00
invoiceninja/app/Services/Invoice/HandleCancellation.php

114 lines
3.5 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @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)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Invoice;
use App\Events\Invoice\InvoiceWasCancelled;
use App\Models\Invoice;
use App\Services\AbstractService;
2020-07-08 14:02:16 +02:00
use App\Utils\Ninja;
use App\Utils\Traits\GeneratesCounter;
2020-10-28 11:10:49 +01:00
use stdClass;
class HandleCancellation extends AbstractService
{
use GeneratesCounter;
2023-06-03 14:49:48 +02:00
public function __construct(private Invoice $invoice)
{
$this->invoice = $invoice;
}
public function run()
{
/* Check again!! */
if (! $this->invoice->invoiceCancellable($this->invoice)) {
return $this->invoice;
}
2024-01-14 05:05:00 +01:00
$adjustment = ($this->invoice->balance < 0) ? abs($this->invoice->balance) : $this->invoice->balance * -1;
$this->backupCancellation($adjustment);
//set invoice balance to 0
2023-11-07 11:16:33 +01:00
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} cancellation");
$this->invoice->balance = 0;
$this->invoice = $this->invoice->service()->setStatus(Invoice::STATUS_CANCELLED)->save();
2023-11-07 04:54:44 +01:00
// $this->invoice->client->service()->updateBalance($adjustment)->save();
2023-11-07 11:16:33 +01:00
$this->invoice->client->service()->calculateBalance();
$this->invoice->service()->workFlow()->save();
event(new InvoiceWasCancelled($this->invoice, $this->invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
2022-11-14 21:23:14 +01:00
event('eloquent.updated: App\Models\Invoice', $this->invoice);
return $this->invoice;
}
public function reverse()
{
2021-01-21 02:33:39 +01:00
/* The stored cancelled object - contains the adjustment and status*/
2020-06-15 01:53:57 +02:00
$cancellation = $this->invoice->backup->cancellation;
2021-01-21 02:33:39 +01:00
/* Will turn the negative cancellation amount to a positive adjustment*/
$adjustment = $cancellation->adjustment * -1;
2023-11-07 11:58:52 +01:00
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} reversal");
2023-11-07 04:54:44 +01:00
2023-11-08 05:19:30 +01:00
$this->invoice = $this->invoice->fresh();
/* Reverse the invoice status and balance */
$this->invoice->balance += $adjustment;
$this->invoice->status_id = $cancellation->status_id;
$this->invoice->client->service()->updateBalance($adjustment)->save();
2023-11-26 08:41:42 +01:00
$this->invoice->client->service()->calculateBalance();
2023-11-07 04:54:44 +01:00
/* Pop the cancellation out of the backup*/
$backup = $this->invoice->backup;
unset($backup->cancellation);
$this->invoice->backup = $backup;
2021-10-14 08:54:38 +02:00
$this->invoice->saveQuietly();
2022-05-27 01:51:54 +02:00
$this->invoice->fresh();
return $this->invoice;
}
/**
* Backup the cancellation in case we ever need to reverse it.
*
* @param float $adjustment The amount the balance has been reduced by to cancel the invoice
* @return void
*/
private function backupCancellation($adjustment)
{
if (! is_object($this->invoice->backup)) {
2024-01-14 05:05:00 +01:00
$backup = new stdClass();
$this->invoice->backup = $backup;
}
2024-01-14 05:05:00 +01:00
$cancellation = new stdClass();
$cancellation->adjustment = $adjustment;
$cancellation->status_id = $this->invoice->status_id;
$invoice_backup = $this->invoice->backup;
$invoice_backup->cancellation = $cancellation;
$this->invoice->backup = $invoice_backup;
2021-10-14 08:54:38 +02:00
$this->invoice->saveQuietly();
}
}