370 lines
14 KiB
PHP
370 lines
14 KiB
PHP
|
<?php
|
||
|
if(!defined('_PS_VERSION_')) {
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
class AdminGenerateVouchers extends AdminTab {
|
||
|
|
||
|
public $_html = '';
|
||
|
|
||
|
public function postProcess() {
|
||
|
global $cookie;
|
||
|
|
||
|
if(Tools::getValue('exportList')) {
|
||
|
if($id_generate = (int)Tools::getValue('id')) {
|
||
|
$generate = self::getGenerateVoucher($id_generate);
|
||
|
|
||
|
if(isset($generate['id_generate'])) {
|
||
|
$vouchers = self::getVouchers($generate['vouchers']);
|
||
|
|
||
|
if(count($vouchers) > 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 = '<p class="conf">' .$this->l('Export completed') .'
|
||
|
<br /> <a href="../modules/generate_vouchers/export.csv" target="_blank">'. $this->l('Export') .'</a>
|
||
|
</p>';
|
||
|
}
|
||
|
} else {
|
||
|
$this->_html = '<p class="error">' .$this->l('This id doesnt exist') .'</p>';
|
||
|
}
|
||
|
}
|
||
|
} 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 = '<p class="error">' .$this->l('Error') .'</p>';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$sql = 'UPDATE `'._DB_PREFIX_.'generate_vouchers`
|
||
|
SET vouchers = "' . json_encode($vouchers) .'" WHERE id_generate = "' . $id_generate_voucher .'"';
|
||
|
|
||
|
if(Db::getInstance()->Execute($sql)) {
|
||
|
$this->_html = '<p class="conf">' .$this->l('Generation Ok') . '</p>';
|
||
|
} else {
|
||
|
$this->_html = '<p class="error">' .$this->l('Error before generation') .'</p>';
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
$this->_html = '<p class="error">' .$this->l('Error before generation') .'</p>';
|
||
|
}
|
||
|
} else {
|
||
|
$this->_html = '<p class="error">' .$this->l('Please, verify information ( required fields)') . '</p>';
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function display() {
|
||
|
global $currentIndex, $cookie;
|
||
|
|
||
|
$this->_html .= '<h2>' . $this->l('List generate') . '</h2>
|
||
|
<table style="width: 100%;" class="table">
|
||
|
<thead>
|
||
|
<tr>
|
||
|
<th>' . $this->l('Id') . '</th>
|
||
|
<th>' . $this->l('Name') . '</th>
|
||
|
<th>' . $this->l('Prefix') . '</th>
|
||
|
<th width="100">' . $this->l('Actions') . '</th>
|
||
|
</tr>
|
||
|
<thead>
|
||
|
<tbody>';
|
||
|
|
||
|
$vouchers = self::getGenerateVouchers();
|
||
|
foreach ($vouchers as $key => $voucher) {
|
||
|
$this->_html .= '
|
||
|
<tr>
|
||
|
<td>'. $voucher['id_generate'] .'</td>
|
||
|
<td>'. $voucher['name'] .'</td>
|
||
|
<td>'. $voucher['prefix'] .'</td>
|
||
|
<td>
|
||
|
<a href="'.$currentIndex.'&id='. $voucher['id_generate'] .'&exportList=1&token='.$this->token.'">' . $this->l('Export') . '</a>
|
||
|
</td>
|
||
|
</tr>
|
||
|
';
|
||
|
}
|
||
|
$this->_html .= '</tbody>
|
||
|
</table>
|
||
|
<br />';
|
||
|
|
||
|
$this->_html .= '
|
||
|
<form action="'.$currentIndex.'&submitAdd'.$this->table.'=1&token='.$this->token.'" id="discount" name="discount" method="post" enctype="multipart/form-data">
|
||
|
<h2>'. $this->l('Generate Vouchers') .'</h2>
|
||
|
<fieldset>
|
||
|
<legend><img src="../img/admin/money.gif"/>'. $this->l('Launch Generation') .'</legend>';
|
||
|
|
||
|
$this->_html .= '<label>'. $this->l('Title generation') .'</label>
|
||
|
<div class="margin-form">
|
||
|
<input type="text" name="title" size="100" />
|
||
|
</div>
|
||
|
<label>'. $this->l('Prefix of vouchers') .'</label>
|
||
|
<div class="margin-form">
|
||
|
<input type="text" name="prefix" size="10" />
|
||
|
<p class="desc">'. $this->l('Prefix : For example : AAAA for generate vouchers like AAAA-BBBBBBB') .'</p>
|
||
|
</div>
|
||
|
<label>'. $this->l('Number of generating vouchers') .'</label>
|
||
|
<div class="margin-form">
|
||
|
<input type="text" name="number" size="4" />
|
||
|
</div>
|
||
|
<hr />
|
||
|
<label>'.$this->l('Type:').' </label>
|
||
|
<div class="margin-form">
|
||
|
<select name="id_discount_type" id="id_discount_type" onchange="free_shipping()">
|
||
|
<option value="0">'.$this->l('-- Choose --').'</option>';
|
||
|
$discountTypes = Discount::getDiscountTypes((int)($cookie->id_lang));
|
||
|
foreach ($discountTypes AS $discountType)
|
||
|
$this->_html .='<option value="'.(int)($discountType['id_discount_type']).'" >'.$discountType['name'].'</option>';
|
||
|
$this->_html .= '</select> <sup>*</sup>
|
||
|
</div>
|
||
|
<div id="value-div" style="display:none">
|
||
|
<label>'.$this->l('Value').'</label>
|
||
|
<div class="margin-form">
|
||
|
<input style="float:left;width:80px" type="text" name="value" id="discount_value" value="" onKeyUp="javascript:this.value = this.value.replace(/,/g, \'.\'); " />
|
||
|
<select id="id_currency" name="id_currency" style="float:left;margin-left:10px;width:50px;display:none">
|
||
|
<option value="0">--</option>';
|
||
|
foreach (Currency::getCurrencies() as $row)
|
||
|
$this->_html .= '<option value="'.(int)$row['id_currency'].'">'.$row['sign'].'</option>';
|
||
|
$this->_html .= '
|
||
|
</select>
|
||
|
<span id="percent-span" style="margin-left:10px;display:none;float:left;font-size:12px;font-weight:bold;color:black"> %</span>
|
||
|
<sup style="float:left;margin-left:5px">*</sup>
|
||
|
<p class="clear">'.$this->l('Either the monetary amount or the %, depending on Type selected above').'</p>
|
||
|
</div>
|
||
|
<div id="behavior_not_exhausted" style="display:none;">
|
||
|
<label>'.$this->l('Behavior not exhausted:').'</label>
|
||
|
<div class="margin-form">
|
||
|
<select name="behavior_not_exhausted">
|
||
|
<option value="1">'.$this->l('Reduce the voucher to the total order amount').'</option>
|
||
|
<option value="2">'.$this->l('Create a new voucher with remaining amount').'</option>
|
||
|
<option value="3">'.$this->l('Create negative invoice').'</option>
|
||
|
</select>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
<label>'.$this->l('Description:').' </label>
|
||
|
<div class="margin-form">';
|
||
|
|
||
|
$languages = Language::getLanguages(TRUE);
|
||
|
$divLangName = 'description';
|
||
|
$defaultFormLanguage = (int) Configuration::get('PS_LANG_DEFAULT');
|
||
|
|
||
|
foreach ($languages as $language)
|
||
|
$this->_html .= '<div id="description_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $defaultFormLanguage ? 'block' : 'none').'; float: left;">
|
||
|
<input size="33" type="text" name="description_'.$language['id_lang'].'" /><sup> *</sup>
|
||
|
<span class="hint" name="help_box">'.$this->l('Invalid characters:').' <>;=#{}<span class="hint-pointer"> </span></span>
|
||
|
<p class="clear">'.$this->l('Will appear in cart next to voucher code').'</p>
|
||
|
</div>';
|
||
|
$this->displayFlags($languages, $defaultFormLanguage, 'description', 'description');
|
||
|
$this->_html .= ' </div>
|
||
|
<div class="clear" / >
|
||
|
<label>'.$this->l('Total quantity:').' </label>
|
||
|
<div class="margin-form">
|
||
|
<input type="text" size="15" name="quantity" value="1" /> <sup>*</sup>
|
||
|
<p class="clear">'.$this->l('Total quantity available (mainly for vouchers open to everyone)').'</p>
|
||
|
</div>
|
||
|
<label>'.$this->l('Qty per each user:').' </label>
|
||
|
<div class="margin-form">
|
||
|
<input type="text" size="15" name="quantity_per_user" value="1" /> <sup>*</sup>
|
||
|
<p class="clear">'.$this->l('Number of times a single customer can use this voucher').'</p>
|
||
|
</div>
|
||
|
<label>'.$this->l('Minimum amount').'</label>
|
||
|
<div class="margin-form">
|
||
|
<input type="text" size="15" name="minimal" value="0" onkeyup="javascript:this.value = this.value.replace(/,/g, \'.\'); " /> <sup>*</sup>
|
||
|
<select name="include_tax" id="include_tax" style="vertical-align: middle;">
|
||
|
<option value="0">'.$this->l('tax excl.').'</option>
|
||
|
<option value="1">'.$this->l('tax incl.').'</option>
|
||
|
</select>
|
||
|
<p class="clear">'.$this->l('0 if not applicable').'</p>
|
||
|
</div>
|
||
|
<div class="margin-form">
|
||
|
<p>
|
||
|
<input type="checkbox" name="cumulable" id="cumulable_on" value="1" />
|
||
|
<label class="t" for="cumulable_on"> '.$this->l('Cumulative with other vouchers').'</label>
|
||
|
</p>
|
||
|
</div>
|
||
|
<div class="margin-form">
|
||
|
<p>
|
||
|
<input type="checkbox" checked name="cumulable_reduction" id="cumulable_reduction_on" value="1" />
|
||
|
<label class="t" for="cumulable_reduction_on"> '.$this->l('Cumulative with price reductions').'</label>
|
||
|
</p>
|
||
|
</div>
|
||
|
<label>'.$this->l('From:').' </label>
|
||
|
<div class="margin-form">
|
||
|
<input type="text" size="20" id="date_from" name="date_from" value="'.date('Y-m-d H:i:s').'" /> <sup>*</sup>
|
||
|
<p class="clear">'.$this->l('Start date/time from which voucher can be used').'<br />'.$this->l('Format: YYYY-MM-DD HH:MM:SS').'</p>
|
||
|
</div>
|
||
|
<label>'.$this->l('To:').' </label>
|
||
|
<div class="margin-form">
|
||
|
<input type="text" size="20" id="date_to" name="date_to" value="'. (date('Y') + 1).date('-m-d H:i:s') .'" /> <sup>*</sup>
|
||
|
<p class="clear">'.$this->l('End date/time at which voucher is no longer valid').'<br />'.$this->l('Format: YYYY-MM-DD HH:MM:SS').'</p>
|
||
|
</div>
|
||
|
<br />
|
||
|
<div class="margin-form">
|
||
|
<input type="submit" value="'.$this->l('Save').'" name="submitGenerate" class="button" />
|
||
|
</div>
|
||
|
<div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
|
||
|
</fieldset>
|
||
|
</form>';
|
||
|
|
||
|
/* Javascript for display/hide value of voucher*/
|
||
|
$this->_html .= '
|
||
|
<script type="text/javascript">
|
||
|
function discountType()
|
||
|
{
|
||
|
if ($("#id_discount_type").val() == 0)
|
||
|
$("#value-div").css("display", "none");
|
||
|
else if ($("#id_discount_type").val() == 1)
|
||
|
{
|
||
|
$("#value-div").css("display", "block");
|
||
|
$("#percent-span").css("display", "block");
|
||
|
$("#id_currency").css("display", "none");
|
||
|
}
|
||
|
else if ($("#id_discount_type").val() == 2)
|
||
|
{
|
||
|
$("#value-div").css("display", "block");
|
||
|
$("#percent-span").css("display", "none");
|
||
|
$("#id_currency").css("display", "block");
|
||
|
$("#behavior_not_exhausted").show();
|
||
|
|
||
|
}
|
||
|
else if ($("#id_discount_type").val() == 3)
|
||
|
$("#value-div").css("display", "none");
|
||
|
if ($("#id_discount_type").val() != 2)
|
||
|
$("#behavior_not_exhausted").hide();
|
||
|
|
||
|
}
|
||
|
$(document).ready(function(){
|
||
|
$("#id_discount_type").change(function(){discountType();});
|
||
|
discountType();
|
||
|
});
|
||
|
</script>';
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|