Esempio n. 1
0
 public function postProcess()
 {
     global $currentIndex, $cookie;
     $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
     if ($discountName = Tools::getValue('name') and Validate::isDiscountName($discountName) and Discount::discountExists($discountName, Tools::getValue('id_discount'))) {
         $this->_errors[] = Tools::displayError('A voucher of this name already exists. Please choose another name.');
     }
     if (Tools::getValue('submitAdd' . $this->table)) {
         if (Tools::getValue('id_discount_type') == 0) {
             $this->_errors[] = Tools::displayError('Please set a type for this voucher.');
         }
         if (Tools::getValue('id_discount_type') == 2 and Tools::getValue('id_currency') == 0) {
             $this->_errors[] = Tools::displayError('Please set a currency for this voucher.');
         }
         if ((Tools::getValue('id_discount_type') == 1 || Tools::getValue('id_discount_type') == 2) && !Tools::getValue('value')) {
             $this->_errors[] = Tools::displayError('Please set a amount for this voucher.');
         }
         if (!Validate::isBool_Id(Tools::getValue('id_target'))) {
             $this->_errors[] = Tools::displayError('Invalid customer or group ID field');
         } else {
             $rules = explode('_', Tools::getValue('id_target'));
             /* In form, there is one field for two differents fields in object*/
             $_POST[$rules[0] ? 'id_group' : 'id_customer'] = $rules[1];
         }
         /* Checking fields validity */
         $this->validateRules();
         if (!sizeof($this->_errors)) {
             $id = (int) Tools::getValue($this->identifier);
             /* Object update */
             if (isset($id) and !empty($id)) {
                 if ($this->tabAccess['edit'] === '1') {
                     $object = new $this->className($id);
                     if (Validate::isLoadedObject($object)) {
                         /* Specific to objects which must not be deleted */
                         if ($this->deleted and $this->beforeDelete($object)) {
                             $object->deleted = 1;
                             $object->update();
                             $objectNew = new $this->className();
                             $this->copyFromPost($objectNew, $this->table);
                             $result = $objectNew->add();
                             if (Validate::isLoadedObject($objectNew)) {
                                 $this->afterDelete($objectNew, $object->id);
                             }
                         } else {
                             if (($categories = Tools::getValue('categoryBox')) === false or !empty($categories) and !is_array($categories)) {
                                 $this->_errors[] = Tools::displayError('Please set a category for this voucher.');
                             }
                             $this->copyFromPost($object, $this->table);
                             if ($object->id_discount_type == 3) {
                                 $object->id_currency = 0;
                                 $object->value = 0;
                             } elseif ($object->id_discount_type == 1) {
                                 $object->id_currency = 0;
                             }
                             $result = $object->update(true, false, $categories);
                         }
                         if (!$result) {
                             $this->_errors[] = Tools::displayError('An error occurred while updating object.') . ' <b>' . $this->table . '</b>';
                         } elseif ($this->postImage($object->id)) {
                             if ($back = Tools::getValue('back')) {
                                 Tools::redirectAdmin(urldecode($back) . '&conf=4');
                             }
                             if (Tools::getValue('stay_here') == 'on' || Tools::getValue('stay_here') == 'true' || Tools::getValue('stay_here') == '1') {
                                 Tools::redirectAdmin($currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=4&updatescene&token=' . $token);
                             }
                             Tools::redirectAdmin($currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=4&token=' . $token);
                         }
                     } else {
                         $this->_errors[] = Tools::displayError('An error occurred while updating object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)');
                     }
                 } else {
                     $this->_errors[] = Tools::displayError('You do not have permission to edit here.');
                 }
             } else {
                 if ($this->tabAccess['add'] === '1') {
                     $object = new $this->className();
                     $this->copyFromPost($object, $this->table);
                     $categories = Tools::getValue('categoryBox', null);
                     if (!$object->add(true, false, $categories)) {
                         $this->_errors[] = Tools::displayError('An error occurred while creating object.') . ' <b>' . $this->table . '</b>';
                     } elseif ($_POST[$this->identifier] = $object->id and $this->postImage($object->id) and $this->_redirect) {
                         Tools::redirectAdmin($currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=3&token=' . $token);
                     }
                 } else {
                     $this->_errors[] = Tools::displayError('You do not have permission to add here.');
                 }
             }
         }
         $this->_errors = array_unique($this->_errors);
     } else {
         return parent::postProcess();
     }
 }
include_once dirname(__FILE__) . '/LoyaltyModule.php';
include_once dirname(__FILE__) . '/LoyaltyStateModule.php';
if (!$cookie->isLogged()) {
    Tools::redirect('authentication.php?back=modules/loyalty/loyalty-program.php');
}
// CSS ans JS file calls
$css_files = array(_PS_CSS_DIR_ . 'jquery.cluetip.css' => 'all');
$js_files = array(_PS_JS_DIR_ . 'jquery/jquery.dimensions.js', _PS_JS_DIR_ . 'jquery/jquery.cluetip.js');
$customerPoints = intval(LoyaltyModule::getPointsByCustomer(intval($cookie->id_customer)));
/* transform point into voucher if needed */
if (Tools::getValue('transform-points') == 'true' and $customerPoints > 0) {
    /* generate a voucher code */
    $voucherCode = null;
    do {
        $voucherCode = 'FID' . rand(1000, 100000);
    } while (Discount::discountExists($voucherCode));
    /* voucher creation and add to customer */
    $voucher = new Discount();
    $voucher->name = $voucherCode;
    $voucher->id_discount_type = 2;
    // Discount on order (amount)
    $voucher->id_customer = intval($cookie->id_customer);
    $voucher->id_currency = intval($cookie->id_currency);
    $voucher->value = LoyaltyModule::getVoucherValue($customerPoints);
    $voucher->quantity = 1;
    $voucher->quantity_per_user = 1;
    $voucher->cumulable = 1;
    $voucher->cumulable_reduction = 1;
    $dateFrom = time();
    if (Configuration::get('PS_ORDER_RETURN')) {
        $dateFrom = $dateFrom + 60 * 60 * 24 * intval(Configuration::get('PS_ORDER_RETURN_NB_DAYS'));
Esempio n. 3
0
 public function postProcess()
 {
     global $currentIndex, $cookie;
     $token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
     if ($discountName = Tools::getValue('name') and Validate::isDiscountName($discountName) and Discount::discountExists($discountName, Tools::getValue('id_discount'))) {
         $this->_errors[] = Tools::displayError('A voucher of this name already exists. Please choose another name.');
     }
     if (Tools::getValue('submitAdd' . $this->table)) {
         /* Checking fields validity */
         $this->validateRules();
         if (!sizeof($this->_errors)) {
             $id = intval(Tools::getValue($this->identifier));
             /* Object update */
             if (isset($id) and !empty($id)) {
                 if ($this->tabAccess['edit'] === '1') {
                     $object = new $this->className($id);
                     if (Validate::isLoadedObject($object)) {
                         /* Specific to objects which must not be deleted */
                         if ($this->deleted and $this->beforeDelete($object)) {
                             $object->deleted = 1;
                             $object->update();
                             $objectNew = new $this->className();
                             $this->copyFromPost($objectNew, $this->table);
                             $result = $objectNew->add();
                             if (Validate::isLoadedObject($objectNew)) {
                                 $this->afterDelete($objectNew, $object->id);
                             }
                         } else {
                             if (($categories = Tools::getValue('categoryBox')) === false or !empty($categories) and !is_array($categories)) {
                                 die(Tools::displayError());
                             }
                             $this->copyFromPost($object, $this->table);
                             $result = $object->update(true, false, $categories);
                         }
                         if (!$result) {
                             $this->_errors[] = Tools::displayError('an error occurred while updating object') . ' <b>' . $this->table . '</b>';
                         } elseif ($this->postImage($object->id)) {
                             if ($back = Tools::getValue('back')) {
                                 Tools::redirectAdmin(urldecode($back) . '&conf=4');
                             }
                             if (Tools::getValue('stay_here') == 'on' || Tools::getValue('stay_here') == 'true' || Tools::getValue('stay_here') == '1') {
                                 Tools::redirectAdmin($currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=4&updatescene&token=' . $token);
                             }
                             Tools::redirectAdmin($currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=4&token=' . $token);
                         }
                     } else {
                         $this->_errors[] = Tools::displayError('an error occurred while updating object') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)');
                     }
                 } else {
                     $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.');
                 }
             } else {
                 if ($this->tabAccess['add'] === '1') {
                     $object = new $this->className();
                     $this->copyFromPost($object, $this->table);
                     $categories = Tools::getValue('categoryBox', null);
                     if (!$object->add(true, false, $categories)) {
                         $this->_errors[] = Tools::displayError('an error occurred while creating object') . ' <b>' . $this->table . '</b>';
                     } elseif ($_POST[$this->identifier] = $object->id and $this->postImage($object->id) and $this->_redirect) {
                         Tools::redirectAdmin($currentIndex . '&' . $this->identifier . '=' . $object->id . '&conf=3&token=' . $token);
                     }
                 } else {
                     $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
                 }
             }
         }
         $this->_errors = array_unique($this->_errors);
     } else {
         return parent::postProcess();
     }
 }