akaunting/app/Jobs/Common/CreateCompany.php

110 lines
2.7 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\CompanyCreated;
use App\Events\Common\CompanyCreating;
2019-11-16 10:21:14 +03:00
use App\Models\Common\Company;
use Artisan;
class CreateCompany extends Job
{
2019-12-22 15:58:48 +03:00
protected $company;
2020-06-26 13:40:19 +03:00
protected $request;
2019-11-16 10:21:14 +03:00
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Company
*/
public function handle()
{
2020-09-13 21:57:10 +03:00
event(new CompanyCreating($this->request));
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$this->company = Company::create($this->request->all());
2020-01-01 00:53:47 +03:00
2020-06-26 13:40:19 +03:00
// Clear settings
setting()->setExtraColumns(['company_id' => $this->company->id]);
setting()->forgetAll();
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
$this->callSeeds();
2019-12-22 15:58:48 +03:00
2020-06-26 13:40:19 +03:00
$this->updateSettings();
});
2019-11-16 10:21:14 +03:00
2020-09-13 21:57:10 +03:00
event(new CompanyCreated($this->company));
2019-12-22 15:58:48 +03:00
return $this->company;
}
protected function callSeeds()
{
2020-02-27 11:32:44 +03:00
// Set custom locale
if ($this->request->has('locale')) {
app()->setLocale($this->request->get('locale'));
}
2019-12-22 15:58:48 +03:00
// Company seeds
Artisan::call('company:seed', [
'company' => $this->company->id
]);
2019-12-22 18:57:04 +03:00
if (!$user = user()) {
return;
}
2019-12-22 15:58:48 +03:00
// Attach company to user logged in
2019-12-22 18:57:04 +03:00
$user->companies()->attach($this->company->id);
2019-12-22 15:58:48 +03:00
// User seeds
2019-11-16 10:21:14 +03:00
Artisan::call('user:seed', [
2019-12-22 18:57:04 +03:00
'user' => $user->id,
2019-12-22 15:58:48 +03:00
'company' => $this->company->id,
2019-11-16 10:21:14 +03:00
]);
2019-12-22 15:58:48 +03:00
}
2019-11-16 10:21:14 +03:00
2019-12-22 18:57:04 +03:00
protected function updateSettings()
2019-12-22 15:58:48 +03:00
{
2019-11-16 10:21:14 +03:00
if ($this->request->file('logo')) {
2019-12-22 15:58:48 +03:00
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->company->id);
2019-11-16 10:21:14 +03:00
if ($company_logo) {
2019-12-22 15:58:48 +03:00
$this->company->attachMedia($company_logo, 'company_logo');
2019-11-16 10:21:14 +03:00
setting()->set('company.logo', $company_logo->id);
}
}
// Create settings
setting()->set([
'company.name' => $this->request->get('name'),
'company.email' => $this->request->get('email'),
'company.address' => $this->request->get('address'),
'default.currency' => $this->request->get('currency'),
'default.locale' => $this->request->get('locale', 'en-GB'),
]);
2019-12-22 18:57:04 +03:00
if (!empty($this->request->settings)) {
foreach ($this->request->settings as $name => $value) {
setting()->set([$name => $value]);
}
}
2019-11-16 10:21:14 +03:00
setting()->save();
setting()->forgetAll();
}
}