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 33
/
ReferralProgramModule.php
196 lines (175 loc) · 6.47 KB
/
ReferralProgramModule.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?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 [email protected] 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 <[email protected]>
* @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
*/
if (!defined('_PS_VERSION_'))
exit;
class ReferralProgramModule extends ObjectModel
{
public $id_sponsor;
public $email;
public $lastname;
public $firstname;
public $id_customer;
public $id_cart_rule;
public $id_cart_rule_sponsor;
public $date_add;
public $date_upd;
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'referralprogram',
'primary' => 'id_referralprogram',
'fields' => array(
'id_sponsor' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'email' => array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'required' => true, 'size' => 255),
'lastname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 128),
'firstname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 128),
'id_customer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_cart_rule' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_cart_rule_sponsor' =>array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
),
);
public static function getDiscountPrefix()
{
return 'SP';
}
public function registerDiscountForSponsor($id_currency)
{
if ((int)$this->id_cart_rule_sponsor > 0)
return false;
return $this->registerDiscount((int)$this->id_sponsor, 'sponsor', (int)$id_currency);
}
public function registerDiscountForSponsored($id_currency)
{
if (!(int)$this->id_customer OR (int)$this->id_cart_rule > 0)
return false;
return $this->registerDiscount((int)$this->id_customer, 'sponsored', (int)$id_currency);
}
public function registerDiscount($id_customer, $register = false, $id_currency = 0)
{
$configurations = Configuration::getMultiple(array('REFERRAL_DISCOUNT_TYPE', 'REFERRAL_PERCENTAGE', 'REFERRAL_DISCOUNT_CUMULATIVE', 'REFERRAL_DISCOUNT_VALUE_'.(int)$id_currency, 'REFERRAL_TAX'));
$cartRule = new CartRule();
if ($configurations['REFERRAL_DISCOUNT_TYPE'] == Discount::PERCENT)
$cartRule->reduction_percent = (float)$configurations['REFERRAL_PERCENTAGE'];
elseif ($configurations['REFERRAL_DISCOUNT_TYPE'] == Discount::AMOUNT AND isset($configurations['REFERRAL_DISCOUNT_VALUE_'.(int)$id_currency]))
{
$cartRule->reduction_amount = (float)$configurations['REFERRAL_DISCOUNT_VALUE_'.(int)$id_currency];
$cartRule->reduction_tax = (int)$configurations['REFERRAL_TAX'];
}
$cartRule->cart_rule_restriction = !(int)$configurations['REFERRAL_DISCOUNT_CUMULATIVE'];
$cartRule->quantity = 1;
$cartRule->quantity_per_user = 1;
$cartRule->date_from = date('Y-m-d H:i:s', time());
$cartRule->date_to = date('Y-m-d H:i:s', time() + 31536000); // + 1 year
$cartRule->code = $this->getDiscountPrefix().Tools::passwdGen(6);
$cartRule->name = Configuration::getInt('REFERRAL_DISCOUNT_DESCRIPTION');
if (empty($cartRule->name))
$cartRule->name = 'Referral reward';
$cartRule->id_customer = (int)$id_customer;
$cartRule->reduction_currency = (int)$id_currency;
if ($cartRule->add())
{
if ($register != false)
{
if ($register == 'sponsor')
$this->id_cart_rule_sponsor = (int)$cartRule->id;
elseif ($register == 'sponsored')
$this->id_cart_rule = (int)$cartRule->id;
return $this->save();
}
return true;
}
return false;
}
/**
* Return sponsored friends
*
* @return array Sponsor
*/
public static function getSponsorFriend($id_customer, $restriction = false)
{
if (!(int)($id_customer))
return array();
$query = '
SELECT s.*
FROM `'._DB_PREFIX_.'referralprogram` s
WHERE s.`id_sponsor` = '.(int)$id_customer;
if ($restriction)
{
if ($restriction == 'pending')
$query.= ' AND s.`id_customer` = 0';
elseif ($restriction == 'subscribed')
$query.= ' AND s.`id_customer` != 0';
}
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
}
/**
* Return if a customer is sponsorised
*
* @return boolean
*/
public static function isSponsorised($id_customer, $getId=false)
{
$result = Db::getInstance()->getRow('
SELECT s.`id_referralprogram`
FROM `'._DB_PREFIX_.'referralprogram` s
WHERE s.`id_customer` = '.(int)$id_customer);
if (isset($result['id_referralprogram']) AND $getId === true)
return (int)$result['id_referralprogram'];
return isset($result['id_referralprogram']);
}
public static function isSponsorFriend($id_sponsor, $id_friend)
{
if (!(int)($id_sponsor) OR !(int)($id_friend))
return false;
$result = Db::getInstance()->getRow('
SELECT s.`id_referralprogram`
FROM `'._DB_PREFIX_.'referralprogram` s
WHERE s.`id_sponsor` = '.(int)($id_sponsor).' AND s.`id_referralprogram` = '.(int)($id_friend));
return isset($result['id_referralprogram']);
}
/**
* Return if an email is already register
*
* @return boolean OR int idReferralProgram
*/
public static function isEmailExists($email, $getId = false, $checkCustomer = true)
{
if (empty($email) OR !Validate::isEmail($email))
die (Tools::displayError('The email address is invalid.'));
if ($checkCustomer === true AND Customer::customerExists($email))
return false;
$result = Db::getInstance()->getRow('
SELECT s.`id_referralprogram`
FROM `'._DB_PREFIX_.'referralprogram` s
WHERE s.`email` = \''.pSQL($email).'\'');
if ($getId)
return (int)$result['id_referralprogram'];
return isset($result['id_referralprogram']);
}
}