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;
 }
Пример #2
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 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 redeem($cart)
 {
     // Check the discount is valid
     if (!$this->is_valid()) {
         throw new Error500('Discount is not valid');
     }
     // Check the cart hasn't been fully discounted already
     if ($cart->has_exclusive_discount()) {
         throw new Error500('This cart cannot have any more discounts');
     }
     if ($this->exclusive && count($cart->discounts()) > 0) {
         throw new Error500('You cannot add this discount while there are other discounts');
     }
     // Grab all cart items referenced by a discount
     $id = mysql_real_escape_string($cart->id);
     $discounts = DiscountRedemption::find_all("carts.id = '{$id}'");
     foreach ($discounts as $discount) {
         if ($discount->discount->id == $this->id) {
             throw new Error500('That discount has already been added');
         }
     }
     if ($this->loyalty) {
         $user_id = mysql_real_escape_string($cart->user_id);
         $id = mysql_real_escape_string($this->id);
         $ld = LoyaltyDiscount::find("users.id = '{$user_id}' AND discount_codes.id = '{$id}'");
         if (!$ld) {
             throw new Error500('You are not allowed to use this discount');
         }
     }
     // If this is a full cart discount, apply it
     if ($this->full_cart_discount) {
         $this->add_for_cart($cart);
         return true;
     }
     // Let's get some sorted discount items
     $services = array();
     foreach ($this->services() as $item) {
         $services[] = $item->item->id;
     }
     $tickets = array();
     foreach ($this->tickets() as $item) {
         $tickets[] = $item->item->id;
     }
     $discounted = false;
     $count = $this->count_redemptions();
     $availableQuantity = $this->quantity - $count;
     foreach ($cart->items() as $item) {
         if ($availableQuantity <= 0) {
             break;
         }
         // We now need to check if the item can be discounted
         if (is_a($item->object, 'EventSignup')) {
             // We need to check the item's EventTicket
             if (!in_array($item->object->event_ticket_id, $tickets)) {
                 continue;
             }
         } elseif (is_a($item->object, 'EventService')) {
             // Check that the Service is allowed
             if (!in_array($item->object->service_id, $services)) {
                 continue;
             }
         }
         $this->add_for_cart($cart, $item);
         $discounted = true;
         $availableQuantity--;
     }
     if (!$discounted) {
         throw new Error500('There is nothing that can be discounted');
     }
     return true;
 }