-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rule.php
412 lines (357 loc) · 13.2 KB
/
Rule.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/**
* @package Bronto\Reminder
* @copyright 2011-2013 Bronto Software, Inc.
*
* @method Bronto_Reminder_Model_Mysql4_Rule _getResource()
*/
class Bronto_Reminder_Model_Rule
extends Mage_Rule_Model_Rule
{
/**
* Contains data defined per store view, will be used in Messages as variables
*
* @var array
*/
protected $_messageData = array();
protected function _construct()
{
parent::_construct();
$this->_init('bronto_reminder/rule');
}
/**
* Perform actions after object load
*
* @return Bronto_Reminder_Model_Rule
*/
protected function _afterLoad()
{
Mage_Core_Model_Abstract::_afterLoad();
if (Mage::helper('bronto_verify')->isVersionMatch(
Mage::getVersionInfo(),
1,
array(array('<=', 6), array('edition' => 'Professional', 'major' => 9), 10, 11))
) {
$conditionsArr = unserialize($this->getConditionsSerialized());
if (!empty($conditionsArr) && is_array($conditionsArr)) {
$this->getConditions()->loadArray($conditionsArr);
}
}
$messageData = $this->_getResource()->getMessageData($this->getId());
foreach ($messageData as $data) {
$message = (empty($data['message_id'])) ? null : $data['message_id'];
$sendType = (empty($data['send_type'])) ? 'transactional' : $data['send_type'];
$this->setData('store_message_' . $data['store_id'], $message)
->setData('store_message_sendtype_' . $data['store_id'], $sendType)
->setData('store_message_sendflags_' . $data['store_id'], $data['send_flags']);
}
return $this;
}
/**
* Perform actions before object save.
*/
protected function _beforeSave()
{
$this->setConditionSql(
$this->getConditions()->getConditionsSql(null, new Zend_Db_Expr(':website_id'))
);
if (!$this->getSalesruleId()) {
$this->setSalesruleId(null);
}
if (!$this->getProductRecommendationId()) {
$this->setProductRecommendationId(null);
}
parent::_beforeSave();
}
/**
* Live website ids data as is
*
* @return Bronto_Reminder_Model_Rule
*/
protected function _prepareWebsiteIds()
{
return $this;
}
/**
* Return conditions instance
*
* @return Bronto_Reminder_Model_Rule_Condition_Combine
*/
public function getConditionsInstance()
{
return Mage::getModel('bronto_reminder/rule_condition_combine_root');
}
/**
* Get rule associated website ids
*
* @return array
*/
public function getWebsiteIds()
{
if (!$this->hasData('website_ids')) {
$this->setData('website_ids', $this->_getResource()->getWebsiteIds($this->getId()));
}
return $this->_getData('website_ids');
}
/**
* Get array of Registered User abandons and then Guest abandons
*
* @param int $limit
* @param int $threshold (Optional)
*
* @return array
*/
protected function _getRecipients($limit, $threshold = null)
{
// Pull in array of customers who abandoned their cart
return (array)$this->_getResource()->getCustomersForNotification($limit, $this->getRuleId(), $threshold);
}
/**
* Get customer object for recipient
*
* @param array $recipient
* @param Mage_Sales_Model_Quote $quote
*
* @return boolean|Mage_Customer_Model_Customer
*/
protected function _getRecipientCustomer(array $recipient, $quote)
{
if ($recipient['customer_id'] != 0) {
/* @var $customer Mage_Customer_Model_Customer */
$customer = Mage::getModel('customer/customer')->load($recipient['customer_id']);
} elseif ($quote) {
// Guest Abandon. Create Customer on the fly
$storeId = $recipient['store_id'];
$customer = Mage::getModel('customer/customer')
->setFirstName($quote->getCustomerFirstname())
->setLastName($quote->getCustomerLastname())
->setEmail($quote->getCustomerEmail())
->setStoreId($storeId)
->setId($recipient['customer_id'])
->setWebsiteId(Mage::getModel('core/store')->load($storeId)->getWebsiteId());
}
if (!$customer || false === $customer->getId()) {
return false;
}
return $customer;
}
/**
* Send reminder emails
*
* @param bool $dontSend
*
* @return Bronto_Reminder_Model_Rule
*/
public function sendReminderEmails($dontSend = false)
{
// If we aren't matching and we aren't allow to send emails, say so
if (!$dontSend &&
!Mage::helper('bronto_reminder')->isAllowSendForAny() ||
!Mage::helper('bronto_reminder')->isEnabledForAny()
) {
Mage::helper('bronto_reminder')->writeInfo(Mage::helper('bronto_reminder')->getNotAllowedText());
return $this;
}
/* @var $mail Bronto_Reminder_Model_Email_Message */
$mail = Mage::getModel('bronto_reminder/email_message');
$limit = Mage::helper('bronto_reminder')->getOneRunLimit();
$identity = Mage::helper('bronto_reminder')->getEmailIdentity();
$threshold = Mage::helper('bronto_reminder')->getSendFailureThreshold();
$this->_matchCustomers();
if ($dontSend) {
return $this;
}
// Get Array of Recipients
$recipients = $this->_getRecipients($limit, $threshold);
$total = 0;
$success = 0;
$error = 0;
foreach ($recipients as $recipient) {
$total++;
$quote = false;
$wishlist = false;
// Load Store
/* @var $store Mage_Core_Model_Store */
$store = Mage::getModel('core/store')->load($recipient['store_id']);
// If Sending not allowed for this store
if (!Mage::helper('bronto_reminder')->isAllowSend('store', $store->getId())) {
$error++;
continue;
}
// Load Quote
if ($recipient['quote_id'] > 0) {
/* @var $quote Mage_Sales_Model_Quote */
$quote = Mage::getModel('sales/quote')
->setStoreId($store->getId())
->loadActive($recipient['quote_id']);
}
// Load Wishlist
if ($recipient['wishlist_id'] > 0) {
/* @var $wishlist Mage_Wishlist_Model_Wishlist */
$wishlist = Mage::getModel('wishlist/wishlist')->load($recipient['wishlist_id']);
}
// If quote and wishlist are empty, move on to next recipient
if (false === $quote && false === $wishlist) {
$error++;
continue;
}
// Load Customer
/* @var $customer Mage_Customer_Model_Customer */
if (!$customer = $this->_getRecipientCustomer($recipient, $quote)) {
$error++;
continue;
}
$messageData = $this->getMessageData($recipient['rule_id'], $store->getId(), $store->getWebsiteId());
if (!$messageData) {
Mage::helper('bronto_reminder')->writeInfo("Rule doesn't have an associated Bronto message.");
$error++;
continue;
}
$coupon = false;
if (class_exists('Mage_SalesRule_Model_Coupon', false)) {
/* @var $coupon Mage_SalesRule_Model_Coupon */
$coupon = Mage::getModel('salesrule/coupon')->load($recipient['coupon_id']);
}
$templateVars = array(
'store' => $store,
'customer' => $customer,
'promotion_name' => $messageData['label'],
'promotion_description' => $messageData['description'],
'coupon' => $coupon,
'rule' => $this,
);
if ($quote) {
$templateVars['quote'] = $quote;
}
if ($wishlist) {
$templateVars['wishlist'] = $wishlist;
}
Mage::helper('bronto_reminder')->writeDebug('Sending message to: ' . $customer->getEmail());
$appEmulation = Mage::getSingleton('core/app_emulation');
$emulatedInfo = $appEmulation->startEnvironmentEmulation($store->getId());
try {
$message = Mage::helper('bronto_reminder/message')->getMessageById(
$messageData['message_id'],
$store->getId(),
$store->getWebsiteId()
);
$mail->setTemplateSendType($messageData['send_type']);
$mail->setSendFlags($messageData['send_flags']);
$mail->setSalesRule($recipient['coupon_id']);
$mail->setProductRecommendation($recipient['product_recommendation_id']);
$mail->sendTransactional(
$message,
$identity,
$customer->getEmail(),
null,
$templateVars,
$store->getId()
);
} catch (Exception $e) {
Mage::helper('bronto_reminder')->writeError(' ' . $e->getMessage());
}
$appEmulation->stopEnvironmentEmulation($emulatedInfo);
if ($mail->getSentSuccess()) {
Mage::helper('bronto_reminder')->writeDebug(' Success');
$this->_getResource()
->deactivateMatchedCustomer($recipient['unique_id'])
->addNotificationLog(
$recipient['rule_id'],
$recipient['unique_id'],
$mail->getLastDeliveryId(),
$messageData['message_id']
);
$success++;
} else {
Mage::helper('bronto_reminder')->writeDebug(' Failed');
$this->_getResource()->updateFailedEmailsCounter($recipient['rule_id'], $recipient['unique_id']);
$error++;
}
}
return array(
'total' => $total,
'success' => $success,
'error' => $error,
);
}
/**
* Match customers and assign coupons
*
* @return Bronto_Reminder_Model_Observer
*/
protected function _matchCustomers()
{
$threshold = Mage::helper('bronto_reminder')->getSendFailureThreshold();
$currentDate = Mage::getModel('core/date')->date('Y-m-d');
$rules = $this->getCollection()
->addDateFilter($currentDate)
->addIsActiveFilter(1);
if ($ruleId = $this->getRuleId()) {
$rules->addRuleFilter($ruleId);
}
foreach ($rules as $rule) {
// $this->_getResource()->deactivateMatchedCustomers($rule->getId());
if ($rule->getSalesruleId()) {
/* @var $salesRule Mage_SalesRule_Model_Rule */
$salesRule = Mage::getSingleton('salesrule/rule')->load($rule->getSalesruleId());
$websiteIds = array_intersect($rule->getWebsiteIds(), $salesRule->getWebsiteIds());
} else {
$salesRule = null;
$websiteIds = $rule->getWebsiteIds();
}
$rule->setConditions(null);
$rule->afterLoad();
foreach ($websiteIds as $websiteId) {
$this->_getResource()->saveMatchedCustomers($rule, $salesRule, $websiteId, $threshold);
}
}
return $this;
}
/**
* Return Message data
*
* @param int $ruleId
* @param int $storeId
*
* @return array|false
*/
public function getMessageData($ruleId, $storeId)
{
if (!isset($this->_messageData[$ruleId][$storeId])) {
if ($data = $this->_getResource()->getStoreMessageData($ruleId, $storeId)) {
if (empty($data['message_id'])) {
$data['message_id'] = null;
}
$this->_messageData[$ruleId][$storeId] = $data;
} else {
return false;
}
}
return $this->_messageData[$ruleId][$storeId];
}
/**
* @param int $ruleId
* @param int $storeId
* @param int $customerId
* @param string $messageId
*
* @return boolean|array
*/
public function getRuleLogItems($ruleId, $storeId, $customerId, $messageId = null)
{
if ($data = $this->_getResource()->getRuleLogItemsData($ruleId, $storeId, $customerId, $messageId)) {
return $data;
}
return false;
}
/**
* Remove row from coupon table by column, value and store_id
*
* @param $column
* @param $value
*/
public function removeFromReminders($column, $value)
{
$this->_getResource()->removeFromReminders($column, $value);
}
}