Fixes for invoice sum calculations

This commit is contained in:
David Bomba 2022-06-07 21:07:14 +10:00
parent d7c7289ca9
commit 25b48994d6
5 changed files with 24 additions and 29 deletions

View File

@ -52,10 +52,10 @@ class InvoiceItemSum
$this->invoice = $invoice; $this->invoice = $invoice;
if($this->invoice->vendor) if($this->invoice->client)
$this->currency = $this->invoice->vendor->currency();
else
$this->currency = $this->invoice->client->currency(); $this->currency = $this->invoice->client->currency();
else
$this->currency = $this->invoice->vendor->currency();
$this->line_items = []; $this->line_items = [];
} }

View File

@ -46,7 +46,10 @@ class InvoiceItemSumInclusive
$this->invoice = $invoice; $this->invoice = $invoice;
$this->currency = $this->invoice->client->currency(); if($this->invoice->client)
$this->currency = $this->invoice->client->currency();
else
$this->currency = $this->invoice->vendor->currency();
$this->line_items = []; $this->line_items = [];
} }

View File

@ -55,10 +55,10 @@ class InvoiceSum
{ {
$this->invoice = $invoice; $this->invoice = $invoice;
if($this->invoice->vendor) if($this->invoice->client)
$this->precision = $this->invoice->vendor->currency()->precision;
else
$this->precision = $this->invoice->client->currency()->precision; $this->precision = $this->invoice->client->currency()->precision;
else
$this->precision = $this->invoice->vendor->currency()->precision;
$this->tax_map = new Collection; $this->tax_map = new Collection;
} }

View File

@ -41,6 +41,7 @@ class InvoiceSumInclusive
private $sub_total; private $sub_total;
private $precision;
/** /**
* Constructs the object with Invoice and Settings object. * Constructs the object with Invoice and Settings object.
* *
@ -50,6 +51,11 @@ class InvoiceSumInclusive
{ {
$this->invoice = $invoice; $this->invoice = $invoice;
if($this->invoice->client)
$this->precision = $this->invoice->client->currency()->precision;
else
$this->precision = $this->invoice->vendor->currency()->precision;
$this->tax_map = new Collection; $this->tax_map = new Collection;
} }
@ -164,32 +170,15 @@ class InvoiceSumInclusive
private function calculateTotals() private function calculateTotals()
{ {
//$this->total += $this->total_taxes;
// if (is_numeric($this->invoice->custom_value1)) {
// $this->total += $this->invoice->custom_value1;
// }
// if (is_numeric($this->invoice->custom_value2)) {
// $this->total += $this->invoice->custom_value2;
// }
// if (is_numeric($this->invoice->custom_value3)) {
// $this->total += $this->invoice->custom_value3;
// }
// if (is_numeric($this->invoice->custom_value4)) {
// $this->total += $this->invoice->custom_value4;
// }
return $this; return $this;
} }
public function getRecurringInvoice() public function getRecurringInvoice()
{ {
$this->invoice->amount = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); $this->invoice->amount = $this->formatValue($this->getTotal(), $this->precision);
$this->invoice->total_taxes = $this->getTotalTaxes(); $this->invoice->total_taxes = $this->getTotalTaxes();
$this->invoice->balance = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); $this->invoice->balance = $this->formatValue($this->getTotal(), $this->precision);
$this->invoice->saveQuietly(); $this->invoice->saveQuietly();
@ -249,14 +238,14 @@ class InvoiceSumInclusive
if ($this->invoice->amount != $this->invoice->balance) { if ($this->invoice->amount != $this->invoice->balance) {
$paid_to_date = $this->invoice->amount - $this->invoice->balance; $paid_to_date = $this->invoice->amount - $this->invoice->balance;
$this->invoice->balance = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision) - $paid_to_date; $this->invoice->balance = $this->formatValue($this->getTotal(), $this->precision) - $paid_to_date;
} else { } else {
$this->invoice->balance = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); $this->invoice->balance = $this->formatValue($this->getTotal(), $this->precision);
} }
} }
/* Set new calculated total */ /* Set new calculated total */
$this->invoice->amount = $this->formatValue($this->getTotal(), $this->invoice->client->currency()->precision); $this->invoice->amount = $this->formatValue($this->getTotal(), $this->precision);
$this->invoice->total_taxes = $this->getTotalTaxes(); $this->invoice->total_taxes = $this->getTotalTaxes();

View File

@ -107,6 +107,9 @@ class Vendor extends BaseModel
if(!$currencies) if(!$currencies)
$this->buildCache(true); $this->buildCache(true);
if(!$this->currency_id)
$this->currency_id = 1;
return $currencies->filter(function ($item) { return $currencies->filter(function ($item) {
return $item->id == $this->currency_id; return $item->id == $this->currency_id;
})->first(); })->first();