From a4dc930c88980602d2d3e3b4358ea5e24c7c9c48 Mon Sep 17 00:00:00 2001 From: Marion Muszynski Date: Mon, 20 Feb 2017 12:58:40 +0100 Subject: [PATCH] addition of cron for new customer discounts --- .../AdminAntConfigurations.php | 30 ++++- modules/ant_configurations/cron_discount.php | 116 ++++++++++++++++++ 2 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 modules/ant_configurations/cron_discount.php diff --git a/modules/ant_configurations/AdminAntConfigurations.php b/modules/ant_configurations/AdminAntConfigurations.php index fd310ca6..0da43df5 100644 --- a/modules/ant_configurations/AdminAntConfigurations.php +++ b/modules/ant_configurations/AdminAntConfigurations.php @@ -139,6 +139,14 @@ class AdminAntConfigurations extends AdminTab } else { HelperFormBootstrap::echoError($this->l('Veuillez choisir une categorie')); } + } elseif(Tools::isSubmit('submitExportDiscount')){ + HelperFormBootstrap::echoError($this->l('L\'outils n\'est pas encore disponible')); + // $results = DB::getInstance()->ExecuteS(' + // SELECT dh.`id_customer`, o.`id_order`, dh.`name`, o.`value` + // FROM `'._DB_PREFIX_.'ant_discount_history` dh + // LEFT JOIN `'._DB_PREFIX_.'order_discount` o ON (o.`id_discount` = dh.`id_discount`) + // WHERE 1 + // '); } } @@ -236,12 +244,32 @@ class AdminAntConfigurations extends AdminTab ), ) ); + $helperForm->_forms[] = array( + 'action' => $base_link, + 'title' => $this->l('Outils'), + 'icon' => ' ', + 'class' => 'form-horizontal', + 'class_div' => 'col-md-3', + 'information' => 'Petit outils d\'exports utiles pour les suivis', + 'sections' => array( + array( + 'inputs' => array( + array( + 'type' => 'submit', + 'class' => 'btn-default', + 'name' => 'submitExportDiscount', + 'value' => $this->l('Exporter Suivi Code promo'), + ), + ), + ), + ), + ); $helperForm->_forms[] = array( 'action' => $base_link, 'title' => $this->l('Association categories/ventes'), 'icon' => ' ', 'class' => 'form-horizontal', - 'class_div' => 'col-md-9', + 'class_div' => 'col-md-6', 'sections' => array( array( 'inputs' => array( diff --git a/modules/ant_configurations/cron_discount.php b/modules/ant_configurations/cron_discount.php new file mode 100644 index 00000000..bdff0c25 --- /dev/null +++ b/modules/ant_configurations/cron_discount.php @@ -0,0 +1,116 @@ +ExecuteS(' + SELECT c.`id_customer` + FROM `'._DB_PREFIX_.'customer` c + LEFT JOIN `'._DB_PREFIX_.'orders` o ON (o.`id_customer` = c.`id_customer`) + LEFT JOIN `'._DB_PREFIX_.'ant_discount_history` dh ON (dh.`id_customer` = c.`id_customer`) + WHERE c.`date_add` < DATE_SUB(NOW(), INTERVAL 90 DAY) + AND o.`id_order` IS NULL + AND dh.`id_discount_history` IS NULL + 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 = 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 H:i:s'); + + $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 = 50; + + 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() + ) + '); +} \ No newline at end of file