From 693f247865fbf7ade053aeaaf28b15dc762468ce Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 5 Jul 2024 23:36:28 +1000 Subject: [PATCH] Fix handling of discounts in invoiceSummary(), added invoiceSummaryCredit() to show invoices in credit --- app/Models/Account.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/app/Models/Account.php b/app/Models/Account.php index 07ccf5c..0c6fc04 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -27,6 +27,13 @@ class Account extends Model implements IDs /* STATIC */ + public static function InvoicesCredit(Collection $invoices=NULL): Collection + { + return (new self) + ->invoiceSummaryCredit($invoices,TRUE) + ->get(); + } + public static function InvoicesDue(Collection $invoices=NULL): Collection { return (new self) @@ -255,11 +262,11 @@ class Account extends Model implements IDs DB::raw('SUM(item) AS _item'), DB::raw('SUM(tax) AS _tax'), DB::raw('SUM(payments) AS _payment'), - DB::raw('SUM(discount) AS _discount'), + DB::raw('SUM(discount)+COALESCE(invoices.discount_amt,0) AS _discount'), DB::raw('SUM(item_total) AS _item_total'), DB::raw('SUM(payment_fees) AS _payment_fee'), - DB::raw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0) AS NUMERIC),2) AS _total'), - DB::raw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) AS _balance'), + DB::raw('ROUND(CAST(SUM(item_total)-COALESCE(invoices.discount_amt,0) AS NUMERIC),2) AS _total'), + DB::raw('ROUND(CAST(SUM(item_total)-COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) AS _balance'), 'invoices.due_at', 'invoices.created_at', ]) @@ -284,8 +291,8 @@ class Account extends Model implements IDs 'invoice_id', DB::raw('ROUND(CAST(SUM(quantity*price_base) AS NUMERIC),2) AS item'), DB::raw('ROUND(CAST(SUM(amount) AS NUMERIC),2) AS tax'), - DB::raw('SUM(COALESCE(invoice_items.discount_amt,0)) AS discount'), - DB::raw('ROUND(CAST(SUM(ROUND(CAST(quantity*price_base AS NUMERIC),2))+SUM(ROUND(CAST(amount AS NUMERIC),2))-SUM(COALESCE(invoice_items.discount_amt,0)) AS NUMERIC),2) AS item_total'), + DB::raw('ROUND(CAST(SUM(COALESCE(invoice_items.discount_amt,0)) AS NUMERIC),2) AS discount'), + DB::raw('ROUND(CAST(SUM(ROUND(CAST(quantity*price_base AS NUMERIC),2))+SUM(ROUND(CAST(amount AS NUMERIC),2))-SUM(ROUND(CAST(COALESCE(invoice_items.discount_amt,0) AS NUMERIC),2)) AS NUMERIC),2) AS item_total'), DB::raw('0 as payments'), DB::raw('0 as payment_fees'), ]) @@ -305,7 +312,13 @@ class Account extends Model implements IDs public function invoiceSummaryDue(Collection $invoices=NULL,bool $all=FALSE): Builder { return $this->invoiceSummary($invoices,$all) - ->havingRaw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) > 0'); + ->havingRaw('ROUND(CAST(SUM(item_total)-COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) > 0'); + } + + public function invoiceSummaryCredit(Collection $invoices=NULL,bool $all=FALSE): Builder + { + return $this->invoiceSummary($invoices,$all) + ->havingRaw('ROUND(CAST(SUM(item_total)-COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) < 0'); } public function invoiceSummaryPast(Collection $invoices=NULL,bool $all=FALSE): Builder @@ -314,7 +327,7 @@ class Account extends Model implements IDs ->join('payment_items',['payment_items.invoice_id'=>'invoices.id']) ->join('payments',['payments.id'=>'payment_items.payment_id']) ->addSelect(DB::raw('max(paid_at) as _paid_at')) - ->havingRaw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) <= 0'); + ->havingRaw('ROUND(CAST(SUM(item_total)-COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) <= 0'); } /**