1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Eccube\Service\PurchaseFlow\Processor; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\LockMode; |
6
|
|
|
use Eccube\Common\Constant; |
7
|
|
|
use Eccube\Entity\ItemInterface; |
8
|
|
|
use Eccube\Entity\ShipmentItem; |
9
|
|
|
use Eccube\Service\PurchaseFlow\ItemProcessor; |
10
|
|
|
use Eccube\Service\PurchaseFlow\ProcessResult; |
11
|
|
|
use Eccube\Service\PurchaseFlow\PurchaseContext; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* 在庫制御. |
15
|
|
|
*/ |
16
|
|
|
class StockReduceProcessor implements ItemProcessor |
17
|
|
|
{ |
18
|
|
|
private $app; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* DeliveryFeeProcessor constructor. |
22
|
|
|
* |
23
|
|
|
* @param $app |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public function __construct($app) |
26
|
|
|
{ |
27
|
|
|
$this->app = $app; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param ItemInterface $item |
32
|
|
|
* @param PurchaseContext $context |
33
|
|
|
* |
34
|
|
|
* @return ProcessResult |
35
|
|
|
* |
36
|
|
|
* @internal param ItemHolderInterface $itemHolder |
37
|
|
|
*/ |
38
|
|
|
public function process(ItemInterface $item, PurchaseContext $context) |
39
|
|
|
{ |
40
|
|
|
if (!$item instanceof ShipmentItem) { |
41
|
|
|
// ShipmentItem 以外の場合は何もしない |
42
|
|
|
return ProcessResult::success(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// 在庫処理を実装 |
46
|
|
|
// warning も作りたい |
47
|
|
|
if (!$item->isProduct()) { |
48
|
|
|
// FIXME 配送明細を考慮する必要がある |
49
|
|
|
return ProcessResult::success(); |
50
|
|
|
} |
51
|
|
|
// 在庫が無制限かチェックし、制限ありなら在庫数をチェック |
52
|
|
|
if ($item->getProductClass()->getStockUnlimited() == Constant::DISABLED) { |
53
|
|
|
// 在庫チェックあり |
54
|
|
|
// 在庫に対してロック(select ... for update)を実行 |
55
|
|
|
$productStock = $this->app['orm.em']->getRepository('Eccube\Entity\ProductStock')->find( |
56
|
|
|
$item->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE |
57
|
|
|
); |
58
|
|
|
// 購入数量と在庫数をチェックして在庫がなければエラー |
59
|
|
|
if ($productStock->getStock() < 1) { |
60
|
|
|
return ProcessResult::fail('在庫エラー'); |
|
|
|
|
61
|
|
|
} elseif ($item->getQuantity() > $productStock->getStock()) { |
62
|
|
|
return ProcessResult::fail('在庫エラー'); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return ProcessResult::success(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|