osb/app/Models/Tax.php

52 lines
946 B
PHP
Raw Permalink Normal View History

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use App\Traits\ProviderRef;
class Tax extends Model
{
use ProviderRef;
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);
}
public function providers()
{
return $this->belongsToMany(ProviderOauth::class,'tax__provider')
->withPivot('ref','synctoken','created_at','updated_at');
}
/* METHODS */
/**
2023-05-05 05:48:24 +00:00
* Calculate Tax on a value, and return that value with tax applied
*
* @param float $value
* @param Collection $taxes
2023-05-05 05:48:24 +00:00
* @return float
*/
2023-05-05 05:48:24 +00:00
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);
}
}