From d5be958de0fbc6e6cc6334b73813f4830e5035a1 Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 3 Jul 2024 13:52:16 +1000 Subject: [PATCH] Fix dashboard SQL query error, doesnt work with postgres --- app/Models/User.php | 31 ++++++++++--------- .../adminlte/r/invoice/widgets/due.blade.php | 4 +-- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index a39cde5..70b925f 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -350,13 +350,12 @@ class User extends Authenticatable implements IDs ->select([ 'invoice_id', DB::raw('invoice_items.id AS invoice_item_id'), - DB::raw('IFNULL(invoice_items.discount_amt,0) AS discount'), - DB::raw('ROUND(CAST(quantity*price_base AS decimal(8,2)),2) AS base'), - DB::raw('ROUND(invoice_item_taxes.amount,2) AS tax'), - + DB::raw('COALESCE(invoice_items.discount_amt,0) AS discount'), + DB::raw('quantity*price_base AS base'), + DB::raw('invoice_item_taxes.amount AS tax'), ]) ->leftjoin('invoice_item_taxes',['invoice_item_taxes.invoice_item_id'=>'invoice_items.id']) - ->where('active',TRUE); + ->where('invoice_items.active',TRUE); } /** @@ -381,6 +380,7 @@ class User extends Authenticatable implements IDs * * @return \Illuminate\Database\Query\Builder * @todo change this to just return outstanding invoices as a collection. + * @deprecated Use account->invoiceSummary */ public function query_invoice_summary() { @@ -390,23 +390,23 @@ class User extends Authenticatable implements IDs DB::raw('SUM(discount) AS discount'), DB::raw('SUM(base) AS base'), DB::raw('SUM(tax) AS tax'), - DB::raw('ROUND(SUM(base)+SUM(tax)-SUM(discount),2) AS total'), - DB::raw('false AS payments'), - DB::raw('false AS payment_fees'), + DB::raw('base+tax-discount AS total'), + DB::raw('0 AS payments'), + DB::raw('0 AS payment_fees'), ]) ->from($this->query_invoice_items(),'II') ->join('invoices',['invoices.id'=>'II.invoice_id']) ->whereIN('account_id',$this->accounts->pluck('id')) ->where('invoices.active',TRUE) - ->groupBy(['invoice_id']); + ->groupBy(['invoice_id','base','tax','discount']); $payments = (new Payment) ->select([ 'invoice_id', - DB::raw('false AS discount'), - DB::raw('false AS base'), - DB::raw('false AS tax'), - DB::raw('false AS total'), + DB::raw('0 AS discount'), + DB::raw('0 AS base'), + DB::raw('0 AS tax'), + DB::raw('0 AS total'), DB::raw('SUM(allocate) AS payments'), DB::raw('SUM(fees_amt) AS payment_fees'), ]) @@ -442,11 +442,12 @@ class User extends Authenticatable implements IDs 'invoice_total', 'payments', 'payment_fees', - DB::raw('ROUND(invoice_total-payments,2) AS balance'), + DB::raw('invoice_total-payments AS balance'), ]) ->join('invoices',['invoices.id'=>'invoice_id']) ->with(['items.taxes']) - ->from($summary,'summary'); + ->from($summary,'summary') + ->groupBy(['id','account_id','discount','invoice_base','invoice_tax','invoice_total','payments','payment_fees']); } /** diff --git a/resources/views/theme/backend/adminlte/r/invoice/widgets/due.blade.php b/resources/views/theme/backend/adminlte/r/invoice/widgets/due.blade.php index a095b8d..4a60296 100644 --- a/resources/views/theme/backend/adminlte/r/invoice/widgets/due.blade.php +++ b/resources/views/theme/backend/adminlte/r/invoice/widgets/due.blade.php @@ -6,7 +6,7 @@
- @if($o->query_invoice_summary()->having('balance','>',0)->count()) + @if($o->query_invoice_summary()->havingRaw('invoice_total-payments > 0')->count()) @@ -21,7 +21,7 @@ - @foreach ($o->query_invoice_summary()->having('balance','>',0)->get() as $oo) + @foreach ($o->query_invoice_summary()->havingRaw('invoice_total-payments > 0')->get() as $oo) due_at->isPast()) class="table-danger" @endif>
{{ $oo->account->name }} {{ $oo->sid }}