This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
loyalty-program.php
169 lines (143 loc) · 6.5 KB
/
loyalty-program.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* 2007-2016 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-2016 PrestaShop SA
* @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');
include_once(dirname(__FILE__).'/loyalty.php');
Tools::displayFileAsDeprecated();
$context = Context::getContext();
if (!$context->customer->isLogged())
Tools::redirect('index.php?controller=authentication&back=modules/loyalty/loyalty-program.php');
$context->controller->addJqueryPlugin(array('dimensions', 'cluetip'));
$customerPoints = (int)LoyaltyModule::getPointsByCustomer((int)$cookie->id_customer);
/* transform point into voucher if needed */
if (Tools::getValue('transform-points') == 'true' AND $customerPoints > 0)
{
/* Generate a voucher code */
$voucherCode = NULL;
do
$voucherCode = 'FID'.rand(1000, 100000);
while (CartRule::cartRuleExists($voucherCode));
/* Voucher creation and affectation to the customer */
$cartRule = new CartRule();
$cartRule->code = $voucherCode;
$cartRule->id_customer = (int)$context->customer->id;
$cartRule->id_currency = (int)$context->currency->id;
$cartRule->reduction_amount = LoyaltyModule::getVoucherValue((int)$customerPoints);
$cartRule->quantity = 1;
$cartRule->quantity_per_user = 1;
$cartRule->reduction_tax = (bool)Configuration::get('PS_LOYALTY_TAX');
/* 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_cart_rule = 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');
$cartRule->date_from = date('Y-m-d H:i:s', $dateFrom);
$cartRule->date_to = date('Y-m-d H:i:s', $dateFrom + 31536000); // + 1 year
$cartRule->minimum_amount = (float)Configuration::get('PS_LOYALTY_MINIMAL');
$cartRule->active = 1;
$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']);
$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);
$discounts[$key]->orders = LoyaltyModule::getOrdersByIdDiscount((int)$discount['id_cart_rule']);
}
}
$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;
}
$smarty->assign(array(
'nbDiscounts' => (int)$nbDiscounts,
'discounts' => $discounts,
'minimalLoyalty' => (float)Configuration::get('PS_LOYALTY_MINIMAL'),
'categories' => $categoriesNames));
$loyalty = new Loyalty();
echo $loyalty->display(dirname(__FILE__).'/loyalty.php', 'loyalty.tpl');
include(dirname(__FILE__).'/../../footer.php');