akaunting/app/Jobs/Common/UpdateCompany.php

122 lines
3.3 KiB
PHP
Raw Normal View History

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;
2019-11-16 10:21:14 +03:00
use App\Models\Common\Company;
use App\Traits\Users;
class UpdateCompany extends Job
{
use Users;
protected $company;
protected $request;
2020-12-25 12:08:15 +03:00
protected $active_company_id;
2019-11-16 10:21:14 +03:00
/**
* Create a new job instance.
*
* @param $company
* @param $request
*/
2020-12-25 12:08:15 +03:00
public function __construct($company, $request, $active_company_id)
2019-11-16 10:21:14 +03:00
{
$this->company = $company;
$this->request = $this->getRequestInstance($request);
2020-12-25 12:08:15 +03:00
$this->active_company_id = $active_company_id;
2019-11-16 10:21:14 +03:00
}
/**
* Execute the job.
*
* @return Company
*/
public function handle()
{
$this->authorize();
2020-09-13 21:57:10 +03:00
event(new CompanyUpdating($this->company, $this->request));
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$this->company->update($this->request->all());
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
// Clear current and load given company settings
setting()->setExtraColumns(['company_id' => $this->company->id]);
setting()->forgetAll();
setting()->load(true);
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
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
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')) {
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->company->id);
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
if ($company_logo) {
$this->company->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();
setting()->forgetAll();
});
2019-11-16 10:21:14 +03:00
2020-09-13 21:57:10 +03:00
event(new CompanyUpdated($this->company, $this->request));
2019-11-16 10:21:14 +03:00
return $this->company;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
2020-02-13 16:09:24 +03:00
// Can't disable active company
2020-12-25 12:08:15 +03:00
if (($this->request->get('enabled', 1) == 0) && ($this->company->id == $this->active_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
if (!$this->isUserCompany($this->company->id)) {
$message = trans('companies.error.not_user_company');
throw new \Exception($message);
}
}
}