From d1fbb4b42bca225cfc864a9510043fd46156fddf Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 13 Jun 2022 18:01:29 +1000 Subject: [PATCH] Count negative payment_items towards payment balance --- app/Models/Payment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/Payment.php b/app/Models/Payment.php index a3fe116..3434074 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -95,7 +95,7 @@ class Payment extends Model implements IDs public function scopeUnapplied($query) { return $query - ->select(['payments.id','paid_at','account_id','checkout_id','total_amt',DB::raw("SUM(amount) as allocated")]) + ->select(['payments.id','paid_at','account_id','checkout_id','total_amt',DB::raw("SUM(ABS(amount)) as allocated")]) ->leftJoin('payment_items',['payment_items.payment_id'=>'payments.id']) ->groupBy(['payments.id','paid_at','total_amt','account_id','checkout_id']) ->having(DB::raw('ROUND(total_amt-IFNULL(allocated,0),2)'),'>',self::threshold);