162 lines
4.9 KiB
PHP
162 lines
4.9 KiB
PHP
|
<?php
|
||
|
if (!defined('_PS_VERSION_'))
|
||
|
exit;
|
||
|
|
||
|
require_once(_PS_ROOT_DIR_.'/modules/product_vouchers/productvouchers.php');
|
||
|
|
||
|
class Product_Vouchers extends Module {
|
||
|
|
||
|
public function __construct() {
|
||
|
$this->name = 'product_vouchers';
|
||
|
$this->tab = 'administration';
|
||
|
$this->need_instance = 0;
|
||
|
|
||
|
parent::__construct();
|
||
|
|
||
|
$this->displayName = $this->l('Product Vouchers');
|
||
|
$this->description = $this->l('Associate vouchers to product.');
|
||
|
|
||
|
$this->version = '1.0';
|
||
|
$this->author = 'Antadis';
|
||
|
}
|
||
|
|
||
|
public function install() {
|
||
|
$query = '
|
||
|
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'product_voucher` (
|
||
|
`id_product_voucher` int(11) NOT NULL AUTO_INCREMENT,
|
||
|
`name` varchar(255) NOT NULL,
|
||
|
`product_id` int(11) NOT NULL,
|
||
|
`date_add` datetime NOT NULL,
|
||
|
`active` tinyint(4) NOT NULL,
|
||
|
`max_vouchers` tinyint(4) NOT NULL,
|
||
|
PRIMARY KEY (`id_product_voucher`),
|
||
|
INDEX (`product_id`)
|
||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8
|
||
|
';
|
||
|
if(!Db::getInstance()->Execute($query)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
$query = '
|
||
|
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'product_voucher_lang` (
|
||
|
`id_product_voucher` int(11) NOT NULL,
|
||
|
`objet` varchar(255) NOT NULL,
|
||
|
`mail` TEXT NOT NULL,
|
||
|
`id_lang` tinyint(4) NOT NULL,
|
||
|
PRIMARY KEY (`id_product_voucher`, `id_lang`)
|
||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8
|
||
|
';
|
||
|
if(!Db::getInstance()->Execute($query)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
$query_item = '
|
||
|
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'product_voucher_item` (
|
||
|
`id_product_voucher_item` int(11) NOT NULL AUTO_INCREMENT,
|
||
|
`id_product_voucher` int(11) NOT NULL,
|
||
|
`code` varchar(255) NOT NULL,
|
||
|
`id_order` int(11) NOT NULL,
|
||
|
`statut` tinyint(4) NOT NULL,
|
||
|
PRIMARY KEY (`id_product_voucher_item`),
|
||
|
INDEX (`id_product_voucher`),
|
||
|
INDEX (`id_order`)
|
||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8';
|
||
|
|
||
|
if(!Db::getInstance()->Execute($query_item)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (parent::install() == false
|
||
|
OR !$this->registerHook('adminOrder')
|
||
|
OR !$this->registerHook('orderDetailDisplayed')
|
||
|
OR !$this->registerHook('postUpdateOrderStatus')
|
||
|
OR !$this->registerHook('cancelProduct') )
|
||
|
return false;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
public function hookpostUpdateOrderStatus($params) {
|
||
|
$id_order = $params['id_order'];
|
||
|
|
||
|
if($params['newOrderStatus']->id == 2) {
|
||
|
$vouchers_available = ProductVouchers::getProductEligibleForOrder($id_order);
|
||
|
if(count($vouchers_available) > 0) {
|
||
|
$vouchers = ProductVouchers::getVouchersCodeByOrder($id_order);
|
||
|
if(count($vouchers) == 0) {
|
||
|
foreach($vouchers_available as $voucher) {
|
||
|
ProductVouchers::associateCodeToOrder($voucher['id_product_voucher'], $voucher['product_quantity'], $id_order);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
} elseif ($params['newOrderStatus']->id == 6
|
||
|
|| $params['newOrderStatus']->id == 7) {
|
||
|
ProductVouchers::refundOrder($id_order);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function hookcancelProduct($params) {
|
||
|
$refunds = ProductVouchers::getQuantityRefundbyProduct($params['order']->id);
|
||
|
if(!empty($refunds)) {
|
||
|
foreach($refunds as $key => $refund) {
|
||
|
$diff = $refund['product_quantity_refunded'] - $refund['product_quantity'];
|
||
|
|
||
|
if($diff == $refund['product_quantity']) {
|
||
|
continue;
|
||
|
} else if($diff == 0) {
|
||
|
ProductVouchers::refundCode($refund['product_id'], $params['order']->id, $refund['product_quantity']);
|
||
|
} else {
|
||
|
ProductVouchers::refundCode($refund['product_id'], $params['order']->id, $refund['product_quantity_refunded']);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function hookadminOrder($params) {
|
||
|
global $cookie;
|
||
|
$vouchers = ProductVouchers::getVouchersCodeByOrder($params['id_order']);
|
||
|
|
||
|
if(!empty($vouchers)) {
|
||
|
echo '<br /> <fieldset style="width: 400px">
|
||
|
<legend>'. $this->l('Product Vouchers') .'</legend>';
|
||
|
foreach ($vouchers as $voucher) {
|
||
|
$product = new Product($voucher['product_id'], false, $cookie->id_lang);
|
||
|
echo '<u>' . $product->name . '</u>';
|
||
|
echo '<p><b>Code</b> : '. $voucher['code'] . '
|
||
|
<br /> <b>Statut</b> :' . $this->getState($voucher['statut']);
|
||
|
echo '</p><hr>';
|
||
|
}
|
||
|
echo '</fieldset>';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function hookorderDetailDisplayed($params) {
|
||
|
global $cookie, $smarty;
|
||
|
$vouchers = ProductVouchers::getVouchersCodeByOrder($params['order']->id);
|
||
|
|
||
|
if(!empty($vouchers)) {
|
||
|
foreach ($vouchers as $key => $voucher) {
|
||
|
$product = new Product($voucher['product_id'], false, $cookie->id_lang);
|
||
|
$vouchers[$key]['product'] = $product;
|
||
|
$vouchers[$key]['state'] = $this->getState($voucher['statut']);
|
||
|
}
|
||
|
$smarty->assign('vouchers', $vouchers);
|
||
|
return $this->display(__FILE__, 'order_details.tpl');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|