Passed
Branch feature/ECO-573-per-item-proce... (fe5bf4)
by Andrey
04:56
created

AbstractAmazonpayCommandPlugin::getPriceToPay()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Apache OSL-2
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Amazonpay\Communication\Plugin\Oms\Command;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\AddressTransfer;
12
use Generated\Shared\Transfer\AmazonpayCallTransfer;
13
use Generated\Shared\Transfer\ItemTransfer;
14
use IteratorAggregate;
15
use Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpay;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpay 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 Orm\Zed\Oms\Persistence\SpyOmsOrderItemStateQuery;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Oms\Persistence\SpyOmsOrderItemStateQuery 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 Orm\Zed\Sales\Persistence\SpySalesOrder;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrder 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 Orm\Zed\Sales\Persistence\SpySalesOrderAddress;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrderAddress 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 Orm\Zed\Sales\Persistence\SpySalesOrderItem;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrderItem 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 Spryker\Shared\Shipment\ShipmentConstants;
21
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
22
use Spryker\Zed\Oms\Dependency\Plugin\Command\CommandByOrderInterface;
23
24
/**
25
 * @method \SprykerEco\Zed\Amazonpay\Business\AmazonpayFacadeInterface getFacade()
26
 * @method \SprykerEco\Zed\Amazonpay\Communication\AmazonpayCommunicationFactory getFactory()
27
 */
28
abstract class AbstractAmazonpayCommandPlugin extends AbstractPlugin implements CommandByOrderInterface
29
{
30
31
    /**
32
     * @var bool
33
     */
34
    protected $wasShippingCharged;
35
36
    /**
37
     * @return string
38
     */
39
    protected function getAffectingRequestedAmountItemsStateFlag()
40
    {
41
        return '';
42
    }
43
44
    /**
45
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
46
     * @param \ArrayObject|\Generated\Shared\Transfer\ItemTransfer[] $itemTransfers
47
     *
48
     * @return int
49
     */
50
    protected function getRequestedAmountByOrderAndItems(SpySalesOrder $orderEntity, ArrayObject $itemTransfers)
51
    {
52
        $subtotal = $this->getPriceToPay($itemTransfers);
53
54
        if (!$this->wasShippingCharged
55
            && $this->getFactory()
56
                ->createRequestAmountCalculator()
57
                ->shouldChargeShipping($orderEntity, $this->getAffectingRequestedAmountItemsStateFlag())) {
58
            $subtotal += $this->getShipmentPrice($orderEntity);
59
            $this->wasShippingCharged = true;
60
        }
61
62
        return $subtotal;
63
    }
64
65
    /**
66
     * @param \ArrayObject $itemTransfers
67
     *
68
     * @return int
69
     */
70
    protected function getPriceToPay(ArrayObject $itemTransfers)
71
    {
72
        $subtotal = 0;
73
74
        foreach ($itemTransfers as $itemTransfer) {
75
            $subtotal += $itemTransfer->getUnitPriceToPayAggregation();
76
        }
77
78
        return $subtotal;
79
    }
80
81
    /**
82
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
83
     *
84
     * @return int
85
     */
86
    protected function getShipmentPrice(SpySalesOrder $orderEntity)
87
    {
88
        return $this->getExpenseByType($orderEntity, ShipmentConstants::SHIPMENT_EXPENSE_TYPE)
89
            ->getPriceToPayAggregation();
90
    }
91
92
    /**
93
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
94
     * @param string $type
95
     *
96
     * @return \Orm\Zed\Sales\Persistence\SpySalesExpense
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesExpense 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...
97
     */
98
    protected function getExpenseByType(SpySalesOrder $orderEntity, $type)
99
    {
100
        foreach ($orderEntity->getExpenses() as $expense) {
101
            if ($expense->getType() === $type) {
102
                return $expense;
103
            }
104
        }
105
106
        return null;
107
    }
108
109
    /**
110
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $salesOrderItems
111
     *
112
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer[]
113
     */
114 View Code Duplication
    protected function groupSalesOrderItemsByAuthId(array $salesOrderItems)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        $groups = [];
117
118
        foreach ($salesOrderItems as $salesOrderItem) {
119
            $payment = $this->getPaymentDetails($salesOrderItem);
120
121
            if (!$payment) {
122
                continue;
123
            }
124
125
            $groupData = $groups[$payment->getAuthorizationReferenceId()] ?? $this->createAmazonpayCallTransfer($payment);
126
127
            $groupData->addItem(
128
                $this->mapSalesOrderItemToItemTransfer($salesOrderItem)
129
            );
130
131
            $groups[$payment->getAuthorizationReferenceId()] = $groupData;
132
        }
133
134
        return $groups;
135
    }
136
137
    /**
138
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $salesOrderItems
139
     *
140
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer[]
141
     */
142 View Code Duplication
    protected function groupSalesOrderItemsByCaptureId(array $salesOrderItems)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144
        $groups = [];
145
146
        foreach ($salesOrderItems as $salesOrderItem) {
147
            $payment = $this->getPaymentDetails($salesOrderItem);
148
149
            if (!$payment) {
150
                continue;
151
            }
152
153
            $groupData = $groups[$payment->getAmazonCaptureId()] ?? $this->createAmazonpayCallTransfer($payment);
154
155
            $groupData->addItem(
156
                $this->mapSalesOrderItemToItemTransfer($salesOrderItem)
157
            );
158
159
            $groups[$payment->getAmazonCaptureId()] = $groupData;
160
        }
161
162
        return $groups;
163
    }
164
165
    /**
166
     * @param \Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpay $payment
167
     *
168
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer
169
     */
170
    protected function createAmazonpayCallTransfer(SpyPaymentAmazonpay $payment)
171
    {
172
        $amazonPayment = $this->getFactory()
173
            ->createPaymentAmazonpayConverter()
174
            ->mapEntityToTransfer($payment);
175
176
        $amazonpayCallTransfer = new AmazonpayCallTransfer();
177
        $amazonpayCallTransfer->setAmazonpayPayment($amazonPayment);
178
179
        return $amazonpayCallTransfer;
180
    }
181
182
    /**
183
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
184
     * @param array $salesOrderItems
185
     *
186
     * @return void
187
     */
188
    protected function populateItems(AmazonpayCallTransfer $amazonpayCallTransfer, array $salesOrderItems)
189
    {
190
        $amazonpayCallTransfer->setItems(
191
            new ArrayObject(
192
                array_map([$this, 'mapSalesOrderItemToItemTransfer'], $salesOrderItems)
193
            )
194
        );
195
    }
196
197
    /**
198
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem $salesOrderItem
199
     *
200
     * @return null|\Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpay
201
     */
202
    protected function getPaymentDetails(SpySalesOrderItem $salesOrderItem)
203
    {
204
        /** @var \Orm\Zed\Amazonpay\Persistence\SpyPaymentAmazonpaySalesOrderItem $payment */
205
        $payment = $salesOrderItem->getSpyPaymentAmazonpaySalesOrderItems()->getLast();
206
207
        if (!$payment) {
208
            return null;
209
        }
210
211
        return $payment->getSpyPaymentAmazonpay();
212
    }
213
214
    /**
215
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem $salesOrderItemEntity
216
     *
217
     * @return \Generated\Shared\Transfer\ItemTransfer
218
     */
219
    protected function mapSalesOrderItemToItemTransfer(SpySalesOrderItem $salesOrderItemEntity)
220
    {
221
        $itemTransfer = (new ItemTransfer())
222
            ->fromArray($salesOrderItemEntity->toArray(), true);
223
224
        $itemTransfer->setUnitGrossPrice($salesOrderItemEntity->getGrossPrice());
225
        $itemTransfer->setUnitNetPrice($salesOrderItemEntity->getNetPrice());
226
        $itemTransfer->setUnitPrice($salesOrderItemEntity->getPrice());
227
        $itemTransfer->setUnitPriceToPayAggregation($salesOrderItemEntity->getPriceToPayAggregation());
228
        $itemTransfer->setUnitSubtotalAggregation($salesOrderItemEntity->getSubtotalAggregation());
229
        $itemTransfer->setUnitProductOptionPriceAggregation($salesOrderItemEntity->getProductOptionPriceAggregation());
230
        $itemTransfer->setUnitExpensePriceAggregation($salesOrderItemEntity->getExpensePriceAggregation());
231
        $itemTransfer->setUnitTaxAmount($salesOrderItemEntity->getTaxAmount());
232
        $itemTransfer->setUnitTaxAmountFullAggregation($salesOrderItemEntity->getTaxAmountFullAggregation());
233
        $itemTransfer->setUnitDiscountAmountAggregation($salesOrderItemEntity->getDiscountAmountAggregation());
234
        $itemTransfer->setUnitDiscountAmountFullAggregation($salesOrderItemEntity->getDiscountAmountFullAggregation());
235
        $itemTransfer->setRefundableAmount($salesOrderItemEntity->getRefundableAmount());
236
237
        return $itemTransfer;
238
    }
239
240
    /**
241
     * @param \IteratorAggregate|\Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $salesOrderItems
242
     * @param string $statusName
243
     *
244
     * @return void
245
     */
246
    protected function setOrderItemsStatus(IteratorAggregate $salesOrderItems, $statusName)
247
    {
248
        $statusEntity = SpyOmsOrderItemStateQuery::create()->findByName($statusName)[0];
249
250
        foreach ($salesOrderItems as $salesOrderItem) {
251
            $salesOrderItem->setFkOmsOrderItemState($statusEntity->getIdOmsOrderItemState());
252
            $salesOrderItem->save();
253
        }
254
    }
255
256
    /**
257
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderAddress $address
258
     *
259
     * @return \Generated\Shared\Transfer\AddressTransfer
260
     */
261
    protected function buildAddressTransfer(SpySalesOrderAddress $address)
262
    {
263
        return (new AddressTransfer())
264
            ->fromArray($address->toArray(), true)
265
            ->fromArray($address->getCountry()->toArray(), true);
266
    }
267
268
}
269