Add validation to service cancellation, and displaying cancellation costs if any

This commit is contained in:
2024-08-17 10:33:56 +10:00
parent 7a41dd803f
commit 6ac1b11864
9 changed files with 142 additions and 31 deletions

View File

@@ -14,8 +14,9 @@ use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Leenooks\Casts\LeenooksCarbon;
use App\Models\Product\Type;
use App\Interfaces\IDs;
use App\Models\Product\Type;
use App\Models\Service\Broadband;
use App\Traits\{ScopeAccountUserAuthorised,ScopeServiceActive,SiteID};
/**
@@ -565,6 +566,26 @@ class Service extends Model implements IDs
return Invoice::billing_name($this->getBillingIntervalAttribute());
}
/**
* Return the earliest date that the service can be cancelled
*
* @return Carbon
*/
public function getCancelDateAttribute(): Carbon
{
switch (get_class($this->type)) {
// Broadband needs 30 days notice
case Broadband::class:
$date = Carbon::now()->addMonth();
break;
default:
$date = Carbon::now()->addDay();
}
return $this->getContractEndAttribute()->lessThan($date) ? $date : $this->getContractEndAttribute();
}
/**
* The date the contract ends
*
@@ -582,7 +603,7 @@ class Service extends Model implements IDs
if (! $this->start_at)
return $this->type->expire_at;
$end = $this->start_at->addMonths($this->getContractTermAttribute());
$end = $this->start_at->clone()->addMonths($this->getContractTermAttribute());
// If we dont have an expire date, use the start date + contract_term
if (! $this->type->expire_at)
@@ -892,6 +913,27 @@ class Service extends Model implements IDs
: $this->price;
}
/**
* Provide billing charge to a future date
*
* @param Carbon $date
* @return float
* @throws Exception
*/
public function billing_charge_to(Carbon $date): float
{
// if the date is less than the paid to, but less than the cancel date to, return cancel-paid to charge
// If the date is greater than the paid to, and less than the cancel date to, return cancel-paid to charge
if ($this->getPaidToAttribute()->lessThan($this->getCancelDateAttribute())) {
$max = max($date,$this->getPaidToAttribute())->clone();
$d = $max->diffInDays($this->getCancelDateAttribute());
return $this->account->taxed($d/30*$this->getBillingChargeNormalisedAttribute());
}
return 0;
}
/**
* Get the stage parameters
*