bebeboutik/modules/ant_configurations/cron_discount.php
2017-02-24 14:22:10 +01:00

124 lines
3.7 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';
$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 = 2;
$discount->behavior_not_exhausted = 1;
$discount->cumulable = 0;
$discount->cumulable_reduction = 1;
$discount->minimal = 50;
$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 = 1;
$discount->value = 10;
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()
)
');
}