Account next invoice, and authorisations

This commit is contained in:
Deon George
2019-07-04 14:55:05 +10:00
parent 59a8ef2476
commit 21ea60c4f9
26 changed files with 532 additions and 102 deletions

View File

@@ -30,6 +30,7 @@ class Service extends Model
protected $appends = [
'account_name',
'admin_service_id_url',
'billing_price',
'name_short',
'next_invoice',
'product_category',
@@ -43,6 +44,7 @@ class Service extends Model
'account_name',
'admin_service_id_url',
'active',
'billing_price',
'data_orig',
'id',
'name_short',
@@ -207,6 +209,16 @@ class Service extends Model
});
}
/**
* Enable to perform queries without eager loading
*
* @param $query
* @return mixed
*/
public function scopeNoEagerLoads($query){
return $query->setEagerLoads([]);
}
/** ATTRIBUTES **/
/**
@@ -229,7 +241,11 @@ class Service extends Model
public function getBillingPriceAttribute(): float
{
return $this->addTax($this->price ?: $this->product->price($this->recur_schedule));
// @todo Temporary for services that dont have recur_schedule set.
if (is_null($this->recur_schedule) OR is_null($this->product->price($this->recur_schedule)))
$this->price=0;
return $this->addTax(is_null($this->price) ? $this->product->price($this->recur_schedule) : $this->price);
}
/**
@@ -481,7 +497,7 @@ class Service extends Model
* @param float $value
* @return float
*/
public function addTax(float $value): float
private function addTax(float $value): float
{
return round($value*1.1,2);
}
@@ -506,23 +522,51 @@ class Service extends Model
return $this->active OR ($this->order_status AND ! in_array($this->order_status,$this->inactive_status));
}
public function next_invoice_items()
/**
* Should this service be invoiced soon
*
* @todo get the number of days from account setup
* @return bool
*/
public function isInvoiceDueSoon($days=30): bool
{
return (! $this->external_billing) AND (! $this->suspend_billing) AND $this->getInvoiceNextAttribute()->lessThan(now()->addDays($days));
}
public function next_invoice_items(): \Illuminate\Support\Collection
{
$result = collect();
$result->push([
'item'=>0,
'desc'=>sprintf('Product/Service [%s->%s]',
$this->invoice_next->format('Y-m-d'),
$this->invoice_next_end->format('Y-m-d')),
'amt'=>$this->getBillingPriceAttribute()]);
$o = new InvoiceItem;
$o->active = TRUE;
$o->service_id = $this->id;
$o->product_id = $this->product_id;
$o->quantity = 1;
$o->item_type = 0;
$o->price_base = $this->price ?: $this->product->price($this->recur_schedule); // @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;
foreach ($this->charges->filter(function($item) { return ! $item->processed; }) as $o)
$o->addTaxes();
$result->push($o);
foreach ($this->charges->filter(function($item) { return ! $item->processed; }) as $oo)
{
$result->push([
'item'=>5, // @tod Charges
'desc'=>sprintf('%d@%3.2f - %s',$o->quantity,$this->addTax($o->amount),$o->name),
'amt'=>$this->addTax($o->amount*$o->quantity)]);
$o = new InvoiceItem;
$o->active = TRUE;
$o->service_id = $oo->service_id;
$o->product_id = $this->product_id;
$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->module_id = 30; // @todo This shouldnt be hard coded
$o->module_ref = $oo->id;
$o->addTaxes();
$result->push($o);
}
return $result;