From 3e23534a1c49afdc3cd0e6f9ff785eaa7cb32fea Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 20 Jul 2016 11:15:48 +0200 Subject: Calculatable trait --- classes/Calculatable.php | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 classes/Calculatable.php (limited to 'classes/Calculatable.php') diff --git a/classes/Calculatable.php b/classes/Calculatable.php new file mode 100644 index 0000000..d98f969 --- /dev/null +++ b/classes/Calculatable.php @@ -0,0 +1,58 @@ +. + */ + +/** + * The calculatable trait, to be used by something of which subtotal, VAT and + * total can be calculated + */ +trait Calculatable { + abstract public function calculateSubtotal(); + abstract public function calculateVAT(); + + public function calculateTotal() { + return $this->calculateSubtotal() + $this->calculateVAT(); + } + + 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; + } + } +} -- cgit v1.2.3