Пример #1
0
 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 editpostAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         $html = '';
         try {
             // Test s'il y a un value_id
             if (empty($datas['value_id'])) {
                 throw new Exception($this->_('An error occurred while saving. Please try again later.'));
             }
             // Récupère l'option_value en cours
             $option_value = new Application_Model_Option_Value();
             $option_value->find($datas['value_id']);
             // Instancie une nouvelle promotion
             $promotion = new Promotion_Model_Promotion();
             // Test si l'option des réductions spéciales est activée et payée
             if ($id = $this->getRequest()->getParam('id')) {
                 $promotion->find($id);
                 if ($promotion->getValueId() and $promotion->getValueId() != $option_value->getId()) {
                     throw new Exception('An error occurred while saving. Please try again later.');
                 }
             }
             //Vérifs champs
             if (empty($datas['title']) || empty($datas['description']) || empty($datas['title'])) {
                 throw new Exception($this->_('An error occurred while saving your discount. Please fill in all fields'));
                 die;
             }
             if (!isset($datas['is_illimited']) && empty($datas['end_at'])) {
                 throw new Exception($this->_('An error occurred while saving your discount. Please fill in all fields'));
                 die;
             }
             if (!empty($datas['end_at'])) {
                 $date_actuelle = new Zend_Date();
                 $date_modif = new Zend_Date($datas['end_at'], 'y-MM-dd');
                 if ($date_modif < $date_actuelle) {
                     throw new Exception($this->_('Please select an end date greater than the current date.'));
                     die;
                 }
             }
             if (!empty($datas['is_illimited'])) {
                 $datas['end_at'] = null;
             }
             $datas['force_validation'] = !empty($datas['force_validation']);
             $datas['is_unique'] = !empty($datas['is_unique']);
             $datas['owner'] = 1;
             $promotion->setData($datas);
             if (isset($datas['available_for']) and $datas['available_for'] == 'all') {
                 $promotion->resetConditions();
             }
             $promotion->save();
             $html = array('promotion_id' => $promotion->getId(), 'success_message' => $this->_('Discount successfully saved'), 'message_timeout' => 2, 'message_button' => 0, 'message_loader' => 0);
         } catch (Exception $e) {
             $html = array('message' => $e->getMessage(), 'url' => '/promotion/admin/list');
         }
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
 }
 public function deletepostAction()
 {
     $id = $this->getRequest()->getParam("id");
     $html = '';
     try {
         $promotion = new Promotion_Model_Promotion();
         $promotion->find($id);
         if ($promotion->getUnlockBy() == "qrcode") {
             $image_name = $promotion->getId() . "-qrpromotion_qrcode.png";
             $file = Core_Model_Directory::getBasePathTo("/images/application/" . $this->getApplication()->getId() . "/application/qrpromotion/" . $image_name);
             if (file_exists($file)) {
                 unlink($file);
             }
         }
         $promotion->delete();
         $html = array('promotion_id' => $id, 'success' => 1, 'success_message' => $this->_('Discount successfully deleted'), 'message_timeout' => 2, 'message_button' => 0, 'message_loader' => 0);
     } catch (Exception $e) {
         $html = array('message' => $e->getMessage(), 'url' => '/promotion/admin/list');
     }
     $this->getLayout()->setHtml(Zend_Json::encode($html));
 }