Fix dashboard SQL query error, doesnt work with postgres

This commit is contained in:
Deon George 2024-07-03 13:52:16 +10:00
parent 74864e66cb
commit 1b4504cee2
2 changed files with 18 additions and 17 deletions

View File

@ -350,13 +350,12 @@ class User extends Authenticatable implements IDs
->select([ ->select([
'invoice_id', 'invoice_id',
DB::raw('invoice_items.id AS invoice_item_id'), DB::raw('invoice_items.id AS invoice_item_id'),
DB::raw('IFNULL(invoice_items.discount_amt,0) AS discount'), DB::raw('COALESCE(invoice_items.discount_amt,0) AS discount'),
DB::raw('ROUND(CAST(quantity*price_base AS decimal(8,2)),2) AS base'), DB::raw('quantity*price_base AS base'),
DB::raw('ROUND(invoice_item_taxes.amount,2) AS tax'), DB::raw('invoice_item_taxes.amount AS tax'),
]) ])
->leftjoin('invoice_item_taxes',['invoice_item_taxes.invoice_item_id'=>'invoice_items.id']) ->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 * @return \Illuminate\Database\Query\Builder
* @todo change this to just return outstanding invoices as a collection. * @todo change this to just return outstanding invoices as a collection.
* @deprecated Use account->invoiceSummary
*/ */
public function query_invoice_summary() public function query_invoice_summary()
{ {
@ -390,23 +390,23 @@ class User extends Authenticatable implements IDs
DB::raw('SUM(discount) AS discount'), DB::raw('SUM(discount) AS discount'),
DB::raw('SUM(base) AS base'), DB::raw('SUM(base) AS base'),
DB::raw('SUM(tax) AS tax'), DB::raw('SUM(tax) AS tax'),
DB::raw('ROUND(SUM(base)+SUM(tax)-SUM(discount),2) AS total'), DB::raw('base+tax-discount AS total'),
DB::raw('false AS payments'), DB::raw('0 AS payments'),
DB::raw('false AS payment_fees'), DB::raw('0 AS payment_fees'),
]) ])
->from($this->query_invoice_items(),'II') ->from($this->query_invoice_items(),'II')
->join('invoices',['invoices.id'=>'II.invoice_id']) ->join('invoices',['invoices.id'=>'II.invoice_id'])
->whereIN('account_id',$this->accounts->pluck('id')) ->whereIN('account_id',$this->accounts->pluck('id'))
->where('invoices.active',TRUE) ->where('invoices.active',TRUE)
->groupBy(['invoice_id']); ->groupBy(['invoice_id','base','tax','discount']);
$payments = (new Payment) $payments = (new Payment)
->select([ ->select([
'invoice_id', 'invoice_id',
DB::raw('false AS discount'), DB::raw('0 AS discount'),
DB::raw('false AS base'), DB::raw('0 AS base'),
DB::raw('false AS tax'), DB::raw('0 AS tax'),
DB::raw('false AS total'), DB::raw('0 AS total'),
DB::raw('SUM(allocate) AS payments'), DB::raw('SUM(allocate) AS payments'),
DB::raw('SUM(fees_amt) AS payment_fees'), DB::raw('SUM(fees_amt) AS payment_fees'),
]) ])
@ -442,11 +442,12 @@ class User extends Authenticatable implements IDs
'invoice_total', 'invoice_total',
'payments', 'payments',
'payment_fees', 'payment_fees',
DB::raw('ROUND(invoice_total-payments,2) AS balance'), DB::raw('invoice_total-payments AS balance'),
]) ])
->join('invoices',['invoices.id'=>'invoice_id']) ->join('invoices',['invoices.id'=>'invoice_id'])
->with(['items.taxes']) ->with(['items.taxes'])
->from($summary,'summary'); ->from($summary,'summary')
->groupBy(['id','account_id','discount','invoice_base','invoice_tax','invoice_total','payments','payment_fees']);
} }
/** /**

View File

@ -6,7 +6,7 @@
</div> </div>
<div class="card-body"> <div class="card-body">
@if($o->query_invoice_summary()->having('balance','>',0)->count()) @if($o->query_invoice_summary()->havingRaw('invoice_total-payments > 0')->count())
<table class="table table-bordered w-100" id="reseller_invoices_due"> <table class="table table-bordered w-100" id="reseller_invoices_due">
<thead> <thead>
<tr> <tr>
@ -21,7 +21,7 @@
</thead> </thead>
<tbody> <tbody>
@foreach ($o->query_invoice_summary()->having('balance','>',0)->get() as $oo) @foreach ($o->query_invoice_summary()->havingRaw('invoice_total-payments > 0')->get() as $oo)
<tr @if ($oo->due_at->isPast()) class="table-danger" @endif> <tr @if ($oo->due_at->isPast()) class="table-danger" @endif>
<td>{{ $oo->account->name }}</td> <td>{{ $oo->account->name }}</td>
<td><a href="{{ url('u/invoice',$oo->id) }}">{{ $oo->sid }}</a></td> <td><a href="{{ url('u/invoice',$oo->id) }}">{{ $oo->sid }}</a></td>