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\Service\PurchaseFlow\Processor; |
15
|
|
|
|
16
|
|
|
use Eccube\Entity\ItemHolderInterface; |
17
|
|
|
use Eccube\Entity\Master\OrderStatus; |
18
|
|
|
use Eccube\Entity\ProductClass; |
19
|
|
|
use Eccube\Entity\ProductStock; |
20
|
|
|
use Eccube\Repository\ProductClassRepository; |
21
|
|
|
use Eccube\Service\PurchaseFlow\ItemHolderValidator; |
22
|
|
|
use Eccube\Service\PurchaseFlow\PurchaseContext; |
23
|
|
|
use Eccube\Service\PurchaseFlow\PurchaseException; |
24
|
|
|
use Eccube\Service\PurchaseFlow\PurchaseProcessor; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* 編集前/編集後の個数の差分にもとづいて在庫を更新します. |
28
|
|
|
*/ |
29
|
|
|
class StockDiffProcessor extends ItemHolderValidator implements PurchaseProcessor |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var ProductClassRepository |
33
|
|
|
*/ |
34
|
|
|
protected $productClassRepository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* StockProcessor constructor. |
38
|
|
|
* |
39
|
|
|
* @param ProductClassRepository $productClassRepository |
40
|
|
|
*/ |
41
|
|
|
public function __construct(ProductClassRepository $productClassRepository) |
42
|
|
|
{ |
43
|
|
|
$this->productClassRepository = $productClassRepository; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ItemHolderInterface $itemHolder |
48
|
|
|
* @param PurchaseContext $context |
49
|
|
|
* |
50
|
|
|
* @throws \Eccube\Service\PurchaseFlow\InvalidItemException |
51
|
|
|
*/ |
52
|
|
|
public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
53
|
|
|
{ |
54
|
|
|
if (is_null($context->getOriginHolder())) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$From = $context->getOriginHolder(); |
59
|
|
|
$To = $itemHolder; |
60
|
|
|
$diff = $this->getDiffOfQuantities($From, $To); |
61
|
|
|
|
62
|
|
|
foreach ($diff as $id => $quantity) { |
63
|
|
|
/** @var ProductClass $ProductClass */ |
64
|
|
|
$ProductClass = $this->productClassRepository->find($id); |
65
|
|
|
if ($ProductClass->isStockUnlimited()) { |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
$stock = $ProductClass->getStock(); |
69
|
|
|
// 更新後ステータスがキャンセルの場合は, 差分ではなく更新後の個数で確認. |
70
|
|
|
if ($To->getOrderStatus() && $To->getOrderStatus()->getId() == OrderStatus::CANCEL) { |
|
|
|
|
71
|
|
|
$Items = $To->getProductOrderItems(); |
|
|
|
|
72
|
|
|
$Items = array_filter($Items, function ($Item) use ($id) { |
73
|
|
|
return $Item->getProductClass()->getId() == $id; |
74
|
|
|
}); |
75
|
|
|
$toQuantity = array_reduce($Items, function ($quantity, $Item) { |
76
|
|
|
return $quantity += $Item->getQuantity(); |
77
|
|
|
}, 0); |
78
|
|
|
if ($stock - $toQuantity < 0) { |
79
|
|
|
$this->throwInvalidItemException(sprintf('%s: 在庫がたりません', $ProductClass->formatedProductName())); |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
if ($stock - $quantity < 0) { |
83
|
|
|
$this->throwInvalidItemException(sprintf('%s: 在庫がたりません', $ProductClass->formatedProductName())); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getDiffOfQuantities(ItemHolderInterface $From, ItemHolderInterface $To) |
90
|
|
|
{ |
91
|
|
|
$FromItems = $this->getQuantityByProductClass($From); |
92
|
|
|
$ToItems = $this->getQuantityByProductClass($To); |
93
|
|
|
$ids = array_unique(array_merge(array_keys($FromItems), array_keys($ToItems))); |
94
|
|
|
|
95
|
|
|
$diff = []; |
96
|
|
|
foreach ($ids as $id) { |
97
|
|
|
// 更新された明細 |
98
|
|
|
if (isset($FromItems[$id]) && isset($ToItems[$id])) { |
99
|
|
|
// 2 -> 3 = +1 |
|
|
|
|
100
|
|
|
// 2 -> 1 = -1 |
|
|
|
|
101
|
|
|
$diff[$id] = $ToItems[$id] - $FromItems[$id]; |
102
|
|
|
} // 削除された明細 |
103
|
|
View Code Duplication |
elseif (isset($FromItems[$id]) && empty($ToItems[$id])) { |
|
|
|
|
104
|
|
|
// 2 -> 0 = -2 |
|
|
|
|
105
|
|
|
$diff[$id] = $FromItems[$id] * -1; |
106
|
|
|
} // 追加された明細 |
107
|
|
View Code Duplication |
elseif (!isset($FromItems[$id]) && isset($ToItems[$id])) { |
|
|
|
|
108
|
|
|
// 0 -> 2 = +2 |
|
|
|
|
109
|
|
|
$diff[$id] = $ToItems[$id]; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $diff; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function getQuantityByProductClass(ItemHolderInterface $ItemHolder) |
117
|
|
|
{ |
118
|
|
|
$ItemsByProductClass = []; |
119
|
|
|
foreach ($ItemHolder->getItems() as $Item) { |
120
|
|
|
if ($Item->isProduct()) { |
121
|
|
|
$id = $Item->getProductClass()->getId(); |
122
|
|
|
if (isset($ItemsByProductClass[$id])) { |
123
|
|
|
$ItemsByProductClass[$id] += $Item->getQuantity(); |
124
|
|
|
} else { |
125
|
|
|
$ItemsByProductClass[$id] = $Item->getQuantity(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $ItemsByProductClass; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* 受注の仮確定処理を行います。 |
135
|
|
|
* |
136
|
|
|
* @param ItemHolderInterface $target |
137
|
|
|
* @param PurchaseContext $context |
138
|
|
|
* |
139
|
|
|
* @throws PurchaseException |
140
|
|
|
*/ |
141
|
|
|
public function prepare(ItemHolderInterface $target, PurchaseContext $context) |
142
|
|
|
{ |
143
|
|
|
if (is_null($context->getOriginHolder())) { |
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$diff = $this->getDiffOfQuantities($context->getOriginHolder(), $target); |
148
|
|
|
|
149
|
|
|
foreach ($diff as $id => $quantity) { |
150
|
|
|
/** @var ProductClass $ProductClass */ |
151
|
|
|
$ProductClass = $this->productClassRepository->find($id); |
152
|
|
|
if ($ProductClass->isStockUnlimited()) { |
153
|
|
|
continue; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$stock = $ProductClass->getStock() - $quantity; |
157
|
|
|
$ProductStock = $ProductClass->getProductStock(); |
158
|
|
|
if (!$ProductStock) { |
159
|
|
|
$ProductStock = new ProductStock(); |
160
|
|
|
$ProductStock->setProductClass($ProductClass); |
161
|
|
|
$ProductClass->setProductStock($ProductStock); |
162
|
|
|
} |
163
|
|
|
$ProductClass->setStock($stock); |
164
|
|
|
$ProductStock->setStock($stock); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* 受注の確定処理を行います。 |
170
|
|
|
* |
171
|
|
|
* @param ItemHolderInterface $target |
172
|
|
|
* @param PurchaseContext $context |
173
|
|
|
* |
174
|
|
|
* @throws PurchaseException |
175
|
|
|
*/ |
176
|
|
|
public function commit(ItemHolderInterface $target, PurchaseContext $context) |
177
|
|
|
{ |
178
|
|
|
// 何もしない. |
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* 仮確定した受注データの取り消し処理を行います。 |
184
|
|
|
* |
185
|
|
|
* @param ItemHolderInterface $itemHolder |
186
|
|
|
* @param PurchaseContext $context |
187
|
|
|
*/ |
188
|
|
|
public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context) |
189
|
|
|
{ |
190
|
|
|
// 何もしない. |
191
|
|
|
return; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: