Completed
Push — feature/eco-574/eco-2266-check... ( b72c03...e5ed68 )
by Ruslan
11s queued 10s
created

addExpensesToOrderRequestTransfer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\AfterPay\Business\Payment\Mapper;
9
10
use Generated\Shared\Transfer\AfterPayAvailablePaymentMethodsRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tMethodsRequestTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\AfterPayRequestAddressTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...yRequestAddressTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Generated\Shared\Transfer\AfterPayRequestCustomerTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...RequestCustomerTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Generated\Shared\Transfer\AfterPayRequestOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...equestOrderItemTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Generated\Shared\Transfer\AfterPayRequestOrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PayRequestOrderTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Generated\Shared\Transfer\ExpenseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ExpenseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Generated\Shared\Transfer\ItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ItemTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use SprykerEco\Shared\AfterPay\AfterPayConfig;
19
use SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface;
20
use SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToStoreFacadeInterface;
21
22
class QuoteToRequestTransfer implements QuoteToRequestTransferInterface
23
{
24
    public const NEGATIVE_MULTIPLIER = -1;
25
    public const GIFT_CARD_PROVIDER = 'GiftCard';
26
27
    /**
28
     * @var \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface
29
     */
30
    protected $moneyFacade;
31
32
    /**
33
     * @var \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToStoreFacadeInterface
34
     */
35
    protected $storeFacade;
36
37
    /**
38
     * @param \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface $moneyFacade
39
     * @param \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToStoreFacadeInterface $storeFacade
40
     */
41
    public function __construct(
42
        AfterPayToMoneyFacadeInterface $moneyFacade,
43
        AfterPayToStoreFacadeInterface $storeFacade
44
    ) {
45
        $this->moneyFacade = $moneyFacade;
46
        $this->storeFacade = $storeFacade;
47
    }
48
49
    /**
50
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
51
     *
52
     * @return \Generated\Shared\Transfer\AfterPayAvailablePaymentMethodsRequestTransfer
53
     */
54
    public function quoteToAvailablePaymentMethods(QuoteTransfer $quoteTransfer): AfterPayAvailablePaymentMethodsRequestTransfer
55
    {
56
        $requestTransfer = new AfterPayAvailablePaymentMethodsRequestTransfer();
57
58
        $requestTransfer
59
            ->setCustomer(
60
                $this->buildCustomerRequestTransfer($quoteTransfer)
61
            )
62
            ->setOrder(
63
                $this->buildOrderRequestTransfer($quoteTransfer)
64
            );
65
66
        return $requestTransfer;
67
    }
68
69
    /**
70
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
71
     *
72
     * @return \Generated\Shared\Transfer\AfterPayRequestCustomerTransfer
73
     */
74
    protected function buildCustomerRequestTransfer(QuoteTransfer $quoteTransfer): AfterPayRequestCustomerTransfer
75
    {
76
        $quoteBillingAddressTransfer = $quoteTransfer->getBillingAddress();
77
        $customerRequestTransfer = new AfterPayRequestCustomerTransfer();
78
79
        $customerRequestTransfer
80
            ->setFirstName($quoteBillingAddressTransfer->getFirstName())
81
            ->setLastName($quoteBillingAddressTransfer->getLastName())
82
            ->setConversationalLanguage($this->getStoreCountryIso2())
83
            ->setCustomerCategory(AfterPayConfig::API_CUSTOMER_CATEGORY_PERSON)
84
            ->setSalutation($quoteBillingAddressTransfer->getSalutation())
85
            ->setEmail($quoteTransfer->getCustomer()->getEmail());
86
87
        $customerRequestTransfer->setAddress(
88
            $this->buildCustomerBillingAddressRequestTransfer($quoteTransfer)
89
        );
90
91
        return $customerRequestTransfer;
92
    }
93
94
    /**
95
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
96
     *
97
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderTransfer
98
     */
99
    protected function buildOrderRequestTransfer(QuoteTransfer $quoteTransfer): AfterPayRequestOrderTransfer
100
    {
101
        $orderRequestTransfer = (new AfterPayRequestOrderTransfer())
102
            ->setTotalGrossAmount($this->getStringDecimalQuoteGrossTotal($quoteTransfer))
103
            ->setCurrency($quoteTransfer->getCurrency()->getCode());
104
105
        foreach ($quoteTransfer->getItems() as $itemTransfer) {
106
            $orderRequestTransfer->addItem(
107
                $this->buildOrderItemRequestTransfer($itemTransfer)
108
            );
109
        }
110
111
        $orderRequestTransfer = $this->addExpensesToOrderRequestTransfer($orderRequestTransfer, $quoteTransfer);
112
113
        $this->addGiftcardItems($quoteTransfer, $orderRequestTransfer);
114
115
        return $orderRequestTransfer;
116
    }
117
118
    /**
119
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
120
     *
121
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderItemTransfer
122
     */
123
    protected function buildOrderItemRequestTransfer(ItemTransfer $itemTransfer): AfterPayRequestOrderItemTransfer
124
    {
125
        $orderItemRequestTransfer = new AfterPayRequestOrderItemTransfer();
126
127
        $orderItemRequestTransfer
128
            ->setProductId($itemTransfer->getSku())
129
            ->setDescription($itemTransfer->getName())
130
            ->setGrossUnitPrice($this->getStringDecimalItemGrossUnitPrice($itemTransfer))
131
            ->setQuantity($itemTransfer->getQuantity())
132
            ->setVatAmount($this->getStringDecimalItemVatAmountPrice($itemTransfer))
133
            ->setVatPercent($itemTransfer->getTaxRate())
134
            ->setGroupId($itemTransfer->getGroupKey());
135
136
        return $orderItemRequestTransfer;
137
    }
138
139
    /**
140
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
141
     *
142
     * @return \Generated\Shared\Transfer\AfterPayRequestAddressTransfer
143
     */
144
    protected function buildCustomerBillingAddressRequestTransfer(QuoteTransfer $quoteTransfer): AfterPayRequestAddressTransfer
145
    {
146
        $customerAddressTransfer = $quoteTransfer->getBillingAddress();
147
        $customerAddressRequestTransfer = new AfterPayRequestAddressTransfer();
148
149
        $customerAddressRequestTransfer
150
            ->setCountryCode($customerAddressTransfer->getIso2Code())
151
            ->setStreet($customerAddressTransfer->getAddress1())
152
            ->setStreetNumber($customerAddressTransfer->getAddress2())
153
            ->setPostalCode($customerAddressTransfer->getZipCode())
154
            ->setPostalPlace($customerAddressTransfer->getCity());
155
156
        return $customerAddressRequestTransfer;
157
    }
158
159
    /**
160
     * @return string
161
     */
162
    protected function getStoreCountryIso2(): string
163
    {
164
        return $this->storeFacade->getCurrentStore()->getName();
165
    }
166
167
    /**
168
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
169
     *
170
     * @return string
171
     */
172
    protected function getStringDecimalQuoteGrossTotal(QuoteTransfer $quoteTransfer): string
173
    {
174
        $quoteTotal = $quoteTransfer->getTotals()->getGrandTotal();
175
        if ($quoteTransfer->getTotals()->getPriceToPay()) {
176
            $quoteTotal = $quoteTransfer->getTotals()->getPriceToPay();
177
        }
178
179
        return (string)$this->moneyFacade->convertIntegerToDecimal($quoteTotal);
180
    }
181
182
    /**
183
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
184
     *
185
     * @return string
186
     */
187
    protected function getStringDecimalQuoteNetTotal(QuoteTransfer $quoteTransfer): string
188
    {
189
        $quoteGrossTotal = $quoteTransfer->getTotals()->getGrandTotal();
190
        $quoteTaxTotal = $quoteTransfer->getTotals()->getTaxTotal()->getAmount();
191
        $quoteNetTotal = $quoteGrossTotal - $quoteTaxTotal;
192
193
        return (string)$this->moneyFacade->convertIntegerToDecimal($quoteNetTotal);
194
    }
195
196
    /**
197
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
198
     *
199
     * @return string
200
     */
201
    protected function getStringDecimalItemGrossUnitPrice(ItemTransfer $itemTransfer): string
202
    {
203
        $itemUnitGrossPrice = $itemTransfer->getUnitPriceToPayAggregation();
204
205
        return (string)$this->moneyFacade->convertIntegerToDecimal($itemUnitGrossPrice);
206
    }
207
208
    /**
209
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
210
     *
211
     * @return string
212
     */
213
    protected function getStringDecimalItemNetUnitPrice(ItemTransfer $itemTransfer): string
214
    {
215
        $itemUnitGrossPriceAmount = $itemTransfer->getUnitPriceToPayAggregation();
216
        $itemUnitTaxAmount = $itemTransfer->getUnitTaxAmountFullAggregation();
217
        $itemUnitNetAmount = $itemUnitGrossPriceAmount - $itemUnitTaxAmount;
218
219
        return (string)$this->moneyFacade->convertIntegerToDecimal($itemUnitNetAmount);
220
    }
221
222
    /**
223
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
224
     *
225
     * @return string
226
     */
227
    protected function getStringDecimalItemVatAmountPrice(ItemTransfer $itemTransfer): string
228
    {
229
        $itemVatAmountPrice = $itemTransfer->getUnitTaxAmountFullAggregation();
230
231
        return (string)$this->moneyFacade->convertIntegerToDecimal($itemVatAmountPrice);
232
    }
233
234
    /**
235
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
236
     * @param \Generated\Shared\Transfer\AfterPayRequestOrderTransfer $orderRequestTransfer
237
     *
238
     * @return void
239
     */
240
    protected function addGiftcardItems(
241
        QuoteTransfer $quoteTransfer,
242
        AfterPayRequestOrderTransfer $orderRequestTransfer
243
    ): void {
244
        foreach ($this->getGiftcards($quoteTransfer) as $index => $paymentTransfer) {
245
            $orderItemRequestTransfer = new AfterPayRequestOrderItemTransfer();
246
            $amount = (string)$this->moneyFacade->convertIntegerToDecimal(static::NEGATIVE_MULTIPLIER * $paymentTransfer->getAmount());
247
248
            $orderItemRequestTransfer
249
                ->setProductId(static::GIFT_CARD_PROVIDER . $index)
250
                ->setDescription(static::GIFT_CARD_PROVIDER . $index)
251
                ->setGrossUnitPrice($amount)
252
                ->setQuantity(1);
253
254
            $orderRequestTransfer->addItem($orderItemRequestTransfer);
255
        }
256
    }
257
258
    /**
259
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
260
     *
261
     * @return \Generated\Shared\Transfer\PaymentTransfer[]
262
     */
263
    protected function getGiftcards(QuoteTransfer $quoteTransfer): array
264
    {
265
        $giftCardPayments = [];
266
        foreach ($quoteTransfer->getPayments() as $paymentTransfer) {
267
            if ($paymentTransfer->getPaymentMethod() !== static::GIFT_CARD_PROVIDER) {
268
                continue;
269
            }
270
271
            $giftCardPayments[] = $paymentTransfer;
272
        }
273
274
        return $giftCardPayments;
275
    }
276
277
    /**
278
     * @param \Generated\Shared\Transfer\AfterPayRequestOrderTransfer $orderRequestTransfer
279
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
280
     *
281
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderTransfer
282
     */
283
    protected function addExpensesToOrderRequestTransfer(
284
        AfterPayRequestOrderTransfer $orderRequestTransfer,
285
        QuoteTransfer $quoteTransfer
286
    ): AfterPayRequestOrderTransfer {
287
        foreach ($quoteTransfer->getExpenses() as $expenseTransfer) {
288
            if ($expenseTransfer->getSumPriceToPayAggregation() > 0) {
289
                $orderRequestTransfer->addItem(
290
                    $this->buildOrderExpenseRequestTransfer($expenseTransfer)
291
                );
292
            }
293
        }
294
295
        return $orderRequestTransfer;
296
    }
297
298
    /**
299
     * @param \Generated\Shared\Transfer\ExpenseTransfer $expenseTransfer
300
     *
301
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderItemTransfer
302
     */
303
    protected function buildOrderExpenseRequestTransfer(ExpenseTransfer $expenseTransfer): AfterPayRequestOrderItemTransfer
304
    {
305
        return (new AfterPayRequestOrderItemTransfer())
306
            ->setProductId($expenseTransfer->getType())
307
            ->setDescription($expenseTransfer->getName())
308
            ->setGrossUnitPrice($this->getStringDecimalExpenseGrossUnitPrice($expenseTransfer))
309
            ->setNetUnitPrice($this->getStringDecimalExpenseNetUnitPrice($expenseTransfer))
310
            ->setQuantity($expenseTransfer->getQuantity());
311
    }
312
313
    /**
314
     * @param \Generated\Shared\Transfer\ExpenseTransfer $expenseTransfer
315
     *
316
     * @return string
317
     */
318
    protected function getStringDecimalExpenseGrossUnitPrice(ExpenseTransfer $expenseTransfer): string
319
    {
320
        $expenseUnitGrossPrice = $expenseTransfer->getUnitPriceToPayAggregation();
321
322
        return (string)$this->moneyFacade->convertIntegerToDecimal($expenseUnitGrossPrice);
323
    }
324
325
    /**
326
     * @param \Generated\Shared\Transfer\ExpenseTransfer $expenseTransfer
327
     *
328
     * @return string
329
     */
330
    protected function getStringDecimalExpenseNetUnitPrice(ExpenseTransfer $expenseTransfer): string
331
    {
332
        $expenseUnitGrossPriceAmount = $expenseTransfer->getUnitPriceToPayAggregation();
333
        $expenseUnitTaxAmount = $expenseTransfer->getUnitTaxAmount();
334
        $expenseUnitNetAmount = $expenseUnitGrossPriceAmount - $expenseUnitTaxAmount;
335
336
        return (string)$this->moneyFacade->convertIntegerToDecimal($expenseUnitNetAmount);
337
    }
338
}
339