Optimise product tables
This commit is contained in:
@@ -263,7 +263,7 @@ class Invoice extends Model implements IDs
|
||||
{
|
||||
return $this->paymentitems
|
||||
->filter(function($item) { return ! $item->payment->pending_status; })
|
||||
->sum('alloc_amt');
|
||||
->sum('amount');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -293,7 +293,7 @@ class Invoice extends Model implements IDs
|
||||
{
|
||||
return $this->paymentitems
|
||||
->filter(function($item) { return $item->payment->pending_status; })
|
||||
->sum('alloc_amt');
|
||||
->sum('amount');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -26,11 +26,9 @@ class Payment extends Model implements IDs
|
||||
{
|
||||
use PushNew;
|
||||
|
||||
const CREATED_AT = 'date_orig';
|
||||
const UPDATED_AT = 'date_last';
|
||||
|
||||
protected $dates = ['payment_date'];
|
||||
protected $dateFormat = 'U';
|
||||
protected $dates = [
|
||||
'paid_at',
|
||||
];
|
||||
|
||||
// Array of items that can be updated with PushNew
|
||||
protected $pushable = ['items'];
|
||||
@@ -75,7 +73,7 @@ class Payment extends Model implements IDs
|
||||
public function scopeUnapplied($query)
|
||||
{
|
||||
return $query
|
||||
->select(['payments.id','payment_date','account_id','checkout_id','total_amt',DB::raw("SUM(alloc_amt) as allocated")])
|
||||
->select(['payments.id','payment_date','account_id','checkout_id','total_amt',DB::raw("SUM(amount) as allocated")])
|
||||
->leftJoin('payment_items',['payment_items.payment_id'=>'payments.id'])
|
||||
->groupBy(['payments.id','payment_date','total_amt','account_id','checkout_id'])
|
||||
->having(DB::raw('ROUND(total_amt-IFNULL(allocated,0),2)'),'>',self::threshold);
|
||||
@@ -85,7 +83,7 @@ class Payment extends Model implements IDs
|
||||
|
||||
public function getBalanceAttribute(): float
|
||||
{
|
||||
$balance = $this->getTotalAttribute()-$this->items->sum('alloc_amt');
|
||||
$balance = $this->getTotalAttribute()-$this->items->sum('amount');
|
||||
|
||||
return ($balance < self::threshold) ? 0 : $balance;
|
||||
}
|
||||
|
@@ -4,16 +4,11 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use App\Traits\{NextKey,PushNew};
|
||||
use App\Traits\PushNew;
|
||||
|
||||
class PaymentItem extends Model
|
||||
{
|
||||
use PushNew;
|
||||
const RECORD_ID = 'payment_item';
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
const CREATED_AT = 'date_orig';
|
||||
const UPDATED_AT = 'date_last';
|
||||
|
||||
/* RELATIONS */
|
||||
|
||||
|
@@ -357,9 +357,9 @@ class User extends Authenticatable implements IDs
|
||||
->select([
|
||||
'payment_id',
|
||||
'invoice_id',
|
||||
DB::raw('SUM(alloc_amt) AS allocate'),
|
||||
DB::raw('SUM(amount) AS allocate'),
|
||||
])
|
||||
->where('alloc_amt','>',0)
|
||||
->where('amount','>',0)
|
||||
->groupBy(['invoice_id','payment_id']);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user