Skip to content

Instantly share code, notes, and snippets.

@dawidgora-old-account
Last active April 18, 2017 13:29
Show Gist options
  • Save dawidgora-old-account/f4b38dc0d280a564e9c08389912c8901 to your computer and use it in GitHub Desktop.
Save dawidgora-old-account/f4b38dc0d280a564e9c08389912c8901 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Dawid Góra <[email protected]>
*/
class TaxCalculator
{
const TYPE_NET = 1;
const TYPE_GROSS = 2;
/**
* @var float
*/
private $netValue = 0.0;
/**
* @var float
*/
private $grossValue = 0.0;
/**
* @var float
*/
private $taxValue = 0.0;
/**
* @var Model_Tax
*/
private $taxModel;
/**
* @var int
*/
private $last = 0;
/**
* TaxCalculator constructor.
* @param Model_Tax $taxModel
*/
public function __construct(Model_Tax $taxModel = null)
{
if (!$taxModel || !$taxModel->loaded()) {
$taxModel = Model_Tax::getDefault();
}
$this->taxModel = $taxModel;
}
/**
* @return float
*/
public function getNetValue()
{
return $this->netValue;
}
/**
* @param float $netValue
*/
public function setNetValue($netValue)
{
$this->netValue = $netValue;
$this->taxValue = $this->netValue * $this->taxModel->getValue();
$this->grossValue = $this->netValue + $this->taxValue;
$this->last = self::TYPE_NET;
}
/**
* @return float
*/
public function getGrossValue()
{
return $this->grossValue;
}
/**
* @param float $grossValue
*/
public function setGrossValue($grossValue)
{
$this->grossValue = $grossValue;
$this->taxValue = $this->grossValue * $this->taxModel->getValue() / (1 + $this->taxModel->getValue());
$this->netValue = $this->grossValue - $this->taxValue;
$this->last = self::TYPE_GROSS;
}
/**
* @return float
*/
public function getTaxValue()
{
return $this->taxValue;
}
/**
* @return Model_Tax
*/
public function getTaxModel()
{
return $this->taxModel;
}
/**
* @param Model_Tax $taxModel
*/
public function setTaxModel($taxModel)
{
$this->taxModel = $taxModel;
if ($this->last === self::TYPE_NET) {
$this->setNetValue($this->netValue);
} elseif ($this->last === self::TYPE_GROSS) {
$this->setGrossValue($this->grossValue);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment