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

101 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
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @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
{
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-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
}
$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
}