osb/app/Models/Invoice.php

389 lines
8.2 KiB
PHP

<?php
namespace App\Models;
use Carbon\Carbon;
use Clarkeash\Doorman\Facades\Doorman;
use Clarkeash\Doorman\Models\Invite;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Leenooks\Traits\ScopeActive;
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,ScopeActive;
const RECORD_ID = 'invoice';
public $incrementing = FALSE;
protected $table = 'ab_invoice';
const CREATED_AT = 'date_orig';
const UPDATED_AT = 'date_last';
protected $dates = ['date_orig','due_date'];
public $dateFormat = 'U';
// Array of items that can be updated with PushNew
protected $pushable = ['items'];
/*
protected $with = [
'account.country.currency',
'items.taxes',
'paymentitems'
];
*/
// Caching variables
private int $_paid = 0;
private int $_total = 0;
private int $_total_tax = 0;
/* RELATIONS */
public function account()
{
return $this->belongsTo(Account::class);
}
public function items()
{
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()
{
return $this->hasMany(PaymentItem::class);
}
/* SCOPES */
/**
* Search for a record
*
* @param $query
* @param string $term
* @return mixed
*/
public function scopeSearch($query,string $term)
{
return $query->where('id','like','%'.$term.'%');
}
/* ATTRIBUTES */
/**
* Balance due on an invoice
* @return string
*/
public function getDueAttribute(): float
{
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');
}
/**
* Date the invoices was created
*
* @return Carbon
*/
public function getInvoiceDateAttribute(): Carbon
{
return $this->date_orig;
}
/**
* Get account System ID
* @return string
* @deprecated use getSIDAttribute()
*/
public function getInvoiceAccountIdAttribute()
{
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')->site_name);
}
/**
* Invoice Local ID
*
* @return string
*/
public function getLIDAttribute(): string
{
return sprintf('%06s',$this->id);
}
/**
* 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
->filter(function($item) { return $item->payment->pending_status; })
->sum('alloc_amt'));
}
/**
* 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);
}
/**
* 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) {
if ($o->active)
$this->_total_tax += $this->currency()->round($o->tax);
}
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total_tax);
}
/**
* Invoice total due
*
* @return string
*/
public function getTotalAttribute(): float
{
if (! $this->_total)
foreach ($this->items as $o) {
if ($o->active)
$this->_total += $this->currency()->round($o->total);
}
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total);
}
public function currency()
{
return $this->account->country->currency;
}
/**
* Return a download link for non-auth downloads
*
* @return string
*/
public function download_link(): string
{
// Re-use an existing code
$io = Invite::where('for',$this->account->user->email)->first();
$tokendate = ($x=Carbon::now()->addDays(21)) > ($y=$this->due_date->addDays(21)) ? $x : $y;
// Extend the expire date
if ($io AND ($tokendate > $io->valid_until)) {
$io->valid_until = $tokendate;
$io->save();
}
$code = (! $io) ? Doorman::generate()->for($this->account->user->email)->uses(0)->expiresOn($tokendate)->make()->first()->code : $io->code;
return url('u/invoice',[$this->id,'email',$code]);
}
public function products()
{
$return = collect();
foreach ($this->items->groupBy('product_id') as $o)
{
$po = $o->first()->product;
$po->count = count($o->pluck('service_id')->unique());
$return->push($po);
}
$lo = $this->account->user->language;
return $return->sortBy(function ($item) use ($lo) {
return $item->name($lo);
});
}
public function product_services(Product $po)
{
$return = collect();
$this->items->load(['service']);
foreach ($this->items->filter(function ($item) use ($po) {
return $item->product_id == $po->id;
}) as $o)
{
$so = $o->service;
$return->push($so);
};
return $return->unique()->sortBy('name');
}
public function product_service_items(Product $po,Service $so)
{
return $this->items->filter(function ($item) use ($po,$so) {
return $item->product_id == $po->id AND $item->service_id == $so->id;
})->filter()->sortBy('item_type');
}
/**
* @param string $key
* @return string
* @todo Ugly hack to update reminders
*/
public function reminders(string $key) {
$r = unserialize($this->reminders);
if (! Arr::get($r,$key)) {
$r[$key] = time();
return serialize($r);
}
}
/**
* Automatically set our due_date at save time.
*
* @param array $options
* @return bool
*/
public function save(array $options = []) {
// Automatically set the date_due attribute for new records.
if (! $this->exists AND ! $this->due_date) {
$this->due_date = $this->items->min('date_start');
// @todo This 7 days should be sysetm configurable
if (($x=Carbon::now()->addDay(7)) > $this->due_date)
$this->due_date = $x;
}
return parent::save($options);
}
}