1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-23 01:41:34 +02:00
invoiceninja/app/Jobs/Company/CompanyTaxRate.php

104 lines
3.0 KiB
PHP
Raw Normal View History

2023-05-24 14:26:10 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2023-05-24 14:26:10 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Company;
2023-06-02 07:53:33 +02:00
use App\DataMapper\Tax\ZipTax\Response;
2023-10-26 04:57:44 +02:00
use App\DataProviders\USStates;
use App\Libraries\MultiDB;
use App\Models\Company;
2023-05-24 14:26:10 +02:00
use App\Services\Tax\Providers\TaxProvider;
2023-10-26 04:57:44 +02:00
use Illuminate\Bus\Queueable;
2023-05-24 14:26:10 +02:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2023-10-26 04:57:44 +02:00
use Illuminate\Queue\InteractsWithQueue;
2023-05-24 14:40:40 +02:00
use Illuminate\Queue\Middleware\WithoutOverlapping;
2023-10-26 04:57:44 +02:00
use Illuminate\Queue\SerializesModels;
2023-05-24 14:26:10 +02:00
class CompanyTaxRate implements ShouldQueue
{
2024-01-14 05:05:00 +01:00
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
2023-05-24 14:26:10 +02:00
2023-05-24 14:40:40 +02:00
public $tries = 1;
2023-05-24 14:26:10 +02:00
/**
* Create a new job instance.
*
* @param Company $company
*/
public function __construct(public Company $company)
{
}
public function handle()
{
2024-01-14 05:05:00 +01:00
2023-05-24 14:26:10 +02:00
MultiDB::setDB($this->company->db);
$tp = new TaxProvider($this->company);
$tp->updateCompanyTaxData();
2024-01-14 05:05:00 +01:00
2023-06-02 07:53:33 +02:00
if(!$tp->updatedTaxStatus() && $this->company->settings->country_id == '840') {
$calculated_state = false;
/** State must be calculated else default to the company state for taxes */
if(array_key_exists($this->company->settings->state, USStates::get())) {
2023-07-26 00:31:22 +02:00
$calculated_state = $this->company->settings->state;
2023-10-26 04:57:44 +02:00
} else {
2023-06-02 07:53:33 +02:00
2023-10-26 04:57:44 +02:00
try {
2023-06-02 07:53:33 +02:00
$calculated_state = USStates::getState($this->company->settings->postal_code);
2023-10-26 04:57:44 +02:00
} catch(\Exception $e) {
2023-06-02 07:53:33 +02:00
nlog("could not calculate state from postal code => {$this->company->settings->postal_code} or from state {$this->company->settings->state}");
}
2023-10-26 04:57:44 +02:00
if(!$calculated_state && $this->company->tax_data?->seller_subregion) {
2023-06-02 07:53:33 +02:00
$calculated_state = $this->company->tax_data?->seller_subregion;
2023-10-26 04:57:44 +02:00
}
2023-06-02 07:53:33 +02:00
2023-10-26 04:57:44 +02:00
if(!$calculated_state) {
2023-06-02 07:53:33 +02:00
return;
2023-10-26 04:57:44 +02:00
}
2023-06-02 07:53:33 +02:00
}
2024-01-14 05:05:00 +01:00
2023-06-02 07:53:33 +02:00
$data = [
'seller_subregion' => $this->company->origin_tax_data?->seller_subregion ?: '',
'geoPostalCode' => $this->company->settings->postal_code ?? '',
'geoCity' => $this->company->settings->city ?? '',
'geoState' => $calculated_state,
'taxSales' => $this->company->tax_data->regions->US->subregions?->{$calculated_state}?->taxSales ?? 0,
];
$tax_data = new Response($data);
$this->company->origin_tax_data = $tax_data;
$this->company->saveQuietly();
}
2023-05-24 14:26:10 +02:00
}
2023-05-24 14:40:40 +02:00
public function middleware()
{
return [new WithoutOverlapping($this->company->id)];
}
2023-10-26 04:57:44 +02:00
public function failed($e)
{
2023-06-02 07:53:33 +02:00
nlog($e->getMessage());
}
2023-10-26 04:57:44 +02:00
}