akaunting/app/Jobs/Document/DeleteDocument.php

50 lines
1.2 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
2020-12-24 01:28:38 +03:00
namespace App\Jobs\Document;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Job;
use App\Events\Document\DocumentDeleted;
use App\Events\Document\DocumentDeleting;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\ShouldDelete;
2020-03-11 14:15:24 +03:00
use App\Observers\Transaction;
2020-12-24 01:28:38 +03:00
use Illuminate\Support\Str;
2019-11-16 10:21:14 +03:00
2021-09-06 11:53:57 +03:00
class DeleteDocument extends Job implements ShouldDelete
2019-11-16 10:21:14 +03:00
{
2021-09-06 11:53:57 +03:00
public function handle(): bool
2019-11-16 10:21:14 +03:00
{
$this->authorize();
event(new DocumentDeleting($this->model));
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
Transaction::mute();
2021-09-06 11:53:57 +03:00
$this->deleteRelationships($this->model, [
2020-06-26 13:40:19 +03:00
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
]);
2019-11-16 10:21:14 +03:00
2021-09-06 11:53:57 +03:00
$this->model->delete();
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
Transaction::unmute();
});
event(new DocumentDeleted($this->model));
2019-11-16 10:21:14 +03:00
return true;
}
/**
* Determine if this action is applicable.
*/
2021-09-06 11:53:57 +03:00
public function authorize(): void
{
2021-09-06 11:53:57 +03:00
if ($this->model->transactions()->isReconciled()->count()) {
$type = Str::plural($this->model->type);
2020-12-24 01:28:38 +03:00
$message = trans('messages.warning.reconciled_doc', ['type' => trans_choice("general.$type", 1)]);
throw new \Exception($message);
}
}
2019-11-16 10:21:14 +03:00
}