bebeboutik/modules/product_vouchers/AdminProductVouchers.php

469 lines
19 KiB
PHP
Raw Normal View History

2016-01-04 12:49:26 +01:00
<?php
require_once(_PS_ROOT_DIR_.'/modules/product_vouchers/productvouchers.php');
require_once(_PS_ROOT_DIR_.'/modules/privatesales/Sale.php');
class AdminProductVouchers extends AdminTab {
public function displayForm() {
global $currentIndex, $cookie;
$iso = Language::getIsoById((int)($cookie->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 ' <form action="'.$currentIndex.'&token='.Tools::getAdminTokenLite('AdminProductVouchers').'" method="post" enctype="multipart/form-data">
<fieldset>
<legend>'. (isset($this->product_voucher['id_product_voucher']) ? $this->l('Edit Product Voucher') : $this->l('Add Product Voucher') ) .'</legend>';
echo '<label>'.$this->l('Name:').'</label>
<div class="margin-form">
<div id="title" style="float: left;">
<input type="text" style="width: 500px;" name="name" value="'. (isset($this->product_voucher['name']) ? $this->product_voucher['name'] : '' ).'" />
</div>
<div class="clear"></div>
</div>
<label>'.$this->l('Id Product:').'</label>
<div class="margin-form">
<div id="title" style="float: left;">
<input type="text" style="width: 500px;" name="id_product" value="'. (isset($this->product_voucher['product_id']) ? $this->product_voucher['product_id'] : '' ).'" />
<p class="info">'. $this->l('Insert id product').'</p>
</div>
<div class="clear"></div>
</div>';
if(!isset($this->product_voucher['id_product_voucher'])){
echo '<label>'.$this->l('CSV File:').'</label>
<div class="margin-form">
<div id="title" style="float: left;">
<input type="file" style="width: 500px;" name="file" />
<p class="info">'. $this->l('Format: 1 column / 1 voucher by line').'</p>
</div>
<div class="clear"></div>
</div>';
}
echo '<label>'.$this->l('Max vouchers by member').'</label>
<div class="margin-form">
<div id="title" style="float: left;">
<input type="number" style="width: 500px;" name="max_vouchers" value="'. (isset($this->product_voucher['max_vouchers']) ? $this->product_voucher['max_vouchers'] : '' ).'" min="0" step="1"/>
<p class="info">'. $this->l('0 to disable').'</p>
</div>
<div class="clear"></div></div>';
echo '<label>'.$this->l('Mail Object:').'</label>
<div class="margin-form">';
foreach($this->_languages as $language) {
echo '<div id="mail_object_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $this->_defaultFormLanguage ? 'block' : 'none').'; float: left;">
<input type="text" style="width: 500px;" name="mail_object['.$language['id_lang'].']" value="'. (isset($this->product_voucher['mail_object'][$language['id_lang']]) ? $this->product_voucher['mail_object'][$language['id_lang']] : '').'" />';
echo '</div>';
}
$this->displayFlags($this->_languages, $this->_defaultFormLanguage, $divLangName, 'mail_object');
echo '<div class="clear"></div>
</div>';
echo '<label>'.$this->l('Mail:').'</label>
<div class="margin-form">';
foreach($this->_languages as $language) {
echo '<div id="mail_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $this->_defaultFormLanguage ? 'block' : 'none').'; float: left;">
<textarea name="mail['.$language['id_lang'].']" class="rte" cols="30" rows="10">'.(isset($this->product_voucher['mail'][$language['id_lang']]) ? $this->product_voucher['mail'][$language['id_lang']] : '').'</textarea>';
echo '</div>';
}
$this->displayFlags($this->_languages, $this->_defaultFormLanguage, $divLangName, 'mail');
echo '
<div class="clear"></div>
<p class="info">'. $this->l('These variables are replaced before sending a email').'
<span style="color:red">'. $this->l('%bon% is required') .'</span></p>
<ul>
<li>'. $this->l('%bon% by voucher') .'</li>
<li>'. $this->l('%prenom% by firstname') .'</li>
<li>'. $this->l('%nom% by lastname') .'</li>
</ul>
</div>
<label>'.$this->l('Active:').'</label>
<div class="margin-form">
<div id="title" style="float: left;">
<input type="radio" '.(isset($this->product_voucher) ? ( $this->product_voucher['active'] == 1 ? "checked" : '') : 'checked').' value="1" id="active_on" name="active">
<label class="t" style="float:none" for="active_on"><img title="'. $this->l('Yes') .'" alt="'. $this->l('Yes') .'" src="../img/admin/enabled.gif">'. $this->l('Yes') .'</label>
<br />
<input type="radio" '.(isset($this->product_voucher) ? ( $this->product_voucher['active'] == 0 ? "checked" : '') : '').' value="0" id="active_off" name="active">
<label class="t" for="active_off"><img title="'. $this->l('No') .'" alt="'. $this->l('No') .'" src="../img/admin/disabled.gif">'. $this->l('No') .'</label>
</div>
<div class="clear"></div>
</div>';
echo '<br class="clear" />
<div class="margin-form">';
if(isset($this->product_voucher['id_product_voucher'])) {
echo '<input type="hidden" name="id_product_voucher" value="'.$this->product_voucher['id_product_voucher'].'">';
echo '<input type="submit" class="button" name="submitProductVouchersEdit" value="'. $this->l('Edit this product vouchers').'" />';
} else {
echo '<input type="submit" class="button" name="submitProductVouchersAdd" value="'. $this->l('Add this product vouchers').'" />';
}
echo '</div>
</fieldset>
</form>';
// TinyMCE
echo '
<script type="text/javascript">
var iso = \''.$isoTinyMCE.'\' ;
var pathCSS = \''._THEME_CSS_DIR_.'\' ;
var ad = \''.$ad.'\' ;
</script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tinymce.inc.js"></script>
';
}
public function displayFormAddVoucher() {
global $currentIndex;
if(!empty($this->product_voucher)) {
echo '<form action="'.$currentIndex.'&token='.Tools::getAdminTokenLite('AdminProductVouchers').'" method="post" enctype="multipart/form-data">
<fieldset>
<legend>'. $this->l('Import Vouchers') .'</legend>';
echo '<h3>'. $this->l('Import vouchers in') .' : ' . $this->product_voucher['name'].'</h3>
<label>'.$this->l('CSV File:').'</label>
<div class="margin-form">
<div id="title" style="float: left;">
<input type="file" name="file" />
<p class="info">'. $this->l('Format: 1 column / 1 voucher by line').'</p>
</div>
<div class="clear"></div>
</div>
<br class="clear" />
<div class="margin-form">
<input type="hidden" name="id_product_voucher" value="'.$this->product_voucher['id_product_voucher'].'">
<input type="submit" class="button" name="submitProductVouchersImport" value="'.$this->l('Import').'" />
</div>
</fieldset>
</form>';
} else {
echo '<div class="error">'. $this->l('This id doesnt exists').'</div>';
}
}
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 '<fieldset class="space">
<legend>Statistique '. $product_voucher['name'] .'</legend>';
echo '<table>
<tr>
<td>'. $this->l('Total vouchers pending') .'</td>
<td>' . ProductVouchers::getProductVoucherItems($product_voucher['id_product_voucher'], true, ProductVouchers::AVAILABLE) .'</td>
</tr>
<tr>
<td>'. $this->l('Total vouchers buy') .'</td>
<td>' . ProductVouchers::getProductVoucherItems($product_voucher['id_product_voucher'], true, ProductVouchers::ACTIVE) .'</td>
</tr>
<tr>
<td>'. $this->l('Total vouchers refund') .'</td>
<td>' . ProductVouchers::getProductVoucherItems($product_voucher['id_product_voucher'], true, ProductVouchers::REFUND) .'</td>
</tr>
<tr>
<td><b>'. $this->l('Total vouchers') .'</b></td>
<td>' . $total_product_voucher_items .'</td>
</tr>
</table>';
echo '<form action="'.$currentIndex.'&token='.Tools::getAdminTokenLite('AdminProductVouchers').'&showVoucher&id='. (int)Tools::getValue('id').'" method="post" enctype="multipart/form-data">';
echo '<hr> <br />
<label>'.$this->l('Export statut').'</label>
<div class="margin-form">
<select name="statut" id="statut_export">
<option value="0">'. $this->l('All vouchers item') .'</option>
<option value="'. ProductVouchers::AVAILABLE.'">'. $this->getState(ProductVouchers::AVAILABLE) .'</option>
<option value="'. ProductVouchers::ACTIVE.'">'. $this->getState(ProductVouchers::ACTIVE) .'</option>
<option value="'. ProductVouchers::REFUND.'">'. $this->getState(ProductVouchers::REFUND) .'</option>
<option value="'. ProductVouchers::INACTIVE.'">'. $this->getState(ProductVouchers::INACTIVE) .'</option>
</select>
<div class="clear"></div>
</div>
<br class="clear" />
<div class="margin-form">
<input type="hidden" name="id_product_voucher" value="'.$product_voucher['id_product_voucher'].'">
<input type="submit" class="button" name="submitProductVouchersExport" value="'.$this->l('Export').'" />
</div>
</form>
</fieldset>';
}
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 '
<script type="text/javascript">
i18n_delete = "'.$this->l('Are you sure you want to delete this vouchers ?').'";
current_location = "'.$currentIndex.'&token='.Tools::getAdminTokenLite('AdminProductVouchers').'";
function itemDeletion(linkId) {
var ok = confirm(i18n_delete)
if(ok == true) {
document.location.href = current_location + "&id=" + linkId + "&delete=1";
}else{
return false;
}
}
</script>';
echo '<fieldset class="space">
<legend>'.$this->l('Product Vouchers').'</legend>
<table id="vouchers" class="table" style="width: 100%; clear:both; margin-top: 15px; float:left;">
<thead>
<tr>
<th style="width: 55px;">'.$this->l('ID').'</th>
<th>'.$this->l('Name').'</th>
<th>'.$this->l('Product').'</th>
<th>'.$this->l('Sale').'</th>
<th>'.$this->l('NB Vouchers').'</th>
<th>'.$this->l('Date add').'</th>
<th style="width: 40px;">'.$this->l('Active').'</th>
<th style="width: 100px;">'.$this->l('Actions').'</th>
</tr>
</thead>
<tbody>';
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 '<tr>
<td>'.$product_voucher['id_product_voucher'].'</td>
<td>'.$product_voucher['name'].'</td>
<td>'.$product->name.'</td>
<td>'.$sale->title[$cookie->id_lang].'</td>
<td>'.$nb_vouchers.'</td>
<td>'.$product_voucher['date_add'].'</td>
<td>'. ($product_voucher['active'] == 1 ? $this->l('Yes') : $this->l('No') ) .'</td>
<td>'.
'<a href="'.$currentIndex.'&token='.Tools::getAdminTokenLite('AdminProductVouchers').'&addVoucher=1&id='.$product_voucher['id_product_voucher'].'">
<img src="'.__PS_BASE_URI__.'img/admin/add.gif" alt="'. $this->l('Add Vouchers') .'" title="'. $this->l('Add Vouchers') .'" />
</a>
<a href="'.$currentIndex.'&token='.Tools::getAdminTokenLite('AdminProductVouchers').'&showVoucher=1&id='.$product_voucher['id_product_voucher'].'">
<img src="'.__PS_BASE_URI__.'img/admin/details.gif" alt="'. $this->l('Add Vouchers') .'" title="'. $this->l('Show détails') .'" />
</a>
<a href="'.$currentIndex.'&token='.Tools::getAdminTokenLite('AdminProductVouchers').'&editVoucher=1&id='.$product_voucher['id_product_voucher'].'">
<img src="'.__PS_BASE_URI__.'img/admin/edit.gif" alt="'. $this->l('Edit Voucher') .'" title="'. $this->l('Edit Voucher') .'" />
</a>
<img style="cursor:pointer" onclick="return itemDeletion('.$product_voucher['id_product_voucher'].')" src="'.__PS_BASE_URI__.'img/admin/delete.gif" alt="'. $this->l('Delete Vouchers') .'" title="'. $this->l('Delete voucher') .'" />
</td>
</tr>';
}
echo '</tbody>
</table>
</fieldset>
<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/product_vouchers/js/jquery.js"></script>
<script type="text/javascript" src="'.__PS_BASE_URI__.'modules/product_vouchers/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
$("#vouchers").dataTable({
paging : false,
language: {
processing: "Traitement en cours...",
search: "Rechercher&nbsp;:",
lengthMenu: "Afficher _MENU_ &eacute;l&eacute;ments",
info: "Affichage de l\'&eacute;lement _START_ &agrave; _END_ sur _TOTAL_ &eacute;l&eacute;ments",
infoEmpty: "Affichage de l\'&eacute;lement 0 &agrave; 0 sur 0 &eacute;l&eacute;ments",
infoFiltered: "(filtr&eacute; de _MAX_ &eacute;l&eacute;ments au total)",
infoPostFix: "",
loadingRecords: "Chargement en cours...",
zeroRecords: "Aucun &eacute;l&eacute;ment &agrave; afficher",
emptyTable: "Aucune donnée disponible dans le tableau",
paginate: {
first: "Premier",
previous: "Pr&eacute;c&eacute;dent",
next: "Suivant",
last: "Dernier"
},
aria: {
sortAscending: ": activer pour trier la colonne par ordre croissant",
sortDescending: ": activer pour trier la colonne par ordre décroissant"
}
}
});
});
</script>
<style>
#vouchers_filter label{
width: 350px;
text-align: left;
}
#vouchers_filter input{ margin-left: 10px}
#vouchers_info{
clear: both;
color: #7f7f7f;
float: right;
font-size: 12px;
position: relative;
top: 10px;
}
</style>';
}
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 '<div class="conf">'. $nb_import . $this->l('Import successful') .'</div>';
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 '<div class="conf">'. $nb_import . $this->l('Import successful') . '</div>';
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 '<div class="conf"><img alt="" src="../img/admin/ok2.png">'. $this->l('Export completed') . ' <a href="'._MODULE_DIR_.'product_vouchers/'.$filename.'.csv" onclick="window.open(this.href); return false;">'. $this->l('Download'). '</a>
</div>';
} else {
echo '<div class="error">'. $this->l('No vouchers with this statut') .'</div>';
}
}
}
}