/** * Transform loyalty point to a voucher */ public function processTransformPoints() { $customer_points = (int) LoyaltyModule::getPointsByCustomer((int) $this->context->customer->id); if ($customer_points > 0) { /* Generate a voucher code */ $voucher_code = null; do { $voucher_code = 'FID' . rand(1000, 100000); } while (CartRule::cartRuleExists($voucher_code)); // Voucher creation and affectation to the customer $cart_rule = new CartRule(); $cart_rule->code = $voucher_code; $cart_rule->id_customer = (int) $this->context->customer->id; $cart_rule->reduction_currency = (int) $this->context->currency->id; $cart_rule->reduction_amount = LoyaltyModule::getVoucherValue((int) $customer_points); $cart_rule->quantity = 1; $cart_rule->highlight = 1; $cart_rule->quantity_per_user = 1; $cart_rule->reduction_tax = (bool) Configuration::get('PS_LOYALTY_TAX'); // If merchandise returns are allowed, the voucher musn't be usable before this max return date $date_from = Db::getInstance()->getValue(' SELECT UNIX_TIMESTAMP(date_add) n FROM ' . _DB_PREFIX_ . 'loyalty WHERE id_cart_rule = 0 AND id_customer = ' . (int) $this->context->cookie->id_customer . ' ORDER BY date_add DESC'); if (Configuration::get('PS_ORDER_RETURN')) { $date_from += 60 * 60 * 24 * (int) Configuration::get('PS_ORDER_RETURN_NB_DAYS'); } $cart_rule->date_from = date('Y-m-d H:i:s', $date_from); $cart_rule->date_to = date('Y-m-d H:i:s', strtotime($cart_rule->date_from . ' +1 year')); $cart_rule->minimum_amount = (double) Configuration::get('PS_LOYALTY_MINIMAL'); $cart_rule->minimum_amount_currency = (int) $this->context->currency->id; $cart_rule->active = 1; $categories = Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY'); if ($categories != '' && $categories != 0) { $categories = explode(',', Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY')); } else { die(Tools::displayError()); } $languages = Language::getLanguages(true); $default_text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', (int) Configuration::get('PS_LANG_DEFAULT')); foreach ($languages as $language) { $text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', (int) $language['id_lang']); $cart_rule->name[(int) $language['id_lang']] = $text ? strval($text) : strval($default_text); } $contains_categories = is_array($categories) && count($categories); if ($contains_categories) { $cart_rule->product_restriction = 1; } $cart_rule->add(); //Restrict cartRules with categories if ($contains_categories) { //Creating rule group $id_cart_rule = (int) $cart_rule->id; $sql = "INSERT INTO " . _DB_PREFIX_ . "cart_rule_product_rule_group (id_cart_rule, quantity) VALUES ('{$id_cart_rule}', 1)"; Db::getInstance()->execute($sql); $id_group = (int) Db::getInstance()->Insert_ID(); //Creating product rule $sql = "INSERT INTO " . _DB_PREFIX_ . "cart_rule_product_rule (id_product_rule_group, type) VALUES ('{$id_group}', 'categories')"; Db::getInstance()->execute($sql); $id_product_rule = (int) Db::getInstance()->Insert_ID(); //Creating restrictions $values = array(); foreach ($categories as $category) { $category = (int) $category; $values[] = "('{$id_product_rule}', '{$category}')"; } $values = implode(',', $values); $sql = "INSERT INTO " . _DB_PREFIX_ . "cart_rule_product_rule_value (id_product_rule, id_item) VALUES {$values}"; Db::getInstance()->execute($sql); } // Register order(s) which contributed to create this voucher if (!LoyaltyModule::registerDiscount($cart_rule)) { $cart_rule->delete(); } } Tools::redirect($this->context->link->getModuleLink('loyalty', 'default', array('process' => 'summary'))); }
$dateFrom = $dateFrom + 60 * 60 * 24 * intval(Configuration::get('PS_ORDER_RETURN_NB_DAYS')); } $voucher->date_from = date('Y-m-d H:i:s', $dateFrom); $voucher->date_to = date('Y-m-d H:i:s', $dateFrom + 31536000); // + 1 year $voucher->minimal = 0; $voucher->active = 1; $languages = Language::getLanguages(true); $default_text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', intval(Configuration::get('PS_LANG_DEFAULT'))); foreach ($languages as $language) { $text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', intval($language['id_lang'])); $voucher->description[intval($language['id_lang'])] = $text ? strval($text) : strval($default_text); } $voucher->save(); /* register order(s) which contribute to create this voucher */ LoyaltyModule::registerDiscount($voucher); Tools::redirect('modules/loyalty/loyalty-program.php'); } include dirname(__FILE__) . '/../../header.php'; $orders = LoyaltyModule::getAllByIdCustomer(intval($cookie->id_customer), intval($cookie->id_lang)); $smarty->assign(array('orders' => $orders, 'totalPoints' => $customerPoints, 'voucher' => LoyaltyModule::getVoucherValue($customerPoints, intval($cookie->id_currency)), 'validation_id' => LoyaltyStateModule::getValidationId(), 'transformation_allowed' => $customerPoints > 0)); /* Discounts */ $nbDiscounts = 0; $discounts = array(); if ($ids_discount = LoyaltyModule::getDiscountByIdCustomer(intval($cookie->id_customer))) { $nbDiscounts = count($ids_discount); foreach ($ids_discount as $key => $discount) { $discounts[$key] = new Discount($discount['id_discount'], intval($cookie->id_lang)); $discounts[$key]->date_add = $discount['date_add']; $discounts[$key]->orders = LoyaltyModule::getOrdersByIdDiscount($discount['id_discount']); }
} else { die(Tools::displayError()); } $languages = Language::getLanguages(true); $default_text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', (int) Configuration::get('PS_LANG_DEFAULT')); foreach ($languages as $language) { $text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', (int) $language['id_lang']); $cartRule->name[(int) $language['id_lang']] = $text ? strval($text) : strval($default_text); } if (is_array($categories) and sizeof($categories)) { $cartRule->add(true, false, $categories); } else { $cartRule->add(); } /* Register order(s) which contributed to create this voucher */ if (!LoyaltyModule::registerDiscount($cartRule)) { $cartRule->delete(); } Tools::redirect('modules/loyalty/loyalty-program.php'); } include dirname(__FILE__) . '/../../header.php'; $orders = LoyaltyModule::getAllByIdCustomer((int) $cookie->id_customer, (int) $cookie->id_lang); $displayorders = LoyaltyModule::getAllByIdCustomer((int) $cookie->id_customer, (int) $cookie->id_lang, false, true, (int) Tools::getValue('n') > 0 ? (int) Tools::getValue('n') : 10, (int) Tools::getValue('p') > 0 ? (int) Tools::getValue('p') : 1); $smarty->assign(array('orders' => $orders, 'displayorders' => $displayorders, 'pagination_link' => __PS_BASE_URI__ . 'modules/loyalty/loyalty-program.php', 'totalPoints' => (int) $customerPoints, 'voucher' => LoyaltyModule::getVoucherValue($customerPoints, (int) $context->currency->id), 'validation_id' => LoyaltyStateModule::getValidationId(), 'transformation_allowed' => $customerPoints > 0, 'page' => (int) Tools::getValue('p') > 0 ? (int) Tools::getValue('p') : 1, 'nbpagination' => (int) (Tools::getValue('n') > 0) ? (int) Tools::getValue('n') : 10, 'nArray' => array(10, 20, 50), 'max_page' => floor(sizeof($orders) / ((int) (Tools::getValue('n') > 0) ? (int) Tools::getValue('n') : 10)))); /* Discounts */ $nbDiscounts = 0; $discounts = array(); if ($ids_discount = LoyaltyModule::getDiscountByIdCustomer((int) $cookie->id_customer)) { $nbDiscounts = count($ids_discount); foreach ($ids_discount as $key => $discount) { $discounts[$key] = new Discount((int) $discount['id_cart_rule'], (int) $cookie->id_lang);
/** * Transform loyalty point to a voucher */ public function processTransformPoints() { $customer_points = (int) LoyaltyModule::getPointsByCustomer((int) $this->context->customer->id); if ($customer_points > 0) { /* Generate a voucher code */ $voucher_code = null; do { $voucher_code = 'FID' . rand(1000, 100000); } while (CartRule::cartRuleExists($voucher_code)); // Voucher creation and affectation to the customer $cart_rule = new CartRule(); $cart_rule->code = $voucher_code; $cart_rule->id_customer = (int) $this->context->customer->id; $cart_rule->reduction_currency = (int) $this->context->currency->id; $cart_rule->reduction_amount = LoyaltyModule::getVoucherValue((int) $customer_points); $cart_rule->quantity = 1; $cart_rule->quantity_per_user = 1; // If merchandise returns are allowed, the voucher musn't be usable before this max return date $date_from = Db::getInstance()->getValue(' SELECT UNIX_TIMESTAMP(date_add) n FROM ' . _DB_PREFIX_ . 'loyalty WHERE id_cart_rule = 0 AND id_customer = ' . (int) $this->context->cookie->id_customer . ' ORDER BY date_add DESC'); if (Configuration::get('PS_ORDER_RETURN')) { $date_from += 60 * 60 * 24 * (int) Configuration::get('PS_ORDER_RETURN_NB_DAYS'); } $cart_rule->date_from = date('Y-m-d H:i:s', $date_from); $cart_rule->date_to = date('Y-m-d H:i:s', strtotime($cart_rule->date_from . ' +1 year')); $cart_rule->minimum_amount = (double) Configuration::get('PS_LOYALTY_MINIMAL'); $cart_rule->active = 1; $categories = Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY'); if ($categories != '' && $categories != 0) { $categories = explode(',', Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY')); } else { die(Tools::displayError()); } $languages = Language::getLanguages(true); $default_text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', (int) Configuration::get('PS_LANG_DEFAULT')); foreach ($languages as $language) { $text = Configuration::get('PS_LOYALTY_VOUCHER_DETAILS', (int) $language['id_lang']); $cart_rule->name[(int) $language['id_lang']] = $text ? strval($text) : strval($default_text); } if (is_array($categories) && count($categories)) { $cart_rule->add(true, false, $categories); } else { $cart_rule->add(); } // Register order(s) which contributed to create this voucher LoyaltyModule::registerDiscount($cart_rule); Tools::redirect($this->context->link->getModuleLink('loyalty', 'default', array('process' => 'summary'))); } }