CaiWuXiTong/app/Jobs/Document/DeleteDocument.php
WuYongJie 9595267ee7
Some checks failed
Tests / PHP 8.1 (push) Has been cancelled
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
chore: 导入 Akaunting 初始源码与团队协作规则
2026-07-20 18:20:25 +08:00

52 lines
1.3 KiB
PHP

<?php
namespace App\Jobs\Document;
use App\Abstracts\Job;
use App\Events\Document\DocumentDeleted;
use App\Events\Document\DocumentDeleting;
use App\Interfaces\Job\ShouldDelete;
use App\Observers\Transaction;
use Illuminate\Support\Str;
class DeleteDocument extends Job implements ShouldDelete
{
public function handle(): bool
{
$this->authorize();
event(new DocumentDeleting($this->model));
\DB::transaction(function () {
try {
Transaction::mute();
$this->deleteRelationships($this->model, [
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
]);
$this->model->delete();
} finally {
Transaction::unmute();
}
});
event(new DocumentDeleted($this->model));
return true;
}
/**
* Determine if this action is applicable.
*/
public function authorize(): void
{
if ($this->model->transactions()->isReconciled()->count()) {
$type = Str::plural($this->model->type);
$message = trans('messages.warning.reconciled_doc', ['type' => trans_choice("general.$type", 1)]);
throw new \Exception($message);
}
}
}