1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Jobs/Company/CreateCompany.php

235 lines
6.3 KiB
PHP
Raw Normal View History

2018-10-24 05:50:15 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
2018-10-24 05:50:15 +02:00
namespace App\Jobs\Company;
2023-10-26 04:57:44 +02:00
use App\DataMapper\ClientRegistrationFields;
use App\DataMapper\CompanySettings;
use App\DataMapper\Tax\TaxModel;
use App\Libraries\MultiDB;
2023-04-28 08:42:15 +02:00
use App\Models\Company;
2023-05-17 01:16:43 +02:00
use App\Models\Country;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
2018-10-24 05:50:15 +02:00
use Illuminate\Foundation\Bus\Dispatchable;
class CreateCompany
{
use MakesHash;
2018-10-24 05:50:15 +02:00
use Dispatchable;
protected $request;
protected $account;
/**
* Create a new job instance.
*
2020-10-28 11:10:49 +01:00
* @param array $request
* @param $account
2018-10-24 05:50:15 +02:00
*/
2019-06-17 01:58:33 +02:00
public function __construct(array $request, $account)
2018-10-24 05:50:15 +02:00
{
$this->request = $request;
2019-06-17 01:58:33 +02:00
2018-10-24 05:50:15 +02:00
$this->account = $account;
}
/**
* Execute the job.
*
2020-10-28 11:10:49 +01:00
* @return Company|null
2018-10-24 05:50:15 +02:00
*/
public function handle() : ?Company
2018-10-24 05:50:15 +02:00
{
2019-10-05 02:11:04 +02:00
$settings = CompanySettings::defaults();
$settings->name = isset($this->request['name']) ? $this->request['name'] : '';
2018-10-24 05:50:15 +02:00
2023-10-26 04:57:44 +02:00
if($country_id = $this->resolveCountry()) {
2023-05-17 01:16:43 +02:00
$settings->country_id = $country_id;
}
2018-10-24 05:50:15 +02:00
$company = new Company();
$company->account_id = $this->account->id;
$company->company_key = $this->createHash();
$company->ip = request()->ip();
2019-10-05 02:11:04 +02:00
$company->settings = $settings;
$company->db = config('database.default');
$company->enabled_modules = config('ninja.enabled_modules');
2023-12-12 00:46:23 +01:00
$company->subdomain = isset($this->request['subdomain']) ? $this->request['subdomain'] : MultiDB::randomSubdomainGenerator();
2021-03-13 07:45:41 +01:00
$company->custom_fields = new \stdClass;
2021-03-22 11:55:09 +01:00
$company->default_password_timeout = 1800000;
2021-09-30 00:14:48 +02:00
$company->client_registration_fields = ClientRegistrationFields::generate();
2022-09-05 09:18:08 +02:00
$company->markdown_email_enabled = true;
$company->markdown_enabled = false;
2023-04-28 08:42:15 +02:00
$company->tax_data = new TaxModel();
if (Ninja::isHosted()) {
2021-05-24 11:39:21 +02:00
$company->subdomain = MultiDB::randomSubdomainGenerator();
} else {
2021-05-24 11:39:21 +02:00
$company->subdomain = '';
}
2023-05-17 01:16:43 +02:00
/** Location Specific Configuration */
match($settings->country_id) {
'724' => $company = $this->spanishSetup($company),
'36' => $company = $this->australiaSetup($company),
'710' => $company = $this->southAfticaSetup($company),
2023-05-17 01:16:43 +02:00
default => $company->save(),
};
return $company;
}
/**
* Resolve Country
*
* @return string
*/
2023-10-26 04:57:44 +02:00
private function resolveCountry(): string
2023-05-17 01:16:43 +02:00
{
2023-10-26 04:57:44 +02:00
try {
2023-05-17 01:16:43 +02:00
$ip = request()->ip();
2023-10-26 04:57:44 +02:00
if(request()->hasHeader('cf-ipcountry')) {
2023-05-17 01:16:43 +02:00
2023-11-24 22:55:02 +01:00
$c = Country::query()->where('iso_3166_2', request()->header('cf-ipcountry'))->first();
2023-05-17 01:16:43 +02:00
2023-10-26 04:57:44 +02:00
if($c) {
2023-05-17 01:16:43 +02:00
return (string)$c->id;
2023-10-26 04:57:44 +02:00
}
2023-05-17 01:16:43 +02:00
}
$details = json_decode(file_get_contents("http://ip-api.com/json/{$ip}"));
2023-10-26 04:57:44 +02:00
if($details && property_exists($details, 'countryCode')) {
2023-05-17 01:16:43 +02:00
2023-11-24 22:55:02 +01:00
$c = Country::query()->where('iso_3166_2', $details->countryCode)->first();
2023-05-17 01:16:43 +02:00
2023-10-26 04:57:44 +02:00
if($c) {
2023-05-17 01:16:43 +02:00
return (string)$c->id;
2023-10-26 04:57:44 +02:00
}
2023-05-17 01:16:43 +02:00
}
2023-10-26 04:57:44 +02:00
} catch(\Exception $e) {
2023-05-17 01:16:43 +02:00
nlog("Could not resolve country => {$e->getMessage()}");
}
return '840';
}
private function spanishSetup(Company $company): Company
{
try {
$custom_fields = new \stdClass;
$custom_fields->contact1 = "Rol|CONTABLE,FISCAL,GESTOR,RECEPTOR,TRAMITADOR,PAGADOR,PROPONENTE,B2B_FISCAL,B2B_PAYER,B2B_BUYER,B2B_COLLECTOR,B2B_SELLER,B2B_PAYMENT_RECEIVER,B2B_COLLECTION_RECEIVER,B2B_ISSUER";
$custom_fields->contact2 = "Code|single_line_text";
$custom_fields->contact3 = "Nombre|single_line_text";
$custom_fields->client1 = "Administración Pública|switch";
$company->custom_fields = $custom_fields;
$company->enabled_item_tax_rates = 1;
$settings = $company->settings;
$settings->language_id = '7';
$settings->e_invoice_type = 'Facturae_3.2.2';
$settings->currency_id = '3';
$settings->timezone_id = '42';
$company->settings = $settings;
$company->save();
return $company;
} catch(\Exception $e) {
nlog("SETUP: could not complete setup for Spanish Locale");
}
$company->save();
return $company;
}
private function southAfticaSetup(Company $company): Company
{
try {
$company->enabled_item_tax_rates = 1;
$company->enabled_tax_rates = 1;
$translations = new \stdClass;
$translations->invoice = "Tax Invoice";
$settings = $company->settings;
$settings->currency_id = '4';
$settings->timezone_id = '56';
$settings->translations = $translations;
2023-05-17 01:16:43 +02:00
$company->settings = $settings;
$company->save();
2023-05-17 01:16:43 +02:00
return $company;
2023-10-26 04:57:44 +02:00
} catch(\Exception $e) {
nlog($e->getMessage());
nlog("SETUP: could not complete setup for South African Locale");
2023-05-17 01:16:43 +02:00
}
$company->save();
return $company;
2023-05-17 01:16:43 +02:00
}
private function australiaSetup(Company $company): Company
{
try {
$company->enabled_item_tax_rates = 1;
$company->enabled_tax_rates = 1;
$translations = new \stdClass;
$translations->invoice = "Tax Invoice";
$settings = $company->settings;
$settings->currency_id = '12';
$settings->timezone_id = '109';
$settings->translations = $translations;
$company->settings = $settings;
$company->save();
return $company;
2023-10-26 04:57:44 +02:00
} catch(\Exception $e) {
2023-05-28 09:23:32 +02:00
nlog($e->getMessage());
2023-05-28 08:50:45 +02:00
nlog("SETUP: could not complete setup for Australian Locale");
2023-05-17 01:16:43 +02:00
}
2018-10-24 05:50:15 +02:00
$company->save();
return $company;
2023-05-17 01:16:43 +02:00
2018-10-24 05:50:15 +02:00
}
2023-05-17 01:16:43 +02:00
2018-10-24 05:50:15 +02:00
}