1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Zed\MerchantRelationshipSalesOrderThreshold\Business\MerchantRelationshipThreshold; |
||
9 | |||
10 | use Generated\Shared\Transfer\CurrencyTransfer; |
||
11 | use Generated\Shared\Transfer\ItemTransfer; |
||
12 | use Generated\Shared\Transfer\QuoteTransfer; |
||
13 | use Generated\Shared\Transfer\StoreTransfer; |
||
14 | use Spryker\Zed\MerchantRelationshipSalesOrderThreshold\Business\Translation\MerchantRelationshipSalesOrderThresholdTranslationReaderInterface; |
||
15 | use Spryker\Zed\MerchantRelationshipSalesOrderThreshold\Persistence\MerchantRelationshipSalesOrderThresholdRepositoryInterface; |
||
16 | |||
17 | class MerchantRelationshipThresholdReader implements MerchantRelationshipThresholdReaderInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var \Spryker\Zed\MerchantRelationshipSalesOrderThreshold\Persistence\MerchantRelationshipSalesOrderThresholdRepositoryInterface |
||
21 | */ |
||
22 | protected $merchantRelationshipSalesOrderThresholdRepository; |
||
23 | |||
24 | /** |
||
25 | * @var \Spryker\Zed\MerchantRelationshipSalesOrderThreshold\Business\Translation\MerchantRelationshipSalesOrderThresholdTranslationReaderInterface |
||
26 | */ |
||
27 | protected $translationReader; |
||
28 | |||
29 | /** |
||
30 | * @param \Spryker\Zed\MerchantRelationshipSalesOrderThreshold\Persistence\MerchantRelationshipSalesOrderThresholdRepositoryInterface $merchantRelationshipSalesOrderThresholdRepository |
||
31 | * @param \Spryker\Zed\MerchantRelationshipSalesOrderThreshold\Business\Translation\MerchantRelationshipSalesOrderThresholdTranslationReaderInterface $translationReader |
||
32 | */ |
||
33 | public function __construct( |
||
34 | MerchantRelationshipSalesOrderThresholdRepositoryInterface $merchantRelationshipSalesOrderThresholdRepository, |
||
35 | MerchantRelationshipSalesOrderThresholdTranslationReaderInterface $translationReader |
||
36 | ) { |
||
37 | $this->merchantRelationshipSalesOrderThresholdRepository = $merchantRelationshipSalesOrderThresholdRepository; |
||
38 | $this->translationReader = $translationReader; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
||
43 | * |
||
44 | * @return array<\Generated\Shared\Transfer\SalesOrderThresholdValueTransfer> |
||
45 | */ |
||
46 | public function findApplicableThresholds(QuoteTransfer $quoteTransfer): array |
||
47 | { |
||
48 | $customerMerchantRelationships = $this->getCustomerMerchantRelationships($quoteTransfer); |
||
49 | |||
50 | if (!$customerMerchantRelationships) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
51 | return []; |
||
52 | } |
||
53 | |||
54 | $itemMerchantRelationshipSubTotals = $this->getItemsMerchantRelationshipSubTotals($quoteTransfer); |
||
55 | |||
56 | if (!$itemMerchantRelationshipSubTotals) { |
||
57 | return []; |
||
58 | } |
||
59 | |||
60 | $cartMerchantRelationshipIds = $this->getCartMerchantRelationshipIds($customerMerchantRelationships, $itemMerchantRelationshipSubTotals); |
||
61 | |||
62 | $this->assertRequiredAttributes($quoteTransfer); |
||
63 | $merchantRelationshipSalesOrderThresholdTransfers = $this->merchantRelationshipSalesOrderThresholdRepository |
||
64 | ->getMerchantRelationshipSalesOrderThresholds( |
||
65 | $quoteTransfer->getStore(), |
||
66 | $quoteTransfer->getCurrency(), |
||
67 | $cartMerchantRelationshipIds, |
||
68 | ); |
||
69 | |||
70 | return $this->getSalesOrderThresholdTransfers($merchantRelationshipSalesOrderThresholdTransfers, $itemMerchantRelationshipSubTotals); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | protected function assertRequiredAttributes(QuoteTransfer $quoteTransfer): void |
||
79 | { |
||
80 | $quoteTransfer |
||
81 | ->requireStore() |
||
82 | ->requireCurrency(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param \Generated\Shared\Transfer\StoreTransfer $storeTransfer |
||
87 | * @param \Generated\Shared\Transfer\CurrencyTransfer $currencyTransfer |
||
88 | * @param array<int> $merchantRelationshipIds |
||
89 | * |
||
90 | * @return array<\Generated\Shared\Transfer\MerchantRelationshipSalesOrderThresholdTransfer> |
||
91 | */ |
||
92 | public function getMerchantRelationshipSalesOrderThresholds( |
||
93 | StoreTransfer $storeTransfer, |
||
94 | CurrencyTransfer $currencyTransfer, |
||
95 | array $merchantRelationshipIds |
||
96 | ): array { |
||
97 | $merchantRelationshipSalesOrderThresholdTransfers = $this->merchantRelationshipSalesOrderThresholdRepository |
||
98 | ->getMerchantRelationshipSalesOrderThresholds( |
||
99 | $storeTransfer, |
||
100 | $currencyTransfer, |
||
101 | $merchantRelationshipIds, |
||
102 | ); |
||
103 | |||
104 | foreach ($merchantRelationshipSalesOrderThresholdTransfers as $merchantRelationshipSalesOrderThresholdTransfer) { |
||
105 | $this->translationReader->hydrateLocalizedMessages($merchantRelationshipSalesOrderThresholdTransfer); |
||
106 | } |
||
107 | |||
108 | return $merchantRelationshipSalesOrderThresholdTransfers; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
||
113 | * |
||
114 | * @return array<\Generated\Shared\Transfer\MerchantRelationshipTransfer> |
||
115 | */ |
||
116 | protected function getCustomerMerchantRelationships(QuoteTransfer $quoteTransfer): array |
||
117 | { |
||
118 | if ($this->haveCustomerMerchantRelationships($quoteTransfer)) { |
||
119 | return $quoteTransfer->getCustomer()->getCompanyUserTransfer()->getCompanyBusinessUnit()->getMerchantRelationships()->getArrayCopy(); |
||
120 | } |
||
121 | |||
122 | return []; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | protected function haveCustomerMerchantRelationships(QuoteTransfer $quoteTransfer): bool |
||
131 | { |
||
132 | return $quoteTransfer->getCustomer() && |
||
133 | $quoteTransfer->getCustomer()->getCompanyUserTransfer() && |
||
134 | $quoteTransfer->getCustomer()->getCompanyUserTransfer()->getCompanyBusinessUnit() && |
||
135 | $quoteTransfer->getCustomer()->getCompanyUserTransfer()->getCompanyBusinessUnit()->getMerchantRelationships()->count(); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
||
140 | * |
||
141 | * @return array<int> |
||
142 | */ |
||
143 | protected function getItemsMerchantRelationshipSubTotals(QuoteTransfer $quoteTransfer): array |
||
144 | { |
||
145 | $itemMerchantRelationshipSubTotals = []; |
||
146 | foreach ($quoteTransfer->getItems() as $key => $itemTransfer) { |
||
147 | if (!$this->isMerchantRelationshipItem($itemTransfer)) { |
||
148 | continue; |
||
149 | } |
||
150 | |||
151 | $itemTransfer->requireSumSubtotalAggregation(); |
||
152 | $itemIdMerchantRelationship = $itemTransfer->getPriceProduct()->getPriceDimension()->getIdMerchantRelationship(); |
||
153 | $itemMerchantRelationshipSubTotals[$itemIdMerchantRelationship] = $itemMerchantRelationshipSubTotals[$itemIdMerchantRelationship] ?? 0; |
||
154 | $itemMerchantRelationshipSubTotals[$itemIdMerchantRelationship] += $itemTransfer->getSumSubtotalAggregation(); |
||
155 | } |
||
156 | |||
157 | return $itemMerchantRelationshipSubTotals; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer |
||
162 | * |
||
163 | * @return bool |
||
164 | */ |
||
165 | protected function isMerchantRelationshipItem(ItemTransfer $itemTransfer): bool |
||
166 | { |
||
167 | return $itemTransfer->getPriceProduct() && |
||
168 | $itemTransfer->getPriceProduct()->getPriceDimension() && |
||
169 | $itemTransfer->getPriceProduct()->getPriceDimension()->getIdMerchantRelationship(); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param array<\Generated\Shared\Transfer\MerchantRelationshipTransfer> $customerMerchantRelationships |
||
174 | * @param array<int> $itemMerchantRelationshipSubTotals |
||
175 | * |
||
176 | * @return array<int> |
||
177 | */ |
||
178 | public function getCartMerchantRelationshipIds(array $customerMerchantRelationships, array $itemMerchantRelationshipSubTotals): array |
||
179 | { |
||
180 | $cartMerchantRelationshipIds = []; |
||
181 | foreach ($customerMerchantRelationships as $merchantRelationshipTransfer) { |
||
182 | if (isset($itemMerchantRelationshipSubTotals[$merchantRelationshipTransfer->getIdMerchantRelationship()])) { |
||
183 | $cartMerchantRelationshipIds[$merchantRelationshipTransfer->getIdMerchantRelationship()] = $merchantRelationshipTransfer->getIdMerchantRelationship(); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | return $cartMerchantRelationshipIds; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param array<\Generated\Shared\Transfer\MerchantRelationshipSalesOrderThresholdTransfer> $merchantRelationshipSalesOrderThresholdTransfers |
||
192 | * @param array<int> $itemMerchantRelationshipSubTotals |
||
193 | * |
||
194 | * @return array<\Generated\Shared\Transfer\SalesOrderThresholdValueTransfer> |
||
195 | */ |
||
196 | protected function getSalesOrderThresholdTransfers( |
||
197 | array $merchantRelationshipSalesOrderThresholdTransfers, |
||
198 | array $itemMerchantRelationshipSubTotals |
||
199 | ): array { |
||
200 | $salesOrderThresholdTransfers = []; |
||
201 | foreach ($merchantRelationshipSalesOrderThresholdTransfers as $merchantRelationshipSalesOrderThresholdTransfer) { |
||
202 | $salesOrderThresholdTransfer = $merchantRelationshipSalesOrderThresholdTransfer->getSalesOrderThresholdValue(); |
||
203 | $salesOrderThresholdTransfer->setValue( |
||
204 | $itemMerchantRelationshipSubTotals[$merchantRelationshipSalesOrderThresholdTransfer->getMerchantRelationship()->getIdMerchantRelationship()], |
||
205 | ); |
||
206 | $salesOrderThresholdTransfers[] = $salesOrderThresholdTransfer; |
||
207 | } |
||
208 | |||
209 | return $salesOrderThresholdTransfers; |
||
210 | } |
||
211 | } |
||
212 |