bebeboutik/modules/loyalty/loyalty-program.php

195 lines
7.4 KiB
PHP
Raw Normal View History

2016-01-04 12:49:26 +01:00
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 6963 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/* SSL Management */
$useSSL = true;
require_once(dirname(__FILE__).'/../../config/config.inc.php');
require_once(dirname(__FILE__).'/../../init.php');
include_once(dirname(__FILE__).'/LoyaltyModule.php');
include_once(dirname(__FILE__).'/LoyaltyStateModule.php');
2016-05-10 15:44:36 +02:00
$controller->preProcess();
2016-01-04 12:49:26 +01:00
if (!$cookie->isLogged())
Tools::redirect('authentication.php?back=modules/loyalty/loyalty-program.php');
Tools::addCSS(_PS_CSS_DIR_.'jquery.cluetip.css', 'all');
Tools::addJS(array(_PS_JS_DIR_.'jquery/jquery.dimensions.js',_PS_JS_DIR_.'jquery/jquery.cluetip.js'));
2016-04-20 15:33:56 +02:00
$displayorders = LoyaltyModule::getAllByIdCustomerCustom((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));
2016-05-03 15:38:20 +02:00
//$orderToConvert = LoyaltyModule::getAllByIdCustomerCustom((int)($cookie->id_customer), (int)($cookie->id_lang), true, false, $nb = 10, $page = 1);
//$total_discount = LoyaltyModule::getVoucherValueByPercentOfOrder($orderToConvert, (int)($cookie->id_currency));
2016-06-08 12:02:16 +02:00
/*$total_discount = Db::getInstance()->getValue('
2016-05-03 15:38:20 +02:00
SELECT SUM(`discount_value`) FROM `'._DB_PREFIX_.'loyalty`
WHERE `id_loyalty_state`=2
AND `id_discount`=0
AND `id_customer` = ' .(int) $cookie->id_customer
2016-06-08 12:02:16 +02:00
);*/
$allOrders = LoyaltyModule::getOrdersWithDiscountByIdCustomer((int) $cookie->id_customer);
$total_discount = 0;
if (!empty($allOrders)) {
$nb_orders = count($allOrders);
foreach ($allOrders as $key => $order) {
if ($order['id_loyalty_state'] !=2){
continue;
}
$total_discount += (float)$order['id_loyalty_state'];
}
} else {
$nb_orders = 0;
}
2016-04-21 17:41:04 +02:00
2016-01-04 12:49:26 +01:00
/* transform point into voucher if needed */
2016-04-21 11:14:33 +02:00
if (Tools::getValue('transform-points') == 'true' AND $total_discount > 0)
2016-01-04 12:49:26 +01:00
{
/* Generate a voucher code */
$voucherCode = NULL;
do $voucherCode = 'FID'.rand(1000, 100000);
while (Discount::discountExists($voucherCode));
/* Voucher creation and affectation to the customer */
$voucher = new Discount();
$voucher->name = $voucherCode;
$voucher->id_discount_type = 2; // Discount on order (amount)
$voucher->id_customer = (int)($cookie->id_customer);
$voucher->id_currency = (int)($cookie->id_currency);
2016-04-21 11:14:33 +02:00
$voucher->value = $total_discount;
2016-01-04 12:49:26 +01:00
$voucher->quantity = 1;
$voucher->quantity_per_user = 1;
$voucher->cumulable = 1;
$voucher->cumulable_reduction = 1;
$voucher->cart_display = 1;
/* If merchandise returns are allowed, the voucher musn't be usable before this max return date */
$dateFrom = Db::getInstance()->getValue('
SELECT UNIX_TIMESTAMP(date_add) n
FROM '._DB_PREFIX_.'loyalty
WHERE id_discount = 0 AND id_customer = '.(int)$cookie->id_customer.'
ORDER BY date_add DESC');
if (Configuration::get('PS_ORDER_RETURN'))
$dateFrom += 60 * 60 * 24 * (int)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 = (float)Configuration::get('PS_LOYALTY_MINIMAL');
$voucher->active = 1;
//ANTADIS
// $categories = Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY');
// if ($categories != '' AND $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']));
$voucher->description[(int)($language['id_lang'])] = $text ? strval($text) : strval($default_text);
}
//ANTADIS
// if (is_array($categories) AND sizeof($categories))
// $voucher->add(true, false, $categories);
// else
$voucher->add();
/* Register order(s) which contributed to create this voucher */
2016-05-03 15:38:20 +02:00
$orderToConvert = LoyaltyModule::getAllByIdCustomerCustom((int)($cookie->id_customer), (int)($cookie->id_lang), true, false, $nb = 10, $page = 1);
2016-04-20 15:33:56 +02:00
LoyaltyModule::registerDiscountCustom($voucher, $orderToConvert);
2016-01-04 12:49:26 +01:00
Tools::redirect('modules/loyalty/loyalty-program.php');
}
include(dirname(__FILE__).'/../../header.php');
2016-04-21 17:41:04 +02:00
//$orders = LoyaltyModule::getAllByIdCustomer((int)($cookie->id_customer), (int)($cookie->id_lang));
2016-01-04 12:49:26 +01:00
$smarty->assign(array(
2016-06-08 12:02:16 +02:00
/*'nb_orders' => (int)LoyaltyModule::getNbOrdersByIdCustomer((int)($cookie->id_customer)),*/
'nb_orders' => $nb_orders,
2016-01-04 12:49:26 +01:00
'displayorders' => $displayorders,
'pagination_link' => __PS_BASE_URI__.'modules/loyalty/loyalty-program.php',
2016-04-21 11:14:33 +02:00
'voucher' => $total_discount,
2016-01-04 12:49:26 +01:00
'validation_id' => LoyaltyStateModule::getValidationId(),
2016-04-21 11:14:33 +02:00
'transformation_allowed' => $total_discount > 0,
2016-01-04 12:49:26 +01:00
'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),
2016-04-20 15:33:56 +02:00
'max_page' => floor(sizeof($displayorders) / ((int)(Tools::getValue('n') > 0) ? (int)(Tools::getValue('n')) : 10))
2016-01-04 12:49:26 +01:00
));
/* 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_discount'], (int)($cookie->id_lang));
2016-04-21 17:41:04 +02:00
$discounts[$key]->orders = LoyaltyModule::getOrdersByIdDiscountCustom((int)$discounts[$key]->id);
2016-01-04 12:49:26 +01:00
}
}
$allCategories = Category::getSimpleCategories((int)($cookie->id_lang));
$voucherCategories = Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY');
if ($voucherCategories != '' AND $voucherCategories != 0)
$voucherCategories = explode(',', Configuration::get('PS_LOYALTY_VOUCHER_CATEGORY'));
else
die(Tools::displayError());
if (sizeof($voucherCategories) == sizeof($allCategories))
$categoriesNames = null;
else
{
$categoriesNames = array();
foreach ($allCategories AS $k => $allCategory)
if (in_array($allCategory['id_category'], $voucherCategories))
$categoriesNames[$allCategory['id_category']] = trim($allCategory['name']);
if (!empty($categoriesNames))
$categoriesNames = Tools::truncate(implode(', ', $categoriesNames), 100).'.';
else
$categoriesNames = null;
2016-04-21 17:41:04 +02:00
}
2016-01-04 12:49:26 +01:00
$smarty->assign(array(
'nbDiscounts' => (int)$nbDiscounts,
'discounts' => $discounts,
'minimalLoyalty' => (float)Configuration::get('PS_LOYALTY_MINIMAL'),
2016-04-21 17:41:04 +02:00
'categories' => $categoriesNames
));
2016-01-04 12:49:26 +01:00
echo Module::display(dirname(__FILE__).'/loyalty.php', 'loyalty.tpl');
include(dirname(__FILE__).'/../../footer.php');