2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Common;
|
|
|
|
|
|
|
|
use App\Abstracts\Job;
|
2020-09-13 21:57:10 +03:00
|
|
|
use App\Events\Common\CompanyUpdated;
|
|
|
|
use App\Events\Common\CompanyUpdating;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldUpdate;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Models\Common\Company;
|
2022-10-25 11:21:05 +03:00
|
|
|
use App\Models\Setting\Currency;
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Traits\Users;
|
2022-10-25 11:21:05 +03:00
|
|
|
use Akaunting\Money\Currency as MoneyCurrency;
|
|
|
|
use OutOfBoundsException;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
class UpdateCompany extends Job implements ShouldUpdate
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
use Users;
|
|
|
|
|
2021-04-16 00:59:43 +03:00
|
|
|
protected $current_company_id;
|
2020-12-25 12:08:15 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function booted(...$arguments): void
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
2021-04-16 00:59:43 +03:00
|
|
|
$this->current_company_id = company_id();
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
public function handle(): Company
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
$this->authorize();
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
event(new CompanyUpdating($this->model, $this->request));
|
2020-09-13 21:57:10 +03:00
|
|
|
|
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());
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->makeCurrent();
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->has('name')) {
|
|
|
|
setting()->set('company.name', $this->request->get('name'));
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->has('email')) {
|
|
|
|
setting()->set('company.email', $this->request->get('email'));
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-01-11 15:52:27 +03:00
|
|
|
if ($this->request->has('tax_number')) {
|
|
|
|
setting()->set('company.tax_number', $this->request->get('tax_number'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->request->has('phone')) {
|
|
|
|
setting()->set('company.phone', $this->request->get('phone'));
|
|
|
|
}
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->has('address')) {
|
|
|
|
setting()->set('company.address', $this->request->get('address'));
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-03 11:43:55 +03:00
|
|
|
if ($this->request->has('city')) {
|
|
|
|
setting()->set('company.city', $this->request->get('city'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->request->has('zip_code')) {
|
|
|
|
setting()->set('company.zip_code', $this->request->get('zip_code'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->request->has('state')) {
|
|
|
|
setting()->set('company.state', $this->request->get('state'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->request->has('country')) {
|
|
|
|
setting()->set('company.country', $this->request->get('country'));
|
|
|
|
}
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->has('currency')) {
|
|
|
|
setting()->set('default.currency', $this->request->get('currency'));
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->has('locale')) {
|
|
|
|
setting()->set('default.locale', $this->request->get('locale'));
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($this->request->file('logo')) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->model->id);
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
if ($company_logo) {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model->attachMedia($company_logo, 'company_logo');
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
setting()->set('company.logo', $company_logo->id);
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
setting()->save();
|
2022-10-25 11:21:05 +03:00
|
|
|
|
|
|
|
$this->updateCurrency();
|
2020-06-26 13:40:19 +03:00
|
|
|
});
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
event(new CompanyUpdated($this->model, $this->request));
|
2020-09-13 21:57:10 +03:00
|
|
|
|
2021-04-16 00:59:43 +03:00
|
|
|
if (!empty($this->current_company_id)) {
|
|
|
|
company($this->current_company_id)->makeCurrent();
|
|
|
|
}
|
|
|
|
|
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-02-13 16:09:24 +03:00
|
|
|
// Can't disable active company
|
2021-09-06 11:53:57 +03:00
|
|
|
if (($this->request->get('enabled', 1) == 0) && ($this->model->id == $this->current_company_id)) {
|
2020-02-13 16:09:24 +03:00
|
|
|
$message = trans('companies.error.disable_active');
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
// Check if user can access company
|
2021-09-06 11:53:57 +03:00
|
|
|
if ($this->isNotUserCompany($this->model->id)) {
|
2019-11-16 10:21:14 +03:00
|
|
|
$message = trans('companies.error.not_user_company');
|
|
|
|
|
|
|
|
throw new \Exception($message);
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 11:21:05 +03:00
|
|
|
|
|
|
|
protected function updateCurrency()
|
|
|
|
{
|
|
|
|
$currency_code = $this->request->get('currency');
|
|
|
|
|
|
|
|
if (empty($currency_code)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$currency = Currency::where('company_id', $this->model->id)
|
|
|
|
->where('code', $currency_code)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($currency) {
|
|
|
|
$currency->rate = '1';
|
|
|
|
$currency->enabled = '1';
|
|
|
|
|
|
|
|
$currency->save();
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$data = (new MoneyCurrency($currency_code))->toArray()[$currency_code];
|
|
|
|
$data['rate'] = '1';
|
|
|
|
$data['enabled'] = '1';
|
|
|
|
$data['company_id'] = $this->model->id;
|
|
|
|
$data['code'] = $currency_code;
|
|
|
|
$data['created_from'] = 'core::ui';
|
|
|
|
$data['created_by'] = user_id();
|
|
|
|
|
|
|
|
$currency = Currency::create($data);
|
|
|
|
} catch (OutOfBoundsException $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|