diff --git a/app/Jobs/Company/CreateCompany.php b/app/Jobs/Company/CreateCompany.php index 1c0328539a60..c05ff750c44a 100644 --- a/app/Jobs/Company/CreateCompany.php +++ b/app/Jobs/Company/CreateCompany.php @@ -13,12 +13,14 @@ namespace App\Jobs\Company; use App\Utils\Ninja; use App\Models\Company; +use App\Models\Country; use App\Libraries\MultiDB; use App\Utils\Traits\MakesHash; use App\DataMapper\Tax\TaxModel; use App\DataMapper\CompanySettings; use Illuminate\Foundation\Bus\Dispatchable; use App\DataMapper\ClientRegistrationFields; +use App\Factory\TaxRateFactory; class CreateCompany { @@ -53,6 +55,10 @@ class CreateCompany $settings->name = isset($this->request['name']) ? $this->request['name'] : ''; + if($country_id = $this->resolveCountry()){ + $settings->country_id = $country_id; + } + $company = new Company(); $company->account_id = $this->account->id; $company->company_key = $this->createHash(); @@ -74,8 +80,135 @@ class CreateCompany $company->subdomain = ''; } - $company->save(); + /** Location Specific Configuration */ + match($settings->country_id) { + '724' => $company = $this->spanishSetup($company), + '36' => $company = $this->australiaSetup($company), + default => $company->save(), + }; return $company; } + + /** + * Resolve Country + * + * @return string + */ + private function resolveCountry(): string + { + try{ + + $ip = request()->ip(); + + if(request()->hasHeader('cf-ipcountry')){ + + $c = Country::where('iso_3166_2', request()->header('cf-ipcountry'))->first(); + + if($c) + return (string)$c->id; + + } + + $details = json_decode(file_get_contents("http://ip-api.com/json/{$ip}")); + + if($details && property_exists($details, 'countryCode')){ + + $c = Country::where('iso_3166_2', $details->countryCode)->first(); + + if($c) + return (string)$c->id; + + } + } + catch(\Exception $e){ + 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(); + + $user = $company->account->users()->first(); + + $tax_rate = TaxRateFactory::create($company->id, $user->id); + $tax_rate->name = $company->tax_data->regions->EU->subregions->ES->tax_name; + $tax_rate->rate = $company->tax_data->regions->EU->subregions->ES->tax_rate; + $tax_rate->save(); + + return $company; + + } + catch(\Exception $e){ + nlog("SETUP: could not complete setup for Spanish Locale"); + } + + $company->save(); + + return $company; + + } + + 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(); + + $user = $company->account->users()->first(); + + $tax_rate = TaxRateFactory::create($company->id, $user->id); + $tax_rate->name = $company->tax_data->regions->AU->subregions->AU->tax_name; + $tax_rate->rate = $company->tax_data->regions->AU->subregions->AU->tax_rate; + $tax_rate->save(); + + return $company; + + } + catch(\Exception $e){ + nlog("SETUP: could not complete setup for Spanish Locale"); + } + + $company->save(); + + return $company; + + } + } diff --git a/app/Observers/CompanyObserver.php b/app/Observers/CompanyObserver.php index 122518568224..93c2aeaf68c7 100644 --- a/app/Observers/CompanyObserver.php +++ b/app/Observers/CompanyObserver.php @@ -40,6 +40,12 @@ class CompanyObserver //fire event to build new custom portal domain \Modules\Admin\Jobs\Domain\CustomDomain::dispatch($company->getOriginal('portal_domain'), $company)->onQueue('domain'); } + + // if($company->wasChanged()) { + // nlog("updated event"); + // nlog($company->getChanges()); + // } + } /** diff --git a/app/PaymentDrivers/Stripe/Charge.php b/app/PaymentDrivers/Stripe/Charge.php index 05833ccecc63..749b52770903 100644 --- a/app/PaymentDrivers/Stripe/Charge.php +++ b/app/PaymentDrivers/Stripe/Charge.php @@ -43,7 +43,7 @@ class Charge * Create a charge against a payment method. * @param ClientGatewayToken $cgt * @param PaymentHash $payment_hash - * @return bool success/failure + * @return mixed success/failure * @throws \Laracasts\Presenter\Exceptions\PresenterException */ public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash) @@ -86,7 +86,7 @@ class Charge $data['off_session'] = true; } - $response = $this->stripe->createPaymentIntent($data, array_merge($this->stripe->stripe_connect_auth, ['idempotency_key' => uniqid("st", true)])); + $response = $this->stripe->createPaymentIntent($data); SystemLogger::dispatch($response, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->stripe->client, $this->stripe->client->company); } catch (\Exception $e) { diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index bdac90e9c0b2..e5cbc4b895c4 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -777,7 +777,8 @@ class StripePaymentDriver extends BaseDriver ->where('token', $request->data['object']['payment_method']) ->first(); - $clientgateway->delete(); + if($clientgateway) + $clientgateway->delete(); return response()->json([], 200); } elseif ($request->data['object']['status'] == "pending") { diff --git a/app/Services/Invoice/EInvoice/FacturaEInvoice.php b/app/Services/Invoice/EInvoice/FacturaEInvoice.php index 5a2bcce7c35b..79840dad0beb 100644 --- a/app/Services/Invoice/EInvoice/FacturaEInvoice.php +++ b/app/Services/Invoice/EInvoice/FacturaEInvoice.php @@ -18,6 +18,7 @@ use josemmo\Facturae\FacturaeItem; use josemmo\Facturae\FacturaeParty; use Illuminate\Support\Facades\Storage; use josemmo\Facturae\Common\FacturaeSigner; +use josemmo\Facturae\FacturaeCentre; class FacturaEInvoice extends AbstractService { @@ -25,6 +26,24 @@ class FacturaEInvoice extends AbstractService private $calc; + private $centre_codes = [ + 'CONTABLE' => FacturaeCentre::ROLE_CONTABLE, + 'FISCAL' => FacturaeCentre::ROLE_FISCAL, + 'GESTOR' => FacturaeCentre::ROLE_GESTOR, + 'RECEPTOR' => FacturaeCentre::ROLE_RECEPTOR, + 'TRAMITADOR' => FacturaeCentre::ROLE_TRAMITADOR, + 'PAGADOR' => FacturaeCentre::ROLE_PAGADOR, + 'PROPONENTE' => FacturaeCentre::ROLE_PAGADOR, + 'B2B_FISCAL' => FacturaeCentre::ROLE_B2B_FISCAL, + 'B2B_PAYER' => FacturaeCentre::ROLE_B2B_PAYER, + 'B2B_BUYER' => FacturaeCentre::ROLE_B2B_BUYER, + 'B2B_COLLECTOR' => FacturaeCentre::ROLE_B2B_COLLECTOR, + 'B2B_SELLER' => FacturaeCentre::ROLE_B2B_SELLER, + 'B2B_PAYMENT_RECEIVER' => FacturaeCentre::ROLE_B2B_PAYMENT_RECEIVER , + 'B2B_COLLECTION_RECEIVER' => FacturaeCentre::ROLE_B2B_COLLECTION_RECEIVER , + 'B2B_ISSUER' => FacturaeCentre::ROLE_B2B_ISSUER, + ]; + // Facturae::SCHEMA_3_2 Invoice Format 3.2 // Facturae::SCHEMA_3_2_1 Invoice Format 3.2.1 // Facturae::SCHEMA_3_2_2 Invoice Format 3.2.2 @@ -111,6 +130,25 @@ class FacturaEInvoice extends AbstractService // FacturaeCentre::ROLE_B2B_COLLECTION_RECEIVER Collection receiver in FACeB2B // FacturaeCentre::ROLE_B2B_ISSUER Issuer in FACeB2B + /* + const ROLE_CONTABLE = "01"; + const ROLE_FISCAL = "01"; + const ROLE_GESTOR = "02"; + const ROLE_RECEPTOR = "02"; + const ROLE_TRAMITADOR = "03"; + const ROLE_PAGADOR = "03"; + const ROLE_PROPONENTE = "04"; + + const ROLE_B2B_FISCAL = "Fiscal"; + const ROLE_B2B_PAYER = "Payer"; + const ROLE_B2B_BUYER = "Buyer"; + const ROLE_B2B_COLLECTOR = "Collector"; + const ROLE_B2B_SELLER = "Seller"; + const ROLE_B2B_PAYMENT_RECEIVER = "Payment receiver"; + const ROLE_B2B_COLLECTION_RECEIVER = "Collection receiver"; + const ROLE_B2B_ISSUER = "Issuer"; + */ + public function __construct(public Invoice $invoice, private mixed $profile) { @@ -146,6 +184,33 @@ class FacturaEInvoice extends AbstractService } + /** Check if this is a public administration body */ + private function setFace(): array + { + $facturae_centres = []; + + if($this->invoice->client->custom_value1 == 'yes') + { + + foreach($this->invoice->client->contacts() as $contact) + { + + if(in_array($contact->custom_value1, array_keys($this->centre_codes))) + { + $facturae_centres[] = new FacturaeCentre([ + 'role' => $this->centre_codes[$contact->custom_value1], + 'code' => $contact->custom_value2, + 'name' => $contact->custom_value3, + ]); + } + + } + + } + + return $facturae_centres; + } + private function setPoNumber(): self { if(strlen($this->invoice->po_number) > 1) { @@ -280,6 +345,7 @@ class FacturaEInvoice extends AbstractService "fax" => "", "website" => substr($company->settings->website, 0, 50), "contactPeople" => substr($company->owner()->present()->name(), 0, 40), + 'centres' => $this->setFace(), // "cnoCnae" => "04647", // Clasif. Nacional de Act. Económicas // "ineTownCode" => "280796" // Cód. de municipio del INE ]);