. */ /** * The calculatable trait, to be used by something of which subtotal, VAT and * total can be calculated */ trait Calculatable { /** * Calculate the subtotal * * @return float The subtotal */ abstract protected function calculateSubtotal(); /** * Calculate the VAT * * @return float The VAT */ abstract protected function calculateVAT(); /** * Calculate the total * * @return float The total */ protected 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 offer::SUBTOTAL, offer::VAT and offer::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 = self::TOTAL, $round = 2, $format = true) { $return = 0; switch ($what) { case self::SUBTOTAL: $return = $this->calculateSubtotal(); break; case self::VAT: $return = $this->calculateVAT(); break; case self::TOTAL: $return = $this->calculateTotal(); break; default: return false; } if ($format) { return number_format($return, $round); } else { return $return; } } }