Optimise users home page
This commit is contained in:
@@ -6,14 +6,35 @@ use Carbon\Carbon;
|
||||
use Clarkeash\Doorman\Facades\Doorman;
|
||||
use Clarkeash\Doorman\Models\Invite;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use App\Traits\NextKey;
|
||||
use App\Traits\PushNew;
|
||||
use Illuminate\Support\Arr;
|
||||
use Leenooks\Traits\ScopeActive;
|
||||
|
||||
class Invoice extends Model
|
||||
use App\Interfaces\IDs;
|
||||
use App\Traits\{NextKey,PushNew};
|
||||
|
||||
/**
|
||||
* Class Invoice
|
||||
* Invoices that belong to an Account
|
||||
*
|
||||
* Attributes for services:
|
||||
* + due : Balance due on an invoice
|
||||
* + due_date : Date the invoice is due
|
||||
* + invoice_date : Date the invoice was created
|
||||
* + lid : Local ID for invoice
|
||||
* + paid : Total of payments received (excluding pending)
|
||||
* + paid_date : Date the invoice was paid in full
|
||||
* + paid_pending : Total of pending payments received
|
||||
* + sid : System ID for invoice
|
||||
* + total_sub : Invoice sub-total before taxes
|
||||
* + total_tax : Invoices total of taxes
|
||||
* + total : Invoice total
|
||||
*
|
||||
* @package App\Models
|
||||
*/
|
||||
class Invoice extends Model implements IDs
|
||||
{
|
||||
use NextKey,PushNew;
|
||||
use NextKey,PushNew,ScopeActive;
|
||||
|
||||
const RECORD_ID = 'invoice';
|
||||
public $incrementing = FALSE;
|
||||
|
||||
@@ -33,8 +54,12 @@ class Invoice extends Model
|
||||
'paymentitems'
|
||||
];
|
||||
|
||||
private $_total = 0;
|
||||
private $_total_tax = 0;
|
||||
// Caching variables
|
||||
private int $_paid = 0;
|
||||
private int $_total = 0;
|
||||
private int $_total_tax = 0;
|
||||
|
||||
/* RELATIONS */
|
||||
|
||||
public function account()
|
||||
{
|
||||
@@ -43,7 +68,13 @@ class Invoice extends Model
|
||||
|
||||
public function items()
|
||||
{
|
||||
return $this->hasMany(InvoiceItem::class)->where('active',1);
|
||||
return $this->hasMany(InvoiceItem::class)
|
||||
->where('active',TRUE);
|
||||
}
|
||||
|
||||
public function payments()
|
||||
{
|
||||
return $this->hasManyThrough(Payment::class,PaymentItem::class,NULL,'id',NULL,'payment_id');
|
||||
}
|
||||
|
||||
public function paymentitems()
|
||||
@@ -51,56 +82,114 @@ class Invoice extends Model
|
||||
return $this->hasMany(PaymentItem::class);
|
||||
}
|
||||
|
||||
/** SCOPES **/
|
||||
/* SCOPES */
|
||||
|
||||
/**
|
||||
* Search for a record
|
||||
*
|
||||
* @param $query
|
||||
* @param string $term
|
||||
* @return
|
||||
* @return mixed
|
||||
*/
|
||||
public function scopeSearch($query,string $term)
|
||||
{
|
||||
return $query->where('id','like','%'.$term.'%');
|
||||
}
|
||||
|
||||
/** ATTRIBUTES **/
|
||||
/* ATTRIBUTES */
|
||||
|
||||
public function getDueAttribute()
|
||||
/**
|
||||
* Balance due on an invoice
|
||||
* @return string
|
||||
*/
|
||||
public function getDueAttribute(): float
|
||||
{
|
||||
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total - $this->paid);
|
||||
return sprintf('%4.'.$this->currency()->rounding.'f',$this->total-$this->paid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @deprecated use self::due_date;
|
||||
*/
|
||||
public function getDateDueAttribute()
|
||||
{
|
||||
return $this->due_date->format('Y-m-d');
|
||||
}
|
||||
|
||||
public function getInvoiceDateAttribute()
|
||||
/**
|
||||
* Date the invoices was created
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
public function getInvoiceDateAttribute(): Carbon
|
||||
{
|
||||
return $this->date_orig->format('Y-m-d');
|
||||
return $this->date_orig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get account System ID
|
||||
* @return string
|
||||
* @deprecated use getSIDAttribute()
|
||||
*/
|
||||
public function getInvoiceAccountIdAttribute()
|
||||
{
|
||||
return sprintf('%02s-%04s-%06s',$this->site_id,$this->account_id,$this->invoice_id);
|
||||
return $this->getSIDAttribute();
|
||||
}
|
||||
|
||||
// @todo Move this to a site configuration
|
||||
public function getInvoiceTextAttribute()
|
||||
{
|
||||
return sprintf('Thank you for using %s for your Internet Services.',config('SITE_SETUP')->site_name);
|
||||
}
|
||||
|
||||
public function getPaidAttribute()
|
||||
/**
|
||||
* Invoice Local ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLIDAttribute(): string
|
||||
{
|
||||
return $this->currency()->round(
|
||||
$this->paymentitems
|
||||
->filter(function($item) { return ! $item->payment->pending_status; })
|
||||
->sum('alloc_amt'));
|
||||
return sprintf('%06s',$this->id);
|
||||
}
|
||||
|
||||
public function getPendingPaidAttribute()
|
||||
/**
|
||||
* Total of payments received for this invoice
|
||||
* excluding pending payments
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getPaidAttribute(): float
|
||||
{
|
||||
if (! $this->_paid)
|
||||
$this->_paid = $this->currency()->round(
|
||||
$this->paymentitems
|
||||
->filter(function($item) { return ! $item->payment->pending_status; })
|
||||
->sum('alloc_amt'));
|
||||
|
||||
return $this->_paid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date that the invoice was paid in full.
|
||||
* We assume the last payment received pays it in full.
|
||||
*
|
||||
* @return Carbon|null
|
||||
*/
|
||||
public function getPaidDateAttribute(): ?Carbon
|
||||
{
|
||||
$o = $this->payments
|
||||
->filter(function($item) { return ! $item->pending_status; })
|
||||
->last();
|
||||
|
||||
return $o ? $o->date_payment : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Total of pending payments received for this invoice
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPaidPendingAttribute(): float
|
||||
{
|
||||
return $this->currency()->round(
|
||||
$this->paymentitems
|
||||
@@ -108,35 +197,87 @@ class Invoice extends Model
|
||||
->sum('alloc_amt'));
|
||||
}
|
||||
|
||||
public function getSubTotalAttribute()
|
||||
/**
|
||||
* Total of pending payments received for this invoice
|
||||
*
|
||||
* @return mixed
|
||||
* @deprecated use getPaidPendingAttribute()
|
||||
*/
|
||||
public function getPendingPaidAttribute(): float
|
||||
{
|
||||
return $this->getPaidPendingAttribute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoice System ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSIDAttribute(): string
|
||||
{
|
||||
return sprintf('%02s-%04s-%s',$this->site_id,$this->account_id,$this->getLIDAttribute());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get invoice subtotal before taxes
|
||||
*
|
||||
* @return float
|
||||
* @deprecated use getTotalSubAttribute()
|
||||
*/
|
||||
public function getSubTotalAttribute(): float
|
||||
{
|
||||
return $this->getTotalSubAttribute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get invoice subtotal before taxes
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getTotalSubAttribute(): float
|
||||
{
|
||||
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total-$this->tax_total);
|
||||
}
|
||||
|
||||
public function getTaxTotalAttribute()
|
||||
/**
|
||||
* Get the invoices taxes total
|
||||
*
|
||||
* @return float
|
||||
* @deprecated use getTotalTaxAttribute();
|
||||
*/
|
||||
public function getTaxTotalAttribute(): float
|
||||
{
|
||||
return $this->getTotalTaxAttribute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the invoices taxes total
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getTotalTaxAttribute(): float
|
||||
{
|
||||
if (! $this->_total_tax)
|
||||
{
|
||||
foreach ($this->items as $o)
|
||||
{
|
||||
foreach ($this->items as $o) {
|
||||
if ($o->active)
|
||||
$this->_total_tax += $this->currency()->round($o->tax);
|
||||
}
|
||||
}
|
||||
|
||||
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total_tax);
|
||||
}
|
||||
|
||||
public function getTotalAttribute()
|
||||
/**
|
||||
* Invoice total due
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTotalAttribute(): float
|
||||
{
|
||||
if (! $this->_total)
|
||||
{
|
||||
foreach ($this->items as $o)
|
||||
{
|
||||
foreach ($this->items as $o) {
|
||||
if ($o->active)
|
||||
$this->_total += $this->currency()->round($o->total);
|
||||
}
|
||||
}
|
||||
|
||||
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total);
|
||||
}
|
||||
|
Reference in New Issue
Block a user