More product cleanup

This commit is contained in:
2023-05-05 22:05:42 +10:00
parent dc74a064ba
commit 691180b3f0
7 changed files with 109 additions and 87 deletions

View File

@@ -173,7 +173,7 @@ class Product extends Model implements IDs
*/
public function getBaseCostAttribute(): float
{
return $this->supplied->base_cost*Invoice::billing_change($this->type->normalizeBillingInterval(),$this->billing_interval) ?: 0;
return round($this->supplied->base_cost,2)*Invoice::billing_change($this->type->billing_interval,$this->billing_interval) ?: 0;
}
/**
@@ -184,7 +184,7 @@ class Product extends Model implements IDs
*/
public function getBillingIntervalAttribute(): int
{
return max($this->price_recur_default,$this->type->normalizeBillingInterval());
return max($this->price_recur_default,$this->type->billing_interval);
}
/**
@@ -239,7 +239,18 @@ class Product extends Model implements IDs
*/
public function getMinChargeAttribute(int $timeperiod=NULL,Group $go=NULL): float
{
return $this->getSetupChargeAttribute($timeperiod,$go)+$this->getBaseChargeAttribute($timeperiod,$go)*Invoice::billing_term($this->type->contract_term,$this->getBillingIntervalAttribute());
return $this->getSetupChargeAttribute($timeperiod,$go)
+ $this->getBaseChargeAttribute($timeperiod,$go)*Invoice::billing_change($this->billing_interval,$this->type->billing_interval)*$this->type->contract_term;
}
/**
* Get the minimum cost for this product
*
* @return float
*/
public function getMinCostAttribute(): float
{
return $this->supplied->min_cost;
}
/**
@@ -340,7 +351,7 @@ class Product extends Model implements IDs
$go = $default;
if (is_null($timeperiod))
$timeperiod = $this->getBillingIntervalAttribute();
$timeperiod = $this->billing_interval;
// If the price doesnt exist for $go->id, use $go->id = 0 which is all users.
if (! $price=$this->charge($timeperiod,$go,$type)) {

View File

@@ -6,11 +6,11 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use App\Models\Product;
use App\Traits\{OrderServiceOptions,SiteID};
use App\Traits\{OrderServiceOptions,ProductDetails,SiteID};
abstract class Type extends Model
{
use SiteID,OrderServiceOptions;
use SiteID,ProductDetails,OrderServiceOptions;
/* RELATIONS */
@@ -36,6 +36,16 @@ abstract class Type extends Model
/* ATTRIBUTES */
/**
* The Billing interval that the supplier normally uses for this offering
*
* @return int
*/
public function getBillingIntervalAttribute(): int
{
return static::DefaultBill;
}
/**
* The product contract term is the highest of our defined contract_term (in Products/*) vs the suppliers
* contract term (defined in Supplier/*).
@@ -46,11 +56,4 @@ abstract class Type extends Model
{
return max(Arr::get($this->attributes,'contract_term',0),$this->supplied->contract_term);
}
/* METHODs */
final public function normalizeBillingInterval(): int
{
return static::DefaultBill;
}
}

View File

@@ -53,6 +53,7 @@ use App\Traits\SiteID;
*
* @package App\Models
* @todo "Billing Start Date" = "connection date" for sub types??
* @todo Add min_charge
*/
class Service extends Model implements IDs
{
@@ -577,13 +578,13 @@ class Service extends Model implements IDs
/**
* This function will determine the minimum contract term for a service, which is the maximum of
* supplier->type->contract_term, or the product->type->contract_term;
* supplied->contract_term, or the product->type->contract_term;
*
* @return int
*/
public function getContractTermAttribute(): int
{
return $this->getSuppliedAttribute()->contract_term;
return max($this->supplied->contract_term,$this->product->type->contract_term);
}
/**
@@ -1077,7 +1078,7 @@ class Service extends Model implements IDs
*/
public function charge_normalized(): float
{
return number_format($this->getBillingChargeAttribute()*Invoice::billing_change($this->recur_schedule,$this->offering->normalizeBillingInterval()),2);
return number_format($this->getBillingChargeAttribute()*Invoice::billing_change($this->recur_schedule,$this->offering->billing_interval),2);
}
private function getOrderInfoValue(string $key): ?string

View File

@@ -7,11 +7,11 @@ use Illuminate\Support\Arr;
use Leenooks\Traits\ScopeActive;
use App\Models\{Invoice,SupplierDetail};
use App\Traits\{ProductDetails,SiteID};
use App\Traits\SiteID;
abstract class Type extends Model
{
use SiteID,ScopeActive,ProductDetails;
use SiteID,ScopeActive;
/* RELATIONS */
@@ -58,21 +58,14 @@ abstract class Type extends Model
return static::category_name;
}
/**
* This contract term is the highest of
* + The defined contract_term
* + The default months in a billing interval
*
* @return int
*/
public function getContractTermAttribute(): int
final public function getContractTermAttribute(?int $val): int
{
return max(Invoice::billing_period(static::getBillingIntervalAttribute()),Arr::get($this->attributes,'contract_term',0));
return $val ?: 1;
}
public function getMinCostAttribute(): float
{
return $this->attributes['setup_cost']+$this->attributes['base_cost']*Invoice::billing_term($this->getContractTermAttribute(),$this->getBillingIntervalAttribute());
return $this->setup_cost+$this->base_cost*$this->contract_term;
}
public function getNameAttribute(): string