Home page performance optimisations
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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;
|
||||
|
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user