-
Notifications
You must be signed in to change notification settings - Fork 170
/
Customers.php
462 lines (400 loc) · 15.8 KB
/
Customers.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\commerce\services;
use Craft;
use craft\base\Component;
use craft\base\Element;
use craft\commerce\behaviors\CustomerBehavior;
use craft\commerce\db\Table;
use craft\commerce\elements\Order;
use craft\commerce\events\UpdatePrimaryPaymentSourceEvent;
use craft\commerce\Plugin;
use craft\commerce\records\Customer as CustomerRecord;
use craft\commerce\web\assets\commercecp\CommerceCpAsset;
use craft\db\Query;
use craft\elements\User;
use craft\errors\ElementNotFoundException;
use craft\errors\InvalidElementException;
use craft\errors\UnsupportedSiteException;
use craft\helpers\ArrayHelper;
use craft\helpers\Db;
use yii\db\Expression;
/**
* Customer service.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 2.0
*/
class Customers extends Component
{
// Events
// -------------------------------------------------------------------------
/**
* @event RegisterElementSourcesEvent The event that is triggered when a primary payment method is saved.
*/
public const EVENT_UPDATE_PRIMARY_PAYMENT_SOURCE = 'updatePrimaryPaymentSource';
/**
* @param User $user
* @param int|null $addressId
* @return bool
*/
public function savePrimaryShippingAddressId(User $user, ?int $addressId): bool
{
$customerRecord = $this->ensureCustomer($user);
$customerRecord->primaryShippingAddressId = $addressId;
/** @var User|CustomerBehavior $user */
$user->primaryShippingAddressId = $addressId;
return $customerRecord->save();
}
/**
* @param User $user
* @param int|null $addressId
* @return bool
*/
public function savePrimaryBillingAddressId(User $user, ?int $addressId): bool
{
$customerRecord = $this->ensureCustomer($user);
$customerRecord->primaryBillingAddressId = $addressId;
/** @var User|CustomerBehavior $user */
$user->primaryBillingAddressId = $addressId;
return $customerRecord->save();
}
/**
* @param User $user
* @param int|null $paymentSourceId
* @return bool
* @since 4.2
*/
public function savePrimaryPaymentSourceId(User $user, ?int $paymentSourceId): bool
{
$customerRecord = $this->ensureCustomer($user);
$originalPaymentSourceId = $customerRecord->primaryPaymentSourceId;
// Only save customer record if the source is not already primary
if ($customerRecord->primaryPaymentSourceId == $paymentSourceId) {
return true;
}
$customerRecord->primaryPaymentSourceId = $paymentSourceId;
if (!$customerRecord->save()) {
return false;
}
/** @var User|CustomerBehavior $user */
$user->primaryPaymentSourceId = $paymentSourceId;
if ($originalPaymentSourceId != $paymentSourceId) {
$event = new UpdatePrimaryPaymentSourceEvent([
'previousPrimaryPaymentSourceId' => $originalPaymentSourceId,
'newPrimaryPaymentSourceId' => $paymentSourceId,
'customer' => $user,
]);
// trigger the update primary payment source event
$this->trigger(self::EVENT_UPDATE_PRIMARY_PAYMENT_SOURCE, $event);
}
return true;
}
/**
* Handle user login
*/
public function loginHandler(): void
{
$impersonating = Craft::$app->getSession()->get(User::IMPERSONATE_KEY) !== null;
// Don't allow transition of current cart to a user that is being impersonated.
if ($impersonating) {
Plugin::getInstance()->getCarts()->forgetCart();
}
Plugin::getInstance()->getCarts()->restorePreviousCartForCurrentUser();
}
/**
* Add customer info tab to the Edit User page in the control panel.
*/
public function addEditUserCommerceTab(array &$context): void
{
$currentUser = Craft::$app->getUser()->getIdentity();
if (!$context['isNewUser'] && ($currentUser->can('commerce-manageOrders') || $currentUser->can('commerce-manageSubscriptions'))) {
$context['tabs']['customerInfo'] = [
'label' => Craft::t('commerce', 'Commerce'),
'url' => '#commerce',
];
}
}
/**
* Add customer info to the Edit User page in the control panel.
*/
public function addEditUserCommerceTabContent(array $context): string
{
if (!$context['user'] || $context['isNewUser']) {
return '';
}
Craft::$app->getView()->registerAssetBundle(CommerceCpAsset::class);
return Craft::$app->getView()->renderTemplate('commerce/_includes/users/_editUserTab', [
'user' => $context['user'],
]);
}
/**
* Sets the last used addresses on the customer on order completion.
*
* Consolidates any other orders using the same email address.
*
* Duplicates the address records used for the order so they are independent to the
* customers address book.
*
* @param Order $order
*/
public function orderCompleteHandler(Order $order): void
{
// Create a user account if requested
if ($order->registerUserOnOrderComplete) {
$this->_activateUserFromOrder($order);
}
if ($order->saveBillingAddressOnOrderComplete || $order->saveShippingAddressOnOrderComplete) {
$this->_saveAddressesFromOrder($order);
}
}
/**
* @param array|Order[] $orders
* @return Order[]
* @since 3.2.0
*/
public function eagerLoadCustomerForOrders(array $orders): array
{
$customerIds = ArrayHelper::getColumn($orders, 'customerId');
/** @var User[] $users */
$users = User::find()->id($customerIds)->limit(null)->indexBy('id')->all();
foreach ($orders as $key => $order) {
$customerId = $order->getCustomerId();
if (isset($users[$customerId])) {
$order->setCustomer($users[$customerId]);
$orders[$key] = $order;
}
}
return $orders;
}
/**
* Returns a customer record by a user element, creating one if none already exists.
*
* @param User $user
* @return CustomerRecord
*/
public function ensureCustomer(User $user): CustomerRecord
{
/** @var CustomerRecord|null $customerRecord */
$customerRecord = CustomerRecord::find()->where(['customerId' => $user->id])->one();
if (!$customerRecord) {
$customerRecord = new CustomerRecord();
$customerRecord->customerId = $user->id;
$customerRecord->save();
}
return $customerRecord;
}
/**
* @return bool Whether the data moved successfully
* @throws ElementNotFoundException|\yii\db\Exception
* @since 4.1.0
*/
public function transferCustomerData(User $fromCustomer, User $toCustomer): bool
{
$fromId = $fromCustomer->id;
$toId = $toCustomer->id;
/** @var User|null $fromUser */
$fromUser = User::find()->id($fromId)->one();
/** @var User|null $toUser */
$toUser = User::find()->id($toId)->one();
if ($fromUser === null) {
throw new ElementNotFoundException('User ID:', $fromId);
}
if ($toUser === null) {
throw new ElementNotFoundException('User ID:', $toId);
}
$userRefs = [
Table::ORDERHISTORIES => 'userId',
Table::SUBSCRIPTIONS => 'userId',
Table::TRANSACTIONS => 'userId',
Table::ORDERS => 'customerId',
Table::PAYMENTSOURCES => 'customerId',
];
foreach ($userRefs as $table => $column) {
Db::update($table, [
$column => $toId,
], [
$column => $fromId,
], [], false);
}
$previousUses = (new Query())->select(['discountId', 'uses'])->from(Table::CUSTOMER_DISCOUNTUSES)->where(['customerId' => $fromId])->pairs();
$toUses = (new Query())->select(['discountId', 'uses'])->from(Table::CUSTOMER_DISCOUNTUSES)->where(['customerId' => $toId])->pairs();
foreach ($previousUses as $discountId => $uses) {
if (isset($toUses[$discountId])) {
Db::update(
table: Table::CUSTOMER_DISCOUNTUSES,
columns: ['uses' => new Expression("uses + $uses")],
condition: [
'customerId' => $toId,
'discountId' => $discountId,
],
params: [],
updateTimestamp: false
);
} else {
Db::insert(
table: Table::CUSTOMER_DISCOUNTUSES,
columns: [
'uses' => $uses,
'customerId' => $toId,
'discountId' => $discountId,
]
);
}
// Remove uses from fromCustomer
Db::update(
table: Table::CUSTOMER_DISCOUNTUSES,
columns: ['uses' => 0],
condition: [
'customerId' => $fromId,
'discountId' => $discountId,
],
params: [],
updateTimestamp: false
);
}
$fromEmail = $fromUser->email;
$toEmail = $toUser->email;
$emailRefs = [
Table::ORDERS => 'email',
];
foreach ($emailRefs as $table => $column) {
Db::update($table, [
$column => $toEmail,
], [
$column => $fromEmail,
], [], false);
}
return true;
}
/**
* @param Order $order
* @return void
* @throws \Throwable
* @throws InvalidElementException
* @throws UnsupportedSiteException
*/
private function _saveAddressesFromOrder(Order $order): void
{
// Only for completed orders
if ($order->isCompleted === false) {
return;
}
// Check for a credentialed user
if ($order->getCustomer() === null || !$order->getCustomer()->getIsCredentialed()) {
return;
}
$saveBillingAddress = $order->saveBillingAddressOnOrderComplete && $order->sourceBillingAddressId === null && $order->billingAddressId;
$saveShippingAddress = $order->saveShippingAddressOnOrderComplete && $order->sourceShippingAddressId === null && $order->shippingAddressId;
$newSourceBillingAddressId = null;
$newSourceShippingAddressId = null;
if ($saveBillingAddress && $saveShippingAddress && $order->hasMatchingAddresses()) {
// Only save one address if they are matching
$newAddress = Craft::$app->getElements()->duplicateElement($order->getBillingAddress(),
[
'owner' => $order->getCustomer(),
]
);
$newSourceBillingAddressId = $newAddress->id;
$newSourceShippingAddressId = $newAddress->id;
} else {
if ($saveBillingAddress) {
$newBillingAddress = Craft::$app->getElements()->duplicateElement($order->getBillingAddress(),
[
'owner' => $order->getCustomer(),
]);
$newSourceBillingAddressId = $newBillingAddress->id;
}
if ($saveShippingAddress) {
$newShippingAddress = Craft::$app->getElements()->duplicateElement($order->getShippingAddress(), [
'owner' => $order->getCustomer(),
]);
$newSourceShippingAddressId = $newShippingAddress->id;
}
}
if ($newSourceBillingAddressId) {
$order->sourceBillingAddressId = $newSourceBillingAddressId;
}
if ($newSourceShippingAddressId) {
$order->sourceShippingAddressId = $newSourceShippingAddressId;
}
// Manually update the order DB record to avoid looped element saves
if ($newSourceBillingAddressId || $newSourceShippingAddressId) {
\craft\commerce\records\Order::updateAll([
'sourceBillingAddressId' => $order->sourceBillingAddressId,
'sourceShippingAddressId' => $order->sourceShippingAddressId,
],
[
'id' => $order->id,
]
);
}
}
/**
* Makes sure the user has an email address and sets them to pending and sends the activation email
*/
private function _activateUserFromOrder(Order $order): void
{
$user = $order->getCustomer();
if (!$user || $user->active || $user->locked || $user->suspended) {
return;
}
$billingAddress = $order->getBillingAddress();
$shippingAddress = $order->getShippingAddress();
if (!$user->fullName) {
$user->fullName = $billingAddress?->fullName ?? $shippingAddress?->fullName ?? '';
}
$user->username = $order->getEmail();
$user->pending = true;
$user->setScenario(Element::SCENARIO_ESSENTIALS);
if (Craft::$app->getElements()->saveElement($user)) {
Craft::$app->getUsers()->assignUserToDefaultGroup($user);
$emailSent = Craft::$app->getUsers()->sendActivationEmail($user);
if (!$emailSent) {
Craft::warning('"registerUserOnOrderComplete" used to create the user, but couldn’t send an activation email. Check your email settings.', __METHOD__);
}
if ($billingAddress || $shippingAddress) {
$newAttributes = [
'owner' => $user,
];
// If there is only one address make sure we don't add duplicates to the user
if ($order->hasMatchingAddresses()) {
$newAttributes['title'] = Craft::t('app', 'Address');
$shippingAddress = null;
}
// Copy addresses to user
if ($billingAddress) {
$newBillingAddress = Craft::$app->getElements()->duplicateElement($billingAddress, $newAttributes);
/**
* Because we are cloning from an order address the `CustomerAddressBehavior` hasn't been instantiated
* therefore we are unable to simply set the `isPrimaryBilling` property when specifying the new attributes during duplication.
*/
if (!$newBillingAddress->hasErrors()) {
$this->savePrimaryBillingAddressId($user, $newBillingAddress->id);
if ($order->hasMatchingAddresses()) {
$this->savePrimaryShippingAddressId($user, $newBillingAddress->id);
}
}
}
if ($shippingAddress) {
$newShippingAddress = Craft::$app->getElements()->duplicateElement($shippingAddress, $newAttributes);
/**
* Because we are cloning from an order address the `CustomerAddressBehavior` hasn't been instantiated
* therefore we are unable to simply set the `isPrimaryShipping` property when specifying the new attributes during duplication.
*/
if (!$newShippingAddress->hasErrors()) {
$this->savePrimaryShippingAddressId($user, $newShippingAddress->id);
}
}
}
} else {
$errors = $user->getErrors();
Craft::warning('Could not create user on order completion.', __METHOD__);
Craft::warning($errors, __METHOD__);
}
}
}