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 '
';
// TinyMCE
echo '
';
}
public function displayFormAddVoucher() {
global $currentIndex;
if(!empty($this->product_voucher)) {
echo '';
} 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 '';
}
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 '
';
}
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 '';
} else {
echo ''. $this->l('No vouchers with this statut') .'
';
}
}
}
}