2023-05-24 14:26:10 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Company;
|
|
|
|
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use Illuminate\Bus\Queueable;
|
2023-06-02 07:53:33 +02:00
|
|
|
use App\DataProviders\USStates;
|
2023-05-24 14:26:10 +02:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
2023-06-02 07:53:33 +02:00
|
|
|
use App\DataMapper\Tax\ZipTax\Response;
|
2023-05-24 14:26:10 +02:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use App\Services\Tax\Providers\TaxProvider;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2023-05-24 14:40:40 +02:00
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
2023-05-24 14:26:10 +02:00
|
|
|
|
|
|
|
class CompanyTaxRate implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
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()
|
|
|
|
{
|
2023-06-02 07:53:33 +02:00
|
|
|
|
2023-05-24 14:26:10 +02:00
|
|
|
MultiDB::setDB($this->company->db);
|
|
|
|
|
|
|
|
$tp = new TaxProvider($this->company);
|
|
|
|
$tp->updateCompanyTaxData();
|
|
|
|
|
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-06-02 07:53:33 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
try{
|
|
|
|
$calculated_state = USStates::getState($this->company->settings->postal_code);
|
|
|
|
}
|
|
|
|
catch(\Exception $e){
|
|
|
|
nlog("could not calculate state from postal code => {$this->company->settings->postal_code} or from state {$this->company->settings->state}");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$calculated_state && $this->company->tax_data?->seller_subregion)
|
|
|
|
$calculated_state = $this->company->tax_data?->seller_subregion;
|
|
|
|
|
|
|
|
if(!$calculated_state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$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-06-02 07:53:33 +02:00
|
|
|
public function failed($e){
|
|
|
|
nlog($e->getMessage());
|
|
|
|
}
|
2023-05-24 14:26:10 +02:00
|
|
|
}
|