2019-07-04 04:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-12-24 01:14:01 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2019-07-04 04:55:05 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-12-24 01:14:01 +00:00
|
|
|
|
|
|
|
/* METHODS */
|
|
|
|
|
|
|
|
/**
|
2023-05-05 05:48:24 +00:00
|
|
|
* Calculate Tax on a value, and return that value with tax applied
|
2021-12-24 01:14:01 +00:00
|
|
|
*
|
|
|
|
* @param float $value
|
|
|
|
* @param Collection $taxes
|
2023-05-05 05:48:24 +00:00
|
|
|
* @return float
|
2021-12-24 01:14:01 +00:00
|
|
|
*/
|
2023-05-05 05:48:24 +00:00
|
|
|
public static function calc(float $value,Collection $taxes): float
|
2021-12-24 01:14:01 +00:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
2019-07-04 04:55:05 +00:00
|
|
|
}
|