Passed
Push — feature/eco-2295/eco-2802-adju... ( e467b5...a71a32 )
by Volodymyr
09:26
created

calculateExpensesAmountToRefund()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 10
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\CrefoPay\Business\Oms\Command\Builder;
9
10
use Generated\Shared\Transfer\CrefoPayApiAmountTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayApiAmountTransfer 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\CrefoPayApiRefundRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...piRefundRequestTransfer 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\CrefoPayApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiRequestTransfer 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\CrefoPayOmsCommandTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayOmsCommandTransfer 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\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...
15
use Generated\Shared\Transfer\PaymentCrefoPayOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayOrderItemTransfer 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 SprykerEco\Zed\CrefoPay\Business\Exception\InvalidItemsToRefundAggregationException;
17
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
18
19
class RefundOmsCommandRequestBuilder implements CrefoPayOmsCommandRequestBuilderInterface
20
{
21
    protected const INVALID_ITEMS_AGGREGATION_MESSAGE = 'Order items to refund have to have same captureId.';
22
23
    /**
24
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
25
     */
26
    protected $config;
27
28
    /**
29
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
30
     */
31
    public function __construct(CrefoPayConfig $config)
32
    {
33
        $this->config = $config;
34
    }
35
36
    /**
37
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
38
     *
39
     * @return \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer
40
     */
41
    public function buildRequestTransfer(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayOmsCommandTransfer
42
    {
43
        $refundRequest = $this->createRefundRequestTransfer($crefoPayOmsCommandTransfer);
44
        $amountToRefund = $this->calculateItemsAmountToRefund($crefoPayOmsCommandTransfer);
45
        $refundRequest
46
            ->setCaptureID($this->getCaptureId($crefoPayOmsCommandTransfer))
47
            ->setAmount(
48
                $this->createAmountTransfer($amountToRefund)
49
            );
50
51
        $requestTransfer = (new CrefoPayApiRequestTransfer())
52
            ->setRefundRequest($refundRequest);
53
54
        $crefoPayOmsCommandTransfer
55
            ->setRequest($requestTransfer);
56
57
        return $this->addExpensesRequest($crefoPayOmsCommandTransfer);
58
    }
59
60
    /**
61
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
62
     *
63
     * @return \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer
64
     */
65
    protected function addExpensesRequest(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayOmsCommandTransfer
66
    {
67
        if (!$this->config->getRefundExpensesWithLastItem() || !$this->isLastRefund($crefoPayOmsCommandTransfer)) {
68
            return $crefoPayOmsCommandTransfer;
69
        }
70
71
        $amountToRefund = $this->calculateExpensesAmountToRefund($crefoPayOmsCommandTransfer);
72
73
        $refundExpenseRequestTransfer = $this->createRefundRequestTransfer($crefoPayOmsCommandTransfer);
74
        $refundExpenseRequestTransfer
75
            ->setCaptureID($crefoPayOmsCommandTransfer->getPaymentCrefoPay()->getExpensesCaptureId())
76
            ->setAmount($this->createAmountTransfer($amountToRefund));
77
78
        $expensesRequestTransfer = (new CrefoPayApiRequestTransfer())
79
            ->setRefundRequest($refundExpenseRequestTransfer);
80
81
        return $crefoPayOmsCommandTransfer
82
            ->setExpensesRequest($expensesRequestTransfer);
83
    }
84
85
    /**
86
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
87
     *
88
     * @return \Generated\Shared\Transfer\CrefoPayApiRefundRequestTransfer
89
     */
90
    protected function createRefundRequestTransfer(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): CrefoPayApiRefundRequestTransfer
91
    {
92
        return (new CrefoPayApiRefundRequestTransfer())
93
            ->setMerchantID($this->config->getMerchantId())
94
            ->setStoreID($this->config->getStoreId())
95
            ->setOrderID($crefoPayOmsCommandTransfer->getPaymentCrefoPay()->getCrefoPayOrderId())
96
            ->setRefundDescription($this->getRefundDescription($crefoPayOmsCommandTransfer));
97
    }
98
99
    /**
100
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
101
     *
102
     * @throws \SprykerEco\Zed\CrefoPay\Business\Exception\InvalidItemsToRefundAggregationException
103
     *
104
     * @return string
105
     */
106
    protected function getCaptureId(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): string
107
    {
108
        $orderItemCaptureIds = array_unique(
109
            array_map(
110
                function (PaymentCrefoPayOrderItemTransfer $paymentCrefoPayOrderItemTransfer) {
111
                    return $paymentCrefoPayOrderItemTransfer->getCaptureId();
112
                },
113
                $crefoPayOmsCommandTransfer
114
                    ->getPaymentCrefoPayOrderItemCollection()
115
                    ->getCrefoPayOrderItems()
116
                    ->getArrayCopy()
117
            )
118
        );
119
120
        if (count($orderItemCaptureIds) !== 1) {
121
            throw new InvalidItemsToRefundAggregationException(static::INVALID_ITEMS_AGGREGATION_MESSAGE);
122
        }
123
124
        return reset($orderItemCaptureIds);
125
    }
126
127
    /**
128
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
129
     *
130
     * @return int
131
     */
132
    protected function calculateItemsAmountToRefund(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): int
133
    {
134
        $totalAmountToRefund = $crefoPayOmsCommandTransfer->getRefund()->getAmount();
135
136
        if (!$this->isLastRefund($crefoPayOmsCommandTransfer)) {
137
            return $totalAmountToRefund;
138
        }
139
140
        return $totalAmountToRefund - $this->calculateExpensesAmountToRefund($crefoPayOmsCommandTransfer);
141
    }
142
143
    /**
144
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
145
     *
146
     * @return int
147
     */
148
    protected function calculateExpensesAmountToRefund(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): int
149
    {
150
        return (int)array_sum(
151
            array_map(
152
                function (ExpenseTransfer $expenseTransfer) {
153
                    return $expenseTransfer->getSumPriceToPayAggregation();
154
                },
155
                $crefoPayOmsCommandTransfer
156
                    ->getRefund()
157
                    ->getExpenses()
158
                    ->getArrayCopy()
159
            )
160
        );
161
    }
162
163
    /**
164
     * @param int $amount
165
     *
166
     * @return \Generated\Shared\Transfer\CrefoPayApiAmountTransfer
167
     */
168
    protected function createAmountTransfer(int $amount): CrefoPayApiAmountTransfer
169
    {
170
        return (new CrefoPayApiAmountTransfer())
171
            ->setAmount($amount);
172
    }
173
174
    /**
175
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
176
     *
177
     * @return string|null
178
     */
179
    protected function getRefundDescription(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): ?string
180
    {
181
        return $crefoPayOmsCommandTransfer->getRefund()->getComment() ?? $this->config->getRefundDescription();
182
    }
183
184
    /**
185
     * @param \Generated\Shared\Transfer\CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer
186
     *
187
     * @return bool
188
     */
189
    protected function isLastRefund(CrefoPayOmsCommandTransfer $crefoPayOmsCommandTransfer): bool
190
    {
191
        $refundableItemAmount = 0;
192
193
        $itemIdsToRefund = array_map(
194
            function (PaymentCrefoPayOrderItemTransfer $paymentCrefoPayOrderItemTransfer) {
195
                return $paymentCrefoPayOrderItemTransfer->getFkSalesOrderItem();
196
            },
197
            $crefoPayOmsCommandTransfer
198
                ->getPaymentCrefoPayOrderItemCollection()
199
                ->getCrefoPayOrderItems()
200
                ->getArrayCopy()
201
        );
202
203
        foreach ($crefoPayOmsCommandTransfer->getOrder()->getItems() as $itemTransfer) {
204
            if (!in_array($itemTransfer->getIdSalesOrderItem(), $itemIdsToRefund)) {
205
                $refundableItemAmount += (int)$itemTransfer->getRefundableAmount();
206
            }
207
        }
208
209
        return $refundableItemAmount === 0;
210
    }
211
}
212