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->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; $oo->site_id = config('site')->site_id;
$o->items()->save($oo); $o->items()->save($oo);
} }

View File

@ -229,7 +229,7 @@ class PaypalController extends Controller
$pio = new PaymentItem; $pio = new PaymentItem;
$pio->site_id = 1; // @todo To implement $pio->site_id = 1; // @todo To implement
$pio->invoice_id = $cap->invoice_id; $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); $po->items->push($pio);

View File

@ -263,7 +263,7 @@ class Invoice extends Model implements IDs
{ {
return $this->paymentitems return $this->paymentitems
->filter(function($item) { return ! $item->payment->pending_status; }) ->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 return $this->paymentitems
->filter(function($item) { return $item->payment->pending_status; }) ->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; use PushNew;
const CREATED_AT = 'date_orig'; protected $dates = [
const UPDATED_AT = 'date_last'; 'paid_at',
];
protected $dates = ['payment_date'];
protected $dateFormat = 'U';
// Array of items that can be updated with PushNew // Array of items that can be updated with PushNew
protected $pushable = ['items']; protected $pushable = ['items'];
@ -75,7 +73,7 @@ class Payment extends Model implements IDs
public function scopeUnapplied($query) public function scopeUnapplied($query)
{ {
return $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']) ->leftJoin('payment_items',['payment_items.payment_id'=>'payments.id'])
->groupBy(['payments.id','payment_date','total_amt','account_id','checkout_id']) ->groupBy(['payments.id','payment_date','total_amt','account_id','checkout_id'])
->having(DB::raw('ROUND(total_amt-IFNULL(allocated,0),2)'),'>',self::threshold); ->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 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; return ($balance < self::threshold) ? 0 : $balance;
} }

View File

@ -4,16 +4,11 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use App\Traits\{NextKey,PushNew}; use App\Traits\PushNew;
class PaymentItem extends Model class PaymentItem extends Model
{ {
use PushNew; use PushNew;
const RECORD_ID = 'payment_item';
protected $dateFormat = 'U';
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
/* RELATIONS */ /* RELATIONS */

View File

@ -357,9 +357,9 @@ class User extends Authenticatable implements IDs
->select([ ->select([
'payment_id', 'payment_id',
'invoice_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']); ->groupBy(['invoice_id','payment_id']);
} }

View File

@ -35,8 +35,8 @@ return new class extends Migration
$o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig); $o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig);
if ($o->date_last) if ($o->date_last)
$o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last); $o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last);
if ($o->due_date) if ($o->getRawOriginal('due_date'))
$o->due_at = \Carbon\Carbon::createFromTimestamp($o->due_date); $o->due_at = \Carbon\Carbon::createFromTimestamp($o->getRawOriginal('due_date'));
if ($o->reminders) { if ($o->reminders) {
try { try {
$reminders = unserialize($o->reminders); $reminders = unserialize($o->reminders);

View File

@ -0,0 +1,71 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement('ALTER TABLE payments MODIFY account_id int unsigned NOT NULL');
DB::statement('ALTER TABLE payments MODIFY checkout_id int unsigned NOT NULL');
DB::statement('ALTER TABLE payments MODIFY source_id int unsigned DEFAULT NULL');
DB::statement('ALTER TABLE payments MODIFY pending_status tinyint(1) DEFAULT NULL');
Schema::table('payments', function (Blueprint $table) {
$table->dropForeign(['site_id']);
$table->datetime('created_at')->nullable()->after('id');
$table->datetime('updated_at')->nullable()->after('created_at');
$table->boolean('active')->nullable()->after('updated_at');
$table->date('paid_at')->nullable()->after('payment_date');
$table->foreign(['account_id','site_id'])->references(['id','site_id'])->on('accounts');
$table->foreign(['source_id','site_id'])->references(['id','site_id'])->on('users');
});
// Convert out dates
foreach (\App\Models\Payment::withoutGlobalScope(\App\Models\Scopes\SiteScope::class)->cursor() as $o) {
if ($o->date_orig)
$o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig);
if ($o->date_last)
$o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last);
if ($o->payment_date)
$o->paid_at = \Carbon\Carbon::createFromTimestamp($o->payment_date);
$o->save();
}
Schema::table('payments', function (Blueprint $table) {
$table->dropColumn(['date_orig','date_last','payment_date']);
});
DB::statement('ALTER TABLE payment_items MODIFY payment_id int unsigned NOT NULL');
DB::statement('ALTER TABLE payment_items MODIFY invoice_id int unsigned DEFAULT NULL');
DB::statement('ALTER TABLE payment_items RENAME COLUMN alloc_amt TO amount');
Schema::table('payment_items', function (Blueprint $table) {
$table->dropForeign(['site_id']);
$table->boolean('active')->nullable()->after('site_id');
$table->dropColumn(['date_orig','date_last']);
$table->foreign(['payment_id','site_id'])->references(['id','site_id'])->on('payments');
});
DB::statement('UPDATE payment_items SET active=1');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
abort(500,'Cant go back');
}
};

View File

@ -27,7 +27,7 @@
<td>{{ number_format($io->total,2) }}</td> <td>{{ number_format($io->total,2) }}</td>
<td>{{ number_format($io->due,2) }}</td> <td>{{ number_format($io->due,2) }}</td>
<td class="text-right"> <td class="text-right">
<input type="text" class="text-right invoice" name="invoices[{{ $io->id }}]" value="{{ number_format(($x=$io->paymentitems->filter(function($item) use ($pid) { return $item->payment_id == $pid; })) ? $x->sum('alloc_amt') : 0,2) }}"> <input type="text" class="text-right invoice" name="invoices[{{ $io->id }}]" value="{{ number_format(($x=$io->paymentitems->filter(function($item) use ($pid) { return $item->payment_id == $pid; })) ? $x->sum('amount') : 0,2) }}">
</td> </td>
</tr> </tr>
@endforeach @endforeach

View File

@ -5,7 +5,7 @@
</div> </div>
<div class="card-body"> <div class="card-body">
@if(($x=$o->payments()->where('payments.date_orig','>',\Carbon\Carbon::now()->subMonths(12)->unix())->with(['items','account'])->get())->count()) @if(($x=$o->payments()->where('payments.created_at','>',\Carbon\Carbon::now()->subMonths(12)->unix())->with(['items','account'])->get())->count())
<table class="table table-bordered w-100" id="payments_past"> <table class="table table-bordered w-100" id="payments_past">
<thead> <thead>
<tr> <tr>
@ -23,7 +23,7 @@
<tr> <tr>
<td>{{ $oo->account->name }}</td> <td>{{ $oo->account->name }}</td>
<td>{{ $oo->sid }}</td> <td>{{ $oo->sid }}</td>
<td>{{ $oo->payment_date->format('Y-m-d') }}</td> <td>{{ $oo->paid_at->format('Y-m-d') }}</td>
<td class="text-right">${{ number_format($oo->total,2) }}</td> <td class="text-right">${{ number_format($oo->total,2) }}</td>
{{--<td class="text-right">${{ number_format($oo->balance,2) }}</td>--}} {{--<td class="text-right">${{ number_format($oo->balance,2) }}</td>--}}
<td> <td>