From 61fe84498a12c884c9678626f46e5f7a53995646 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 6 Jul 2024 22:17:41 +1000 Subject: [PATCH] Update invoice query to allow for missing invoice_item_taxes records, discount is now removed from price base before calculating item_total --- app/Models/Account.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Models/Account.php b/app/Models/Account.php index c3b7ed5..461d3fa 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -290,16 +290,16 @@ class Account extends Model implements IDs ->select([ '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('ROUND(CAST(SUM(COALESCE(amount,0)) AS NUMERIC),2) AS tax'), 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('ROUND(CAST(SUM(ROUND(CAST(quantity*(price_base-COALESCE(invoice_items.discount_amt,0)) AS NUMERIC),2))+SUM(ROUND(CAST(COALESCE(amount,0) AS NUMERIC),2)) AS NUMERIC),2) AS item_total'), DB::raw('0 as payments'), DB::raw('0 as payment_fees'), ]) ->leftjoin('invoice_item_taxes',['invoice_item_taxes.invoice_item_id'=>'invoice_items.id']) ->rightjoin('invoices',['invoices.id'=>'invoice_items.invoice_id']) ->where('invoice_items.active',TRUE) - ->where('invoice_item_taxes.active',TRUE) + ->where(fn($query)=>$query->where('invoice_item_taxes.active',TRUE)->orWhereNull('invoice_item_taxes.active')) ->where('invoices.active',TRUE) ->groupBy(['invoice_items.invoice_id']), ),'p')