Skip to content

Commit

Permalink
Improvements for rounding calculations for discounted item amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Nov 5, 2024
1 parent ffc1f47 commit d93ac9a
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 355 deletions.
61 changes: 12 additions & 49 deletions app/Helpers/Invoice/InvoiceItemSum.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,46 +207,6 @@ private function shouldCalculateTax(): self
return $this;
}

private function calculateNexus()
{

$company_country_code = $this->invoice->company->country()->iso_3166_2;
$client_country_code = $this->client->country->iso_3166_2;
$base_rule = new \App\DataMapper\Tax\BaseRule();
$eu_countries = $base_rule->eu_country_codes;
$nexus_rule = $company_country_code;

if ($client_country_code == $company_country_code) {
//Domestic Sales
$nexus_rule = $company_country_code;
} elseif (in_array($company_country_code, $eu_countries) && !in_array($client_country_code, $eu_countries)) {
//NON-EU Sale
$nexus_rule = $company_country_code;
} elseif (in_array($company_country_code, $eu_countries) && in_array($client_country_code, $eu_countries)) {

//EU Sale
// Invalid VAT number = seller country nexus
if(isset($this->client->has_valid_vat_number) && !$this->client->has_valid_vat_number){
$nexus_rule = $company_country_code;
}
elseif (isset($this->invoice->company->tax_data->regions->EU->has_sales_above_threshold) && $this->invoice->company->tax_data->regions->EU->has_sales_above_threshold) { //over threshold - tax in buyer country
$nexus_rule = $client_country_code;
} elseif (isset($this->invoice->company->tax_data->regions->EU->has_sales_above_threshold) && !$this->invoice->company->tax_data->regions->EU->has_sales_above_threshold) {
$nexus_rule = $company_country_code;
} elseif ($this->client->classification != 'individual' && (isset($this->client->has_valid_vat_number) && !$this->client->has_valid_vat_number)){
$nexus_rule = $company_country_code;
} else {
$nexus_rule = $company_country_code;
}

}

nlog($nexus_rule);

$class = "App\DataMapper\Tax\\".str_replace("-", "_", $nexus_rule)."\\Rule";
return $class;
}

private function push(): self
{
$this->sub_total += round($this->getLineTotal(), $this->currency->precision);
Expand Down Expand Up @@ -335,23 +295,23 @@ private function calcTaxes()
$item_tax += $item_tax_rate1_total;

if (strlen($this->item->tax_name1) > 1) {
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total, $amount, $this->item->tax_id);
}

$item_tax_rate2_total = $this->calcAmountLineTax($this->item->tax_rate2, $amount);

$item_tax += $item_tax_rate2_total;

if (strlen($this->item->tax_name2) > 1) {
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total, $amount, $this->item->tax_id);
}

$item_tax_rate3_total = $this->calcAmountLineTax($this->item->tax_rate3, $amount);

$item_tax += $item_tax_rate3_total;

if (strlen($this->item->tax_name3) > 1) {
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total, $amount, $this->item->tax_id);
}

$this->setTotalTaxes($this->formatValue($item_tax, $this->currency->precision));
Expand All @@ -375,6 +335,7 @@ private function getPeppolSurchargeTaxes(): self
->map(fn ($i) => [
'name' => $item->{"tax_name{$i}"} ?? '',
'percentage' => $item->{"tax_rate{$i}"} ?? 0,
'tax_id' => $item->tax_id ?? '1',
])
->filter(fn ($tax) => strlen($tax['name']) > 1);
})
Expand All @@ -400,21 +361,23 @@ private function getPeppolSurchargeTaxes(): self
$tax_component += round($this->invoice->custom_surcharge4 * ($tax['percentage'] / 100), 2);
}

$amount = $this->invoice->custom_surcharge4 + $this->invoice->custom_surcharge3 + $this->invoice->custom_surcharge2 + $this->invoice->custom_surcharge1;

if($tax_component > 0)
$this->groupTax($tax['name'], $tax['percentage'], $tax_component);
$this->groupTax($tax['name'], $tax['percentage'], $tax_component, $amount, $tax['tax_id']);

});

return $this;
}

private function groupTax($tax_name, $tax_rate, $tax_total)
private function groupTax($tax_name, $tax_rate, $tax_total, $amount, $tax_id = '')
{
$group_tax = [];

$key = str_replace(' ', '', $tax_name.$tax_rate);

$group_tax = ['key' => $key, 'total' => $tax_total, 'tax_name' => $tax_name.' '.Number::formatValueNoTrailingZeroes(floatval($tax_rate), $this->client).'%'];
$group_tax = ['key' => $key, 'total' => $tax_total, 'tax_name' => $tax_name.' '.Number::formatValueNoTrailingZeroes(floatval($tax_rate), $this->client).'%', 'tax_id' => $tax_id, 'tax_rate' => $tax_rate, 'base_amount' => $amount];

$this->tax_collection->push(collect($group_tax));
}
Expand Down Expand Up @@ -516,23 +479,23 @@ public function calcTaxesWithAmountDiscount()
$item_tax += $item_tax_rate1_total;

if ($item_tax_rate1_total != 0) {
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total, $amount, $this->item->tax_id);
}

$item_tax_rate2_total = $this->calcAmountLineTax($this->item->tax_rate2, $amount);

$item_tax += $item_tax_rate2_total;

if ($item_tax_rate2_total != 0) {
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total, $amount, $this->item->tax_id);
}

$item_tax_rate3_total = $this->calcAmountLineTax($this->item->tax_rate3, $amount);

$item_tax += $item_tax_rate3_total;

if ($item_tax_rate3_total != 0) {
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total, $amount, $this->item->tax_id);
}

$this->item->gross_line_total = $this->getLineTotal() + $item_tax;
Expand Down
16 changes: 8 additions & 8 deletions app/Helpers/Invoice/InvoiceItemSumInclusive.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,23 +244,23 @@ private function calcTaxes()
$item_tax += $this->formatValue($item_tax_rate1_total, $this->currency->precision);

if (strlen($this->item->tax_name1) > 1) {
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total, $amount, $this->item->tax_id);
}

$item_tax_rate2_total = $this->calcInclusiveLineTax($this->item->tax_rate2, $amount);

$item_tax += $this->formatValue($item_tax_rate2_total, $this->currency->precision);

if (strlen($this->item->tax_name2) > 1) {
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total, $amount, $this->item->tax_id);
}

$item_tax_rate3_total = $this->calcInclusiveLineTax($this->item->tax_rate3, $amount);

$item_tax += $this->formatValue($item_tax_rate3_total, $this->currency->precision);

if (strlen($this->item->tax_name3) > 1) {
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total, $amount, $this->item->tax_id);
}

$this->item->tax_amount = $this->formatValue($item_tax, $this->currency->precision);
Expand All @@ -270,13 +270,13 @@ private function calcTaxes()
return $this;
}

private function groupTax($tax_name, $tax_rate, $tax_total)
private function groupTax($tax_name, $tax_rate, $tax_total, $amount, $tax_id = '')
{
$group_tax = [];

$key = str_replace(' ', '', $tax_name.$tax_rate);

$group_tax = ['key' => $key, 'total' => $tax_total, 'tax_name' => $tax_name.' '.Number::formatValueNoTrailingZeroes(floatval($tax_rate), $this->client).'%'];
$group_tax = ['key' => $key, 'total' => $tax_total, 'tax_name' => $tax_name.' '.Number::formatValueNoTrailingZeroes(floatval($tax_rate), $this->client).'%', 'tax_id' => $tax_id, 'base_amoount' => $amount];

$this->tax_collection->push(collect($group_tax));
}
Expand Down Expand Up @@ -376,23 +376,23 @@ public function calcTaxesWithAmountDiscount()
$item_tax += $item_tax_rate1_total;

if ($item_tax_rate1_total != 0) {
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total);
$this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total, $amount, $this->item->tax_id);
}

$item_tax_rate2_total = $this->calcInclusiveLineTax($this->item->tax_rate2, $amount);

$item_tax += $item_tax_rate2_total;

if ($item_tax_rate2_total != 0) {
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total);
$this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total, $amount, $this->item->tax_id);
}

$item_tax_rate3_total = $this->calcInclusiveLineTax($this->item->tax_rate3, $amount);

$item_tax += $item_tax_rate3_total;

if ($item_tax_rate3_total != 0) {
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total);
$this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total, $amount, $this->item->tax_id);
}

$this->setTotalTaxes($this->getTotalTaxes() + $item_tax);
Expand Down
16 changes: 15 additions & 1 deletion app/Helpers/Invoice/InvoiceSum.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,26 @@ public function setTaxMap(): self
$tax_name = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->pluck('tax_name')->first();

$tax_rate = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->pluck('tax_rate')->first();

$tax_id = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->pluck('tax_id')->first();

$total_line_tax = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->sum('total');

$base_amount = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->sum('base_amount');

$tax_id = $values->first()['tax_id'] ?? '';

$this->tax_map[] = ['name' => $tax_name, 'total' => $total_line_tax];
$this->tax_map[] = ['name' => $tax_name, 'total' => $total_line_tax, 'tax_id' => $tax_id, 'tax_rate' => $tax_rate, 'base_amount' => $base_amount];

$this->total_taxes += $total_line_tax;
}
Expand Down
18 changes: 16 additions & 2 deletions app/Helpers/Invoice/InvoiceSumInclusive.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,19 +368,33 @@ public function setTaxMap()
$values = $this->invoice_items->getGroupedTaxes();

foreach ($keys as $key) {

$tax_name = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->pluck('tax_name')->first();

$tax_rate = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->pluck('tax_rate')->first();

$tax_id = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->pluck('tax_id')->first();

$total_line_tax = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->sum('total');

$base_amount = $values->filter(function ($value, $k) use ($key) {
return $value['key'] == $key;
})->sum('base_amount');

//$total_line_tax -= $this->discount($total_line_tax);
$tax_id = $values->first()['tax_id'] ?? '';

$this->tax_map[] = ['name' => $tax_name, 'total' => $total_line_tax];
$this->tax_map[] = ['name' => $tax_name, 'total' => $total_line_tax, 'tax_id' => $tax_id, 'tax_rate' => $tax_rate, 'base_amount' => $base_amount];

$this->total_taxes += $total_line_tax;

}

return $this;
Expand Down
Loading

0 comments on commit d93ac9a

Please sign in to comment.