akaunting/app/Jobs/Setting/UpdateTax.php

77 lines
1.6 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Jobs\Setting;
use App\Abstracts\Job;
use App\Models\Setting\Tax;
class UpdateTax extends Job
{
protected $tax;
protected $request;
/**
* Create a new job instance.
*
* @param $tax
* @param $request
*/
public function __construct($tax, $request)
{
$this->tax = $tax;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Tax
*/
public function handle()
{
$this->authorize();
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$this->tax->update($this->request->all());
});
2019-11-16 10:21:14 +03:00
return $this->tax;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
if (!$relationships = $this->getRelationships()) {
return;
}
2020-06-29 14:29:18 +03:00
if ($this->request->has('type') && ($this->request->get('type') != $this->tax->type)) {
2019-11-16 10:21:14 +03:00
$message = trans('messages.error.type', ['text' => implode(', ', $relationships)]);
throw new \Exception($message);
}
if (!$this->request->get('enabled')) {
$message = trans('messages.warning.disabled', ['name' => $this->tax->name, 'text' => implode(', ', $relationships)]);
throw new \Exception($message);
}
}
public function getRelationships()
{
$rels = [
'items' => 'items',
'invoice_items' => 'invoices',
'bill_items' => 'bills',
];
return $this->countRelationships($this->tax, $rels);
}
}