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 '