akaunting/app/Jobs/Sale/DeleteInvoice.php

59 lines
1.1 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
2019-12-31 15:49:09 +03:00
namespace App\Jobs\Sale;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Job;
2019-12-31 15:49:09 +03:00
use App\Models\Sale\Invoice;
2020-03-11 14:15:24 +03:00
use App\Observers\Transaction;
2019-11-16 10:21:14 +03:00
class DeleteInvoice extends Job
{
protected $invoice;
/**
* Create a new job instance.
*
* @param $invoice
*/
public function __construct($invoice)
{
$this->invoice = $invoice;
}
/**
* Execute the job.
*
* @return Invoice
*/
public function handle()
{
$this->authorize();
2020-03-11 14:15:24 +03:00
Transaction::mute();
2019-11-16 10:21:14 +03:00
$this->deleteRelationships($this->invoice, [
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
]);
$this->invoice->delete();
2020-03-11 14:15:24 +03:00
Transaction::unmute();
2019-11-16 10:21:14 +03:00
return true;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
if ($this->invoice->transactions()->isReconciled()->count()) {
$message = trans('messages.warning.reconciled_doc', ['type' => trans_choice('general.invoices', 1)]);
throw new \Exception($message);
}
}
2019-11-16 10:21:14 +03:00
}