1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Eccube\Service\PurchaseFlow; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Eccube\Entity\ItemInterface; |
8
|
|
|
use Eccube\Entity\Master\OrderItemType; |
9
|
|
|
use Eccube\Entity\Order; |
10
|
|
|
|
11
|
|
|
class ItemCollection extends ArrayCollection |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
protected $type; |
14
|
|
|
|
15
|
197 |
|
public function __construct($Items, $type = null) |
16
|
|
|
{ |
17
|
197 |
|
$this->type = is_null($type) ? Order::class : $type; |
18
|
|
|
|
19
|
197 |
|
if ($Items instanceof Collection) { |
20
|
197 |
|
$Items = $Items->toArray(); |
21
|
|
|
} |
22
|
197 |
|
parent::__construct($Items); |
23
|
|
|
} |
24
|
|
|
|
25
|
191 |
|
public function reduce(\Closure $func, $initial = null) |
26
|
|
|
{ |
27
|
191 |
|
return array_reduce($this->toArray(), $func, $initial); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// 明細種別ごとに返すメソッド作る |
31
|
188 |
|
public function getProductClasses() |
|
|
|
|
32
|
|
|
{ |
33
|
188 |
|
return $this->filter( |
34
|
188 |
|
function (ItemInterface $OrderItem) { |
35
|
184 |
|
return $OrderItem->isProduct(); |
36
|
188 |
|
}); |
37
|
|
|
} |
38
|
|
|
|
39
|
188 |
|
public function getDeliveryFees() |
40
|
|
|
{ |
41
|
188 |
|
return $this->filter( |
42
|
188 |
|
function (ItemInterface $OrderItem) { |
43
|
184 |
|
return $OrderItem->isDeliveryFee(); |
44
|
188 |
|
}); |
45
|
|
|
} |
46
|
|
|
|
47
|
188 |
|
public function getCharges() |
48
|
|
|
{ |
49
|
188 |
|
return $this->filter( |
50
|
188 |
|
function (ItemInterface $OrderItem) { |
51
|
184 |
|
return $OrderItem->isCharge(); |
52
|
188 |
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
188 |
|
public function getDiscounts() |
56
|
|
|
{ |
57
|
188 |
|
return $this->filter( |
58
|
188 |
|
function (ItemInterface $OrderItem) { |
59
|
184 |
|
return $OrderItem->isDiscount(); |
60
|
188 |
|
}); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* 同名の明細が存在するかどうか. |
65
|
|
|
* |
66
|
|
|
* TODO 暫定対応. 本来は明細種別でチェックする. |
67
|
|
|
*/ |
|
|
|
|
68
|
|
View Code Duplication |
public function hasProductByName($productName) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$OrderItems = $this->filter( |
71
|
|
|
function (ItemInterface $OrderItem) use ($productName) { |
72
|
|
|
/* @var OrderItem $OrderItem */ |
73
|
|
|
return $OrderItem->getProductName() == $productName; |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
return !$OrderItems->isEmpty(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 指定した受注明細区分の明細が存在するかどうか. |
81
|
|
|
* |
82
|
|
|
* @param OrderItemType $OrderItemType 受注区分 |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function hasItemByOrderItemType($OrderItemType) |
|
|
|
|
87
|
|
|
{ |
88
|
1 |
|
$filteredItems = $this->filter(function (ItemInterface $OrderItem) use ($OrderItemType) { |
89
|
|
|
/* @var OrderItem $OrderItem */ |
90
|
1 |
|
return $OrderItem->getOrderItemType() && $OrderItem->getOrderItemType()->getId() == $OrderItemType->getId(); |
91
|
1 |
|
}); |
92
|
|
|
|
93
|
1 |
|
return !$filteredItems->isEmpty(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getType() |
97
|
|
|
{ |
98
|
|
|
return $this->type; |
99
|
|
|
} |
100
|
|
|
|
101
|
197 |
|
public function sort() |
102
|
|
|
{ |
103
|
197 |
|
$Items = $this->toArray(); |
104
|
197 |
|
usort($Items, function (ItemInterface $a, ItemInterface $b) { |
105
|
161 |
|
if ($a->getOrderItemType() === $b->getOrderItemType()) { |
106
|
12 |
|
return ($a->getId() < $b->getId()) ? -1 : 1; |
107
|
160 |
|
} elseif ($a->isProduct()) { |
108
|
160 |
|
return -1; |
109
|
130 |
|
} elseif ($a->isDeliveryFee()) { |
110
|
130 |
|
if ($b->isProduct()) { |
111
|
|
|
return 1; |
112
|
|
|
} |
113
|
|
|
|
114
|
130 |
|
return -1; |
115
|
130 |
|
} elseif ($a->isCharge()) { |
116
|
130 |
|
if ($b->isDeliveryFee() || $b->isProduct()) { |
117
|
2 |
|
return 1; |
118
|
|
|
} |
119
|
|
|
|
120
|
130 |
|
return -1; |
121
|
3 |
|
} elseif ($a->isDiscount()) { |
122
|
3 |
|
if (!$b->isTax()) { |
123
|
3 |
|
return 1; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return -1; |
127
|
|
|
} elseif ($a->isTax()) { |
128
|
|
|
return 1; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return 0; |
132
|
197 |
|
}); |
133
|
|
|
|
134
|
197 |
|
return new self($Items); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|