aboutsummaryrefslogtreecommitdiff
path: root/classes/offer.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'classes/offer.class.php')
-rw-r--r--classes/offer.class.php91
1 files changed, 84 insertions, 7 deletions
diff --git a/classes/offer.class.php b/classes/offer.class.php
index 1b80ce0..75cb4f6 100644
--- a/classes/offer.class.php
+++ b/classes/offer.class.php
@@ -145,6 +145,42 @@ class offer {
}
/**
+ * Get all discount ids for this offer
+ *
+ * @see offer::getDiscounts() This funtion returns instances of the discount class instead of just the ids
+ *
+ * @throws PDOException Is something went wrong with the database
+ *
+ * @return int[] The ids
+ */
+ public function getDiscountIds() {
+ $ids = array();
+ $discounts = $this->pdo->query("SELECT `id` FROM `".constants::db_prefix."discount` WHERE `offerId`={$this->id}")->fetchAll(PDO::FETCH_ASSOC);
+ foreach ($discounts as $discount) {
+ $ids[] = $discount['id'];
+ }
+ return $ids;
+ }
+
+ /**
+ * Get all discounts for this offer
+ *
+ * @see offer::getDiscountIds() This function returns just the ids of the discounts, and not instances of the discount class
+ *
+ * @throws PDOException If something went wrong with the database
+ *
+ * @return discount[] An array indexed by id of instances of the discount class
+ */
+ public function getDiscounts() {
+ $ids = $this->getDiscountIds();
+ $discounts = array();
+ foreach ($ids as $id) {
+ $discounts[$id] = new discount($this->pdo, $id);
+ }
+ return $discounts;
+ }
+
+ /**
* Get the start date of the assignment
*
* @return int The start date as a UNIX timestamp
@@ -359,16 +395,21 @@ class offer {
$return = 0;
switch ($what) {
case self::SUBTOTAL:
- $assignments = $this->getAssignments();
- foreach ($assignments as $assignment) {
+ foreach ($this->getAssignments() as $assignment) {
$return += $assignment->calculate(assignment::SUBTOTAL, $round + 1, false);
}
+ foreach ($this->getDiscounts() as $discount) {
+ $return += $discount->calculate(discount::SUBTOTAL, $round + 1, false);
+ }
break;
case self::VAT:
$assignments = $this->getAssignments();
foreach ($assignments as $assignment) {
$return += $assignment->calculate(assignment::VAT, $round + 1, false);
}
+ foreach ($this->getDiscounts() as $discount) {
+ $return += $discount->calculate(discount::VAT, $round + 1, false);
+ }
break;
case self::TOTAL:
$return = $this->calculate(self::SUBTOTAL, $round + 1, false) + $this->calculate(self::VAT, $round + 1, false);
@@ -432,6 +473,36 @@ class offer {
$error = $stmt->errorInfo();
throw new Exception($error[2]);
}
+ }
+
+ /**
+ * Make a new discount linked to this order
+ *
+ * @param string $title The title for this discount
+ * @param string $description The description for this discount
+ * @param float $value The value for this discount
+ * @param float $vat The VAT percentage (so, 21 for 21%, not 0.21!)
+ *
+ * @throws PDOException If something went wrong with the database
+ * @throws Exception If there was a problem with the input
+ *
+ * @return discount A new instance of the discount class containing the new discount
+ */
+ public function createDiscount($title, $description, $value, $vat) {
+ $stmt = $this->pdo->prepare("INSERT INTO `".constants::db_prefix."discount` (`offerId`,`title`,`description`,`value`,`VAT_percentage`) VALUES (?,?,?,?,?)");
+ $stmt->execute(array(
+ $this->id,
+ $title,
+ $description,
+ $value,
+ $vat
+ ));
+ if ($stmt->rowCount() == 1) {
+ return new discount($this->pdo, $this->pdo->lastInsertId());
+ } else {
+ $error = $stmt->errorInfo();
+ throw new Exception($error[2]);
+ }
}
/**
@@ -459,15 +530,21 @@ class offer {
$invoice_nr = str_replace(array('invoice-','.pdf'), array('',''), $file->getFilename());
}
- $assignments = $this->getAssignments();
$list = array();
- foreach ($assignments as $assignment)
+ foreach ($this->getAssignments() as $assignment)
$list[] = array(
- $assignment->getTitle(),
- $assignment->getPricePerHour() * $assignment->getHours(),
- $assignment->getVAT() . "%",
+ $assignment->getTitle(),
+ $assignment->getPricePerHour() * $assignment->getHours(),
+ $assignment->getVAT() . "%",
$assignment->getPricePerHour() * $assignment->getHours() * (1 + $assignment->getVAT() / 100)
);
+ foreach ($this->getDiscounts() as $discount)
+ $list[] = array(
+ $discount->getTitle(),
+ $discount->calculate(discount::SUBTOTAL),
+ $discount->getVAT() . "%",
+ $discount->calculate(discount::TOTAL)
+ );
$pdf = new correspondence();
$pdf->SetContact($this->getContact());