1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Sylius\Component\Core\Model; |
15
|
|
|
|
16
|
|
|
use Sylius\Component\Order\Model\OrderItem as BaseOrderItem; |
17
|
|
|
use Sylius\Component\Order\Model\OrderItemInterface as BaseOrderItemInterface; |
18
|
|
|
|
19
|
|
|
class OrderItem extends BaseOrderItem implements OrderItemInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ProductVariantInterface |
23
|
|
|
*/ |
24
|
|
|
protected $variant; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $productName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $variantName; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function getVariant(): ?ProductVariantInterface |
40
|
|
|
{ |
41
|
|
|
return $this->variant; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function setVariant(?ProductVariantInterface $variant): void |
48
|
|
|
{ |
49
|
|
|
$this->variant = $variant; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return ProductInterface|null |
54
|
|
|
*/ |
55
|
|
|
public function getProduct(): ?ProductInterface |
56
|
|
|
{ |
57
|
|
|
return $this->variant->getProduct(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function getProductName(): ?string |
64
|
|
|
{ |
65
|
|
|
return $this->productName ?: $this->variant->getProduct()->getName(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function setProductName(?string $productName): void |
72
|
|
|
{ |
73
|
|
|
$this->productName = $productName; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function getVariantName(): ?string |
80
|
|
|
{ |
81
|
|
|
return $this->variantName ?: $this->variant->getName(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function setVariantName(?string $variantName): void |
88
|
|
|
{ |
89
|
|
|
$this->variantName = $variantName; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function equals(BaseOrderItemInterface $item): bool |
96
|
|
|
{ |
97
|
|
|
return parent::equals($item) || ($item instanceof static && $item->getVariant() === $this->variant); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns sum of neutral and non neutral tax adjustments on order item and total tax of units. |
102
|
|
|
* |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function getTaxTotal(): int |
106
|
|
|
{ |
107
|
|
|
$taxTotal = 0; |
108
|
|
|
|
109
|
|
|
foreach ($this->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT) as $taxAdjustment) { |
110
|
|
|
$taxTotal += $taxAdjustment->getAmount(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
foreach ($this->units as $unit) { |
114
|
|
|
$taxTotal += $unit->getTaxTotal(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $taxTotal; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns single unit price lowered by order unit promotions (each unit must have the same unit promotion discount) |
122
|
|
|
* |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function getDiscountedUnitPrice(): int |
126
|
|
|
{ |
127
|
|
|
if ($this->units->isEmpty()) { |
128
|
|
|
return $this->unitPrice; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return |
132
|
|
|
$this->unitPrice + |
133
|
|
|
$this->units->first()->getAdjustmentsTotal(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT) |
134
|
|
|
; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function getSubtotal(): int |
141
|
|
|
{ |
142
|
|
|
return $this->getDiscountedUnitPrice() * $this->quantity; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|