|
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\BaseInfo; |
|
17
|
|
|
use Eccube\Entity\ItemHolderInterface; |
|
18
|
|
|
use Eccube\Entity\ItemInterface; |
|
19
|
|
|
use Eccube\Entity\Order; |
|
20
|
|
|
use Eccube\Repository\BaseInfoRepository; |
|
21
|
|
|
use Eccube\Service\PurchaseFlow\ItemHolderPostValidator; |
|
22
|
|
|
use Eccube\Service\PurchaseFlow\PurchaseContext; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* 加算ポイント. |
|
26
|
|
|
*/ |
|
27
|
|
|
class AddPointProcessor extends ItemHolderPostValidator |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var BaseInfo |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $BaseInfo; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* AddPointProcessor constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param BaseInfoRepository $baseInfoRepository |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(BaseInfoRepository $baseInfoRepository) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->BaseInfo = $baseInfoRepository->get(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param ItemHolderInterface $itemHolder |
|
46
|
|
|
* @param PurchaseContext $context |
|
47
|
|
|
*/ |
|
48
|
|
|
public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!$this->supports($itemHolder)) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// 付与ポイントを計算 |
|
55
|
|
|
$addPoint = $this->calculateAddPoint($itemHolder); |
|
56
|
|
|
$itemHolder->setAddPoint($addPoint); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* 付与ポイントを計算. |
|
61
|
|
|
* |
|
62
|
|
|
* @param ItemHolderInterface $itemHolder |
|
63
|
|
|
* |
|
64
|
|
|
* @return int |
|
65
|
|
|
*/ |
|
66
|
|
|
private function calculateAddPoint(ItemHolderInterface $itemHolder) |
|
67
|
|
|
{ |
|
68
|
|
|
$basicPointRate = $this->BaseInfo->getBasicPointRate(); |
|
69
|
|
|
|
|
70
|
|
|
// 明細ごとのポイントを集計 |
|
71
|
|
|
$totalPoint = array_reduce($itemHolder->getItems()->toArray(), |
|
|
|
|
|
|
72
|
|
|
function ($carry, ItemInterface $item) use ($basicPointRate) { |
|
73
|
|
|
$pointRate = $item->isProduct() ? $item->getProductClass()->getPointRate() : null; |
|
74
|
|
|
if ($pointRate === null) { |
|
75
|
|
|
$pointRate = $basicPointRate; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// TODO: ポイントは税抜き分しか割引されない、ポイント明細は税抜きのままでいいのか? |
|
79
|
|
|
$point = 0; |
|
80
|
|
|
if ($item->isPoint()) { |
|
81
|
|
|
$point = round($item->getPrice() * ($pointRate / 100)) * $item->getQuantity(); |
|
82
|
|
|
// Only calc point on product |
|
83
|
|
|
} elseif ($item->isProduct()) { |
|
84
|
|
|
// ポイント = 単価 * ポイント付与率 * 数量 |
|
85
|
|
|
$point = round($item->getPrice() * ($pointRate / 100)) * $item->getQuantity(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $carry + $point; |
|
89
|
|
|
}, 0); |
|
90
|
|
|
|
|
91
|
|
|
return $totalPoint < 0 ? 0 : $totalPoint; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Processorが実行出来るかどうかを返す. |
|
96
|
|
|
* |
|
97
|
|
|
* 以下を満たす場合に実行できる. |
|
98
|
|
|
* |
|
99
|
|
|
* - ポイント設定が有効であること. |
|
100
|
|
|
* - $itemHolderがOrderエンティティであること. |
|
101
|
|
|
* - 会員のOrderであること. |
|
102
|
|
|
* |
|
103
|
|
|
* @param ItemHolderInterface $itemHolder |
|
104
|
|
|
* |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
View Code Duplication |
private function supports(ItemHolderInterface $itemHolder) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
if (!$this->BaseInfo->isOptionPoint()) { |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (!$itemHolder instanceof Order) { |
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (!$itemHolder->getCustomer()) { |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.