akaunting/app/Jobs/Document/DeleteDocument.php

62 lines
1.3 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;
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
2020-12-24 01:28:38 +03:00
class DeleteDocument extends Job
2019-11-16 10:21:14 +03:00
{
2020-12-24 01:28:38 +03:00
protected $document;
2019-11-16 10:21:14 +03:00
/**
* Create a new job instance.
*
2020-12-24 01:28:38 +03:00
* @param $document
2019-11-16 10:21:14 +03:00
*/
2020-12-24 01:28:38 +03:00
public function __construct($document)
2019-11-16 10:21:14 +03:00
{
2020-12-24 01:28:38 +03:00
$this->document = $document;
2019-11-16 10:21:14 +03:00
}
/**
* 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-12-24 01:28:38 +03:00
$this->deleteRelationships($this->document, [
2020-06-26 13:40:19 +03:00
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
]);
2019-11-16 10:21:14 +03:00
2020-12-24 01:28:38 +03:00
$this->document->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()
{
2020-12-24 01:28:38 +03:00
if ($this->document->transactions()->isReconciled()->count()) {
$type = Str::plural($this->document->type);
$message = trans('messages.warning.reconciled_doc', ['type' => trans_choice("general.$type", 1)]);
throw new \Exception($message);
}
}
2019-11-16 10:21:14 +03:00
}