id_lang)); $isoTinyMCE = (file_exists(_PS_ROOT_DIR_.'/js/tiny_mce/langs/'.$iso.'.js') ? $iso : 'en'); $ad = dirname($_SERVER["PHP_SELF"]); $divLangName = 'mail_object¤mail'; parent::displayForm(); echo '
'. (isset($this->product_voucher['id_product_voucher']) ? $this->l('Edit Product Voucher') : $this->l('Add Product Voucher') ) .''; echo '

'. $this->l('Insert id product').'

'; if(!isset($this->product_voucher['id_product_voucher'])){ echo '

'. $this->l('Format: 1 column / 1 voucher by line').'

'; } echo '

'. $this->l('0 to disable').'

'; echo '
'; foreach($this->_languages as $language) { echo '
'; echo '
'; } $this->displayFlags($this->_languages, $this->_defaultFormLanguage, $divLangName, 'mail_object'); echo '
'; echo '
'; foreach($this->_languages as $language) { echo '
'; echo '
'; } $this->displayFlags($this->_languages, $this->_defaultFormLanguage, $divLangName, 'mail'); echo '

'. $this->l('These variables are replaced before sending a email').' '. $this->l('%bon% is required') .'

  • '. $this->l('%bon% by voucher') .'
  • '. $this->l('%prenom% by firstname') .'
  • '. $this->l('%nom% by lastname') .'
product_voucher) ? ( $this->product_voucher['active'] == 1 ? "checked" : '') : 'checked').' value="1" id="active_on" name="active">
product_voucher) ? ( $this->product_voucher['active'] == 0 ? "checked" : '') : '').' value="0" id="active_off" name="active">
'; echo '
'; if(isset($this->product_voucher['id_product_voucher'])) { echo ''; echo ''; } else { echo ''; } echo '
'; // TinyMCE echo ' '; } public function displayFormAddVoucher() { global $currentIndex; if(!empty($this->product_voucher)) { echo '
'. $this->l('Import Vouchers') .''; echo '

'. $this->l('Import vouchers in') .' : ' . $this->product_voucher['name'].'

'. $this->l('Format: 1 column / 1 voucher by line').'


'; } else { echo '
'. $this->l('This id doesnt exists').'
'; } } public function displayDetails() { global $currentIndex; $product_voucher = ProductVouchers::getProductVoucher((int)Tools::getValue('id'), false); $total_product_voucher_items = ProductVouchers::getProductVoucherItems($product_voucher['id_product_voucher'], true, 0); echo '
Statistique '. $product_voucher['name'] .''; echo '
'. $this->l('Total vouchers pending') .' ' . ProductVouchers::getProductVoucherItems($product_voucher['id_product_voucher'], true, ProductVouchers::AVAILABLE) .'
'. $this->l('Total vouchers buy') .' ' . ProductVouchers::getProductVoucherItems($product_voucher['id_product_voucher'], true, ProductVouchers::ACTIVE) .'
'. $this->l('Total vouchers refund') .' ' . ProductVouchers::getProductVoucherItems($product_voucher['id_product_voucher'], true, ProductVouchers::REFUND) .'
'. $this->l('Total vouchers') .' ' . $total_product_voucher_items .'
'; echo '
'; echo '


'; } public function getState($statut) { if($statut == ProductVouchers::AVAILABLE) { return $this->l('Available'); } else if($statut == ProductVouchers::ACTIVE) { return $this->l('Active'); } else if($statut == ProductVouchers::REFUND) { return $this->l('Refund'); } else if($statut == ProductVouchers::INACTIVE) { return $this->l('Inactive'); } } public function display() { global $cookie, $currentIndex; if($id = (int)Tools::getValue('id')) { $this->product_voucher = ProductVouchers::getProductVoucher($id, true); } if(Tools::getValue('addVoucher')) { $this->displayFormAddVoucher(); } else if(Tools::getValue('showVoucher')) { if(isset($this->product_voucher['id_product_voucher'])) { $this->displayDetails(); } } else if(Tools::getValue('delete')) { if(isset($this->product_voucher['id_product_voucher'])) { if(ProductVouchers::deleteProductVoucher($this->product_voucher['id_product_voucher'])) { return Tools::redirectAdmin($currentIndex.'&token='.Tools::getAdminTokenLite('AdminProductVouchers')); } } } else { $this->displayForm(); } echo ' '; echo '
'.$this->l('Product Vouchers').' '; foreach(Db::getInstance()->ExecuteS(' SELECT * FROM `'._DB_PREFIX_.'product_voucher` ORDER BY `id_product_voucher` DESC ') as $product_voucher) { $nb_vouchers = ProductVouchers::getProductVoucherItems($product_voucher['id_product_voucher'], true, 0); $product = new Product($product_voucher['product_id'], false, $cookie->id_lang); $id_category = $product->id_category_default; $sale = Sale::getSaleFromCategory($id_category); echo ''; } echo '
'.$this->l('ID').' '.$this->l('Name').' '.$this->l('Product').' '.$this->l('Sale').' '.$this->l('NB Vouchers').' '.$this->l('Date add').' '.$this->l('Active').' '.$this->l('Actions').'
'.$product_voucher['id_product_voucher'].' '.$product_voucher['name'].' '.$product->name.' '.$sale->title[$cookie->id_lang].' '.$nb_vouchers.' '.$product_voucher['date_add'].' '. ($product_voucher['active'] == 1 ? $this->l('Yes') : $this->l('No') ) .' '. ' '. $this->l('Add Vouchers') .' '. $this->l('Add Vouchers') .' '. $this->l('Edit Voucher') .' '. $this->l('Delete Vouchers') .'
'; } public function postProcess() { if(Tools::isSubmit('submitProductVouchersAdd')) { $product_id = Tools::getValue('id_product'); $name = Tools::getValue('name'); $active = Tools::getValue('active'); $import = $_FILES['file']; $max_vouchers = Tools::getValue('max_vouchers'); $max_vouchers = $max_vouchers>0?$max_vouchers:0; $mail_object = Tools::getValue('mail_object'); $mail = Tools::getValue('mail'); if(Validate::isLoadedObject($product = new Product((int)$product_id) )) { $id_product_voucher = ProductVouchers::createProductVoucher($name, $product_id, $active, $mail_object, $mail, $max_vouchers); if($id_product_voucher){ $errors = array(); $nb_import = 0; if (($handle = fopen($import['tmp_name'], "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $import_voucher = ProductVouchers::associateVouchers($id_product_voucher, $data[0]); if($import_voucher){ $nb_import++; }else{ $errors[] = $data[0]; } } fclose($handle); } }else{ die('This voucher cannot be create'); } echo '
'. $nb_import . $this->l('Import successful') .'
'; if(count($errors) > 0){ p($errors); } } } else if(Tools::isSubmit('submitProductVouchersEdit')) { $id_product_voucher = (int)Tools::getValue('id_product_voucher'); $product_id = (int)Tools::getValue('id_product'); $name = Tools::getValue('name'); $active = (int)Tools::getValue('active'); $mail_object = Tools::getValue('mail_object'); $mail = Tools::getValue('mail'); $max_vouchers = Tools::getValue('max_vouchers'); $max_vouchers = $max_vouchers>0?$max_vouchers:0; $id_product_voucher = ProductVouchers::editProductVoucher($id_product_voucher, $name, $product_id, $active, $mail_object, $mail, $max_vouchers); } else if(Tools::isSubmit('submitProductVouchersImport')) { $id_product_voucher = Tools::getValue('id_product_voucher'); $import = $_FILES['file']; $errors = array(); $nb_import = 0; if (($handle = fopen($import['tmp_name'], "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $import_voucher = ProductVouchers::associateVouchers($id_product_voucher, $data[0]); if($import_voucher){ $nb_import++; }else{ $errors[] = $data[0]; } } fclose($handle); } echo '
'. $nb_import . $this->l('Import successful') . '
'; if(count($errors) > 0) { p($errors); } } else if(Tools::isSubmit('submitProductVouchersExport')) { $id_product_voucher = Tools::getValue('id_product_voucher'); if(Tools::getValue('statut') == 0) { $statut = "all"; } else { $statut = Tools::getValue('statut'); } $vouchers = ProductVouchers::getExportProduct($id_product_voucher, $statut); if(!empty($vouchers)) { $delete_file = Configuration::get('export_vouchers'); unlink(_PS_ROOT_DIR_.'/modules/product_vouchers/'.$delete_file.'.csv'); $filename = Tools::passwdGen(10); Configuration::updateValue('export_vouchers', $filename); $export = fopen(_PS_ROOT_DIR_.'/modules/product_vouchers/'.$filename.'.csv', 'w'); $fields = array( 'Code', 'Statut', 'Numéro commande', 'Numéro client', 'Email', 'Date de la commande', ); fputcsv($export, $fields, ';'); foreach ($vouchers as $voucher) { $fields = array( $voucher['code'], $this->getState($voucher['statut']), $voucher['id_order'], $voucher['id_customer'], $voucher['email'], $voucher['date_add'] ); fputcsv($export, $fields, ';'); } fclose($export); echo '
'. $this->l('Export completed') . ' '. $this->l('Download'). '
'; } else { echo '
'. $this->l('No vouchers with this statut') .'
'; } } } }