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.
|
|
|
|
*
|
2020-03-28 22:50:06 +03:00
|
|
|
* @return boolean|Exception
|
2019-11-16 10:21:14 +03:00
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2020-03-13 14:55:50 +03:00
|
|
|
$this->authorize();
|
|
|
|
|
2020-03-11 14:15:24 +03:00
|
|
|
Transaction::mute();
|
2020-01-21 10:31:49 +03:00
|
|
|
|
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();
|
2020-01-21 10:31:49 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
return true;
|
|
|
|
}
|
2020-03-13 14:55:50 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|