getStringDecimalItemVatAmountPrice()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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\AfterPayAuthorizeRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...uthorizeRequestTransfer 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\AfterPayCallTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\AfterPayCallTransfer 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\AfterPayCancelRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ayCancelRequestTransfer 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\AfterPayCaptureRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...yCaptureRequestTransfer 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\AfterPayRefundRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ayRefundRequestTransfer 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\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...
16
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...
17
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...
18
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...
19
use Generated\Shared\Transfer\AfterPayRequestPaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...yRequestPaymentTransfer 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...
20
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...
21
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...
22
use Generated\Shared\Transfer\OrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer 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...
23
use SprykerEco\Shared\AfterPay\AfterPayConfig as AfterPayConfigShared;
24
use SprykerEco\Zed\AfterPay\AfterPayConfig as AfterPayConfigZed;
25
use SprykerEco\Zed\AfterPay\Business\Payment\Transaction\PriceToPayProviderInterface;
26
use SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface;
27
use SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToStoreFacadeInterface;
28
29
class OrderToRequestTransfer implements OrderToRequestTransferInterface
30
{
31
    public const NEGATIVE_MULTIPLIER = -1;
32
33
    /**
34
     * @var string
35
     */
36
    public const GIFT_CARD_PROVIDER = 'GiftCard';
37
38
    /**
39
     * @var string
40
     */
41
    protected const ZERO_AMOUNT = '0';
42
43
    /**
44
     * @var int
45
     */
46
    protected const ORDER_ITEM_QUANTITY = 1;
47
48
    /**
49
     * @var string
50
     */
51
    protected const SHIPPING_FEE_PROVIDER = 'ShippingFee';
52
53
    /**
54
     * @var \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface
55
     */
56
    protected $moneyFacade;
57
58
    /**
59
     * @var \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToStoreFacadeInterface
60
     */
61
    protected $storeFacade;
62
63
    /**
64
     * @var array
65
     */
66
    protected static $paymentMethods = [
67
        AfterPayConfigShared::PAYMENT_METHOD_INVOICE => AfterPayConfigShared::PAYMENT_TYPE_INVOICE,
68
    ];
69
70
    /**
71
     * @var \SprykerEco\Zed\AfterPay\Business\Payment\Transaction\PriceToPayProviderInterface
72
     */
73
    protected $priceToPayProvider;
74
75
    /**
76
     * @var \SprykerEco\Zed\AfterPay\AfterPayConfig
77
     */
78
    protected $config;
79
80
    /**
81
     * @param \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToMoneyFacadeInterface $moneyFacade
82
     * @param \SprykerEco\Zed\AfterPay\Dependency\Facade\AfterPayToStoreFacadeInterface $storeFacade
83
     * @param \SprykerEco\Zed\AfterPay\Business\Payment\Transaction\PriceToPayProviderInterface $priceToPayProvider
84
     * @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config
85
     */
86
    public function __construct(
87
        AfterPayToMoneyFacadeInterface $moneyFacade,
88
        AfterPayToStoreFacadeInterface $storeFacade,
89
        PriceToPayProviderInterface $priceToPayProvider,
90
        AfterPayConfigZed $config
91
    ) {
92
        $this->moneyFacade = $moneyFacade;
93
        $this->storeFacade = $storeFacade;
94
        $this->priceToPayProvider = $priceToPayProvider;
95
        $this->config = $config;
96
    }
97
98
    /**
99
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
100
     *
101
     * @return \Generated\Shared\Transfer\AfterPayAuthorizeRequestTransfer
102
     */
103
    public function orderToAuthorizeRequest(AfterPayCallTransfer $afterPayCallTransfer): AfterPayAuthorizeRequestTransfer
104
    {
105
        return (new AfterPayAuthorizeRequestTransfer())
106
            ->setPayment($this->buildPaymentRequestTransfer($afterPayCallTransfer))
107
            ->setCustomer($this->buildCustomerRequestTransfer($afterPayCallTransfer))
108
            ->setOrder($this->buildOrderWithItemsRequestTransfer($afterPayCallTransfer));
109
    }
110
111
    /**
112
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
113
     *
114
     * @return \Generated\Shared\Transfer\AfterPayCaptureRequestTransfer
115
     */
116
    public function orderToBaseCaptureRequest(AfterPayCallTransfer $afterPayCallTransfer): AfterPayCaptureRequestTransfer
117
    {
118
        $orderRequestTransfer = $this->buildOrderRequestTransfer($afterPayCallTransfer)
119
            ->setTotalGrossAmount(static::ZERO_AMOUNT)
120
            ->setTotalNetAmount(static::ZERO_AMOUNT);
121
122
        return (new AfterPayCaptureRequestTransfer())
123
            ->setOrderDetails($orderRequestTransfer);
124
    }
125
126
    /**
127
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
128
     *
129
     * @return \Generated\Shared\Transfer\AfterPayCancelRequestTransfer
130
     */
131
    public function orderToBaseCancelRequest(AfterPayCallTransfer $afterPayCallTransfer): AfterPayCancelRequestTransfer
132
    {
133
        $orderRequestTransfer = $this->buildOrderRequestTransfer($afterPayCallTransfer)
134
            ->setTotalGrossAmount(static::ZERO_AMOUNT)
135
            ->setTotalNetAmount(static::ZERO_AMOUNT);
136
137
        return (new AfterPayCancelRequestTransfer())
138
            ->setCancellationDetails($orderRequestTransfer);
139
    }
140
141
    /**
142
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
143
     *
144
     * @return \Generated\Shared\Transfer\AfterPayRefundRequestTransfer
145
     */
146
    public function orderToBaseRefundRequest(OrderTransfer $orderTransfer): AfterPayRefundRequestTransfer
147
    {
148
        return (new AfterPayRefundRequestTransfer())
149
            ->setIdSalesOrder($orderTransfer->getIdSalesOrder())
150
            ->setOrderNumber($orderTransfer->getOrderReference());
151
    }
152
153
    /**
154
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
155
     *
156
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderItemTransfer
157
     */
158
    public function orderItemToAfterPayItemRequest(ItemTransfer $itemTransfer): AfterPayRequestOrderItemTransfer
159
    {
160
        return $this->buildOrderItemRequestTransfer($itemTransfer);
161
    }
162
163
    /**
164
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
165
     *
166
     * @return \Generated\Shared\Transfer\AfterPayRequestCustomerTransfer
167
     */
168
    protected function buildCustomerRequestTransfer(AfterPayCallTransfer $afterPayCallTransfer): AfterPayRequestCustomerTransfer
169
    {
170
        $billingAddressTransfer = $afterPayCallTransfer->getBillingAddress();
171
172
        return (new AfterPayRequestCustomerTransfer())
173
            ->setFirstName($billingAddressTransfer->getFirstName())
174
            ->setLastName($billingAddressTransfer->getLastName())
175
            ->setConversationalLanguage($this->getStoreCountryIso2())
176
            ->setCustomerCategory(AfterPayConfigShared::API_CUSTOMER_CATEGORY_PERSON)
177
            ->setSalutation($this->config->getSalutation($billingAddressTransfer->getSalutation()))
178
            ->setEmail($afterPayCallTransfer->getEmail())
179
            ->setAddress($this->buildCustomerBillingAddressRequestTransfer($afterPayCallTransfer));
180
    }
181
182
    /**
183
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
184
     *
185
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderTransfer
186
     */
187
    protected function buildOrderWithItemsRequestTransfer(AfterPayCallTransfer $afterPayCallTransfer): AfterPayRequestOrderTransfer
188
    {
189
        $orderRequestTransfer = $this->buildOrderRequestTransfer($afterPayCallTransfer);
190
191
        foreach ($afterPayCallTransfer->getItems() as $itemTransfer) {
192
            $orderRequestTransfer->addItem(
193
                $this->buildOrderItemRequestTransfer($itemTransfer),
194
            );
195
        }
196
197
        $orderRequestTransfer = $this->addExpensesToOrderRequestTransfer($orderRequestTransfer, $afterPayCallTransfer);
198
        $orderRequestTransfer = $this->addGiftcardItems($orderRequestTransfer, $afterPayCallTransfer);
199
200
        return $orderRequestTransfer;
201
    }
202
203
    /**
204
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
205
     *
206
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderTransfer
207
     */
208
    protected function buildOrderRequestTransfer(AfterPayCallTransfer $afterPayCallTransfer): AfterPayRequestOrderTransfer
209
    {
210
        return (new AfterPayRequestOrderTransfer())
211
            ->setNumber($afterPayCallTransfer->getOrderReference())
212
            ->setTotalNetAmount($this->getStringDecimalOrderNetTotal($afterPayCallTransfer))
213
            ->setTotalGrossAmount($this->getStringDecimalOrderGrossTotal($afterPayCallTransfer))
214
            ->setCurrency($afterPayCallTransfer->getCurrency());
215
    }
216
217
    /**
218
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
219
     *
220
     * @return \Generated\Shared\Transfer\AfterPayRequestPaymentTransfer
221
     */
222
    protected function buildPaymentRequestTransfer(AfterPayCallTransfer $afterPayCallTransfer): AfterPayRequestPaymentTransfer
223
    {
224
        return (new AfterPayRequestPaymentTransfer())
225
            ->setType(static::$paymentMethods[$afterPayCallTransfer->getPaymentMethod()] ?? null);
226
    }
227
228
    /**
229
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
230
     *
231
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderItemTransfer
232
     */
233
    protected function buildOrderItemRequestTransfer(ItemTransfer $itemTransfer): AfterPayRequestOrderItemTransfer
234
    {
235
        return (new AfterPayRequestOrderItemTransfer())
236
            ->setProductId($itemTransfer->getSku())
237
            ->setDescription($itemTransfer->getName())
238
            ->setGrossUnitPrice($this->getStringDecimalItemGrossUnitPrice($itemTransfer))
239
            ->setQuantity($itemTransfer->getQuantity())
240
            ->setNetUnitPrice($this->getStringDecimalItemNetUnitPrice($itemTransfer))
241
            ->setVatAmount($this->getStringDecimalItemVatAmountPrice($itemTransfer))
242
            ->setVatPercent($itemTransfer->getTaxRate())
243
            ->setGroupId($itemTransfer->getGroupKey());
244
    }
245
246
    /**
247
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
248
     *
249
     * @return \Generated\Shared\Transfer\AfterPayRequestAddressTransfer
250
     */
251
    protected function buildCustomerBillingAddressRequestTransfer(AfterPayCallTransfer $afterPayCallTransfer): AfterPayRequestAddressTransfer
252
    {
253
        $customerAddressTransfer = $afterPayCallTransfer->getBillingAddress();
254
255
        return (new AfterPayRequestAddressTransfer())
256
            ->setCountryCode($customerAddressTransfer->getIso2Code())
257
            ->setStreet($customerAddressTransfer->getAddress1())
258
            ->setStreetNumber($customerAddressTransfer->getAddress2())
259
            ->setPostalCode($customerAddressTransfer->getZipCode())
260
            ->setPostalPlace($customerAddressTransfer->getCity());
261
    }
262
263
    /**
264
     * @return string
265
     */
266
    protected function getStoreCountryIso2(): string
267
    {
268
        return $this->storeFacade->getCurrentStore()->getName();
269
    }
270
271
    /**
272
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
273
     *
274
     * @return string
275
     */
276
    protected function getStringDecimalOrderGrossTotal(AfterPayCallTransfer $afterPayCallTransfer): string
277
    {
278
        $orderGrossTotal = $afterPayCallTransfer->getTotals()->getGrandTotal();
279
280
        return (string)$this->moneyFacade->convertIntegerToDecimal($orderGrossTotal);
281
    }
282
283
    /**
284
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
285
     *
286
     * @return string
287
     */
288
    protected function getStringDecimalOrderNetTotal(AfterPayCallTransfer $afterPayCallTransfer): string
289
    {
290
        $orderGrossTotal = $afterPayCallTransfer->getTotals()->getGrandTotal();
291
        $orderTaxTotal = $afterPayCallTransfer->getTotals()->getTaxTotal()->getAmount();
292
        $orderNetTotal = $orderGrossTotal - $orderTaxTotal;
293
294
        return (string)$this->moneyFacade->convertIntegerToDecimal($orderNetTotal);
295
    }
296
297
    /**
298
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
299
     *
300
     * @return string
301
     */
302
    protected function getStringDecimalItemGrossUnitPrice(ItemTransfer $itemTransfer): string
303
    {
304
        $itemUnitGrossPrice = $itemTransfer->getUnitPriceToPayAggregation();
305
306
        return (string)$this->moneyFacade->convertIntegerToDecimal($itemUnitGrossPrice);
307
    }
308
309
    /**
310
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
311
     *
312
     * @return string
313
     */
314
    protected function getStringDecimalItemNetUnitPrice(ItemTransfer $itemTransfer): string
315
    {
316
        $itemUnitGrossPriceAmount = $itemTransfer->getUnitPriceToPayAggregation();
317
        $itemUnitTaxAmount = $itemTransfer->getUnitTaxAmountFullAggregation();
318
        $itemUnitNetAmount = $itemUnitGrossPriceAmount - $itemUnitTaxAmount;
319
320
        return (string)$this->moneyFacade->convertIntegerToDecimal($itemUnitNetAmount);
321
    }
322
323
    /**
324
     * @param \Generated\Shared\Transfer\ItemTransfer $itemTransfer
325
     *
326
     * @return string
327
     */
328
    protected function getStringDecimalItemVatAmountPrice(ItemTransfer $itemTransfer): string
329
    {
330
        $itemVatAmountPrice = $itemTransfer->getUnitTaxAmountFullAggregation();
331
332
        return (string)$this->moneyFacade->convertIntegerToDecimal($itemVatAmountPrice);
333
    }
334
335
    /**
336
     * @param \Generated\Shared\Transfer\AfterPayRequestOrderTransfer $orderRequestTransfer
337
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
338
     *
339
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderTransfer
340
     */
341
    protected function addGiftcardItems(
342
        AfterPayRequestOrderTransfer $orderRequestTransfer,
343
        AfterPayCallTransfer $afterPayCallTransfer
344
    ): AfterPayRequestOrderTransfer {
345
        foreach ($this->getGiftcards($afterPayCallTransfer) as $index => $paymentTransfer) {
346
            $amount = (string)$this->moneyFacade
347
                ->convertIntegerToDecimal(static::NEGATIVE_MULTIPLIER * $paymentTransfer->getAmount());
348
349
            $orderRequestTransfer->addItem(
350
                (new AfterPayRequestOrderItemTransfer())
351
                    ->setProductId(static::GIFT_CARD_PROVIDER . $index)
352
                    ->setDescription(static::GIFT_CARD_PROVIDER . $index)
353
                    ->setGrossUnitPrice($amount)
354
                    ->setQuantity(static::ORDER_ITEM_QUANTITY),
355
            );
356
        }
357
358
        return $orderRequestTransfer;
359
    }
360
361
    /**
362
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
363
     *
364
     * @return array<\Generated\Shared\Transfer\PaymentTransfer>
365
     */
366
    protected function getGiftcards(AfterPayCallTransfer $afterPayCallTransfer): array
367
    {
368
        $giftCardPayments = [];
369
        foreach ($afterPayCallTransfer->getPayments() as $paymentTransfer) {
370
            if ($paymentTransfer->getPaymentMethod() !== static::GIFT_CARD_PROVIDER) {
371
                continue;
372
            }
373
374
            $giftCardPayments[] = $paymentTransfer;
375
        }
376
377
        return $giftCardPayments;
378
    }
379
380
    /**
381
     * @param \Generated\Shared\Transfer\AfterPayRequestOrderTransfer $orderRequestTransfer
382
     * @param \Generated\Shared\Transfer\AfterPayCallTransfer $afterPayCallTransfer
383
     *
384
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderTransfer
385
     */
386
    protected function addExpensesToOrderRequestTransfer(
387
        AfterPayRequestOrderTransfer $orderRequestTransfer,
388
        AfterPayCallTransfer $afterPayCallTransfer
389
    ): AfterPayRequestOrderTransfer {
390
        foreach ($afterPayCallTransfer->getExpenses() as $expenseTransfer) {
391
            if ($expenseTransfer->getSumPriceToPayAggregation() > 0) {
392
                $orderRequestTransfer->addItem(
393
                    $this->buildOrderExpenseRequestTransfer($expenseTransfer),
394
                );
395
            }
396
        }
397
398
        return $orderRequestTransfer;
399
    }
400
401
    /**
402
     * @param \Generated\Shared\Transfer\ExpenseTransfer $expenseTransfer
403
     *
404
     * @return \Generated\Shared\Transfer\AfterPayRequestOrderItemTransfer
405
     */
406
    protected function buildOrderExpenseRequestTransfer(ExpenseTransfer $expenseTransfer): AfterPayRequestOrderItemTransfer
407
    {
408
        $item = (new AfterPayRequestOrderItemTransfer())
409
            ->setProductId($expenseTransfer->getType())
410
            ->setDescription($expenseTransfer->getName())
411
            ->setGrossUnitPrice($this->getStringDecimalExpenseGrossUnitPrice($expenseTransfer))
412
            ->setNetUnitPrice($this->getStringDecimalExpenseNetUnitPrice($expenseTransfer))
413
            ->setVatAmount($this->getStringDecimalExpenseVatAmount($expenseTransfer))
414
            ->setVatPercent($expenseTransfer->getTaxRate())
415
            ->setQuantity($expenseTransfer->getQuantity());
416
417
        return $item;
418
    }
419
420
    /**
421
     * @param \Generated\Shared\Transfer\ExpenseTransfer $expenseTransfer
422
     *
423
     * @return string
424
     */
425
    protected function getStringDecimalExpenseGrossUnitPrice(ExpenseTransfer $expenseTransfer): string
426
    {
427
        $expenseUnitGrossPrice = $expenseTransfer->getUnitPriceToPayAggregation();
428
429
        return (string)$this->moneyFacade->convertIntegerToDecimal($expenseUnitGrossPrice);
430
    }
431
432
    /**
433
     * @param \Generated\Shared\Transfer\ExpenseTransfer $expenseTransfer
434
     *
435
     * @return string
436
     */
437
    protected function getStringDecimalExpenseNetUnitPrice(ExpenseTransfer $expenseTransfer): string
438
    {
439
        $expenseUnitGrossPriceAmount = $expenseTransfer->getUnitPriceToPayAggregation();
440
        $expenseUnitTaxAmount = $expenseTransfer->getUnitTaxAmount();
441
        $expenseUnitNetAmount = $expenseUnitGrossPriceAmount - $expenseUnitTaxAmount;
442
443
        return (string)$this->moneyFacade->convertIntegerToDecimal($expenseUnitNetAmount);
444
    }
445
446
    /**
447
     * @param \Generated\Shared\Transfer\ExpenseTransfer $expenseTransfer
448
     *
449
     * @return string
450
     */
451
    protected function getStringDecimalExpenseVatAmount(ExpenseTransfer $expenseTransfer): string
452
    {
453
        $expenseVatAmount = $expenseTransfer->getSumTaxAmount();
454
455
        return (string)$this->moneyFacade->convertIntegerToDecimal($expenseVatAmount);
456
    }
457
}
458