Home page performance optimisations

This commit is contained in:
Deon George 2024-07-05 16:38:31 +10:00
parent 648d941893
commit e7ac329d24
12 changed files with 127 additions and 232 deletions

View File

@ -2,6 +2,7 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
@ -24,6 +25,15 @@ class Account extends Model implements IDs
{
use HasFactory,ScopeActive;
/* STATIC */
public static function InvoicesDue(Collection $invoices=NULL): Collection
{
return (new self)
->invoiceSummaryDue($invoices,TRUE)
->get();
}
/* INTERFACES */
public function getLIDAttribute(): string
@ -121,7 +131,7 @@ class Account extends Model implements IDs
public function services()
{
return $this->hasMany(Service::class)
->with(['product.translate','invoice_items']);
->with(['product.translate','product.type.supplied']);
}
/**
@ -236,11 +246,12 @@ class Account extends Model implements IDs
* @param Collection|NULL $invoices
* @return Collection
*/
public function invoiceSummary(Collection $invoices=NULL): Collection
public function invoiceSummary(Collection $invoices=NULL,bool $all=FALSE): Builder
{
return (new Invoice)
->select([
'invoice_id as id',
'invoices.account_id',
'invoices.id as id',
DB::raw('SUM(item) AS _item'),
DB::raw('SUM(tax) AS _tax'),
DB::raw('SUM(payments) AS _payment'),
@ -249,7 +260,8 @@ class Account extends Model implements IDs
DB::raw('SUM(payment_fees) AS _payment_fee'),
DB::raw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0) AS NUMERIC),2) AS _total'),
DB::raw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) AS _balance'),
'due_at',
'invoices.due_at',
'invoices.created_at',
])
->from(
(new Payment)
@ -284,10 +296,25 @@ class Account extends Model implements IDs
->groupBy(['invoice_items.invoice_id']),
),'p')
->join('invoices',['invoices.id'=>'invoice_id'])
->where('account_id',$this->id)
->groupBy(['p.invoice_id'])
->groupBy(['due_at','discount_amt'])
->get();
->when(($all === FALSE),fn($query)=>$query->where('invoices.account_id',$this->id))
->orderBy('due_at')
->groupBy(['invoices.account_id','invoices.id','invoices.created_at','invoices.due_at','invoices.discount_amt'])
->with(['account']);
}
public function invoiceSummaryDue(Collection $invoices=NULL,bool $all=FALSE): Builder
{
return $this->invoiceSummary($invoices,$all)
->havingRaw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) > 0');
}
public function invoiceSummaryPast(Collection $invoices=NULL,bool $all=FALSE): Builder
{
return $this->invoiceSummary($invoices,$all)
->join('payment_items',['payment_items.invoice_id'=>'invoices.id'])
->join('payments',['payments.id'=>'payment_items.payment_id'])
->addSelect(DB::raw('max(paid_at) as _paid_at'))
->havingRaw('ROUND(CAST(SUM(item_total)-SUM(COALESCE(discount,0))+COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) <= 0');
}
/**

View File

@ -36,8 +36,10 @@ class Invoice extends Model implements IDs
use PushNew,ScopeActive;
protected $casts = [
'created_at' => 'datetime:Y-m-d',
'due_at' => 'datetime:Y-m-d',
'reminders'=>'json',
'due_at'=>'datetime:y-m-d',
'_paid_at' => 'datetime:Y-m-d',
];
public const BILL_WEEKLY = 0;

View File

@ -1180,7 +1180,8 @@ class Service extends Model implements IDs
public function invoices()
{
return $this->account
->invoiceSummary($this->invoiced_service_items_active->pluck('invoice_id'));
->invoiceSummary($this->invoiced_service_items_active->pluck('invoice_id'))
->get();
}
/**

View File

@ -0,0 +1,16 @@
<!-- $o = Account::class -->
<!-- Show outstanding invoices -->
<div class="card card-warning">
<div class="card-header">
<h3 class="card-title">Invoices Due</h3>
</div>
<div class="card-body">
@if(($list=$o->invoiceSummaryDue()->get())->count())
@include('theme.backend.adminlte.invoice.widget.due')
@else
<p>No invoice due</p>
@endif
</div>
</div>

View File

@ -6,7 +6,7 @@
</div>
<div class="card-body">
@if(($x=$o->invoices->where('created_at','>',\Carbon\Carbon::now()->subMonths(12))->where('due','<=',0))->count())
@if(($list=$o->invoiceSummaryPast()->where('invoices.created_at','>=',\Carbon\Carbon::now()->subYears(2)->startOfYear())->get())->count())
<table class="table table-bordered w-100" id="invoices_past_{{ $o->id }}">
<thead>
<tr>
@ -19,13 +19,13 @@
</thead>
<tbody>
@foreach ($x as $oo)
@foreach ($list as $oo)
<tr>
<td>{{ $oo->account->name }}</td>
<td><a href="{{ url('u/invoice',$oo->id) }}">{{ $oo->sid }}</a></td>
<td><a href="{{ url('u/invoice',$oo->id) }}">{{ $oo->lid }}</a></td>
<td>{{ $oo->created_at->format('Y-m-d') }}</td>
<td>{{ $oo->paid_date ? $oo->paid_date->format('Y-m-d') : '' }}</td>
<td class="text-right">${{ number_format($oo->total,2) }}</td>
<td>{{ $oo->_paid_at?->format('Y-m-d') }}</td>
<td class="text-right">${{ number_format($oo->_total,2) }}</td>
</tr>
@endforeach
</tbody>
@ -37,12 +37,15 @@
</div>
</div>
@section('page-scripts')
@section('page-styles')
@css(datatables,bootstrap4|rowgroup)
@append
@section('page-scripts')
@js(datatables,bootstrap4|rowgroup)
<script type="text/javascript">
@if ($x->count())
@if ($list->count())
$(document).ready(function() {
$('#invoices_past_{{ $o->id }}').DataTable({
order: [[2,'desc'],[0,'asc']],

View File

@ -20,13 +20,13 @@
<tbody>
@foreach ($x as $so)
<tr>
<td><a href="{{ url('u/service',[$so->id]) }}">{{ $so->sid }}</a></td>
<td>{{ $so->product->category_name }}</td>
<td>{{ $so->name_short }}</td>
<td>{{ $so->product->name }}</td>
<td>{{ $so->external_billing ? '-' : $so->invoice_next->format('Y-m-d') }}</td>
</tr>
<tr>
<td><a href="{{ url('u/service',[$so->id]) }}">{{ $so->sid }}</a></td>
<td>{{ $so->product->category_name }}</td>
<td>{{ $so->name_short }}</td>
<td>{{ $so->product->name }}</td>
<td>{{ $so->external_billing ? '-' : $so->invoice_next->format('Y-m-d') }}</td>
</tr>
@endforeach
</tbody>
<tfoot>
@ -43,8 +43,11 @@
</div>
</div>
@section('page-scripts')
@section('page-styles')
@css(datatables,bootstrap4|rowgroup)
@append
@section('page-scripts')
@js(datatables,bootstrap4|rowgroup)
<script type="text/javascript">
@ -64,10 +67,6 @@
},
],
});
$('#services_active_{{ $ao->id }} tbody').on('click','tr', function () {
$(this).toggleClass('selected');
});
});
</script>
@append

View File

@ -45,7 +45,7 @@
<div class="info-box-content">
<span class="info-box-text">Account Balance</span>
<span class="info-box-number"><small>$</small> {{ number_format(($x=$o->invoices()->with(['items.taxes','paymentitems.payment','account'])->get()->where('due','>',0))->sum('due'),2) }}</span>
<span class="info-box-number"><small>$</small> {{ number_format(($x=$o->accounts->map(fn($item)=>$item->invoiceSummaryDue()->get()->pluck('_balance'))->flatten())->sum(),2) }}</span>
</div>
</div>
</div>

View File

@ -17,7 +17,7 @@
@section('main-content')
<!-- Our Summary Home Page Boxes -->
<div class="row">
@include('theme.backend.adminlte.common.account.widget.summary')
@include('theme.backend.adminlte.account.widget.summary_boxes')
</div>
<div class="card card-light card-tabs">
@ -49,10 +49,6 @@
<div class="card-header bg-white">
<ul class="nav nav-pills">
<li class="nav-item"><a class="nav-link {{ (! session()->has('supplier_update')) ? 'active' : '' }}" href="#tab-services" data-toggle="tab">Services</a></li>
{{--
<!-- @todo this is not working -->
<li class="nav-item"><a class="nav-link" href="#tab-nextinvoice" data-toggle="tab">Next Invoice</a></li>
--}}
<li class="nav-item"><a class="nav-link" href="#tab-futureinvoice" data-toggle="tab">Future Invoice</a></li>
@canany('reseller','wholesaler')
<li class="nav-item ml-auto">
@ -67,30 +63,16 @@
<div class="tab-pane {{ (! session()->has('supplier_update')) ? 'active' : '' }}" id="tab-services">
<div class="row">
<div class="col-12 col-xl-7">
@include('theme.backend.adminlte.service.widget.active',['o'=>$ao])
@include('theme.backend.adminlte.account.widget.service_active',['o'=>$ao])
</div>
<div class="col-12 col-xl-5">
@include('theme.backend.adminlte.invoice.widget.due',['o'=>$ao])
@include('theme.backend.adminlte.invoice.widget.list',['o'=>$ao])
@include('theme.backend.adminlte.payment.widget.list',['o'=>$ao])
@include('theme.backend.adminlte.account.widget.invoice_due',['o'=>$ao])
@include('theme.backend.adminlte.account.widget.invoice_past',['o'=>$ao])
</div>
</div>
</div>
{{--
<!-- @todo this is not working -->
<div class="tab-pane" id="tab-nextinvoice">
<div class="row">
<div class="col-12">
@include('theme.backend.adminlte.u.invoice.widgets.next',['future'=>FALSE])
</div>
</div>
</div>
--}}
<div class="tab-pane" id="tab-futureinvoice">
<div class="row">
<div class="col-12 col-xl-9">

View File

@ -1,62 +1,53 @@
<!-- $o = Account::class -->
<!-- Show outstanding invoices -->
<div class="card card-warning">
<div class="card-header">
<h3 class="card-title">Invoices Due</h3>
</div>
<table class="table table-bordered w-100" id="invoices_due_{{ $o->id }}">
<thead>
<tr>
<th>Account</th>
<th>#</th>
<th>Due</th>
<th class="text-right">Total</th>
<th class="text-right">Outstanding</th>
</tr>
</thead>
<div class="card-body">
@if(($x=$o->invoices->where('due','>',0))->count())
<table class="table table-bordered w-100" id="invoices_due_{{ $o->id }}">
<thead>
<tr>
<th>Account</th>
<th>#</th>
<th>Due</th>
<th class="text-right">Total</th>
<th class="text-right">Outstanding</th>
</tr>
</thead>
<tbody>
@foreach ($list as $oo)
<tr @if ($oo->due_at->isPast()) class="table-danger" @endif>
<td>{{ $oo->account->name }}</td>
<td><a href="{{ url('u/invoice',$oo->id) }}">{{ $oo->lid }}</a></td>
<td>{{ $oo->due_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>
</tr>
@endforeach
</tbody>
</table>
<tbody>
@foreach ($x as $oo)
<tr @if ($oo->due_at->isPast()) class="table-danger" @endif>
<td>{{ $oo->account->name }}</td>
<td><a href="{{ url('u/invoice',$oo->id) }}">{{ $oo->lid }}</a></td>
<td>{{ $oo->due_at->format('Y-m-d') }}</td>
<td class="text-right">${{ number_format($oo->total,2) }}</td>
<td class="text-right">${{ number_format($oo->due,2) }}</td>
</tr>
@endforeach
</tbody>
</table>
@else
<p>No invoice due</p>
@endif
</div>
</div>
@section('page-styles')
@css(datatables,bootstrap4|rowgroup)
@append
@section('page-scripts')
@css(datatables,bootstrap4|rowgroup)
@js(datatables,bootstrap4|rowgroup)
<script type="text/javascript">
@if ($x->count())
$(document).ready(function() {
$('#invoices_due_{{ $o->id }}').DataTable({
order: [[0,'asc'],[3,'desc']],
rowGroup: {
dataSrc: 0,
},
columnDefs: [
{
targets: [0],
visible: false,
}
],
});
@if ($list->count())
$(document).ready(function() {
$('#invoices_due_{{ $o->id }}').DataTable({
// If we have more than 1 account id, order by account
order: [[0,'asc'],[2,'asc'],[1,'desc']],
rowGroup: {
dataSrc: 0,
},
columnDefs: [
{
targets: [0],
visible: false,
}
],
});
});
@endif
</script>
@append

View File

@ -1,11 +1,11 @@
<!-- @todo These needs to be optimised, and change for $o = Account::class -->
<!-- Show next items for an invoice -->
@if ($o->next_invoice_items($future)->count())
@if (($x=$o->next_invoice_items($future))->count())
<div class="card">
<div class="card-body">
<table class="table">
<!-- Group by Account -->
@foreach (($x=$o->next_invoice_items($future))->groupBy('product_id') as $id => $oo)
@foreach ($x->groupBy('product_id') as $id => $oo)
<tr>
<th colspan="4">{{ $oo->first()->product->name }}</th>
<th class="text-right">${{ number_format($oo->sum('total'),2) }}</th>

View File

@ -1,71 +0,0 @@
<!-- $o = Account::class -->
<!-- Show past 12 months payments -->
<div class="card card-success">
<div class="card-header">
<h3 class="card-title">Past Payments</h3>
</div>
<div class="card-body">
@if(($x=$o->payments->where('created_at','>',\Carbon\Carbon::now()->subMonths(12)))->count())
<table class="table table-bordered w-100" id="payments_past">
<thead>
<tr>
<th>Account</th>
<th>#</th>
<th>Received</th>
<th class="text-right">Total</th>
{{--<th class="text-right">Balance</th>--}}
<th>Invoice(s)</th>
</tr>
</thead>
<tbody>
@foreach ($x as $oo)
<tr>
<td>{{ $oo->account->name }}</td>
<td>{{ $oo->lid }}</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>
{!! join(', ',$oo->items
->filter(function($item) { return $item->invoice_id; })
->transform(function($item) { return sprintf('<a href="%s">%s</a>',url('u/invoice',$item->invoice_id),$item->invoice_id); })
->toArray()) !!}
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<p>No payments to list</p>
@endif
</div>
</div>
@section('page-scripts')
@css(datatables,bootstrap4|rowgroup)
@js(datatables,bootstrap4|rowgroup)
<script type="text/javascript">
$(document).ready(function() {
$('#payments_past').DataTable({
order: [2,'desc'],
rowGroup: {
dataSrc: 0,
},
columnDefs: [
{
targets: [0],
visible: false,
}
],
});
$('#payments_past tbody').on('click','tr', function () {
$(this).toggleClass('selected');
});
});
</script>
@append

View File

@ -6,66 +6,11 @@
</div>
<div class="card-body">
@if($o->query_invoice_summary()->havingRaw('invoice_total-payments > 0')->count())
<table class="table table-bordered w-100" id="reseller_invoices_due">
<thead>
<tr>
<th>Account</th>
<th>#</th>
<th>Issued</th>
<th>Due</th>
<th class="text-right">Total</th>
<th class="text-right">Payments</th>
<th class="text-right">Outstanding</th>
</tr>
</thead>
<tbody>
@foreach ($o->query_invoice_summary()->havingRaw('invoice_total-payments > 0')->get() as $oo)
<tr @if ($oo->due_at->isPast()) class="table-danger" @endif>
<td>{{ $oo->account->name }}</td>
<td><a href="{{ url('u/invoice',$oo->id) }}">{{ $oo->sid }}</a></td>
<td>{{ $oo->created_at->format('Y-m-d') }}</td>
<td>{{ $oo->due_at->format('Y-m-d') }}</td>
<td class="text-right">${{ number_format($oo->total,2) }}</td>
<td class="text-right">${{ number_format($oo->paid,2) }}</td>
<td class="text-right">${{ number_format($oo->due,2) }}</td>
</tr>
@endforeach
</tbody>
</table>
@if(($list=\App\Models\Account::InvoicesDue())->count())
@include('theme.backend.adminlte.invoice.widget.due')
@else
<p>No invoice due</p>
@endif
</div>
</div>
@section('page-styles')
@css(datatables,bootstrap4|rowgroup)
@append
@section('page-scripts')
@js(datatables,bootstrap4|rowgroup)
<script type="text/javascript">
$(document).ready(function() {
$('#reseller_invoices_due').DataTable({
order: [[0,'asc'],[3,'desc']],
rowGroup: {
dataSrc: 0,
},
columnDefs: [
{
targets: [0],
visible: false,
}
],
});
$('#invoices_due tbody').on('click','tr', function () {
$(this).toggleClass('selected');
});
});
</script>
@append
</div>