CaiWuXiTong/app/Jobs/Document/CancelDocument.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

51 lines
1.2 KiB
PHP

<?php
namespace App\Jobs\Document;
use App\Abstracts\Job;
use App\Models\Document\Document;
use Illuminate\Support\Str;
class CancelDocument extends Job
{
protected $model;
public function __construct(Document $model)
{
$this->model = $model;
parent::__construct($model);
}
public function handle(): Document
{
$this->authorize();
\DB::transaction(function () {
$this->deleteRelationships($this->model, [
'transactions', 'recurring'
]);
$this->model->status = 'cancelled';
$this->model->save();
});
return $this->model;
}
/**
* Determine if this action is applicable.
* Mirrors the same guard in DeleteDocument to prevent cancelling a document
* that has reconciled transactions, which would silently destroy bank records.
*/
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);
}
}
}