id); } /** * Payment System ID * * @return string */ public function getSIDAttribute(): string { return sprintf('%02s-%04s#%s',$this->site_id,$this->account_id,$this->getLIDattribute()); } /* RELATIONS */ public function account() { return $this->belongsTo(Account::class); } public function checkout() { return $this->belongsTo(Checkout::class); } public function items() { return $this->hasMany(PaymentItem::class); } /* SCOPES */ /** * Search for a record * * @param $query * @param string $term * @return mixed */ public function scopeSearch($query,string $term) { // Build our where clause $query->where('id','like','%'.$term.'%'); return $query; } public function scopeUnapplied($query) { return $query ->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); } /* ATTRIBUTES */ /** * Work out the remaining amount from this payment that has not been allocated. * * @return float */ public function getBalanceAttribute(): float { $balance = $this->getTotalAttribute()-$this->items->sum('amount'); return ($balance < self::threshold) ? 0 : $balance; } /** * The total of the payment. * * @return float * @todo Change the DB column to total */ public function getTotalAttribute(): float { return sprintf('%3.2f',$this->total_amt); } }