2020-04-08 15:31:22 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @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\Events\Payment\PaymentWasCreated;
|
|
|
|
use App\Factory\CreditFactory;
|
|
|
|
use App\Factory\InvoiceItemFactory;
|
|
|
|
use App\Factory\PaymentFactory;
|
|
|
|
use App\Helpers\Invoice\InvoiceSum;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\Paymentable;
|
|
|
|
use App\Services\AbstractService;
|
|
|
|
use App\Services\Client\ClientService;
|
|
|
|
use App\Services\Payment\PaymentService;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
|
|
|
|
|
|
|
class HandleCancellation extends AbstractService
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
|
|
|
|
|
|
|
private $invoice;
|
|
|
|
|
|
|
|
public function __construct(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
/* Check again!! */
|
|
|
|
if(!$this->invoice->invoiceCancellable($this->invoice))
|
|
|
|
return $this->invoice;
|
|
|
|
|
|
|
|
$adjustment = $this->invoice->balance*-1;
|
|
|
|
//set invoice balance to 0
|
|
|
|
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice cancellation");
|
|
|
|
|
|
|
|
$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-04-09 01:35:40 +02:00
|
|
|
event(new InvoiceWasCancelled($this->invoice));
|
|
|
|
|
2020-04-08 15:31:22 +02:00
|
|
|
return $this->invoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|