osb/app/Models/Tax.php

44 lines
728 B
PHP
Raw Normal View History

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class Tax extends Model
{
2021-12-17 03:59:55 +00:00
public $timestamps = FALSE;
2021-06-30 04:39:33 +00:00
2021-12-17 03:59:55 +00:00
/* RELATIONS */
public function country()
{
return $this->belongsTo(Country::class);
}
/* METHODS */
/**
* Calculate Tax on a value
*
* @param float $value
* @param Collection $taxes
* @return void
*/
public static function tax_calc(?float $value,Collection $taxes): float
{
if (! $value)
$value = 0;
$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);
}
}