Woring on invoice is_amount_discounts

This commit is contained in:
David Bomba 2019-10-17 14:14:17 +11:00
parent 66f1f9889b
commit 6fb9ba89ee
7 changed files with 61 additions and 4 deletions

View File

@ -61,7 +61,7 @@ class InvoiceItemFactory
$item->quantity = $faker->numberBetween(1,10);
$item->cost = $faker->randomFloat(2, 1, 1000);
$item->line_total = $item->quantity * $item->cost;
$item->is_amount_discount = false;
$item->is_amount_discount = true;
$item->discount = $faker->numberBetween(1,10);
$item->notes = $faker->realText(20);
$item->product_key = $faker->word();

View File

@ -20,8 +20,9 @@ trait Discounter
public function discount($amount)
{
if($this->invoice->is_amount_discount === true)
if($this->invoice->is_amount_discount == true){
return $this->invoice->discount;
}
return round($amount * ($this->invoice->discount / 100), 2);

View File

@ -213,4 +213,51 @@ class InvoiceItemSum
return $this;
}
/**
* Invoice Amount Discount
*
* The problem, when calculating invoice level discounts,
* the tax collected changes.
*
* We need to synthetically reduce the line_total amounts
* and recalculate the taxes and then pass back
* the updated map
*/
public function calcTaxesWithAmountDiscount()
{
$this->setGroupedTaxes(collect([]));
foreach($this->line_items as $this->item)
{
$item_tax = 0;
$amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount/$this->sub_total));
$item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount);
$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);
$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);
$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->setTotalTaxes($item_tax);
}
}

View File

@ -233,6 +233,9 @@ class InvoiceSum
public function setTaxMap()
{
// if($this->invoice->is_amount_discount == true)
// $this->invoice_items->calcTaxesWithAmountDiscount();
$this->tax_map = collect();
$keys = $this->invoice_items->getGroupedTaxes()->pluck('key')->unique();

View File

@ -42,6 +42,11 @@ trait Taxer
}
public function calcAmountLineTax($tax_rate, $amount)
{
return $this->formatValue(($amount * $tax_rate/100), $this->currency->precision);
}
public function inclusiveTax($tax_rate, $item)
{

View File

@ -267,7 +267,8 @@ class Invoice extends BaseModel
*/
public function calc()
{
//todo will need to switch between inclusive and exclusive implementations of InvoiceSum
$invoice_calc = new InvoiceSum($this, $this->settings);
return $invoice_calc->build();

View File

@ -10,7 +10,7 @@ $factory->define(App\Models\Invoice::class, function (Faker $faker) {
'status_id' => App\Models\Invoice::STATUS_SENT,
'invoice_number' => $faker->ean13(),
'discount' => $faker->numberBetween(1,10),
'is_amount_discount' => false,
'is_amount_discount' => true,
'tax_name1' => 'GST',
'tax_rate1' => 10,
'tax_name2' => 'VAT',