Home page performance optimisations

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

View File

@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -24,6 +25,15 @@ class Account extends Model implements IDs
{ {
use HasFactory,ScopeActive; use HasFactory,ScopeActive;
/* STATIC */
public static function InvoicesDue(Collection $invoices=NULL): Collection
{
return (new self)
->invoiceSummaryDue($invoices,TRUE)
->get();
}
/* INTERFACES */ /* INTERFACES */
public function getLIDAttribute(): string public function getLIDAttribute(): string
@ -121,7 +131,7 @@ class Account extends Model implements IDs
public function services() public function services()
{ {
return $this->hasMany(Service::class) 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 * @param Collection|NULL $invoices
* @return Collection * @return Collection
*/ */
public function invoiceSummary(Collection $invoices=NULL): Collection public function invoiceSummary(Collection $invoices=NULL,bool $all=FALSE): Builder
{ {
return (new Invoice) return (new Invoice)
->select([ ->select([
'invoice_id as id', 'invoices.account_id',
'invoices.id as id',
DB::raw('SUM(item) AS _item'), DB::raw('SUM(item) AS _item'),
DB::raw('SUM(tax) AS _tax'), DB::raw('SUM(tax) AS _tax'),
DB::raw('SUM(payments) AS _payment'), 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('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) 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'), 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( ->from(
(new Payment) (new Payment)
@ -284,10 +296,25 @@ class Account extends Model implements IDs
->groupBy(['invoice_items.invoice_id']), ->groupBy(['invoice_items.invoice_id']),
),'p') ),'p')
->join('invoices',['invoices.id'=>'invoice_id']) ->join('invoices',['invoices.id'=>'invoice_id'])
->where('account_id',$this->id) ->when(($all === FALSE),fn($query)=>$query->where('invoices.account_id',$this->id))
->groupBy(['p.invoice_id']) ->orderBy('due_at')
->groupBy(['due_at','discount_amt']) ->groupBy(['invoices.account_id','invoices.id','invoices.created_at','invoices.due_at','invoices.discount_amt'])
->get(); ->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; use PushNew,ScopeActive;
protected $casts = [ protected $casts = [
'created_at' => 'datetime:Y-m-d',
'due_at' => 'datetime:Y-m-d',
'reminders'=>'json', 'reminders'=>'json',
'due_at'=>'datetime:y-m-d', '_paid_at' => 'datetime:Y-m-d',
]; ];
public const BILL_WEEKLY = 0; public const BILL_WEEKLY = 0;

View File

@ -1180,7 +1180,8 @@ class Service extends Model implements IDs
public function invoices() public function invoices()
{ {
return $this->account 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>
<div class="card-body"> <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 }}"> <table class="table table-bordered w-100" id="invoices_past_{{ $o->id }}">
<thead> <thead>
<tr> <tr>
@ -19,13 +19,13 @@
</thead> </thead>
<tbody> <tbody>
@foreach ($x as $oo) @foreach ($list as $oo)
<tr> <tr>
<td>{{ $oo->account->name }}</td> <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->created_at->format('Y-m-d') }}</td>
<td>{{ $oo->paid_date ? $oo->paid_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>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>
@ -37,12 +37,15 @@
</div> </div>
</div> </div>
@section('page-scripts') @section('page-styles')
@css(datatables,bootstrap4|rowgroup) @css(datatables,bootstrap4|rowgroup)
@append
@section('page-scripts')
@js(datatables,bootstrap4|rowgroup) @js(datatables,bootstrap4|rowgroup)
<script type="text/javascript"> <script type="text/javascript">
@if ($x->count()) @if ($list->count())
$(document).ready(function() { $(document).ready(function() {
$('#invoices_past_{{ $o->id }}').DataTable({ $('#invoices_past_{{ $o->id }}').DataTable({
order: [[2,'desc'],[0,'asc']], order: [[2,'desc'],[0,'asc']],

View File

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

View File

@ -45,7 +45,7 @@
<div class="info-box-content"> <div class="info-box-content">
<span class="info-box-text">Account Balance</span> <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> </div>
</div> </div>

View File

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

View File

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

View File

@ -1,11 +1,11 @@
<!-- @todo These needs to be optimised, and change for $o = Account::class --> <!-- @todo These needs to be optimised, and change for $o = Account::class -->
<!-- Show next items for an invoice --> <!-- 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">
<div class="card-body"> <div class="card-body">
<table class="table"> <table class="table">
<!-- Group by Account --> <!-- 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> <tr>
<th colspan="4">{{ $oo->first()->product->name }}</th> <th colspan="4">{{ $oo->first()->product->name }}</th>
<th class="text-right">${{ number_format($oo->sum('total'),2) }}</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>
<div class="card-body"> <div class="card-body">
@if($o->query_invoice_summary()->havingRaw('invoice_total-payments > 0')->count()) @if(($list=\App\Models\Account::InvoicesDue())->count())
<table class="table table-bordered w-100" id="reseller_invoices_due"> @include('theme.backend.adminlte.invoice.widget.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>
@else @else
<p>No invoice due</p> <p>No invoice due</p>
@endif @endif
</div> </div>
</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