osb/app/Models/Tax.php

42 lines
733 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class Tax extends Model
{
public $timestamps = FALSE;
/* RELATIONS */
public function country()
{
return $this->belongsTo(Country::class);
}
/* METHODS */
/**
* Calculate Tax on a value, and return that value with tax applied
*
* @param float $value
* @param Collection $taxes
* @return float
*/
public static function calc(float $value,Collection $taxes): float
{
$tax = 0;
foreach ($taxes as $o) {
// Quick sanity check
if (! $o instanceof self)
abort(500,'Invalid object for tax calculation');
$tax += round($value*$o->rate,2);
}
return round($value+$tax,2);
}
}