function save() { $data = JRequest::get('post'); $data['name'] = trim($data['name']); $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; } } } } } //------------------------ //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 coupon 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); } 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'); } } }
function dealSave() { $dAuthorId = $this->checkAccess(); $data = JRequest::get('post'); $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 is edit if ($data['id'] != 0) { $deal = JModel::getInstance('deal', 'enmasseModel')->getById($data['id'], $dAuthorId); if (!deal || $deal->status == "On Sales" && strtotime($deal->start_at) < time()) { $msg = JText::_('SALES_PERSON_EDIT_DEAL_ON_SALE_ERROR'); JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_enmasse&controller=salesPerson&task=dealShow'), $msg, 'error'); } if ($data['max_coupon_qty'] > 0 && $data['max_coupon_qty'] < $deal->max_coupon_qty) { if ($data['max_coupon_qty'] <= $deal->cur_sold_qty) { $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 { $removeCoupons = $deal->max_coupon_qty - $data['max_coupon_qty']; } } else { if ($data['max_coupon_qty'] > 0 && $data['max_coupon_qty'] > $deal->max_coupon_qty) { if ($deal->max_coupon_qty < 0) { $addCoupons = $data['max_coupon_qty']; } else { $addCoupons = $data['max_coupon_qty'] - $deal->max_coupon_qty; } } else { if ($data['max_coupon_qty'] < 0 && $deal->max_coupon_qty > 0) { $unlimit = true; } } } } $data['status'] = "Pending"; //set satus of this deal to Pending (for modify and add new) $data['sales_person_id'] = JFactory::getSession()->get('salesPersonId'); $row = JModel::getInstance('deal', 'enmasseModel')->store($data); if ($row->success) { // add 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 not edit and limited the coupon 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($removeCoupons)) { $freeCoupon = JModel::getInstance('invty', 'enmasseModel')->getCouponFreeByPdtID($data['id']); for ($i = 0; $i < $removeCoupons; $i++) { JModel::getInstance('invty', 'enmasseModel')->removeById($freeCoupon[$i]->id); } } else { if (!empty($addCoupons)) { for ($i = 0; $i < $addCoupons; $i++) { $name = $i + 1; JModel::getInstance('invty', 'enmasseModel')->generateCouponFreeStatus($data['id'], $name, 'Free'); } } else { if ($unlimit) { JModel::getInstance('invty', 'enmasseModel')->removeCouponByPdtIdAndStatus($data['id'], 'Free'); } } } } } $msg = JText::_('DEAL_PENDING_MSG'); } else { $msg = JText::_('DEAL_SAVE_ERROR'); } // Check the table in so it can be edited.... we are done with it anyway $link = 'index.php?option=com_enmasse&controller=salesPerson&task=dealShow'; $this->setRedirect($link, $msg); }