akaunting/app/Jobs/Banking/UpdateAccount.php

63 lines
1.6 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Jobs\Banking;
use App\Abstracts\Job;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\ShouldUpdate;
2019-11-16 10:21:14 +03:00
use App\Models\Banking\Account;
2021-09-06 11:53:57 +03:00
class UpdateAccount extends Job implements ShouldUpdate
2019-11-16 10:21:14 +03:00
{
2021-09-06 11:53:57 +03:00
public function handle(): Account
2019-11-16 10:21:14 +03:00
{
$this->authorize();
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
2021-09-06 11:53:57 +03:00
$this->model->update($this->request->all());
2020-06-26 13:40:19 +03:00
// Set default account
if ($this->request['default_account']) {
2021-09-06 11:53:57 +03:00
setting()->set('default.account', $this->model->id);
2020-06-26 13:40:19 +03:00
setting()->save();
}
});
2019-11-16 10:21:14 +03:00
2021-09-06 11:53:57 +03:00
return $this->model;
2019-11-16 10:21:14 +03:00
}
/**
* Determine if this action is applicable.
*/
2021-09-06 11:53:57 +03:00
public function authorize(): void
2019-11-16 10:21:14 +03:00
{
2020-11-12 18:14:13 +03:00
$relationships = $this->getRelationships();
2019-11-16 10:21:14 +03:00
2021-09-06 11:53:57 +03:00
if (!$this->request->get('enabled') && ($this->model->id == setting('default.account'))) {
2020-11-12 18:14:13 +03:00
$relationships[] = strtolower(trans_choice('general.companies', 1));
2021-09-06 11:53:57 +03:00
$message = trans('messages.warning.disabled', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]);
2019-11-16 10:21:14 +03:00
throw new \Exception($message);
}
2020-11-12 18:14:13 +03:00
if (!$relationships) {
return;
}
2019-11-16 10:21:14 +03:00
2021-09-06 11:53:57 +03:00
if ($this->model->currency_code != $this->request->get('currency_code')) {
$message = trans('messages.warning.disable_code', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]);
2019-11-16 10:21:14 +03:00
throw new \Exception($message);
}
}
2021-09-06 11:53:57 +03:00
public function getRelationships(): array
2019-11-16 10:21:14 +03:00
{
$rels = [
'transactions' => 'transactions',
];
2021-09-06 11:53:57 +03:00
return $this->countRelationships($this->model, $rels);
2019-11-16 10:21:14 +03:00
}
}