* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7556 $ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ class AdminDiscounts extends AdminTab { public function __construct() { global $cookie; $this->table = 'discount'; $this->className = 'Discount'; $this->lang = true; $this->edit = true; $this->delete = true; $this->_select = 'dtl.`name` AS discount_type, IF(a.id_discount_type = 1, CONCAT(a.value, " %"), IF(a.id_discount_type = 2, CONCAT(a.value, " ", c.sign), "--")) as strvalue'; $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'currency` c ON (c.`id_currency` = a.`id_currency`) LEFT JOIN `'._DB_PREFIX_.'discount_type` dt ON (dt.`id_discount_type` = a.`id_discount_type`) LEFT JOIN `'._DB_PREFIX_.'discount_type_lang` dtl ON (dt.`id_discount_type` = dtl.`id_discount_type` AND dtl.`id_lang` = '.(int)($cookie->id_lang).')'; $typesArray = array(); $types = Discount::getDiscountTypes((int)($cookie->id_lang)); foreach ($types AS $type) $typesArray[$type['id_discount_type']] = $type['name']; $this->fieldsDisplay = array( 'id_discount' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25), 'name' => array('title' => $this->l('Code'), 'width' => 85, 'prefix' => '', 'suffix' => '', 'filter_key' => 'a!name'), 'description' => array('title' => $this->l('Description'), 'width' => 100, 'filter_key' => 'b!description'), 'discount_type' => array('title' => $this->l('Type'), 'type' => 'select', 'select' => $typesArray, 'filter_key' => 'dt!id_discount_type'), 'strvalue' => array('title' => $this->l('Value'), 'width' => 50, 'align' => 'right', 'filter_key' => 'a!value'), 'quantity' => array('title' => $this->l('Qty'), 'width' => 40, 'align' => 'right'), 'date_to' => array('title' => $this->l('To'), 'width' => 60, 'type' => 'date', 'align' => 'right'), 'active' => array('title' => $this->l('Status'), 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false)); $this->optionTitle = $this->l('Discounts options'); $this->_fieldsOptions = array( 'PS_VOUCHERS' => array('title' => $this->l('Enable vouchers:'), 'desc' => $this->l('Allow the use of vouchers in shop'), 'cast' => 'intval', 'type' => 'bool'), ); parent::__construct(); } protected function copyFromPost(&$object, $table) { parent::copyFromPost($object, $table); $object->cumulable = (!isset($_POST['cumulable']) ? false : true); $object->cumulable_reduction = (!isset($_POST['cumulable_reduction']) ? false : true); } 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); $result = $object->update(true, false, $categories); } if (!$result) $this->_errors[] = Tools::displayError('An error occurred while updating object.').' '.$this->table.''; 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.').' '.$this->table.' '.Tools::displayError('(cannot load object)'); } else $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); } /* Object creation */ 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.').' '.$this->table.''; elseif (($_POST[$this->identifier] = $object->id /* voluntary */) 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(); } public function displayForm($isMainTab = true) { global $currentIndex, $cookie; parent::displayForm(); if (!($obj = $this->loadObject(true))) return; echo '
'.($obj->id ? '' : '').'
'.$this->l('Vouchers').'
* '.$this->l('Invalid characters: numbers and').' !<>,;?=+()@#"�{}_$%: 

'.$this->l('The voucher\'s code, at least 3 characters long, which the customer types in during check-out').'

*
'; foreach ($this->_languages as $language) echo '
* '.$this->l('Invalid characters:').' <>;=#{} 

'.$this->l('Will appear in cart next to voucher code').'

'; $this->displayFlags($this->_languages, $this->_defaultFormLanguage, 'description', 'description'); echo '
'; $done = array(); $index = array(); $indexedCategories = isset($_POST['categoryBox']) ? $_POST['categoryBox'] : ($obj->id ? Discount::getCategories($obj->id) : array()); $categories = Category::getCategories((int)($cookie->id_lang), false); foreach ($indexedCategories AS $k => $row) $index[] = $row['id_category']; $this->recurseCategoryForInclude((int)(Tools::getValue($this->identifier)), $index, $categories, $categories[0][1], 1, $obj->id); echo '
'.$this->l('ID').' '.$this->l('Name').'

'.$this->l('Mark all checkbox(es) of categories to which the discount is to be applied').' *

*

'.$this->l('Total quantity available (mainly for vouchers open to everyone)').'

*

'.$this->l('Number of times a single customer can use this voucher').'

*

'.$this->l('0 if not applicable').'

getFieldValue($obj, 'cumulable') == 1) ? ' checked="checked"' : '').' id="cumulable_on" value="1" />

getFieldValue($obj, 'cumulable_reduction') == 1) ? ' checked="checked"' : '').' id="cumulable_reduction_on" value="1" />


'.$this->l('Filter:').'

'; includeDatepicker(array('date_from', 'date_to'), true); echo '
*

'.$this->l('Start date/time from which voucher can be used').'
'.$this->l('Format: YYYY-MM-DD HH:MM:SS').'

*

'.$this->l('End date/time at which voucher is no longer valid').'
'.$this->l('Format: YYYY-MM-DD HH:MM:SS').'

getFieldValue($obj, 'cart_display') ? 'checked="checked" ' : '').'/> getFieldValue($obj, 'cart_display') ? 'checked="checked" ' : '').'/>
getFieldValue($obj, 'active') ? 'checked="checked" ' : '').'/> getFieldValue($obj, 'active') ? 'checked="checked" ' : '').'/>

'.$this->l('Enable or disable voucher').'

* '.$this->l('Required field').'
'; } /** * Build a categories tree * * @param array $indexedCategories Array with categories where product is indexed (in order to check checkbox) * @param array $categories Categories to list * @param array $current Current category * @param integer $id_category Current category id */ public static function recurseCategoryForInclude($id_obj, $indexedCategories, $categories, $current, $id_category = 1, $id_category_default = NULL, $has_suite = array()) { global $done; static $irow; if (!isset($done[$current['infos']['id_parent']])) $done[$current['infos']['id_parent']] = 0; $done[$current['infos']['id_parent']] += 1; $todo = sizeof($categories[$current['infos']['id_parent']]); $doneC = $done[$current['infos']['id_parent']]; $level = $current['infos']['level_depth'] + 1; echo ' '.$id_category.' '; for ($i = 2; $i < $level; $i++) echo ''; echo '   '; if ($level > 1) $has_suite[] = ($todo == $doneC ? 0 : 1); if (isset($categories[$id_category])) foreach ($categories[$id_category] AS $key => $row) if ($key != 'infos') self::recurseCategoryForInclude($id_obj, $indexedCategories, $categories, $categories[$id_category][$key], $key, $id_category_default, $has_suite); } }