Optimise product tables
This commit is contained in:
parent
e1a4db700f
commit
16cc0c9f8d
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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']);
|
||||
}
|
||||
|
||||
|
@ -35,8 +35,8 @@ return new class extends Migration
|
||||
$o->created_at = \Carbon\Carbon::createFromTimestamp($o->date_orig);
|
||||
if ($o->date_last)
|
||||
$o->updated_at = \Carbon\Carbon::createFromTimestamp($o->date_last);
|
||||
if ($o->due_date)
|
||||
$o->due_at = \Carbon\Carbon::createFromTimestamp($o->due_date);
|
||||
if ($o->getRawOriginal('due_date'))
|
||||
$o->due_at = \Carbon\Carbon::createFromTimestamp($o->getRawOriginal('due_date'));
|
||||
if ($o->reminders) {
|
||||
try {
|
||||
$reminders = unserialize($o->reminders);
|
||||
|
71
database/migrations/2022_04_22_144438_optimise_payments.php
Normal file
71
database/migrations/2022_04_22_144438_optimise_payments.php
Normal 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');
|
||||
}
|
||||
};
|
@ -27,7 +27,7 @@
|
||||
<td>{{ number_format($io->total,2) }}</td>
|
||||
<td>{{ number_format($io->due,2) }}</td>
|
||||
<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>
|
||||
</tr>
|
||||
@endforeach
|
||||
|
@ -5,7 +5,7 @@
|
||||
</div>
|
||||
|
||||
<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">
|
||||
<thead>
|
||||
<tr>
|
||||
@ -23,7 +23,7 @@
|
||||
<tr>
|
||||
<td>{{ $oo->account->name }}</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->balance,2) }}</td>--}}
|
||||
<td>
|
||||
|
Loading…
Reference in New Issue
Block a user