aboutsummaryrefslogtreecommitdiff
path: root/classes/assignment.php
diff options
context:
space:
mode:
authorCamil Staps2016-07-20 11:15:48 +0200
committerCamil Staps2016-07-20 11:15:48 +0200
commit3e23534a1c49afdc3cd0e6f9ff785eaa7cb32fea (patch)
tree7e63d39bb1cc49222d4164e837d7757ca68c1f83 /classes/assignment.php
parentReorganise classes (diff)
Calculatable trait
Diffstat (limited to 'classes/assignment.php')
-rw-r--r--classes/assignment.php47
1 files changed, 9 insertions, 38 deletions
diff --git a/classes/assignment.php b/classes/assignment.php
index 9b7de09..c480994 100644
--- a/classes/assignment.php
+++ b/classes/assignment.php
@@ -25,6 +25,8 @@
* An interface to the assignment table in the database
*/
class assignment {
+ use Calculatable;
+
/**
* @var pdo $pdo The PDO class for database communication
* @var int $id The id of the assignment
@@ -256,43 +258,12 @@ class assignment {
// Other functions
//------------------------------------------------------------------------------
- /**
- * Calculate useful numbers about the assignment
- *
- * Subtotal: price_per_hour \* hours
- *
- * VAT: subtotal \* VAT_percentage
- *
- * Total: subtotal + VAT
- *
- * @param int $what Any of assignment::SUBTOTAL, assignment::VAT, assignment::TOTAL
- * @param int $round How many decimals to round 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 on $round decimals, or false when the input is incorrect
- */
- public function calculate($what = self::TOTAL, $round = 2, $format = true) {
- $return = 0;
- switch ($what) {
- case self::SUBTOTAL:
- $return = $this->getPricePerHour() * $this->getHours();
- break;
- case self::VAT:
- $return = $this->calculate(self::SUBTOTAL, $round + 1, false) * $this->getVAT() / 100;
- break;
- case self::TOTAL:
- $return = $this->calculate(self::SUBTOTAL, $round + 1, false) + $this->calculate(self::VAT, $round + 1, false);
- break;
- default:
- return false;
- }
- if ($format) {
- return number_format($return, $round);
- } else {
- return round($return, $round);
- }
+ public function calculateSubtotal() {
+ return $this->getHours() * $this->getPricePerHour();
+ }
+
+ public function calculateVAT() {
+ return $this->calculateSubtotal() * $this->getVAT() / 100;
}
/**
@@ -313,4 +284,4 @@ class assignment {
return true;
}
}
-} \ No newline at end of file
+}