Move billing cost service::class methods into __get(), fixed some incorrect displays of costs

This commit is contained in:
2025-05-21 16:28:37 +10:00
parent 4b429cbf20
commit 14c0109efa
4 changed files with 46 additions and 38 deletions

View File

@@ -29,12 +29,18 @@ use App\Traits\{ScopeAccountUserAuthorised,ScopeServiceActive,SiteID};
* - Type, what service we are providing, made up of a product we supply - in the DB these are service/*
*
* Attributes for services:
* + billing_orig : Charge for this service before being overridden by $this->price (ex TAX)
* + billing_orig_normalised_taxed: Charge for this service before being overridden by $this->price, normalised to MONTHLY (with Account TAX)
* + billing_orig_taxed : Charge for this service before being overridden by $this->price (with Account TAX)
* + billing_charge : Charge for this service each invoice period (ex TAX)
* + billing_charge_normalised_taxed: Charge for this service each invoice period, normalised to MONTHLY (with Account TAX)
* + billing_charge_taxed : Charge for this service each invoice period (with Account TAX)
* + billing_charge_orig : Charge for this service before being overridden by $this->price (ex TAX)
* + billing_charge_orig_normalised_taxed: Charge for this service before being overridden by $this->price, normalised to MONTHLY (with Account TAX)
* + billing_charge_orig_taxed : Charge for this service before being overridden by $this->price (with Account TAX)
* + billing_cost : Cost for this service each invoice period (ex TAX)
* + billing_cost_normalised_taxed: Cost for this service each invoice period, normalised to MONTHLY (with Account TAX)
* + billing_cost_taxed : Cost for this service each invoice period (with Account TAX)
* + billing_cost_orig : Cost for this service before being overridden by $this->cost (ex TAX)
* + billing_cost_orig_normalised_taxed: Cost for this service before being overridden by $this->cost, normalised to MONTHLY (with Account TAX)
* + billing_cost_orig_taxed : Cost for this service before being overridden by $this->cost (with Account TAX)
* + billing_interval : The period that this service is billed
* + billing_interval_name : The period that this service is billed as a name
* + is_billed : Does this service generate an invoice
@@ -278,14 +284,23 @@ class Service extends Model implements IDs
public function __get($key): mixed
{
return match ($key) {
'billing_orig' => $this->product->getBaseChargeAttribute($this->billing_interval,$this->account->group),
'billing_orig_normalised_taxed' => $this->billing_orig_taxed*Invoice::billing_change($this->billing_interval,Invoice::BILL_MONTHLY),
'billing_orig_taxed' => $this->account->taxed($this->billing_orig),
'billing_cost' => $this->billing_cost(),
'billing_cost_normalised_taxed' => $this->billing_cost_taxed*Invoice::billing_change($this->billing_interval,Invoice::BILL_MONTHLY),
'billing_cost_taxed' => $this->account->taxed($this->billing_cost),
'billing_cost_orig' => $this->product->base_cost,
'billing_cost_orig_normalised_taxed' => $this->billing_cost_orig_taxed*Invoice::billing_change($this->product->type->billing_interval,Invoice::BILL_MONTHLY),
'billing_cost_orig_taxed' => $this->account->taxed($this->billing_cost_orig),
'billing_charge_orig' => $this->product->getBaseChargeAttribute($this->billing_interval,$this->account->group),
'billing_charge_orig_normalised_taxed' => $this->billing_charge_orig_taxed*Invoice::billing_change($this->billing_interval,Invoice::BILL_MONTHLY),
'billing_charge_orig_taxed' => $this->account->taxed($this->billing_charge_orig),
'billing_charge' => $this->billing_charge(),
'billing_charge_normalised_taxed' => $this->billing_charge_taxed*Invoice::billing_change($this->billing_interval,Invoice::BILL_MONTHLY),
'billing_charge_taxed' => $this->account->taxed($this->billing_charge),
'billing_interval' => $this->recur_schedule ?: $this->product->billing_interval,
'billing_interval_name' => Invoice::billing_name($this->billing_interval),
'is_billed' => (! ($this->external_billing || $this->suspend_billing || ($this->price === 0))),
'is_charge_overridden' => (! is_null($this->price)),
'is_cost_overridden' => (! is_null($this->cost)),
@@ -519,18 +534,6 @@ class Service extends Model implements IDs
/* ATTRIBUTES */
public function getBillingCostAttribute(): float
{
return $this->account->taxed($this->billing_cost());
}
public function getBillingCostNormalisedAttribute(): float
{
return number_format(
$this->getBillingCostAttribute()*Invoice::billing_change($this->billing_interval,$this->offering->billing_interval),
2);
}
/**
* Return the earliest date that the service can be canceled as per contract/billing intervals
*
@@ -874,7 +877,7 @@ class Service extends Model implements IDs
$this->price = 0;
return is_null($this->price)
? $this->billing_orig
? $this->billing_charge_orig
: $this->price;
}
@@ -911,11 +914,11 @@ class Service extends Model implements IDs
*
* @return float
*/
public function billing_cost(): float
private function billing_cost(): float
{
return is_null($this->cost)
? $this->product->getBaseCostAttribute()
: $this->cost*Invoice::billing_change($this->product->type->billing_interval,$this->product->billing_interval);
: $this->cost*Invoice::billing_change($this->product->type->billing_interval,$this->billing_interval);
}
/**
@@ -935,12 +938,12 @@ class Service extends Model implements IDs
$max = max($date,$this->getCancelDateAttribute())->clone();
if (! $this->getInvoicedToAttribute())
return $this->account->taxed($this->getContractTermAttribute()*$this->getBillingCostNormalisedAttribute());
return $this->getContractTermAttribute()*$this->billing_cost_normalised_taxed;
if ($this->getInvoicedToAttribute()->lessThan($max)) {
$d = $this->getInvoicedToAttribute()->diffInDays($max);
return $this->account->taxed($d/30*$this->getBillingCostNormalisedAttribute());
return $d/30*$this->billing_cost_normalised_taxed;
}
return 0;