142 lines
4.2 KiB
PHP
142 lines
4.2 KiB
PHP
<?php
|
|
if(isset($_SERVER['REMOTE_ADDR'])) {
|
|
exit;
|
|
}
|
|
/*
|
|
CREATE TABLE `ps_ant_discount_history` (
|
|
`id_discount_history` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
`id_customer` int(10) unsigned NOT NULL,
|
|
`id_discount` int(10) unsigned NOT NULL,
|
|
`code` varchar(50) NOT NULL,
|
|
`used` int(4) NOT NULL DEFAULT '0',
|
|
`date_add` datetime NOT NULL,
|
|
PRIMARY KEY (`id_discount_history`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
*/
|
|
|
|
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
|
|
$_SERVER['SERVER_NAME'] = 'www.bebeboutik.com';
|
|
$_SERVER['HTTP_PORT'] = 80;
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
|
|
|
include dirname(__FILE__).'/../../config/config.inc.php';
|
|
|
|
// CLEAN DISCOUNT QUAND CHANGEMENTS
|
|
// $discount_toclean = array();
|
|
// foreach(Db::getInstance()->ExecuteS('
|
|
// SELECT dh.id_discount
|
|
// FROM `ps_ant_discount_history` dh
|
|
// LEFT JOIN `ps_order_discount` o ON (o.`id_discount` = dh.`id_discount`)
|
|
// WHERE o.`id_order` IS NULL
|
|
// AND dh.`used` = 0
|
|
// ') as $d){
|
|
// $discount_toclean[] = (int)$d['id_discount'];
|
|
// }
|
|
// Db::getInstance()->ExecuteS('
|
|
// UPDATE ps_discount
|
|
// SET
|
|
// `include_tax`= 1,
|
|
// WHERE id_discount IN ('.implode(',',$discount_toclean).')
|
|
// ');
|
|
|
|
$user_discounts = array();
|
|
foreach(Db::getInstance()->ExecuteS('
|
|
SELECT `id_customer`
|
|
FROM `'._DB_PREFIX_.'ant_discount_history`
|
|
WHERE 1
|
|
') as $customer) {
|
|
$user_discounts[(int)$customer['id_customer']] = (int)$customer['id_customer'];
|
|
}
|
|
|
|
// Create Discount
|
|
$customers = Db::getInstance()->ExecuteS('
|
|
SELECT c.`id_customer`
|
|
FROM `'._DB_PREFIX_.'customer` c
|
|
LEFT JOIN `'._DB_PREFIX_.'orders` o ON (o.`id_customer` = c.`id_customer`)
|
|
WHERE c.`date_add` < DATE_SUB(NOW(), INTERVAL 90 DAY)
|
|
AND o.`id_order` IS NULL
|
|
AND c.`id_customer` NOT IN ('.implode(',',$user_discounts).')
|
|
ORDER BY c.id_customer DESC
|
|
');
|
|
foreach($customers as $customer){
|
|
if($discount = generateDiscount((int)$customer['id_customer'])) {
|
|
registerCustomer((int)$customer['id_customer'],$discount);
|
|
}
|
|
}
|
|
|
|
// Clean used Discount
|
|
$discounts = Db::getInstance()->ExecuteS('
|
|
SELECT dh.`id_discount_history`
|
|
FROM `'._DB_PREFIX_.'ant_discount_history` dh
|
|
LEFT JOIN `'._DB_PREFIX_.'order_discount` o ON (o.`id_discount` = dh.`id_discount`)
|
|
WHERE o.`id_order` IS NOT NULL
|
|
AND dh.`used` = 0
|
|
');
|
|
foreach ($discounts as $key => $discount) {
|
|
Db::getInstance()->Execute('
|
|
UPDATE `'._DB_PREFIX_.'ant_discount_history`
|
|
SET `used` = 1
|
|
WHERE `id_discount_history` ='.(int)$discount['id_discount_history'].'
|
|
');
|
|
}
|
|
|
|
function generateDiscount($id_customer)
|
|
{
|
|
$discount = new Discount();
|
|
$discount->id_customer = (int)$id_customer;
|
|
$discount->id_discount_type = 3; // free shipping
|
|
$discount->behavior_not_exhausted = 1;
|
|
$discount->cumulable = 0;
|
|
$discount->cumulable_reduction = 1;
|
|
$discount->minimal = 10;
|
|
$discount->include_tax = 1;
|
|
$discount->cart_display = 0;
|
|
$discount->active = 1;
|
|
$discount->quantity = 1;
|
|
$discount->quantity_per_user = 1;
|
|
$discount->date_from = date('Y-m-d H:i:s');
|
|
$discount->date_to = (date('Y') + 1).date('-m-d').' 23:59:59';
|
|
|
|
$name = 'KDO-'. Tools::passwdGen(8);
|
|
if(Discount::discountExists($name))
|
|
{
|
|
$name = 'KDO-'. Tools::passwdGen(8);
|
|
}
|
|
$discount->name = $name;
|
|
|
|
$languages = Language::getLanguages(TRUE);
|
|
$description = array(
|
|
1 => 'Welcome Gift',
|
|
2 => 'Cadeau de Bienvenue',
|
|
3 => 'Regalo de Bienvenida',
|
|
4 => 'Welcome Gift',
|
|
5 => 'Welcome Gift'
|
|
);
|
|
foreach ($languages as $language) {
|
|
$discount->description[$language['id_lang']] = $description[$language['id_lang']];
|
|
}
|
|
|
|
$discount->id_currency = 0;
|
|
$discount->value = 0;
|
|
|
|
if($discount->add(true, false, array(1)) ) {
|
|
return $discount;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function registerCustomer($id_customer,$discount) {
|
|
Db::getInstance()->Execute('
|
|
INSERT INTO `'._DB_PREFIX_.'ant_discount_history` (`id_discount_history`,`id_customer`,`id_discount`,`code`,`used`,`date_add`)
|
|
VALUES (
|
|
DEFAULT,
|
|
'.(int)$id_customer.',
|
|
'.(int)$discount->id.',
|
|
"'.pSQL($discount->name).'",
|
|
0,
|
|
NOW()
|
|
)
|
|
');
|
|
}
|