public function unlockbyqrcodeAction()
 {
     try {
         $customer_id = $this->getSession()->getCustomerId();
         if (!$customer_id) {
             throw new Exception($this->_('You must be logged in to use a discount'));
         }
         if ($data = Zend_Json::decode($this->getRequest()->getRawBody())) {
             $promotion = new Promotion_Model_Promotion();
             $promotion->find(array("unlock_code" => $data["qrcode"], "value_id" => $data["value_id"]));
             $promotion_id = $promotion->getId();
             $promotion_customer = new Promotion_Model_Customer();
             $promotion_customer->findLast($promotion_id, $customer_id);
             if ($promotion->getUnlockCode() != $data["qrcode"]) {
                 throw new Exception($this->_("This code is unrecognized"));
             }
             if ($promotion_customer->getIsUsed() != "" && $promotion_customer->getIsUsed() == 0) {
                 throw new Exception($this->_("You have already use this code"));
             }
             if (!$promotion_customer->getId()) {
                 $promotion_customer->setPromotionId($promotion_id)->setCustomerId($customer_id);
             }
             $promotion_customer->setIsUsed(0)->save();
             $html = array("success" => 1);
         }
     } catch (Exception $e) {
         $html = array("error" => 1, "message" => $e->getMessage());
     }
     $this->_sendHtml($html);
 }
 public function validateAction()
 {
     try {
         $customer_id = $this->getSession()->getCustomerId();
         if (!$customer_id) {
             throw new Exception($this->_('You must be logged in to use a discount'));
         }
         $html = array();
         if ($promotion_id = $this->getRequest()->getPost('promotion_id')) {
             // Prépare la promotion
             $promotion = new Promotion_Model_Promotion();
             $promotion->find($promotion_id);
             // Prépare la promotion du client
             $promotion_customer = new Promotion_Model_Customer();
             $promotion_customer->findLast($promotion_id, $customer_id);
             if (!$promotion_customer->getId()) {
                 $promotion_customer->setPromotionId($promotion_id)->setCustomerId($customer_id);
             }
             if ($promotion->getIsUnique() and $promotion_customer->getId() and $promotion_customer->getIsUsed()) {
                 $html['close'] = true;
                 throw new Exception($this->_('You have already use this discount'));
             } else {
                 $promotion_customer->setIsUsed(1)->save();
                 $html = array('ok' => true);
             }
         }
     } catch (Exception $e) {
         $html['error'] = 1;
         $html['message'] = $e->getMessage();
     }
     $this->_sendHtml($html);
 }