0) { $fp = fopen('../modules/generate_vouchers/export.csv', 'w'); fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF)); $header = array( 'ID', 'Name', 'Disponibilité', 'Date d\'utilisation' ); fputcsv($fp, $header, ';'); foreach ($vouchers as $voucher) { $fields = array( $voucher['id_discount'], $voucher['name'], $voucher['availibility'], $voucher['date_add'] ); fputcsv($fp, $fields, ';'); } fclose($fp); $this->_html = '

' .$this->l('Export completed') .'
'. $this->l('Export') .'

'; } } else { $this->_html = '

' .$this->l('This id doesnt exist') .'

'; } } } else if(Tools::isSubmit('submitGenerate')) { if(!empty(Tools::getValue('prefix')) && !empty(Tools::getValue('number')) && !empty(Tools::getValue('title')) ) { // generate vouchers $vouchers = array(); $number = (int)Tools::getValue('number'); $prefix = Tools::getValue('prefix'); $title = Tools::getValue('title'); // add Generation $id_generate_voucher = self::addVoucherGenerate($title, $prefix); if($id_generate_voucher != false) { $params = array( 'value' => (int)Tools::getValue('value'), 'id_currency' => (int)Tools::getValue('id_currency'), 'id_discount_type' => (int)Tools::getValue('id_discount_type'), 'behavior_not_exhausted' => (int)Tools::getValue('behavior_not_exhausted'), 'cumulable' => (int)Tools::getValue('cumulable'), 'cumulable_reduction' => (int)Tools::getValue('cumulable_reduction'), 'minimal' => (int)Tools::getValue('minimal'), 'include_tax' => (int)Tools::getValue('include_tax'), 'quantity' => (int)Tools::getValue('quantity'), 'quantity_per_user' => (int)Tools::getValue('quantity_per_user'), 'date_from' => Tools::getValue('date_from'), 'date_to' => Tools::getValue('date_to'), 'prefix' => Tools::getValue('prefix'), 'description' => Tools::getValue('description_2') ); for ($i=0; $i < $number; $i++) { $discount = $this->_register_discount($params); if(isset($discount->id)) { $vouchers[] = $discount->id; }else{ $this->_html = '

' .$this->l('Error') .'

'; } } $sql = 'UPDATE `'._DB_PREFIX_.'generate_vouchers` SET vouchers = "' . json_encode($vouchers) .'" WHERE id_generate = "' . $id_generate_voucher .'"'; if(Db::getInstance()->Execute($sql)) { $this->_html = '

' .$this->l('Generation Ok') . '

'; } else { $this->_html = '

' .$this->l('Error before generation') .'

'; } } else { $this->_html = '

' .$this->l('Error before generation') .'

'; } } else { $this->_html = '

' .$this->l('Please, verify information ( required fields)') . '

'; } } } public function display() { global $currentIndex, $cookie; $this->_html .= '

' . $this->l('List generate') . '

'; $vouchers = self::getGenerateVouchers(); foreach ($vouchers as $key => $voucher) { $this->_html .= ' '; } $this->_html .= '
' . $this->l('Id') . ' ' . $this->l('Name') . ' ' . $this->l('Prefix') . ' ' . $this->l('Actions') . '
'. $voucher['id_generate'] .' '. $voucher['name'] .' '. $voucher['prefix'] .' ' . $this->l('Export') . '

'; $this->_html .= '

'. $this->l('Generate Vouchers') .'

'. $this->l('Launch Generation') .''; $this->_html .= '

'. $this->l('Prefix : For example : AAAA for generate vouchers like AAAA-BBBBBBB') .'


*
'; $languages = Language::getLanguages(TRUE); $divLangName = 'description'; $defaultFormLanguage = (int) Configuration::get('PS_LANG_DEFAULT'); foreach ($languages as $language) $this->_html .= '
* '.$this->l('Invalid characters:').' <>;=#{} 

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

'; $this->displayFlags($languages, $defaultFormLanguage, 'description', 'description'); $this->_html .= '
*

'.$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').'

*

'.$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').'


* '.$this->l('Required field').'
'; /* Javascript for display/hide value of voucher*/ $this->_html .= ' '; echo $this->_html; } public static function addVoucherGenerate($title, $prefix) { $sql = 'INSERT INTO `'._DB_PREFIX_.'generate_vouchers` VALUES ("", "'. date('Y-m-d h:i:s') .'", "'. $title .'", "'. $prefix .'", "" )'; if(Db::getInstance()->Execute($sql)) { return Db::getInstance()->Insert_ID(); } else { return false; } } public static function getGenerateVoucher($id_generate) { return Db::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.'generate_vouchers` WHERE id_generate = ' . (int)$id_generate); } public static function getGenerateVouchers() { return Db::getInstance()->executes('SELECT * FROM `'._DB_PREFIX_.'generate_vouchers`'); } public static function getVouchers($vouchers) { $list = json_decode($vouchers); return Db::getInstance()->Executes('SELECT d.`id_discount`, d.`name`, IF( EXISTS(SELECT dc.id_discount FROM `'._DB_PREFIX_.'cart_discount` dc WHERE dc.id_discount = d.id_discount LIMIT 1 ) ,1, 0) as availibility, o.date_add FROM `'._DB_PREFIX_.'discount` d LEFT JOIN `'._DB_PREFIX_.'order_discount` od ON d.`id_discount` = od.`id_discount` LEFT JOIN `'._DB_PREFIX_.'orders` o ON od.`id_order` = o.`id_order` WHERE d.`id_discount` IN (' . implode(',', $list). ')'); } private function _register_discount($params) { $discount = new Discount(); $discount->id_customer = 0; $discount->id_discount_type = $params['id_discount_type']; $discount->behavior_not_exhausted = $params['behavior_not_exhausted']; $discount->cumulable = $params['cumulable']; $discount->cumulable_reduction = $params['cumulable_reduction']; $discount->minimal = $params['minimal']; $discount->include_tax = $params['include_tax']; $discount->cart_display = 0; $discount->active = 1; $discount->quantity = $params['quantity']; $discount->quantity_per_user = $params['quantity_per_user']; $discount->date_from = $params['date_from']; $discount->date_to = $params['date_to']; $name = $params['prefix'] .'-'. Tools::passwdGen(8); if(Discount::discountExists($name)) { $name = $params['prefix'] .'-'. Tools::passwdGen(8); } $discount->name = $name; $languages = Language::getLanguages(TRUE); foreach ($languages as $language) { $discount->description[$language['id_lang']] = $params['description']; } $discount->id_currency = $params['id_currency']; $discount->value = $params['value']; if($discount->add(true, false, array(1)) ) { return $discount; } else { return false; } } }