Optimise product tables

This commit is contained in:
Deon George
2022-04-22 15:23:08 +10:00
parent e1a4db700f
commit 16cc0c9f8d
10 changed files with 88 additions and 24 deletions

View File

@@ -123,7 +123,7 @@ class AdminController extends Controller
$oo->invoice_id = $id;
}
$oo->alloc_amt = ($oo->invoice->due >= 0) && ($oo->invoice->due-$amount >= 0) ? $amount : 0;
$oo->amount = ($oo->invoice->due >= 0) && ($oo->invoice->due-$amount >= 0) ? $amount : 0;
$oo->site_id = config('site')->site_id;
$o->items()->save($oo);
}

View File

@@ -229,7 +229,7 @@ class PaypalController extends Controller
$pio = new PaymentItem;
$pio->site_id = 1; // @todo To implement
$pio->invoice_id = $cap->invoice_id;
$pio->alloc_amt = $cap->amount->value-$po->fees_amt;
$pio->amount = $cap->amount->value-$po->fees_amt;
$po->items->push($pio);

View File

@@ -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');
}
/**

View File

@@ -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;
}

View File

@@ -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 */

View File

@@ -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']);
}