Skip to content

Instantly share code, notes, and snippets.

@nietzscheson
Created July 1, 2016 15:04
Show Gist options
  • Save nietzscheson/d727988f3c3350d278c86bee397d0fdc to your computer and use it in GitHub Desktop.
Save nietzscheson/d727988f3c3350d278c86bee397d0fdc to your computer and use it in GitHub Desktop.
<?php
class VAT
{
private $value;
private $percent;
public function setValue($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
public function setPercent($percent)
{
$this->percent = $percent;
}
public function getPercent()
{
return $this->percent;
}
/**
* Add VAT to value
*/
public function withVAT()
{
return $this->operationVAT($this->value, 1);
}
/**
* Show VAT percent to value
*/
public function withoutVAT()
{
return $this->operationVAT($this->value, 2);
}
private function percentOperation()
{
return $this->percent / 100;
}
private function operationVAT($value, $operation)
{
$values = [];
switch ($operation) {
case 1:
$vat = $value * $this->percentOperation();
$values = [
'subtotal' => $value,
'vat' => $vat,
'total' => $value + $vat
];
break;
case 2:
$sutotal = $value / ($this->percentOperation() + 1);
$values = [
'subtotal' => $sutotal,
'vat' => $value - $sutotal,
'total' => $value
];
break;
}
return $values;
}
}
$vat = new VAT();
$vat->setValue(100);
$vat->setPercent(16);
echo "<pre>";print_r($vat->withVAT());
echo "<pre>";print_r($vat->withoutVAT());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment