/**
  * parseTypeSelect
  */
 protected function parseTypeSelect($selected_id)
 {
     $Type = new ecommerce_promotion_type();
     $records = $Type->listing();
     foreach ($records as $item) {
         if ($item['id'] == $selected_id) {
             $item['selected'] = 'selected="selected"';
         }
         $this->tpl->assign("ITEM", $item);
         $this->tpl->parse("content.form.type.item");
     }
     $this->tpl->parse("content.form.type");
 }
 /**
  * check if existing code can be used
  */
 public function checkCodeBeforeApply($code, $customer_id, $basket, $customer_email = '')
 {
     if ($promotion_data = $this->checkCodeMatch($code)) {
         // make sure to avoid rounding errors
         $order_value = round($basket['sub_total']['price'], 2);
         /**
          * check if customer_id is needed (some codes can be used in guest checkout mode)
          */
         if ($customer_id == 0 && ($promotion_data['uses_per_customer'] > 0 || $promotion_data['limit_cumulative_discount'] > 0 || $promotion_data['generated_by_customer_id'] > 0 || $promotion_data['limit_by_customer_id'] > 0)) {
             if (!Zend_Registry::isRegistered('ecommerce_promotion:login_needed')) {
                 if ($_SESSION['client']['customer']['guest']) {
                     msg("We're sorry, but this voucher code cannot be used in conjunction with “Guest Checkout”. Please go to" . " previous step and select “Save Details for Next Order” to create an account.", 'error');
                 } else {
                     msg("You have to login or register to use your voucher code.", 'error');
                 }
                 Zend_Registry::set('ecommerce_promotion:login_needed', true);
             }
             return false;
         }
         /**
          * first order
          */
         if ($promotion_data['limit_to_first_order'] > 0) {
             if ($this->getNumCustomersOrders($customer_id, $customer_email) > 0) {
                 if ($this->getNumCustomersPaidOrders($customer_id, $customer_email) == 0) {
                     if (!Zend_Registry::isRegistered('ecommerce_promotion:first_order_unpaid')) {
                         msg("Code \"{$code}\" can only be applied to your first order. If you cancelled " . "your previous order, please either return to it in “My Account” or contact " . "customer services if you wish to continue with your current order.", 'error');
                         Zend_Registry::set('ecommerce_promotion:first_order_unpaid', true);
                     }
                 } else {
                     if (!Zend_Registry::isRegistered('ecommerce_promotion:first_order')) {
                         msg("We are sorry, this voucher is only valid on your first order", 'error');
                         Zend_Registry::set('ecommerce_promotion:first_order', true);
                     }
                 }
                 return false;
             }
         }
         /**
          *  uses_per_coupon
          */
         if ($promotion_data['uses_per_coupon'] > 0) {
             if ($this->getCountUsageOfSingleCode($code) + 1 > $promotion_data['uses_per_coupon']) {
                 if (substr($promotion_data['code_pattern'], 0, 4) != "REF-") {
                     // referral codes validity is extended automatically
                     if (!Zend_Registry::isRegistered('ecommerce_promotion:total_usage_exceeded')) {
                         msg("Code \"{$code}\" usage exceed number of allowed applications", 'error');
                         Zend_Registry::set('ecommerce_promotion:total_usage_exceeded', true);
                     }
                     return false;
                 }
             }
         }
         /**
          * check uses_per_customer
          */
         if ($promotion_data['uses_per_customer'] > 0) {
             if ($this->getCountUsageOfSingleCode($code, $customer_id) + 1 > $promotion_data['uses_per_customer']) {
                 if (!Zend_Registry::isRegistered('ecommerce_promotion:per_user_usage_exceeded')) {
                     msg("Code \"{$code}\" usage exceed number of allowed applications per one customer", 'error');
                     Zend_Registry::set('ecommerce_promotion:per_user_usage_exceeded', true);
                 }
                 return false;
             }
         }
         /**
          * check limit_cumulative_discount
          */
         if ($promotion_data['limit_cumulative_discount'] > 0) {
             require_once 'models/ecommerce/ecommerce_basket.php';
             $Basket = new ecommerce_basket();
             $Basket->calculateBasketDiscount($basket, $code, false);
             $usage = $this->getUsage($promotion_data['id'], $customer_id);
             if ($usage && $usage['sum_discount'] + $basket['discount'] > $promotion_data['limit_cumulative_discount']) {
                 $limit = money_format("%n", $promotion_data['limit_cumulative_discount']);
                 $provided = money_format("%n", $usage['sum_discount']);
                 if (!Zend_Registry::isRegistered('ecommerce_promotion:limit_cumulative_discount_exceeded')) {
                     $msg = "Code \"{$code}\" is limited to maximum discount value of {$limit}. " . "You’ve already used of {$provided}";
                     if ($promotion_data['discount_percentage_value'] > 0) {
                         $max_order = ($promotion_data['limit_cumulative_discount'] - $usage['sum_discount']) / ($promotion_data['discount_percentage_value'] / 100);
                         if ($max_order > 0) {
                             $max_order = money_format("%n", $max_order);
                             $msg .= ", so your current order would exceed your allotted discount value. " . "Please try again with an order of no greater than {$max_order}";
                         }
                     }
                     $msg .= ".";
                     msg($msg, 'error');
                     Zend_Registry::set('ecommerce_promotion:limit_cumulative_discount_exceeded', true);
                 }
                 return false;
             }
         }
         /**
          * not using self-generated code
          */
         if ($promotion_data['generated_by_customer_id'] > 0 && $promotion_data['generated_by_customer_id'] == $customer_id) {
             if (!Zend_Registry::isRegistered('ecommerce_promotion:own_code')) {
                 msg("You are not allowed to redeem your own code!");
                 Zend_Registry::set('ecommerce_promotion:own_code', true);
             }
             return false;
         }
         /**
          * code limited to customer_id
          */
         if ($promotion_data['limit_by_customer_id'] > 0 && $promotion_data['limit_by_customer_id'] != $customer_id) {
             if (!Zend_Registry::isRegistered('ecommerce_promotion:limit_by_customer_id')) {
                 msg("You are not allowed to redeem the code!");
                 Zend_Registry::set('ecommerce_promotion:limit_by_customer_id', true);
             }
             return false;
         }
         /**
          * minimum order amount
          */
         if ($promotion_data['limit_to_order_amount'] > 0) {
             if ($order_value < $promotion_data['limit_to_order_amount']) {
                 if (!Zend_Registry::isRegistered('ecommerce_promotion:order_amount')) {
                     $amount = money_format("%n", $promotion_data['limit_to_order_amount']);
                     $sub_total = money_format("%n", $order_value);
                     msg("The voucher code \"{$code}\" is restricted to orders in amount of {$amount}. You have only {$sub_total} in you basket.", 'error');
                     Zend_Registry::set('ecommerce_promotion:order_amount', true);
                 }
                 return false;
             }
         }
         /**
          * do not allow to buy gift voucher
          */
         if (substr($promotion_data['code_pattern'], 0, 4) == "REF-" || substr($promotion_data['code_pattern'], 0, 4) == "REW-") {
             $gift_voucher_product_id = (int) $this->getGiftVoucherProductId();
             if ($gift_voucher_product_id > 0 && count($basket['items']) > 0) {
                 foreach ($basket['items'] as $item) {
                     if ($item['product']['id'] == $gift_voucher_product_id) {
                         if (!Zend_Registry::isRegistered('ecommerce_promotion:gift_in_basket')) {
                             msg("Sorry, voucher codes cannot be used to buy Gift Voucher Codes.");
                             Zend_Registry::set('ecommerce_promotion:gift_in_basket', true);
                         }
                         return false;
                     }
                 }
             }
         }
         /**
          * check if limited products are in basket
          */
         $limited_ids = explode(",", $promotion_data['limit_list_products']);
         if (strlen($promotion_data['limit_list_products']) > 0 && is_array($limited_ids)) {
             $prod = 0;
             if (count($basket['items']) > 0) {
                 foreach ($basket['items'] as $item) {
                     if (in_array($item['product']['id'], $limited_ids)) {
                         $prod++;
                     }
                 }
             }
             if ($prod == 0) {
                 if (!Zend_Registry::isRegistered('ecommerce_promotion:limit_to_products')) {
                     msg("Sorry, the voucher code \"{$code}\" is limited to certain products, which you don't have in your basket.");
                     Zend_Registry::set('ecommerce_promotion:limit_to_products', true);
                 }
                 return false;
             }
         }
         /**
          * check if free promo item can be addeded to order
          */
         $promotion_data['free_promo_product'] = $this->checkForFreePromoItem($promotion_data, $order_value);
         require_once 'models/ecommerce/ecommerce_promotion_type.php';
         $Type = new ecommerce_promotion_type();
         $promotion_data['type'] = $Type->detail($promotion_data['type']);
         return $promotion_data;
     } else {
         return false;
     }
 }