Removed redundant functions from Invoice, optimised Invoice tables

This commit is contained in:
Deon George
2022-04-22 14:41:18 +10:00
parent a16277d9bb
commit e1a4db700f
22 changed files with 361 additions and 349 deletions

View File

@@ -25,7 +25,7 @@ use App\Traits\{NextKey,PushNew};
* + 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
* + sub_total : Invoice sub-total before taxes
* + total_tax : Invoices total of taxes
* + total : Invoice total
*
@@ -33,17 +33,11 @@ use App\Traits\{NextKey,PushNew};
*/
class Invoice extends Model implements IDs
{
use NextKey,PushNew,ScopeActive;
use 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';
protected $dates = [
'due_at',
];
public const BILL_WEEKLY = 0;
public const BILL_MONTHLY = 1;
@@ -56,45 +50,44 @@ class Invoice extends Model implements IDs
public const BILL_FIVEYEARS = 8;
/* Our available billing periods */
// @todo change this to a function - with billing_name()?
public const billing_periods = [
0 => [
'name' => 'Weekly',
'interval' => 0.25,
],
1 => [
'name' => 'Monthly',
'interval' => 1,
],
2 => [
'name' => 'Quarterly',
'interval' => 3,
],
3 => [
'name' => 'Semi-Annually',
'interval' => 6,
],
4 => [
'name' => 'Annually',
'interval' => 12,
],
5 => [
'name' => 'Two years',
'interval' => 24,
],
6 => [
'name' => 'Three Years',
'interval' => 36,
],
7 => [
'name' => 'Four Years',
'interval' => 48,
],
8 => [
'name' => 'Five Years',
'interval' => 60,
],
];
self::BILL_WEEKLY => [
'name' => 'Weekly',
'interval' => 0.25,
],
self::BILL_MONTHLY => [
'name' => 'Monthly',
'interval' => 1,
],
self::BILL_QUARTERLY => [
'name' => 'Quarterly',
'interval' => 3,
],
self::BILL_SEMI_YEARLY => [
'name' => 'Semi-Annually',
'interval' => 6,
],
self::BILL_YEARLY => [
'name' => 'Annually',
'interval' => 12,
],
self::BILL_TWOYEARS => [
'name' => 'Two years',
'interval' => 24,
],
self::BILL_THREEYEARS => [
'name' => 'Three Years',
'interval' => 36,
],
self::BILL_FOURYEARS => [
'name' => 'Four Years',
'interval' => 48,
],
SELF::BILL_FIVEYEARS => [
'name' => 'Five Years',
'interval' => 60,
],
];
// Array of items that can be updated with PushNew
protected $pushable = ['items'];
@@ -164,6 +157,28 @@ class Invoice extends Model implements IDs
return ceil(($contract_term ?: 1)/(Arr::get(self::billing_periods,$source.'.interval') ?: 1));
}
/* INTERFACES */
/**
* Invoice Local ID
*
* @return string
*/
public function getLIDAttribute(): string
{
return sprintf('%06s',$this->id);
}
/**
* Invoice System ID
*
* @return string
*/
public function getSIDAttribute(): string
{
return sprintf('%02s-%04s-%s',$this->site_id,$this->account_id,$this->getLIDAttribute());
}
/* RELATIONS */
public function account()
@@ -206,7 +221,7 @@ class Invoice extends Model implements IDs
/**
* Balance due on an invoice
* @return string
* @return float
*/
public function getDueAttribute(): float
{
@@ -215,11 +230,11 @@ class Invoice extends Model implements IDs
/**
* @return mixed
* @deprecated use self::due_date;
* @todo Change references to due_at to use due_date
*/
public function getDateDueAttribute()
public function getDueDateAttribute(): Carbon
{
return $this->due_date->format('Y-m-d');
return $this->due_at;
}
/**
@@ -229,17 +244,7 @@ class Invoice extends Model implements IDs
*/
public function getInvoiceDateAttribute(): Carbon
{
return $this->date_orig;
}
/**
* Get account System ID
* @return string
* @deprecated use getSIDAttribute()
*/
public function getInvoiceAccountIdAttribute()
{
return $this->getSIDAttribute();
return $this->created_at;
}
// @todo Move this to a site configuration
@@ -248,16 +253,6 @@ class Invoice extends Model implements IDs
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
@@ -286,7 +281,7 @@ class Invoice extends Model implements IDs
->filter(function($item) { return ! $item->pending_status; })
->last();
return $o ? $o->payment_date : NULL;
return $o?->payment_date;
}
/**
@@ -301,44 +296,12 @@ class Invoice extends Model implements IDs
->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 $this->items->where('active',TRUE)->sum('sub_total');
}
@@ -371,9 +334,12 @@ class Invoice extends Model implements IDs
*/
public function getTotalAttribute(): float
{
return $this->getTotalSubAttribute()+$this->getTotalTaxAttribute();
return $this->getSubTotalAttribute()+$this->getTotalTaxAttribute();
}
/* METHODS */
// @todo This shouldnt be here - current should be handled at an account level.
public function currency()
{
return $this->account->country->currency;
@@ -389,7 +355,7 @@ class Invoice extends Model implements IDs
// 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;
$tokendate = ($x=Carbon::now()->addDays(21)) > ($y=$this->due_at->addDays(21)) ? $x : $y;
// Extend the expire date
if ($io AND ($tokendate > $io->valid_until)) {
@@ -402,12 +368,12 @@ class Invoice extends Model implements IDs
return url('u/invoice',[$this->id,'email',$code]);
}
// @todo document
public function products()
{
$return = collect();
foreach ($this->items->groupBy('product_id') as $o)
{
foreach ($this->items->groupBy('product_id') as $o) {
$po = $o->first()->product;
$po->count = count($o->pluck('service_id')->unique());
@@ -419,6 +385,7 @@ class Invoice extends Model implements IDs
});
}
// @todo document
public function product_services(Product $po)
{
$return = collect();
@@ -436,6 +403,7 @@ class Invoice extends Model implements IDs
return $return->unique()->sortBy('name');
}
// @todo document
public function product_service_items(Product $po,Service $so)
{
return $this->items->filter(function ($item) use ($po,$so) {
@@ -449,28 +417,28 @@ class Invoice extends Model implements IDs
* @todo Ugly hack to update reminders
*/
public function reminders(string $key) {
$r = unserialize($this->reminders);
$r = json_decode($this->reminders);
if (! Arr::get($r,$key)) {
$r[$key] = time();
return serialize($r);
return json_encode($r);
}
}
/**
* Automatically set our due_date at save time.
* Automatically set our due_at 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');
if (! $this->exists AND ! $this->due_at) {
$this->due_at = $this->items->min('start_at');
// @todo This 7 days should be sysetm configurable
if (($x=Carbon::now()->addDay(7)) > $this->due_date)
$this->due_date = $x;
if (($x=Carbon::now()->addDay(7)) > $this->due_at)
$this->due_at = $x;
}
return parent::save($options);