akaunting/app/Jobs/Purchase/DeleteBill.php

60 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\Purchase;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Job;
2020-03-11 14:15:24 +03:00
use App\Observers\Transaction;
2019-11-16 10:21:14 +03:00
class DeleteBill extends Job
{
protected $bill;
/**
* Create a new job instance.
*
* @param $bill
*/
public function __construct($bill)
{
$this->bill = $bill;
}
/**
* 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()
{
$this->authorize();
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
Transaction::mute();
2020-06-26 13:40:19 +03:00
$this->deleteRelationships($this->bill, [
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
]);
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
$this->bill->delete();
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +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->bill->transactions()->isReconciled()->count()) {
$message = trans('messages.warning.reconciled_doc', ['type' => trans_choice('general.bills', 1)]);
throw new \Exception($message);
}
}
2019-11-16 10:21:14 +03:00
}