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;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\HasOwner;
|
2021-09-07 10:33:34 +03:00
|
|
|
use App\Interfaces\Job\HasSource;
|
2021-09-06 11:53:57 +03:00
|
|
|
use App\Interfaces\Job\ShouldCreate;
|
2022-10-25 11:21:05 +03:00
|
|
|
use App\Models\Banking\Account;
|
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;
|
|
|
|
use Akaunting\Money\Currency as MoneyCurrency;
|
2021-09-06 11:53:57 +03:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2022-10-25 11:21:05 +03:00
|
|
|
use OutOfBoundsException;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2021-09-07 10:33:34 +03:00
|
|
|
class CreateCompany extends Job implements HasOwner, HasSource, ShouldCreate
|
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
|
|
|
{
|
2021-04-16 00:59:43 +03:00
|
|
|
$current_company_id = company_id();
|
|
|
|
|
2020-09-13 21:57:10 +03:00
|
|
|
event(new CompanyCreating($this->request));
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
\DB::transaction(function () {
|
2021-09-06 11:53:57 +03:00
|
|
|
$this->model = Company::create($this->request->all());
|
2020-01-01 00:53:47 +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
|
|
|
$this->callSeeds();
|
2019-12-22 15:58:48 +03:00
|
|
|
|
2022-10-25 11:21:05 +03:00
|
|
|
$this->updateCurrency();
|
|
|
|
|
2020-06-26 13:40:19 +03:00
|
|
|
$this->updateSettings();
|
|
|
|
});
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2022-10-25 10:15:07 +03:00
|
|
|
if (! empty($current_company_id)) {
|
2021-04-16 00:59:43 +03:00
|
|
|
company($current_company_id)->makeCurrent();
|
|
|
|
}
|
|
|
|
|
2022-10-25 10:15:07 +03:00
|
|
|
event(new CompanyCreated($this->model, $this->request));
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
return $this->model;
|
2019-12-22 15:58:48 +03:00
|
|
|
}
|
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
protected function callSeeds(): void
|
2019-12-22 15:58:48 +03:00
|
|
|
{
|
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', [
|
2021-09-06 11:53:57 +03:00
|
|
|
'company' => $this->model->id
|
2019-12-22 15:58:48 +03:00
|
|
|
]);
|
|
|
|
|
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
|
2021-09-06 11:53:57 +03:00
|
|
|
$user->companies()->attach($this->model->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,
|
2021-09-06 11:53:57 +03:00
|
|
|
'company' => $this->model->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
|
|
|
|
2021-09-06 11:53:57 +03:00
|
|
|
protected function updateSettings(): void
|
2019-12-22 15:58:48 +03:00
|
|
|
{
|
2019-11-16 10:21:14 +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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
setting()->set('company.logo', $company_logo->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create settings
|
|
|
|
setting()->set([
|
|
|
|
'company.name' => $this->request->get('name'),
|
|
|
|
'company.email' => $this->request->get('email'),
|
2021-01-11 15:52:27 +03:00
|
|
|
'company.tax_number' => $this->request->get('tax_number'),
|
|
|
|
'company.phone' => $this->request->get('phone'),
|
2019-11-16 10:21:14 +03:00
|
|
|
'company.address' => $this->request->get('address'),
|
2021-09-03 11:43:55 +03:00
|
|
|
'company.city' => $this->request->get('city'),
|
|
|
|
'company.zip_code' => $this->request->get('zip_code'),
|
|
|
|
'company.state' => $this->request->get('state'),
|
|
|
|
'company.country' => $this->request->get('country'),
|
2019-11-16 10:21:14 +03:00
|
|
|
'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();
|
|
|
|
}
|
2022-10-25 11:21:05 +03:00
|
|
|
|
|
|
|
protected function updateCurrency()
|
|
|
|
{
|
|
|
|
$currency_code = $this->request->get('currency');
|
|
|
|
|
|
|
|
if ($currency_code == 'USD') {
|
|
|
|
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) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = Account::where('company_id', $this->model->id)->first();
|
|
|
|
|
|
|
|
$account->currency_code = $currency_code;
|
|
|
|
$account->save();
|
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|