More product cleanup

This commit is contained in:
Deon George 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 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 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 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; $go = $default;
if (is_null($timeperiod)) 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 the price doesnt exist for $go->id, use $go->id = 0 which is all users.
if (! $price=$this->charge($timeperiod,$go,$type)) { if (! $price=$this->charge($timeperiod,$go,$type)) {

View File

@ -6,11 +6,11 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use App\Models\Product; use App\Models\Product;
use App\Traits\{OrderServiceOptions,SiteID}; use App\Traits\{OrderServiceOptions,ProductDetails,SiteID};
abstract class Type extends Model abstract class Type extends Model
{ {
use SiteID,OrderServiceOptions; use SiteID,ProductDetails,OrderServiceOptions;
/* RELATIONS */ /* RELATIONS */
@ -36,6 +36,16 @@ abstract class Type extends Model
/* ATTRIBUTES */ /* 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 * The product contract term is the highest of our defined contract_term (in Products/*) vs the suppliers
* contract term (defined in Supplier/*). * 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); 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 * @package App\Models
* @todo "Billing Start Date" = "connection date" for sub types?? * @todo "Billing Start Date" = "connection date" for sub types??
* @todo Add min_charge
*/ */
class Service extends Model implements IDs 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 * 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 * @return int
*/ */
public function getContractTermAttribute(): 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 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 private function getOrderInfoValue(string $key): ?string

View File

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

View File

@ -17,7 +17,7 @@
<!-- $o = App\Models\Service::class --> <!-- $o = App\Models\Service::class -->
@section('main-content') @section('main-content')
<div class="row"> <div class="row">
<div class="col-4"> <div class="col-12 col-lg-4">
<form role="form" method="POST" enctype="multipart/form-data"> <form role="form" method="POST" enctype="multipart/form-data">
<div class="card card-dark"> <div class="card card-dark">
{{ csrf_field() }} {{ csrf_field() }}
@ -146,26 +146,13 @@
</div> </div>
<!-- Current Plan --> <!-- Current Plan -->
<div class="col-4"> <div class="col-12 col-lg-8">
<div class="card card-dark"> <div class="card card-dark">
<div class="card-body"> <div class="card-header">
<div class="card-header"> <h3 class="card-title">Plan Information</h3>
<h3 class="card-title">Current Plan</h3>
</div>
@include('service.widget.internal',['o'=>$o,'p'=>$o->product])
</div> </div>
</div>
</div>
<!-- Proposed Plan -->
<div class="col-4">
<div class="card card-dark">
<div class="card-body"> <div class="card-body">
<div class="card-header">
<h3 class="card-title">Proposed Plan</h3>
</div>
@include('service.widget.internal',['o'=>$o,'p'=>\App\Models\Product::where('id',Arr::get($o->order_info,'change_product_id'))->singleOrFail()]) @include('service.widget.internal',['o'=>$o,'p'=>\App\Models\Product::where('id',Arr::get($o->order_info,'change_product_id'))->singleOrFail()])
</div> </div>
</div> </div>

View File

@ -89,7 +89,7 @@
</div> </div>
@can('wholesaler') @can('wholesaler')
<div class="tab-pane fade" id="internal" role="tabpanel"> <div class="tab-pane fade" id="internal" role="tabpanel">
@include('service.widget.internal',['o'=>$o,'p'=>$o->product]) @include('service.widget.internal',['o'=>$o,'p'=>new \App\Models\Product()])
</div> </div>
<div class="tab-pane fade {{ session()->pull('service_update') ? 'active show' : '' }}" id="update" role="tabpanel"> <div class="tab-pane fade {{ session()->pull('service_update') ? 'active show' : '' }}" id="update" role="tabpanel">

View File

@ -1,13 +1,24 @@
<!-- $o = Service::class, $p = Product::class --> <!-- $o = Service::class, $p = Product::class -->
@php($c=$o->product)
<table class="table table-sm"> <table class="table table-sm">
<thead> <thead>
<tr> <tr>
<th>&nbsp;</th> <th>&nbsp;</th>
@if (($s=$o->supplied) && $s->exists) <th class="text-center" colspan="2">This Plan</th>
<th>{{ $s->supplier->name }}</th> <th>&nbsp;</th>
@if($p->exists)
<th class="text-center" colspan="2">Proposed Plan</th>
<th>&nbsp;</th>
@endif @endif
<th>Us</th> </tr>
@if ($s->exists) <tr>
<th>&nbsp;</th>
<th>Client</th>
<th>Supplier</th>
<th>&nbsp;</th>
@if($p->exists)
<th>Client</th>
<th>Supplier</th>
<th>&nbsp;</th> <th>&nbsp;</th>
@endif @endif
</tr> </tr>
@ -16,75 +27,91 @@
<tbody> <tbody>
<tr> <tr>
<th>Product</th> <th>Product</th>
@if ($s->exists) <td class="text-center" colspan="2">#{{ $c->supplied->id }}: {{ $c->supplied->name_long }}</td>
<td>#{{ $s->id }}: {{ $s->name }}</td> @if ($p->exists)
@endif <th>&nbsp;</th>
<td>#{{ $p->id }}: {{ $p->name }}</td> <td class="text-center" colspan="2">#{{ $p->supplied->id }}: {{ $p->supplied->name_long }}</td>
@if ($s->exists)
<td>{{ $p->category_name }}</td>
@endif @endif
<td>{{ $c->category_name }}</td>
</tr> </tr>
<tr> <tr>
<th>Setup</th> <th>Setup</th>
@if ($s->exists) <td>${{ number_format($b=$o->account->taxed($c->setup_charge),2) }}</td>
<td>${{ number_format($a=$o->account->taxed($s->setup_cost),2) }}</td> <td>${{ number_format($a=$o->account->taxed($c->setup_cost),2) }}</td>
<td>${{ number_format($b=$o->account->taxed($p->setup_charge),2) }}</td>
<td>{!! markup($a,$b) !!}</td> <td>{!! markup($a,$b) !!}</td>
@else @if ($p->exists)
<td>-</td> <td>${{ number_format($b=$o->account->taxed($p->setup_charge),2) }}</td>
<td>${{ number_format($a=$o->account->taxed($p->setup_cost),2) }}</td>
<td>{!! markup($a,$b) !!}</td>
@endif @endif
</tr> </tr>
{{--
<tr> <tr>
<th>Billed</th> <th>Billed</th>
@if ($s->exists)
<td>{{ $s->billing_interval_string }}</td>
@endif
<td>{{ $o->billing_interval_string }}</td> <td>{{ $o->billing_interval_string }}</td>
@if ($s->exists) <td>{{ $c->type->billing_interval_string }}</td>
<td>&nbsp;</td>
@if ($p->exists)
<td>{{ $o->billing_interval_string }}</td>
<td>{{ $p->type->billing_interval_string }}</td>
<td>&nbsp;</td> <td>&nbsp;</td>
@endif @endif
</tr> </tr>
<tr> <tr>
<th>Billing Price</th> <th>Billing Price</th>
@if ($s->exists) <td @if($o->isChargeOverriden())class="text-danger"@endif>${{ number_format($b=$o->billing_charge,2) }}</td>
<td>${{ number_format($a=$o->account->taxed($s->base_cost*\App\Models\Invoice::billing_change($s->billing_interval,$p->billing_interval)),2) }}</td> <td>${{ number_format($a=$o->account->taxed($c->base_cost),2) }}</td>
@endif <td>{!! markup($a,$b) !!}</td>
<td>${{ number_format($b=$o->billing_charge,2) }}</td> @if ($p->exists)
@if ($s->exists) <td @if($o->isChargeOverriden())class="text-danger"@endif>${{ number_format($b=$o->account->taxed($p->base_charge),2) }}</td>
<td>${{ number_format($a=$o->account->taxed($p->base_cost),2) }}</td>
<td>{!! markup($a,$b) !!}</td> <td>{!! markup($a,$b) !!}</td>
@endif @endif
</tr> </tr>
<tr> <tr>
<th>Monthly Price</th> <th>Monthly Price</th>
@if ($s->exists) <td @if($x=$o->isChargeOverriden()) class="text-danger" @endif>
<td>${{ number_format($a=$o->account->taxed($s->base_cost*\App\Models\Invoice::billing_change($s->billing_interval,1)),2) }}</td> @if($x)
@endif <abbr title="${{ number_format($b=$o->account->taxed($c->base_charge)*\App\Models\Invoice::billing_change($o->billing_interval,\App\Models\Invoice::BILL_MONTHLY),2) }}">${{ number_format($b=$o->billing_monthly_price,2) }}
<td @if($x=$o->isChargeOverriden()) class="text-danger" @endif>${{ number_format($b=($x ? $o->account->taxed($p->base_charge) : $o->billing_monthly_price),2) }}</td> @else
@if ($s->exists) {{ number_format($b=$o->account->taxed($c->base_charge)*\App\Models\Invoice::billing_change($o->billing_interval,\App\Models\Invoice::BILL_MONTHLY),2) }}
@endif
</td>
<td>${{ number_format($a=$o->account->taxed($c->base_cost)*\App\Models\Invoice::billing_change($o->billing_interval,\App\Models\Invoice::BILL_MONTHLY),2) }}</td>
<td>{!! markup($a,$b) !!}</td>
@if ($p->exists)
<td @if($x=$o->isChargeOverriden()) class="text-danger" @endif>${{ number_format($b=$o->account->taxed($p->base_charge)*\App\Models\Invoice::billing_change($o->billing_interval,\App\Models\Invoice::BILL_MONTHLY),2) }}</td>
<td>${{ number_format($a=$o->account->taxed($p->base_cost)*\App\Models\Invoice::billing_change($o->billing_interval,\App\Models\Invoice::BILL_MONTHLY),2) }}</td>
<td>{!! markup($a,$b) !!}</td> <td>{!! markup($a,$b) !!}</td>
@endif @endif
</tr> </tr>
<tr> <tr>
<th>Contract</th> <th>Contract</th>
@if ($s->exists) <td>{{ $o->contract_term }} months</td>
<td>{{ $s->contract_term }} months</td> <td>{{ $c->supplied->contract_term }} months</td>
@endif <td>&nbsp;</td>
<td>{{ $p->contract_term }} months</td> @if ($p->exists)
@if ($s->exists) <td>{{ $p->contract_term }} months</td>
<td>{{ $p->supplied->contract_term }} months</td>
<td>&nbsp;</td> <td>&nbsp;</td>
@endif @endif
</tr> </tr>
<tr> <tr>
<th>Min Price</th> <th>Min Price</th>
@if ($s->exists) <!-- @todo change this to $o->min_charge when implemented -->
<td>${{ number_format($a=$o->account->taxed($s->min_cost),2) }}</td> <td>${{ number_format($b=$o->account->taxed($o->product->min_charge),2) }}</td>
<td>${{ number_format($b=$o->account->taxed($p->getMinChargeAttribute($o->billing_interval)),2) }}</td> <td>${{ number_format($a=$o->account->taxed($c->supplied->min_cost),2) }}</td>
<td>{!! markup($a,$b) !!}</td> <td>{!! markup($a,$b) !!}</td>
@else @if ($p->exists)
<td>-</td> <td>${{ number_format($a=$o->account->taxed($p->min_charge),2) }}</td>
<td>${{ number_format($a=$o->account->taxed($p->supplied->min_cost ?? 0),2) }}</td>
<td>{!! markup($a,$b) !!}</td>
@endif @endif
</tr> </tr>
--}}
</tbody> </tbody>
</table> </table>