Update company tax data

This commit is contained in:
David Bomba 2023-05-24 23:06:27 +10:00
parent 32daee0fa6
commit 36449fa56c
4 changed files with 47 additions and 20 deletions

View File

@ -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();

View File

@ -188,7 +188,7 @@ class InvoiceItemSum
->setEntity($this->invoice)
->init();
$this->calc_tax = true;
$this->calc_tax = $this->rule->shouldCalcTax();
return $this;
}

View File

@ -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);

View File

@ -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();
});
}