Ejemplo n.º 1
0
 function save()
 {
     $data = JRequest::get('post');
     $checkchracter = EnmasseHelper::checkSpecialCharacter($data['name']);
     if ($checkchracter) {
         $msg = JText::_('SAVE_DEAL_NOTICE');
         if ($data['id'] == null) {
             $this->setRedirect('index.php?option=com_enmasse&controller=' . JRequest::getVar('controller') . '&task=add', $msg, 'notice');
             return false;
         } else {
             $this->setRedirect('index.php?option=com_enmasse&controller=' . JRequest::getVar('controller') . '&task=edit&cid[0]=' . $data['id'], $msg, 'notice');
             return false;
         }
     }
     $data['slug_name'] = EnmasseHelper::seoUrl($data['name']);
     $data['description'] = JRequest::getVar('description', '', 'post', 'string', JREQUEST_ALLOWRAW);
     $data['highlight'] = JRequest::getVar('highlight', '', 'post', 'string', JREQUEST_ALLOWRAW);
     $data['terms'] = JRequest::getVar('terms', '', 'post', 'string', JREQUEST_ALLOWRAW);
     if ($data['slug_name'] == '_' || $data['slug_name'] == '') {
         $now = str_replace(":", " ", DatetimeWrapper::getDatetimeOfNow());
         $data['slug_name'] = EnmasseHelper::seoUrl($now);
     }
     $model = JModel::getInstance('deal', 'enmasseModel');
     //---------------------------------------------------------------
     // if edit deal
     if ($data['id'] > 0) {
         //---get deal data
         $deal = JModel::getInstance('deal', 'enmasseModel')->getById($data['id']);
         // get sold coupon qty for deal
         $soldCouponList = JModel::getInstance('invty', 'enmasseModel')->getSoldCouponByPdtId($deal->id);
         //if from unlimited to limited
         if ($deal->max_coupon_qty < 0) {
             if ($data['max_coupon_qty'] > 0) {
                 if ($data['max_coupon_qty'] <= count($soldCouponList)) {
                     $msg = JText::_('MSG_CURRENT_SOLD_GRATER_THAN_MODIFIED_COUPON');
                     JFactory::getApplication()->redirect('index.php?option=com_enmasse&controller=deal&task=edit&cid=' . $data['id'], $msg);
                 } else {
                     $numOfAddCoupon = $data['max_coupon_qty'] - count($soldCouponList);
                 }
             }
         } else {
             //---------------- if change from limited to unlimited
             if ($data['max_coupon_qty'] < 0) {
                 $unlimit = true;
             } else {
                 if ($data['max_coupon_qty'] < $deal->max_coupon_qty) {
                     //---------------------- if new coupon qty <= the sold coupon qty
                     if ($data['max_coupon_qty'] <= count($soldCouponList)) {
                         $msg = JText::_('MSG_CURRENT_SOLD_GRATER_THAN_MODIFIED_COUPON');
                         JFactory::getApplication()->redirect('index.php?option=com_enmasse&controller=deal&task=edit&cid=' . $data['id'], $msg);
                     } else {
                         $numOfRemoveCoupon = $deal->max_coupon_qty - $data['max_coupon_qty'];
                     }
                 } else {
                     if ($data['max_coupon_qty'] > $deal->max_coupon_qty) {
                         $numOfAddCoupon = $data['max_coupon_qty'] - $deal->max_coupon_qty;
                     }
                 }
             }
         }
         // suong.mai edit save description to oder_item
         $db =& JFactory::getDBO();
         $query = "UPDATE #__enmasse_order_item\n\t\t\t\t\t  SET description ='" . $data['name'] . "'" . "WHERE pdt_id=" . $data['id'];
         $db->setQuery($query);
         $db->query();
         //$db->updateObject($this->, $object, $key)
     }
     //------------------------
     //gemerate integration class
     $integrateFileName = EnmasseHelper::getSubscriptionClassFromSetting() . '.class.php';
     $integrationClass = EnmasseHelper::getSubscriptionClassFromSetting();
     require_once JPATH_SITE . DS . "components" . DS . "com_enmasse" . DS . "helpers" . DS . "subscription" . DS . $integrationClass . DS . $integrateFileName;
     $integrationObject = new $integrationClass();
     // store data
     $row = $model->store($data);
     if ($row->success) {
         if ($data['id'] == 0) {
             $integrationObject->integration($row, 'newDeal');
         }
         //--------------------------------------
         // store location and category
         JModel::getInstance('dealCategory', 'enmasseModel')->store($row->id, $data['pdt_cat_id']);
         JModel::getInstance('dealLocation', 'enmasseModel')->store($row->id, $data['location_id']);
         // if is new deal and limited the coupdon then create coupon in invty
         if ($data['id'] == 0 && $row->max_coupon_qty > 0) {
             for ($i = 0; $i < $row->max_coupon_qty; $i++) {
                 $name = $i + 1;
                 JModel::getInstance('invty', 'enmasseModel')->generateCouponFreeStatus($row->id, $name, 'Free');
             }
         } else {
             if ($data['id'] != 0) {
                 if (!empty($numOfRemoveCoupon)) {
                     $freeCouponList = JModel::getInstance('invty', 'enmasseModel')->getCouponFreeByPdtID($data['id']);
                     // removed the coupons from invty
                     for ($i = 0; $i < $numOfRemoveCoupon; $i++) {
                         JModel::getInstance('invty', 'enmasseModel')->removeById($freeCouponList[$i]->id);
                     }
                 } else {
                     if (!empty($numOfAddCoupon)) {
                         // add more coupon to invty
                         for ($i = 0; $i < $numOfAddCoupon; $i++) {
                             $name = $i + 1;
                             JModel::getInstance('invty', 'enmasseModel')->generateCouponFreeStatus($data['id'], $name, 'Free');
                         }
                     } else {
                         if ($unlimit) {
                             //remove all free coupon
                             JModel::getInstance('invty', 'enmasseModel')->removeCouponByPdtIdAndStatus($data['id'], 'Free');
                         }
                     }
                 }
             }
         }
         $msg = JText::_('SAVE_SUCCESS_MSG');
         $this->setRedirect('index.php?option=com_enmasse&controller=' . JRequest::getVar('controller'), $msg, $type);
     } else {
         $msg = JText::_('SAVE_ERROR_MSG') . ": " . $model->getError();
         if ($data['id'] == null) {
             $this->setRedirect('index.php?option=com_enmasse&controller=' . JRequest::getVar('controller') . '&task=add', $msg, 'error');
         } else {
             $this->setRedirect('index.php?option=com_enmasse&controller=' . JRequest::getVar('controller') . '&task=edit&cid[0]=' . $data['id'], $msg, 'error');
         }
     }
 }