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

@@ -318,7 +318,7 @@ class Service extends Model implements IDs
{
$query = $this->hasMany(InvoiceItem::class)
->where('item_type','=',0)
->orderBy('date_start');
->orderBy('start_at');
// @todo Change to $query->active();
if ($active)
@@ -334,14 +334,14 @@ class Service extends Model implements IDs
{
$query = $this->hasManyThrough(Invoice::class,InvoiceItem::class,NULL,'id',NULL,'invoice_id')
->distinct('id')
->where('ab_invoice.site_id','=',$this->site_id)
->where('ab_invoice_item.site_id','=',$this->site_id)
->orderBy('date_orig')
->orderBy('due_date');
->where('invoices.site_id','=',$this->site_id)
->where('invoice_items.site_id','=',$this->site_id)
->orderBy('created_at')
->orderBy('due_at');
if ($active)
$query->where('ab_invoice_item.active','=',TRUE)
->where('ab_invoice.active','=',TRUE);
$query->where('invoice_items.active','=',TRUE)
->where('invoices.active','=',TRUE);
return $query;
}
@@ -469,49 +469,7 @@ class Service extends Model implements IDs
*/
public function getBillingMonthlyPriceAttribute(): float
{
$d = 1;
switch ($this->recur_schedule) {
case Invoice::BILL_WEEKLY:
$d = 12/52;
break;
case Invoice::BILL_MONTHLY:
$d = 1;
break;
case Invoice::BILL_QUARTERLY:
$d = 3;
break;
case Invoice::BILL_SEMI_YEARLY:
$d = 6;
break;
case Invoice::BILL_YEARLY:
$d = 12;
break;
case Invoice::BILL_TWOYEARS:
$d = 24;
break;
case Invoice::BILL_THREEYEARS:
$d = 36;
break;
case Invoice::BILL_FOURYEARS:
$d = 48;
break;
case Invoice::BILL_FIVEYEARS:
$d = 60;
break;
default:
throw new Exception('Unknown recur_schedule');
}
return number_format($this->getBillingChargeAttribute()/$d,2);
return number_format($this->getBillingChargeAttribute()/Arr::get(Invoice::billing_periods,$this->recur_schedule.'.interval',1),2);
}
/**
@@ -593,7 +551,18 @@ class Service extends Model implements IDs
$last = $this->getInvoiceToAttribute();
return $last
? $last->addDay()
: ($this->date_next_invoice ? $this->date_next_invoice->clone() : ($this->start_at ?: LeenooksCarbon::now()));
: ($this->invoice_next_at ? $this->invoice_next_at->clone() : ($this->start_at ?: LeenooksCarbon::now()));
}
/**
* We need to cast some dates to LeenooksCarbon to get access to startOfHalf()/endOfHalf() methods
*
* @param $value
* @return LeenooksCarbon|null
*/
public function getInvoiceNextAtAttribute($value): ?LeenooksCarbon
{
return $value ? LeenooksCarbon::create($value) : NULL;
}
/**
@@ -674,7 +643,7 @@ class Service extends Model implements IDs
}
/**
* Determine how much quantity (at the charge rate) is requite for the next invoice
* Determine how much quantity (at the charge rate) is required for the next invoice
*
* @return float
* @throws Exception
@@ -734,12 +703,12 @@ class Service extends Model implements IDs
/**
* Get the date that the service has been invoiced to
*
* @return Carbon|null
* @return LeenooksCarbon|null
*/
public function getInvoiceToAttribute(): ?Carbon
public function getInvoiceToAttribute(): ?LeenooksCarbon
{
$result = ($x=$this->invoice_items->filter(function($item) { return $item->item_type === 0;}))->count()
? $x->last()->date_stop
? $x->last()->stop_at
: NULL;
// For SSL Certificates, the invoice_to date is the expiry date of the Cert
@@ -823,7 +792,7 @@ class Service extends Model implements IDs
return $item->item_type === 0;
})
->last()
->date_stop;
->stop_at;
}
/**
@@ -1228,8 +1197,8 @@ class Service extends Model implements IDs
$o->item_type = 4; // @todo change to const or something
$o->price_base = $this->product->getSetupChargeAttribute($this->recur_schedule,$this->account->group);
//$o->recurring_schedule = $this->recur_schedule;
$o->date_start = $this->invoice_next;
$o->date_stop = $this->invoice_next;
$o->start_at = $this->invoice_next;
$o->stop_at = $this->invoice_next;
$o->quantity = 1;
$o->site_id = 1; // @todo
@@ -1254,9 +1223,9 @@ class Service extends Model implements IDs
$o->price_base = is_null($this->price)
? (is_null($this->price_override) ? $this->product->getBaseChargeAttribute($this->recur_schedule,$this->account->group) : $this->price_override)
: $this->price; // @todo change to a method in this class
$o->recurring_schedule = $this->recur_schedule;
$o->date_start = $this->invoice_next;
$o->date_stop = $this->invoice_next_end;
$o->recur_schedule = $this->recur_schedule;
$o->start_at = $this->invoice_next;
$o->stop_at = $this->invoice_next_end;
$o->quantity = $this->invoice_next_quantity;
$o->site_id = 1; // @todo
@@ -1277,8 +1246,8 @@ class Service extends Model implements IDs
$o->quantity = $oo->quantity;
$o->item_type = $oo->type;
$o->price_base = $oo->amount;
$o->date_start = $oo->date_charge;
$o->date_stop = $oo->date_charge;
$o->start_at = $oo->date_charge;
$o->stop_at = $oo->date_charge;
$o->module_id = 30; // @todo This shouldnt be hard coded
$o->module_ref = $oo->id;
$o->site_id = 1; // @todo