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

This commit is contained in:
Deon George 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/* * - Type, what service we are providing, made up of a product we supply - in the DB these are service/*
* *
* Attributes for services: * 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 : 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_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_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 : The period that this service is billed
* + billing_interval_name : The period that this service is billed as a name * + billing_interval_name : The period that this service is billed as a name
* + is_billed : Does this service generate an invoice * + is_billed : Does this service generate an invoice
@ -278,14 +284,23 @@ class Service extends Model implements IDs
public function __get($key): mixed public function __get($key): mixed
{ {
return match ($key) { return match ($key) {
'billing_orig' => $this->product->getBaseChargeAttribute($this->billing_interval,$this->account->group), 'billing_cost' => $this->billing_cost(),
'billing_orig_normalised_taxed' => $this->billing_orig_taxed*Invoice::billing_change($this->billing_interval,Invoice::BILL_MONTHLY), 'billing_cost_normalised_taxed' => $this->billing_cost_taxed*Invoice::billing_change($this->billing_interval,Invoice::BILL_MONTHLY),
'billing_orig_taxed' => $this->account->taxed($this->billing_orig), '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' => $this->billing_charge(),
'billing_charge_normalised_taxed' => $this->billing_charge_taxed*Invoice::billing_change($this->billing_interval,Invoice::BILL_MONTHLY), '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_charge_taxed' => $this->account->taxed($this->billing_charge),
'billing_interval' => $this->recur_schedule ?: $this->product->billing_interval, 'billing_interval' => $this->recur_schedule ?: $this->product->billing_interval,
'billing_interval_name' => Invoice::billing_name($this->billing_interval), 'billing_interval_name' => Invoice::billing_name($this->billing_interval),
'is_billed' => (! ($this->external_billing || $this->suspend_billing || ($this->price === 0))), 'is_billed' => (! ($this->external_billing || $this->suspend_billing || ($this->price === 0))),
'is_charge_overridden' => (! is_null($this->price)), 'is_charge_overridden' => (! is_null($this->price)),
'is_cost_overridden' => (! is_null($this->cost)), 'is_cost_overridden' => (! is_null($this->cost)),
@ -519,18 +534,6 @@ class Service extends Model implements IDs
/* ATTRIBUTES */ /* 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 * 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; $this->price = 0;
return is_null($this->price) return is_null($this->price)
? $this->billing_orig ? $this->billing_charge_orig
: $this->price; : $this->price;
} }
@ -911,11 +914,11 @@ class Service extends Model implements IDs
* *
* @return float * @return float
*/ */
public function billing_cost(): float private function billing_cost(): float
{ {
return is_null($this->cost) return is_null($this->cost)
? $this->product->getBaseCostAttribute() ? $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(); $max = max($date,$this->getCancelDateAttribute())->clone();
if (! $this->getInvoicedToAttribute()) if (! $this->getInvoicedToAttribute())
return $this->account->taxed($this->getContractTermAttribute()*$this->getBillingCostNormalisedAttribute()); return $this->getContractTermAttribute()*$this->billing_cost_normalised_taxed;
if ($this->getInvoicedToAttribute()->lessThan($max)) { if ($this->getInvoicedToAttribute()->lessThan($max)) {
$d = $this->getInvoicedToAttribute()->diffInDays($max); $d = $this->getInvoicedToAttribute()->diffInDays($max);
return $this->account->taxed($d/30*$this->getBillingCostNormalisedAttribute()); return $d/30*$this->billing_cost_normalised_taxed;
} }
return 0; return 0;

View File

@ -41,7 +41,7 @@
<td>{{ $o->product->name }}</td> <td>{{ $o->product->name }}</td>
<td>{{ $o->supplierid }}</td> <td>{{ $o->supplierid }}</td>
<td class="text-right">{{ number_format($o->billing_charge_normalised_taxed,2) }}</td> <td class="text-right">{{ number_format($o->billing_charge_normalised_taxed,2) }}</td>
<td class="text-right">{{ number_format($o->billing_cost_normalised,2) }}</td> <td class="text-right">{{ number_format($o->billing_cost_normalised_taxed,2) }}</td>
<td class="text-right">{{ $o->product->hasUsage() ? number_format($o->type->usage_summary(0)->sum()/1000,1) : '-' }}</td> <td class="text-right">{{ $o->product->hasUsage() ? number_format($o->type->usage_summary(0)->sum()/1000,1) : '-' }}</td>
<td>{{ $o->product->supplier->name }}</td> <td>{{ $o->product->supplier->name }}</td>
</tr> </tr>

View File

@ -48,7 +48,7 @@
<tr> <tr>
<th>Amount</th> <th>Amount</th>
@if($o->is_charge_overridden) @if($o->is_charge_overridden)
<td>@if($o->billing_charge < $o->billing_orig)<del>${{ number_format($o->billing_orig_taxed,2) }}</del> @endif${{ number_format($o->billing_charge_taxed,2) }}</td> <td>@if($o->billing_charge < $o->billing_charge_orig)<del>${{ number_format($o->billing_charge_orig_taxed,2) }}</del> @endif${{ number_format($o->billing_charge_taxed,2) }}</td>
@else @else
<td>${{ number_format($o->billing_charge_taxed,2) }}</td> <td>${{ number_format($o->billing_charge_taxed,2) }}</td>
@endif @endif

View File

@ -16,6 +16,7 @@
<th>&nbsp;</th> <th>&nbsp;</th>
@endif @endif
</tr> </tr>
<tr> <tr>
<th>&nbsp;</th> <th>&nbsp;</th>
<th>Client</th> <th>Client</th>
@ -32,13 +33,14 @@
<tbody> <tbody>
<tr> <tr>
<th>Product</th> <th>Product</th>
<td><a href="{{ url('a/product/details',$o->product_id) }}">#{{ $o->product_id }}: {{ $o->product->name }}</a></td> <td><a href="{{ route('product',['pdo'=>$o->product_id]) }}">#{{ $o->product_id }}: {{ $o->product->name }}</a></td>
<td><a href="{{ url('a/product/details',$c->id) }}">#{{ $c->supplied->id }}: {{ $c->supplied->name_long }}</a></td> <td><a href="{{ route('supplier.product.type',['id'=>$c->supplied->id,'spo'=>$c->supplied->supplier_detail_id,'type'=>$c->supplied->category]) }}">#{{ $c->supplied->id }}: {{ $c->supplied->name_long }}</a></td>
@if($p->exists)
<th>&nbsp;</th>
<td class="text-center" colspan="2">#{{ $p->supplied->id }}: {{ $p->supplied->name_long }}</td>
@endif
<td>{{ $c->category_name }}</td> <td>{{ $c->category_name }}</td>
@if($p->exists)
<th><a href="{{ route('product',['pdo'=>$p->id]) }}">#{{ $p->id }}: {{ $p->name }}</a></th>
<td class="text-center"><a href="{{ route('supplier.product.type',['id'=>$p->supplied->id,'spo'=>$p->supplied->supplier_detail_id,'type'=>$p->supplied->category]) }}">#{{ $p->supplied->id }}: {{ $p->supplied->name_long }}</a></td>
<td>&nbsp;</td>
@endif
</tr> </tr>
<tr> <tr>
@ -68,7 +70,7 @@
<tr> <tr>
<th>Billing Price</th> <th>Billing Price</th>
<td @class(['text-danger'=>$o->is_charge_overridden])>${{ number_format($b=$o->billing_charge_taxed,2) }}</td> <td @class(['text-danger'=>$o->is_charge_overridden])>${{ number_format($b=$o->billing_charge_taxed,2) }}</td>
<td @class(['text-danger'=>$o->is_cost_overridden])>${{ number_format($a=$o->billing_cost,2) }}</td> <td @class(['text-danger'=>$o->is_cost_overridden])>${{ number_format($a=$o->billing_cost_taxed,2) }}</td>
<td>{!! markup($a,$b) !!}</td> <td>{!! markup($a,$b) !!}</td>
@if($p->exists) @if($p->exists)
<td @if($o->is_charge_overridden)class="text-danger"@endif>${{ number_format($b=$o->account->taxed($p->base_charge),2) }}</td> <td @if($o->is_charge_overridden)class="text-danger"@endif>${{ number_format($b=$o->account->taxed($p->base_charge),2) }}</td>
@ -81,22 +83,22 @@
<th>Monthly Price</th> <th>Monthly Price</th>
<td @class(['text-danger'=>$o->is_charge_overridden])> <td @class(['text-danger'=>$o->is_charge_overridden])>
@if($o->is_charge_overridden) @if($o->is_charge_overridden)
<abbr title="${{ number_format($b=$o->billing_orig_normalised_taxed,2) }}">${{ number_format($b=$o->billing_charge_normalised_taxed,2) }} <abbr title="${{ number_format($o->billing_charge_orig_normalised_taxed,2) }}">${{ number_format($b=$o->billing_charge_normalised_taxed,2) }}
@else @else
${{ number_format($b=$o->billing_charge_normalised_taxed,2) }} ${{ number_format($b=$o->billing_charge_normalised_taxed,2) }}
@endif @endif
</td> </td>
<td @class(['text-danger'=>$o->is_cost_overridden])> <td @class(['text-danger'=>$o->is_cost_overridden])>
@if($o->is_cost_overridden) @if($o->is_cost_overridden)
<abbr title="${{ number_format($a=$o->account->taxed($c->base_cost)*Invoice::billing_change($c->billing_interval,Invoice::BILL_MONTHLY),2) }}">${{ number_format($b=$o->billing_cost_normalised,2) }} <abbr title="${{ number_format($o->billing_cost_orig_normalised_taxed,2) }}">${{ number_format($a=$o->billing_cost_normalised_taxed,2) }}
@else @else
${{ number_format($a=$o->billing_cost_normalised,2) }} ${{ number_format($a=$o->billing_cost_normalised_taxed,2) }}
@endif @endif
</td> </td>
<td>{!! markup($a,$b) !!}</td> <td>{!! markup($a,$b) !!}</td>
@if($p->exists) @if($p->exists)
<td @if($o->is_charge_overridden) class="text-danger" @endif>${{ number_format($b=$o->account->taxed($p->base_charge)*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }}</td> <td @class(['text-danger'=>$o->is_charge_overridden])>${{ number_format($b=$o->account->taxed($p->base_charge)*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }}</td>
<td>${{ number_format($a=$o->account->taxed($p->base_cost)*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }}</td> <td @class(['text-danger'=>$o->is_cost_overridden])>${{ number_format($a=$o->account->taxed($p->base_cost)*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }}</td>
<td>{!! markup($a,$b) !!}</td> <td>{!! markup($a,$b) !!}</td>
@endif @endif
</tr> </tr>
@ -133,11 +135,14 @@
<td>${{ number_format($o->billing_charge_to($o->contract_end),2) }} (<small>{{ $o->paid_to?->format('Y-m-d') }}</small>)</td> <td>${{ number_format($o->billing_charge_to($o->contract_end),2) }} (<small>{{ $o->paid_to?->format('Y-m-d') }}</small>)</td>
<td>${{ number_format($o->billing_cost_to($o->contract_end),2) }} (<small>{{ $o->invoiced_to?->format('Y-m-d') }}</small>)</td> <td>${{ number_format($o->billing_cost_to($o->contract_end),2) }} (<small>{{ $o->invoiced_to?->format('Y-m-d') }}</small>)</td>
<td>{{ $o->contract_end->format('Y-m-d') }}<br><small>({{ $o->contract_end->diffForHumans(now(),CarbonInterface::DIFF_RELATIVE_TO_OTHER,FALSE,2) }} today)</small></td> <td>{{ $o->contract_end->format('Y-m-d') }}<br><small>({{ $o->contract_end->diffForHumans(now(),CarbonInterface::DIFF_RELATIVE_TO_OTHER,FALSE,2) }} today)</small></td>
@else @else
<td colspan="2" class="text-center">Not on contract</td> <td colspan="2" class="text-center">Not on contract</td>
<td>&nbsp;</td> <td>&nbsp;</td>
@endif @endif
@if($p->exists)
<td colspan="3">&nbsp;</td>
@endif
</tr> </tr>
</tbody> </tbody>
</table> </table>