diff --git a/app/DataMapper/Tax/BaseRule.php b/app/DataMapper/Tax/BaseRule.php index 7fba1ff96c91..c30f8a5d7a21 100644 --- a/app/DataMapper/Tax/BaseRule.php +++ b/app/DataMapper/Tax/BaseRule.php @@ -119,6 +119,8 @@ class BaseRule implements RuleInterface public mixed $invoice; + private bool $should_calc_tax = true; + public function __construct() { } @@ -128,6 +130,10 @@ class BaseRule implements RuleInterface return $this; } + public function shouldCalcTax(): bool + { + return $this->should_calc_tax; + } /** * Initializes the tax rule for the entity. * @@ -185,17 +191,16 @@ class BaseRule implements RuleInterface /** If no company tax data has been configured, lets do that now. */ /** We should never encounter this scenario */ - // if(!$company->origin_tax_data && \DB::transactionLevel() == 0) - // { - - // $tp = new TaxProvider($company); - // $tp->updateCompanyTaxData(); - // $company->fresh(); - - // } + if(!$company->origin_tax_data) + { + $this->should_calc_tax = false; + + return $this; + + } /** If we are in a Origin based state, force the company tax here */ - if($company->origin_tax_data?->originDestination == 'O' && ($company->tax_data?->seller_subregion == $this->client_subregion)) { + if($company?->origin_tax_data?->originDestination == 'O' && ($company->tax_data?->seller_subregion == $this->client_subregion)) { $tax_data = $company->origin_tax_data; @@ -203,14 +208,14 @@ class BaseRule implements RuleInterface else{ /** Ensures the client tax data has been updated */ - if(!$this->client->tax_data && \DB::transactionLevel() == 0) { + // if(!$this->client->tax_data && \DB::transactionLevel() == 0) { - $tp = new TaxProvider($company, $this->client); - $tp->updateClientTaxData(); - $this->client->fresh(); - } - - $tax_data = $this->client->tax_data; + // $tp = new TaxProvider($company, $this->client); + // $tp->updateClientTaxData(); + // $this->client->fresh(); + // } + if($this->client->tax_data) + $tax_data = $this->client->tax_data; } @@ -219,7 +224,7 @@ class BaseRule implements RuleInterface /** Applies the tax data to the invoice */ if($this->invoice instanceof Invoice && $tax_data) { - $this->invoice->tax_data = $tax_data ; + $this->invoice->tax_data = $tax_data; if(\DB::transactionLevel() == 0) $this->invoice->saveQuietly(); diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 68fb8cf541fe..62999672eb0e 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -188,7 +188,7 @@ class InvoiceItemSum ->setEntity($this->invoice) ->init(); - $this->calc_tax = true; + $this->calc_tax = $this->rule->shouldCalcTax(); return $this; } diff --git a/app/Jobs/Client/UpdateTaxData.php b/app/Jobs/Client/UpdateTaxData.php index ec34e7b62b51..314fbff04d24 100644 --- a/app/Jobs/Client/UpdateTaxData.php +++ b/app/Jobs/Client/UpdateTaxData.php @@ -60,7 +60,6 @@ class UpdateTaxData implements ShouldQueue $tax_provider->updateClientTaxData(); - if (!$this->client->state && $this->client->postal_code) { $this->client->state = USStates::getState($this->client->postal_code); diff --git a/app/Jobs/Company/CompanyTaxRate.php b/app/Jobs/Company/CompanyTaxRate.php index 0ca0d813fa4b..9615cdbbf8a4 100644 --- a/app/Jobs/Company/CompanyTaxRate.php +++ b/app/Jobs/Company/CompanyTaxRate.php @@ -11,9 +11,11 @@ namespace App\Jobs\Company; +use App\Models\Client; use App\Models\Company; use App\Libraries\MultiDB; use Illuminate\Bus\Queueable; +use App\Jobs\Client\UpdateTaxData; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use App\Services\Tax\Providers\TaxProvider; @@ -38,11 +40,32 @@ class CompanyTaxRate implements ShouldQueue public function handle() { + + if(!config('services.tax.zip_tax.key')) { + return; + } + MultiDB::setDB($this->company->db); $tp = new TaxProvider($this->company); - + $tp->updateCompanyTaxData(); + + $tp = null; + + Client::query() + ->where('company_id', $this->company->id) + ->where('is_deleted', false) + ->where('country_id', 840) + ->whereNotNull('postal_code') + ->whereNull('tax_data') + ->whereFalse('is_tax_exempt') + ->cursor() + ->each(function ($client) { + + (new UpdateTaxData($client, $this->company))->handle(); + + }); }