public function delete($id = null)
 {
     if (!$id) {
         $id = $this->GetData('id');
     }
     $redemption = DiscountRedemption::find_by_id($id);
     if (!$redemption || $redemption->user->id != Site::CurrentUser()->id) {
         throw new Error404();
     }
     $redemption->destroy();
     Site::Flash("notice", "Discount code removed successfully.");
     RedirectBack("bookings/pay/{$cart_id}/");
 }
 public function redemptions($reload = false)
 {
     if (!$this->redemptions_cache || $reload) {
         $discount_id = mysql_real_escape_string($this->discount_id);
         $id = mysql_real_escape_string($this->item_id);
         $filter = "discount_codes.id = '{$discount_id}' AND cart_items.object_class = ";
         switch ($this->item_class) {
             case 'EventTicket':
                 $filter .= "'EventSignup' AND event_signups.event_ticket_id = '{$id}'";
                 break;
             case 'Service':
                 $filter .= "'EventService' AND event_services.service_id = '{$id}'";
                 break;
         }
         $this->redemptions_cache = DiscountRedemption::find_all($filter);
     }
     return $this->redemptions_cache;
 }
Example #3
0
 public function cost($unpaid_only = false, $ignore_discounts = false)
 {
     $total = 0;
     // Total items in cart
     foreach ($this->items() as $item) {
         if (!$unpaid_only or !$item->paid) {
             $total += $item->cost($unpaid_only, $ignore_discounts);
         }
     }
     if (!$ignore_discounts) {
         // If a full cart discount has been applied, discount it now
         if ($this->full_cart_discount()) {
             $id = mysql_real_escape_string($this->id);
             $discounts = DiscountRedemption::find_all("discount_redemptions.cart_id = '{$id}' AND discount_redemptions.cart_item_id IS NULL");
             foreach ($discounts as $discount) {
                 $total = $discount->discount->apply_discount($total);
             }
         }
     }
     if ($total < 0) {
         $total = 0;
     }
     return $total;
 }
 public static function load_from_row($row, $recurse_limit = 1, $current_level = 0)
 {
     // When PHP 5.3.0 is in, we can shift this over too and just redefine it
     // if needed (eg. loading child objects)
     $class = __CLASS__;
     $object = new $class();
     $fields = self::fields_array();
     $fields[] = array("created_at", "datetime");
     $fields[] = array("updated_at", "datetime");
     $fields[] = array("deleted", "bool");
     foreach ($fields as $field) {
         $property = $field[0];
         if (isset($field[2])) {
             $property = $field[2];
         }
         if (isset($row[self::table . ".{$field[0]}"])) {
             if ($field[1] == "datetime") {
                 $object->{$property} = strtotime($row[self::table . ".{$field[0]}"]);
             } else {
                 $object->{$property} = $row[self::table . ".{$field[0]}"];
             }
         } else {
             $object->{$property} = null;
         }
     }
     $object->is_new = false;
     // Load child objects here
     if ($current_level < $recurse_limit) {
         $current_level++;
         $object->cart = Cart::load_from_row($row, $recurse_limit, $current_level);
         $object->discount = DiscountRedemption::load_from_row($row, $recurse_limit, $current_level);
         // Speshul! The object is an unknown type!
         $object->object = call_user_func(array($object->object_class, "find_by_id"), $object->object_id);
     }
     return $object;
 }
 public function complete()
 {
     if (isset($_GET['merchant_return_link'])) {
         $id = $_GET['id'];
         Redirect("payments/{$id}/complete");
     }
     $id = mysql_real_escape_string($_GET['id']);
     $user_id = mysql_real_escape_string(Site::CurrentUser()->id);
     $cart = Cart::find("carts.user_id = '{$user_id}' AND carts.id = '{$id}'");
     if ($cart) {
         // Validate Cart
         foreach ($cart->items() as $item) {
             if ($item->object == null) {
                 Error404();
             }
         }
         $signups = $cart->get_signups();
         if (count($signups) == 1) {
             $this->assign("signup", current($signups));
         }
         $redemptions = array();
         if ($cart->full_cart_discount()) {
             $id = mysql_real_escape_string($cart->id);
             $redemptions = DiscountRedemption::find_all("discount_redemptions.cart_id = '{$id}' and discount_redemptions.cart_item_id is null");
         }
         $this->assign("cart", $cart);
         $this->assign("cart_discounts", $redemptions);
         $this->title = "Payment Complete";
         $this->render("paymenttransaction/complete.tpl");
     } else {
         Error404();
     }
 }
 public function count_redemptions()
 {
     $id = mysql_real_escape_string($this->id);
     return DiscountRedemption::count("discount_codes.id = '{$id}' AND carts.paid");
 }