From 19ba732adf99d90de871e6bb80d2907ec560141f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 1 Aug 2024 11:25:11 +1000 Subject: [PATCH] Add client model to invoice calculators --- app/Export/CSV/BaseExport.php | 3 --- app/Helpers/Invoice/InvoiceItemSum.php | 5 +++-- .../Invoice/InvoiceItemSumInclusive.php | 5 +++-- app/Helpers/Invoice/InvoiceSum.php | 17 +++++++------- app/Helpers/Invoice/InvoiceSumInclusive.php | 22 +++++++++---------- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index a3bac0bb1204..4ea22fe974ee 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -1269,9 +1269,6 @@ class BaseExport $custom_start_date = now()->startOfYear(); $custom_end_date = now(); } - - nlog($custom_start_date->format('Y-m-d')); - nlog($custom_end_date->format('Y-m-d')); switch ($date_range) { case 'all': diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 9f913c42a8bc..d1d7dd56cff0 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -15,6 +15,7 @@ use App\Models\Quote; use App\Utils\Number; use App\Models\Client; use App\Models\Credit; +use App\Models\Vendor; use App\Models\Invoice; use App\Models\PurchaseOrder; use App\Models\RecurringQuote; @@ -121,7 +122,7 @@ class InvoiceItemSum private $tax_collection; - private ?Client $client; + private Client | Vendor $client; private bool $calc_tax = false; @@ -132,10 +133,10 @@ class InvoiceItemSum $this->tax_collection = collect([]); $this->invoice = $invoice; + $this->client = $invoice->client ?? $invoice->vendor; if ($this->invoice->client) { $this->currency = $this->invoice->client->currency(); - $this->client = $this->invoice->client; $this->shouldCalculateTax(); } else { $this->currency = $this->invoice->vendor->currency(); diff --git a/app/Helpers/Invoice/InvoiceItemSumInclusive.php b/app/Helpers/Invoice/InvoiceItemSumInclusive.php index 8bcf87fc25a3..df9a897abefa 100644 --- a/app/Helpers/Invoice/InvoiceItemSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceItemSumInclusive.php @@ -15,6 +15,7 @@ use App\Models\Quote; use App\Utils\Number; use App\Models\Client; use App\Models\Credit; +use App\Models\Vendor; use App\Models\Invoice; use App\Models\PurchaseOrder; use App\Models\RecurringQuote; @@ -110,7 +111,7 @@ class InvoiceItemSumInclusive private bool $calc_tax = false; - private ?Client $client; + private Client | Vendor $client; private RuleInterface $rule; @@ -119,10 +120,10 @@ class InvoiceItemSumInclusive $this->tax_collection = collect([]); $this->invoice = $invoice; + $this->client = $invoice->client ?? $invoice->vendor; if ($this->invoice->client) { $this->currency = $this->invoice->client->currency(); - $this->client = $this->invoice->client; $this->shouldCalculateTax(); } else { $this->currency = $this->invoice->vendor->currency(); diff --git a/app/Helpers/Invoice/InvoiceSum.php b/app/Helpers/Invoice/InvoiceSum.php index c2979f73219e..12cb3a1b16de 100644 --- a/app/Helpers/Invoice/InvoiceSum.php +++ b/app/Helpers/Invoice/InvoiceSum.php @@ -11,12 +11,14 @@ namespace App\Helpers\Invoice; +use App\Models\Client; use App\Models\Credit; use App\Models\Invoice; use App\Models\PurchaseOrder; use App\Models\Quote; use App\Models\RecurringInvoice; use App\Models\RecurringQuote; +use App\Models\Vendor; use App\Utils\Number; use App\Utils\Traits\NumberFormatter; use Illuminate\Support\Collection; @@ -50,6 +52,8 @@ class InvoiceSum private $precision; + private Client | Vendor $client; + public InvoiceItemSum $invoice_items; private $rappen_rounding = false; @@ -60,18 +64,15 @@ class InvoiceSum */ public function __construct($invoice) { + $this->invoice = $invoice; + $this->client = $invoice->client ?? $invoice->vendor; - if ($this->invoice->client) { - $this->precision = $this->invoice->client->currency()->precision; - $this->rappen_rounding = $this->invoice->client->getSetting('enable_rappen_rounding'); - } else { - $this->precision = $this->invoice->vendor->currency()->precision; - $this->rappen_rounding = $this->invoice->vendor->getSetting('enable_rappen_rounding'); - - } + $this->precision = $this->client->currency()->precision; + $this->rappen_rounding = $this->client->getSetting('enable_rappen_rounding'); $this->tax_map = new Collection(); + } public function build() diff --git a/app/Helpers/Invoice/InvoiceSumInclusive.php b/app/Helpers/Invoice/InvoiceSumInclusive.php index 8b0b6f95cc93..07fdf3e86894 100644 --- a/app/Helpers/Invoice/InvoiceSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceSumInclusive.php @@ -13,7 +13,9 @@ namespace App\Helpers\Invoice; use App\Models\Quote; use App\Utils\Number; +use App\Models\Client; use App\Models\Credit; +use App\Models\Vendor; use App\Models\Invoice; use App\Models\PurchaseOrder; use App\Models\RecurringQuote; @@ -50,6 +52,8 @@ class InvoiceSumInclusive private $rappen_rounding = false; + private Client | Vendor $client; + public InvoiceItemSumInclusive $invoice_items; /** * Constructs the object with Invoice and Settings object. @@ -59,14 +63,10 @@ class InvoiceSumInclusive public function __construct($invoice) { $this->invoice = $invoice; - - if ($this->invoice->client) { - $this->precision = $this->invoice->client->currency()->precision; - $this->rappen_rounding = $this->invoice->client->getSetting('enable_rappen_rounding'); - } else { - $this->precision = $this->invoice->vendor->currency()->precision; - $this->rappen_rounding = $this->invoice->vendor->getSetting('enable_rappen_rounding'); - } + $this->client = $invoice->client ?? $invoice->vendor; + + $this->precision = $this->client->currency()->precision; + $this->rappen_rounding = $this->client->getSetting('enable_rappen_rounding'); $this->tax_map = new Collection(); } @@ -158,19 +158,19 @@ class InvoiceSumInclusive $tax = $this->calcInclusiveLineTax($this->invoice->tax_rate1, $amount); $this->total_taxes += $tax; - $this->total_tax_map[] = ['name' => $this->invoice->tax_name1.' '.Number::formatValueNoTrailingZeroes(floatval($this->invoice->tax_rate1), $this->invoice->client).'%', 'total' => $tax]; + $this->total_tax_map[] = ['name' => $this->invoice->tax_name1.' '.Number::formatValueNoTrailingZeroes(floatval($this->invoice->tax_rate1), $this->client).'%', 'total' => $tax]; } if (is_string($this->invoice->tax_name2) && strlen($this->invoice->tax_name2) > 1) { $tax = $this->calcInclusiveLineTax($this->invoice->tax_rate2, $amount); $this->total_taxes += $tax; - $this->total_tax_map[] = ['name' => $this->invoice->tax_name2.' '.Number::formatValueNoTrailingZeroes(floatval($this->invoice->tax_rate2), $this->invoice->client).'%', 'total' => $tax]; + $this->total_tax_map[] = ['name' => $this->invoice->tax_name2.' '.Number::formatValueNoTrailingZeroes(floatval($this->invoice->tax_rate2), $this->client).'%', 'total' => $tax]; } if (is_string($this->invoice->tax_name3) && strlen($this->invoice->tax_name3) > 1) { $tax = $this->calcInclusiveLineTax($this->invoice->tax_rate3, $amount); $this->total_taxes += $tax; - $this->total_tax_map[] = ['name' => $this->invoice->tax_name3.' '.Number::formatValueNoTrailingZeroes(floatval($this->invoice->tax_rate3), $this->invoice->client).'%', 'total' => $tax]; + $this->total_tax_map[] = ['name' => $this->invoice->tax_name3.' '.Number::formatValueNoTrailingZeroes(floatval($this->invoice->tax_rate3), $this->client).'%', 'total' => $tax]; } return $this;