<?php
namespace App\Utils\BaseParser\Structure\StructureParser\Types; abstract class PriceTax extends \App\Utils\BaseParser\Structure\StructureParser\Types\BaseAbstract { public $amount = null; public $VAT = null; public $currency = null; public $components = []; public function getClone() { return unserialize(serialize($this)); } public function getAmount() { return $this->amount; } public function getVAT() { return $this->VAT; } public function getCurrency() { return $this->currency; } public function setAmount($amount) { $this->amount = $amount; return $this; } public function setVAT($VAT) { $this->VAT = $VAT; return $this; } public function setCurrency($currency) { $this->currency = $currency; return $this; } public function removeAllComponents() { $this->components = []; $this->amount = null; $this->currency = null; $this->VAT = null; return $this; } public function checkIntegrity() { $amount = 0; foreach($this->components as $comp){ $amount+=$comp->amount; } if (abs($amount - $this->amount)>0.001) { throw new \App\Utils\BaseParser\Structure\StructureParser\Types\Exception\Price("The sum of the tax components doesn't match the total tax amount ({$amount} vs {$this->amount} {$this->currency})"); } return $this; } public function append(\App\Utils\BaseParser\Structure\StructureParser\Types\PriceTaxComponent $tax) { if($this->currency && ($tax->currency!=$this->currency)){ throw new \App\Utils\BaseParser\Structure\StructureParser\Types\Exception\Price("Tax currency is different than the total currency(actual currency: {$this->currency}, tax:{$tax->currency})"); } $this->amount+= $tax->amount; $this->currency = $tax->currency; if ($tax->VAT!==null) { $this->VAT+= $tax->VAT; } $this->components[] = $tax; return $this; } public function remove(\App\Utils\BaseParser\Structure\StructureParser\Types\PriceTaxComponent $tax) { foreach ($this->components as $idx=>$comp) { if ($comp->getId()===$tax->getId()) { $this->amount-= $comp->amount; if ($comp->VAT!==null) { $this->VAT-= $comp->VAT; } unset($this->components[$idx]); return $this; } } throw new \App\Utils\BaseParser\Structure\StructureParser\Types\Exception\Price(sprintf("[%s] Cannot remove tax. Tax '%s' not found", __METHOD__, $tax->getId())); } public function sub(\App\Utils\BaseParser\Structure\StructureParser\Types\PriceTaxComponent $component) { $component->amount = 0 - $component->amount; $component->VAT = 0 - $component->VAT; return $this->append($component); } public function add(\App\Utils\BaseParser\Structure\StructureParser\Types\PriceTaxComponent $component) { return $this->append($component); } public function appendAll(\App\Utils\BaseParser\Structure\StructureParser\Types\PriceTax $taxes) { foreach ($taxes->components as $tax) { $this->append($tax); } return $this; } public function getTaxByCode($code, $index = null) { $taxes = []; foreach ($this->components as $comp) { if ($comp->code==$code) { $taxes[] = $comp; } } if ($index===null) { return $taxes; } return $taxes[$index]; } } 