. */ /** * The calculatable interface, to be used by something of which subtotal, VAT * and total can be calculated */ interface Calculatable { const SUBTOTAL = 1; const VAT = 2; const TOTAL = 3; /** * Calculate the subtotal * * @return float The subtotal */ function calculateSubtotal(); /** * Calculate the VAT * * @return float The VAT */ function calculateVAT(); /** * Calculate the total * * @return float The total */ function calculateTotal(); /** * Calculate and possibly format * * Subtotal: the sum of the prices of the assignments excl. VAT * VAT: the sum of all the VAT from all the assignments * Total: the sum of subtotal and total * * @param int $what Any of Calculatable::SUBTOTAL, Calculatable::VAT and Calculatable::TOTAL * @param int $round How many decimals to round the result on * @param bool $format Whether to format the number nicely (for output) or not (for calculations) * * @throws PDOException If something went wrong with the database * * @return float|bool The calculated value rounded to $round decimals, or false on incorrect input */ function calculate($what = Calculatable::TOTAL, $round = 2, $format = true); } /** * The calculatable trait, to be used by something of which subtotal, VAT and * total can be calculated */ trait StandardCalculatable { abstract public function calculateSubtotal(); abstract public function calculateVAT(); /** * Calculate the total * * @return float The total */ public function calculateTotal() { return $this->calculateSubtotal() + $this->calculateVAT(); } /** * Calculate and possibly format * * Subtotal: the sum of the prices of the assignments excl. VAT * VAT: the sum of all the VAT from all the assignments * Total: the sum of subtotal and total * * @param int $what Any of Calculatable::SUBTOTAL, Calculatable::VAT and Calculatable::TOTAL * @param int $round How many decimals to round the result on * @param bool $format Whether to format the number nicely (for output) or not (for calculations) * * @throws PDOException If something went wrong with the database * * @return float|bool The calculated value rounded to $round decimals, or false on incorrect input */ public function calculate($what = Calculatable::TOTAL, $round = 2, $format = true) { $return = 0; switch ($what) { case Calculatable::SUBTOTAL: $return = $this->calculateSubtotal(); break; case Calculatable::VAT: $return = $this->calculateVAT(); break; case Calculatable::TOTAL: $return = $this->calculateTotal(); break; default: return false; } if ($format) { return number_format($return, $round); } else { return $return; } } }