Passed
Push[email protected] ( c856d5...27c8fa )
by Bruno
03:10
created

SellerDataRequest::getInterestCompound()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * Copyright © Wirecard Brasil. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
namespace Moip\Magento2\Gateway\Request;
10
11
use Magento\Framework\Pricing\Helper\Data as PriceHelper;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Pricing\Helper\Data 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 Magento\Payment\Gateway\Request\BuilderInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Request\BuilderInterface 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 Moip\Magento2\Gateway\Config\Config;
14
use Moip\Magento2\Gateway\Config\ConfigCc;
15
use Moip\Magento2\Gateway\Data\Order\OrderAdapterFactory;
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Gateway\Da...der\OrderAdapterFactory 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 Moip\Magento2\Gateway\SubjectReader;
17
18
/**
19
 * Class SellerDataRequest - Order seller amount structure.
20
 */
21
class SellerDataRequest implements BuilderInterface
22
{
23
    /**
24
     * Receivers block name.
25
     */
26
    const RECEIVERS = 'receivers';
27
28
    /**
29
     * Moip Account block name.
30
     */
31
    const RECEIVERS_MOIP_ACCOUNT = 'moipAccount';
32
33
    /**
34
     * Moip Account Id block name.
35
     */
36
    const RECEIVERS_MOIP_ACCOUNT_ID = 'id';
37
38
    /**
39
     * Type Receiver block name.
40
     */
41
    const RECEIVERS_TYPE = 'type';
42
43
    /**
44
     * Secondary Type Receiver.
45
     * required.
46
     */
47
    const RECEIVERS_TYPE_SECONDARY = 'SECONDARY';
48
49
    /**
50
     * Amount Receiver block name.
51
     */
52
    const RECEIVERS_AMOUNT = 'amount';
53
54
    /**
55
     * Fixed Receiver Type block name.
56
     */
57
    const RECEIVERS_TYPE_FIXED = 'fixed';
58
59
    /**
60
     * Percent Receiver Type block name.
61
     */
62
    const RECEIVERS_TYPE_PERCENT = 'percent';
63
64
    /**
65
     * @var SubjectReader
66
     */
67
    private $subjectReader;
68
69
    /**
70
     * @var OrderAdapterFactory
71
     */
72
    private $orderAdapterFactory;
73
74
    /**
75
     * @var Config
76
     */
77
    private $config;
78
79
    /**
80
     * @var configCc
81
     */
82
    private $configCc;
83
84
    /**
85
     * @var priceHelper
86
     */
87
    private $priceHelper;
88
89
    /**
90
     * @param SubjectReader       $subjectReader
91
     * @param OrderAdapterFactory $orderAdapterFactory
92
     * @param Config              $Config
93
     * @param ConfigCc            $ConfigCc
94
     * @param CheckoutHelper      $checkoutHelper
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Gateway\Request\CheckoutHelper 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...
95
     */
96
    public function __construct(
97
        SubjectReader $subjectReader,
98
        OrderAdapterFactory $orderAdapterFactory,
99
        Config $config,
100
        ConfigCc $configCc,
101
        PriceHelper $checkoutHelper
102
    ) {
103
        $this->subjectReader = $subjectReader;
104
        $this->orderAdapterFactory = $orderAdapterFactory;
105
        $this->config = $config;
106
        $this->configCc = $configCc;
107
        $this->priceHelper = $checkoutHelper;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function build(array $buildSubject)
114
    {
115
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
116
        $payment = $paymentDO->getPayment();
117
118
        $result = [];
119
120
        $orderAdapter = $this->orderAdapterFactory->create(
121
            ['order' => $payment->getOrder()]
122
        );
123
124
        $order = $paymentDO->getOrder();
125
126
        $storeId = $order->getStoreId();
127
        $useSplit = $this->config->getSplitValue('use_split', $storeId);
128
        if (!$useSplit) {
129
            return $result;
130
        }
131
132
        $addition = $orderAdapter->getTaxAmount();
133
        $total = $order->getGrandTotalAmount();
134
135
        $secondaryMPA = $this->config->getSplitValue('secondary_mpa', $storeId);
136
        $secondaryPercent = $this->config->getSplitValue('secondary_percent', $storeId);
137
        $commissionUseShipping = $this->config->getSplitValue('secondary_percent_include_shipping', $storeId);
138
        $commissionUseInterest = $this->config->getSplitValue('secondary_percent_include_interest', $storeId);
139
140
        if ($commissionUseInterest) {
141
            if ($payment->getMethod() === 'moip_magento2_cc' || $payment->getMethod() === 'moip_magento2_cc_vault') {
142
                if ($installment = $payment->getAdditionalInformation('cc_installments')) {
143
                    if ($installment > 1) {
144
                        $typeInstallment = $this->configCc->getTypeInstallment($storeId);
145
                        $interest = $this->configCc->getInfoInterest($storeId);
146
                        $installmentInterest = $this->getInterestCompound(
147
                            $total,
148
                            $interest[$installment],
149
                            $installment
150
                        );
151
                        if ($typeInstallment === 'simple') {
152
                            $installmentInterest = $this->getInterestSimple(
153
                                $total,
154
                                $interest[$installment],
155
                                $installment
156
                            );
157
                        }
158
                        if ($installmentInterest) {
159
                            $total_parcelado = $installmentInterest * $installment;
160
                            $additionalPrice = $total_parcelado - $total;
161
                            $additionalPrice = number_format((float) $additionalPrice, 2, '.', '');
162
                            $payment->setAdditionalInformation(
163
                                self::INSTALLMENT_INTEREST,
164
                                $this->priceHelper->currency($additionalPrice, true, false)
165
                            );
166
                            $addition = $addition + $additionalPrice;
167
                        }
168
                    }
169
                }
170
            }
171
        }
172
173
        if (!$commissionUseShipping) {
174
            $total = $total - $orderAdapter->getShippingAmount();
175
        }
176
177
        if ($commissionUseShipping) {
178
            $total = $total + $addition;
179
        }
180
181
        $commission = $total * ($secondaryPercent / 100);
182
183
        $result[self::RECEIVERS][] = [
184
            self::RECEIVERS_MOIP_ACCOUNT => [
185
                self::RECEIVERS_MOIP_ACCOUNT_ID => $secondaryMPA,
186
            ],
187
            self::RECEIVERS_TYPE   => self::RECEIVERS_TYPE_SECONDARY,
188
            self::RECEIVERS_AMOUNT => [
189
                self::RECEIVERS_TYPE_FIXED => $this->config->formatPrice(round($commission, 2)),
0 ignored issues
show
Bug introduced by
round($commission, 2) of type double is incompatible with the type integer expected by parameter $amount of Moip\Magento2\Gateway\Config\Config::formatPrice(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

189
                self::RECEIVERS_TYPE_FIXED => $this->config->formatPrice(/** @scrutinizer ignore-type */ round($commission, 2)),
Loading history...
190
            ],
191
        ];
192
193
        return $result;
194
    }
195
196
    /**
197
     * Get Intereset for Simple.
198
     *
199
     * @param $total
200
     * @param $interest
201
     * @param $portion
202
     *
203
     * @return string
204
     */
205
    public function getInterestSimple($total, $interest, $portion)
206
    {
207
        if ($interest) {
208
            $taxa = $interest / 100;
209
            $valinterest = $total * $taxa;
210
211
            return ($total + $valinterest) / $portion;
212
        }
213
214
        return 0;
215
    }
216
217
    /**
218
     * Get Intereset for Compound.
219
     *
220
     * @param $total
221
     * @param $interest
222
     * @param $portion
223
     *
224
     * @return string
225
     */
226
    public function getInterestCompound($total, $interest, $portion)
227
    {
228
        if ($interest) {
229
            $taxa = $interest / 100;
230
231
            return (($total * $taxa) * 1) / (1 - (pow(1 / (1 + $taxa), $portion)));
232
        }
233
234
        return 0;
235
    }
236
}
237