1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* another great project. |
7
|
|
|
* You can find more information about us on https://bitbag.io and write us |
8
|
|
|
* an email on [email protected]. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace BitBag\SyliusVueStorefrontPlugin\Factory\Cart\Totals; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusVueStorefrontPlugin\View\Cart\Totals\TotalSegmentView; |
16
|
|
|
use Sylius\Component\Core\Model\AdjustmentInterface; |
17
|
|
|
use Sylius\Component\Core\Model\OrderInterface as SyliusOrderInterface; |
18
|
|
|
use Sylius\Component\Core\Model\ShippingMethodInterface; |
19
|
|
|
use Sylius\Component\Order\Model\AdjustmentInterface as SyliusAdjustmentInterface; |
20
|
|
|
use Webmozart\Assert\Assert; |
21
|
|
|
|
22
|
|
|
final class TotalSegmentViewFactory implements TotalSegmentViewFactoryInterface |
23
|
|
|
{ |
24
|
|
|
/** @var string */ |
25
|
|
|
private $totalSegmentViewClass; |
26
|
|
|
|
27
|
|
|
/** @var TotalSegmentExtensionAttributeViewFactoryInterface */ |
28
|
|
|
private $totalSegmentExtensionAttributeViewFactory; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
string $totalSegmentViewClass, |
32
|
|
|
TotalSegmentExtensionAttributeViewFactoryInterface $totalSegmentExtensionAttributeViewFactory |
33
|
|
|
) { |
34
|
|
|
$this->totalSegmentViewClass = $totalSegmentViewClass; |
35
|
|
|
$this->totalSegmentExtensionAttributeViewFactory = $totalSegmentExtensionAttributeViewFactory; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function create(SyliusAdjustmentInterface $syliusAdjustment, ShippingMethodInterface $shippingMethod): TotalSegmentView |
39
|
|
|
{ |
40
|
|
|
return $this->createFromAdjustment($syliusAdjustment); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function createList(SyliusOrderInterface $syliusOrder): array |
44
|
|
|
{ |
45
|
|
|
$syliusShipments = $syliusOrder->getShipments(); |
46
|
|
|
|
47
|
|
|
Assert::lessThanEq($syliusShipments->count(), 1, sprintf('More than one shipment is currently unsupported.')); |
48
|
|
|
|
49
|
|
|
$totalSegmentsList = []; |
50
|
|
|
|
51
|
|
|
$totalSegmentsList[] = $this->createTaxSummaryView($syliusOrder); |
52
|
|
|
$totalSegmentsList[] = $this->createShippingSummaryView($syliusOrder); |
53
|
|
|
|
54
|
|
|
if ($syliusOrder->getPromotionCoupon()) { |
55
|
|
|
$totalSegmentsList[] = $this->createPromotionSummaryView($syliusOrder); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$totalSegmentsList[] = $this->createGrandTotalSummaryView($syliusOrder); |
59
|
|
|
|
60
|
|
|
return $totalSegmentsList; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function createFromAdjustment(SyliusAdjustmentInterface $syliusAdjustment): TotalSegmentView |
64
|
|
|
{ |
65
|
|
|
/** @var TotalSegmentView $totalSegmentView */ |
66
|
|
|
$totalSegmentView = new $this->totalSegmentViewClass(); |
67
|
|
|
|
68
|
|
|
switch ($syliusAdjustment->getType()) { |
69
|
|
|
case AdjustmentInterface::SHIPPING_ADJUSTMENT: |
70
|
|
|
case AdjustmentInterface::TAX_ADJUSTMENT: |
71
|
|
|
$totalSegmentView->code = $syliusAdjustment->getType(); |
72
|
|
|
|
73
|
|
|
break; |
74
|
|
|
case AdjustmentInterface::ORDER_ITEM_PROMOTION_ADJUSTMENT: |
75
|
|
|
case AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT: |
76
|
|
|
case AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT: |
77
|
|
|
$totalSegmentView->code = 'discount'; |
78
|
|
|
|
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$totalSegmentView->title = $syliusAdjustment->getLabel(); |
83
|
|
|
$totalSegmentView->value = $syliusAdjustment->getAmount(); |
84
|
|
|
|
85
|
|
|
return $totalSegmentView; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function createTaxSummaryView(SyliusOrderInterface $syliusOrder): TotalSegmentView |
89
|
|
|
{ |
90
|
|
|
/** @var TotalSegmentView $totalSegmentView */ |
91
|
|
|
$totalSegmentView = new $this->totalSegmentViewClass(); |
92
|
|
|
|
93
|
|
|
$totalSegmentView->title = TotalSegmentViewFactoryInterface::TAX_LABEL; |
94
|
|
|
$totalSegmentView->value = $syliusOrder->getTaxTotal(); |
95
|
|
|
$totalSegmentView->code = AdjustmentInterface::TAX_ADJUSTMENT; |
96
|
|
|
$totalSegmentView->area = 'taxes'; |
97
|
|
|
|
98
|
|
|
return $totalSegmentView; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function createShippingSummaryView(SyliusOrderInterface $syliusOrder): TotalSegmentView |
102
|
|
|
{ |
103
|
|
|
/** @var TotalSegmentView $totalSegmentView */ |
104
|
|
|
$totalSegmentView = new $this->totalSegmentViewClass(); |
105
|
|
|
|
106
|
|
|
$totalSegmentView->title = TotalSegmentViewFactoryInterface::SHIPPING_LABEL; |
107
|
|
|
$totalSegmentView->value = $syliusOrder->getShippingTotal(); |
108
|
|
|
$totalSegmentView->code = AdjustmentInterface::SHIPPING_ADJUSTMENT; |
109
|
|
|
|
110
|
|
|
return $totalSegmentView; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function createPromotionSummaryView(SyliusOrderInterface $syliusOrder): TotalSegmentView |
114
|
|
|
{ |
115
|
|
|
/** @var TotalSegmentView $totalSegmentView */ |
116
|
|
|
$totalSegmentView = new $this->totalSegmentViewClass(); |
117
|
|
|
|
118
|
|
|
$totalSegmentView->title = TotalSegmentViewFactoryInterface::PROMOTION_LABEL; |
119
|
|
|
$totalSegmentView->value = $syliusOrder->getOrderPromotionTotal(); |
120
|
|
|
$totalSegmentView->code = 'discount'; |
121
|
|
|
|
122
|
|
|
return $totalSegmentView; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function createGrandTotalSummaryView(SyliusOrderInterface $syliusOrder): TotalSegmentView |
126
|
|
|
{ |
127
|
|
|
/** @var TotalSegmentView $totalSegmentView */ |
128
|
|
|
$totalSegmentView = new $this->totalSegmentViewClass(); |
129
|
|
|
|
130
|
|
|
$totalSegmentView->title = TotalSegmentViewFactoryInterface::GRAND_TOTAL_LABEL; |
131
|
|
|
$totalSegmentView->value = $syliusOrder->getTotal(); |
132
|
|
|
$totalSegmentView->code = 'grand_total'; |
133
|
|
|
$totalSegmentView->area = 'footer'; |
134
|
|
|
$totalSegmentView->extension_attributes = $this->totalSegmentExtensionAttributeViewFactory->create($syliusOrder); |
135
|
|
|
|
136
|
|
|
return $totalSegmentView; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|