|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of EC-CUBE |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* http://www.lockon.co.jp/ |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Eccube\Doctrine\EventSubscriber; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\EventSubscriber; |
|
17
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
|
18
|
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs; |
|
19
|
|
|
use Doctrine\ORM\Events; |
|
20
|
|
|
use Eccube\Entity\Customer; |
|
21
|
|
|
use Eccube\Entity\Master\OrderStatus; |
|
22
|
|
|
use Eccube\Entity\Order; |
|
23
|
|
|
use Eccube\Service\MailService; |
|
24
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
25
|
|
|
|
|
26
|
|
|
class UpdatePointEventSubscriber implements EventSubscriber |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ContainerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $container; |
|
32
|
|
|
|
|
33
|
1238 |
|
public function __construct(ContainerInterface $container) |
|
34
|
|
|
{ |
|
35
|
1238 |
|
$this->container = $container; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getMailService() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->container->get(MailService::class); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
448 |
|
public function preUpdate(LifecycleEventArgs $eventArgs) |
|
44
|
|
|
{ |
|
45
|
448 |
|
if (!$eventArgs->getObject() instanceof Order) { |
|
46
|
448 |
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
198 |
|
$Order = $eventArgs->getObject(); |
|
50
|
198 |
|
$Customer = $Order->getCustomer(); |
|
51
|
|
|
// 非会員の場合、処理は無効にする |
|
52
|
198 |
|
if (!$Customer) { |
|
53
|
16 |
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** @var PreUpdateEventArgs $eventArgs */ |
|
57
|
182 |
|
if ($eventArgs->hasChangedField('OrderStatus')) { |
|
58
|
66 |
|
$addCustomerPoint = 0; |
|
59
|
|
|
|
|
60
|
|
|
/** @var OrderStatus $prevStatus */ |
|
61
|
66 |
|
$prevStatus = $eventArgs->getOldValue('OrderStatus'); |
|
62
|
|
|
|
|
63
|
|
|
/** @var Order $newOrder */ |
|
64
|
66 |
|
$newOrder = $eventArgs->getEntity(); |
|
65
|
|
|
/** @var OrderStatus $newStatus */ |
|
66
|
66 |
|
$newStatus = $newOrder->getOrderStatus(); |
|
67
|
|
|
|
|
68
|
|
|
/* |
|
69
|
|
|
* 2.13系の処理を移植 |
|
70
|
|
|
* @see https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/data/class/helper/SC_Helper_Purchase.php#L1134-L1163 |
|
71
|
|
|
*/ |
|
72
|
|
|
|
|
73
|
|
|
// 使用ポイントの更新 |
|
74
|
|
|
// 変更前の対応状況が利用対象の場合、変更前の使用ポイント分を戻す |
|
75
|
66 |
|
if ($this->isUsePoint($prevStatus)) { |
|
76
|
66 |
|
if ($eventArgs->hasChangedField('use_point')) { |
|
77
|
4 |
|
$addCustomerPoint += $eventArgs->getOldValue('use_point'); |
|
78
|
|
|
} else { |
|
79
|
63 |
|
$addCustomerPoint += $newOrder->getUsePoint(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// 変更後の対応状況が利用対象の場合、変更後の使用ポイント分を引く |
|
84
|
66 |
|
if ($this->isUsePoint($newStatus)) { |
|
85
|
66 |
|
$addCustomerPoint -= $newOrder->getUsePoint(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// 加算ポイントの更新 |
|
89
|
|
|
// 変更前の対応状況が加算対象の場合、変更前の加算ポイント分を戻す |
|
90
|
66 |
|
if ($this->isAddPoint($prevStatus)) { |
|
91
|
1 |
|
if ($eventArgs->hasChangedField('add_point')) { |
|
92
|
|
|
$addCustomerPoint -= $eventArgs->getOldValue('add_point'); |
|
93
|
|
|
} else { |
|
94
|
1 |
|
$addCustomerPoint -= $newOrder->getAddPoint(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// 変更後の対応状況が加算対象の場合、変更後の加算ポイント分を足す |
|
99
|
66 |
|
if ($this->isAddPoint($newStatus)) { |
|
100
|
3 |
|
$addCustomerPoint += $newOrder->getAddPoint(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
66 |
|
if ($addCustomerPoint != 0) { |
|
104
|
|
|
/** @var Customer $Customer */ |
|
105
|
5 |
|
$Customer = $newOrder->getCustomer(); |
|
106
|
5 |
|
$newPoint = $Customer->getPoint() + $addCustomerPoint; |
|
107
|
5 |
|
if ($newPoint < 0) { |
|
108
|
|
|
// ポイントがマイナスになるためメールを送信する |
|
109
|
|
|
$mailService = $this->getMailService(); |
|
110
|
|
|
$mailService->sendPointNotifyMail($newOrder, $Customer->getPoint(), $addCustomerPoint); |
|
111
|
|
|
} |
|
112
|
5 |
|
$Customer->setPoint($newPoint); |
|
113
|
5 |
|
$entityManager = $eventArgs->getObjectManager(); |
|
114
|
|
|
// この時点で Customer は Doctrine の更新対象となっていないので, 更新対象に設定する |
|
115
|
5 |
|
$meta = $entityManager->getClassMetadata(Customer::class); |
|
116
|
|
|
// Customer の変更内容を設定する |
|
117
|
5 |
|
$entityManager->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $Customer); |
|
118
|
|
|
// Customer の ChangeSet を scheduleExtraUpdate に設定する |
|
119
|
5 |
|
$changeSet = $entityManager->getUnitOfWork()->getEntityChangeSet($Customer); |
|
120
|
5 |
|
$entityManager->getUnitOfWork()->scheduleExtraUpdate($Customer, $changeSet); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
66 |
|
protected function isUsePoint(OrderStatus $Status) |
|
126
|
|
|
{ |
|
127
|
66 |
|
if ($Status->getId() == OrderStatus::CANCEL) { |
|
128
|
5 |
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
66 |
|
return true; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
66 |
|
protected function isAddPoint(OrderStatus $Status) |
|
135
|
|
|
{ |
|
136
|
66 |
|
if ($Status->getId() == OrderStatus::DELIVERED) { |
|
137
|
3 |
|
return true; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
66 |
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Returns an array of events this subscriber wants to listen to. |
|
145
|
|
|
* |
|
146
|
|
|
* @return array |
|
147
|
|
|
*/ |
|
148
|
1238 |
|
public function getSubscribedEvents() |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
return [ |
|
151
|
1238 |
|
Events::preUpdate, |
|
152
|
|
|
]; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|