Issues (570)

Gateway/Request/DetailTotalsDataRequest.php (5 issues)

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
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
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
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 DetailTotalsDataRequest - Payment amount structure.
20
 */
21
class DetailTotalsDataRequest implements BuilderInterface
22
{
23
    /**
24
     * Amount block name.
25
     */
26
    const TOTALS_AMOUNT = 'amount';
27
28
    /**
29
     * Grand Total Amount.
30
     * Require.
31
     */
32
    const TOTALS_AMOUNT_GRAND_TOTAL = 'total';
33
34
    /**
35
     * The Currency. ISO 4217
36
     * Required.
37
     */
38
    const TOTALS_AMOUNT_CURRENCY = 'currency';
39
40
    /**
41
     * Subtotals block name.
42
     */
43
    const TOTALS_AMOUNT_SUBTOTALS = 'subtotals';
44
45
    /**
46
     * The Shipping.
47
     */
48
    const TOTALS_AMOUNT_SUBTOTALS_SHIPPING = 'shipping';
49
50
    /**
51
     * The Discount.
52
     */
53
    const TOTALS_AMOUNT_SUBTOTALS_DISCOUNT = 'discount';
54
55
    /**
56
     * The Addition.
57
     */
58
    const TOTALS_AMOUNT_SUBTOTALS_ADDITION = 'addition';
59
60
    /**
61
     * The interest.
62
     */
63
    const INSTALLMENT_INTEREST = 'cc_installment_interest';
64
65
    /**
66
     * @var SubjectReader
67
     */
68
    private $subjectReader;
69
70
    /**
71
     * @var OrderAdapterFactory
72
     */
73
    private $orderAdapterFactory;
74
75
    /**
76
     * @var Config
77
     */
78
    private $config;
79
80
    /**
81
     * @var configCc
82
     */
83
    private $configCc;
84
85
    /**
86
     * @var priceHelper
87
     */
88
    private $priceHelper;
89
90
    /**
91
     * @param SubjectReader       $subjectReader
92
     * @param OrderAdapterFactory $orderAdapterFactory
93
     * @param Config              $Config
94
     * @param ConfigCc            $ConfigCc
95
     * @param CheckoutHelper      $checkoutHelper
0 ignored issues
show
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...
96
     */
97
    public function __construct(
98
        SubjectReader $subjectReader,
99
        OrderAdapterFactory $orderAdapterFactory,
100
        Config $config,
101
        ConfigCc $configCc,
102
        PriceHelper $checkoutHelper
103
    ) {
104
        $this->subjectReader = $subjectReader;
105
        $this->orderAdapterFactory = $orderAdapterFactory;
106
        $this->config = $config;
107
        $this->configCc = $configCc;
108
        $this->priceHelper = $checkoutHelper;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function build(array $buildSubject)
115
    {
116
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
117
        $payment = $paymentDO->getPayment();
118
119
        $result = [];
120
121
        $orderAdapter = $this->orderAdapterFactory->create(
122
            ['order' => $payment->getOrder()]
123
        );
124
125
        $order = $paymentDO->getOrder();
126
        // $quoteId = $orderAdapter->getQuoteId();
127
        $storeId = $order->getStoreId();
128
        $addition = $orderAdapter->getTaxAmount();
129
        $interest = $orderAdapter->getBaseMoipInterestAmount();
130
        $grandTotal = $order->getGrandTotalAmount();
131
        $total = $grandTotal + $interest;
132
        if ($interest > 0) {
133
            $total = $grandTotal - $interest;
134
        }
135
136
        $discount = $orderAdapter->getDiscountAmount();
137
138
        if ($payment->getMethod() === 'moip_magento2_cc' || $payment->getMethod() === 'moip_magento2_cc_vault') {
139
            if ($installment = $payment->getAdditionalInformation('cc_installments')) {
140
                // $this->moipInterest->saveMoipInterest($quoteId, (int)$installment);
141
                $interestInfo = $this->configCc->getInfoInterest($storeId);
142
                if ($installment > 1) {
143
                    $typeInstallment = $this->configCc->getTypeInstallment($storeId);
144
                    if ($interestInfo[$installment] > 0) {
145
                        $installmentInterest = $this->getInterestCompound($total, $interestInfo[$installment], $installment);
146
                        if ($typeInstallment === 'simple') {
147
                            $installmentInterest = $this->getInterestSimple($total, $interestInfo[$installment], $installment);
0 ignored issues
show
The call to Moip\Magento2\Gateway\Re...st::getInterestSimple() has too many arguments starting with $installment. ( Ignorable by Annotation )

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

147
                            /** @scrutinizer ignore-call */ 
148
                            $installmentInterest = $this->getInterestSimple($total, $interestInfo[$installment], $installment);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
148
                        }
149
150
                        if ($installmentInterest) {
151
                            $installmentInterest = number_format((float) $installmentInterest, 2, '.', '');
152
                            $payment->setAdditionalInformation(
153
                                self::INSTALLMENT_INTEREST,
154
                                $this->priceHelper->currency($installmentInterest, true, false)
155
                            );
156
                            if (!$interest) {
157
                                $orderAdapter->setMoipInterestAmount($installmentInterest)->setBaseMoipInterestAmount($installmentInterest);
158
                            }
159
                            $addition = $addition + $installmentInterest;
160
                        }
161
                    }
162
                } elseif ((int) $installment === 1) {
163
                    if ($interestInfo[$installment] < 0) {
164
                        $totalWithDiscount = $grandTotal + ($interest * -1);
165
                        $discountInterest = $this->getInterestDiscount($totalWithDiscount, $interestInfo[$installment]);
166
                        $discountInterest = number_format((float) $discountInterest, 2, '.', '');
167
168
                        $payment->setAdditionalInformation(
169
                            self::INSTALLMENT_INTEREST,
170
                            $this->priceHelper->currency($discountInterest, true, false)
171
                        );
172
                        if (!$interest) {
173
                            $orderAdapter->setMoipInterestAmount($discountInterest)->setBaseMoipInterestAmount($discountInterest);
174
                        }
175
                        $interest = $discountInterest;
176
                    }
177
                }
178
            }
179
        }
180
181
        if ($interest < 0) {
182
            $discount = $discount + $interest;
183
        }
184
        $discount = $discount * -1;
185
        $result[self::TOTALS_AMOUNT] = [
186
            self::TOTALS_AMOUNT_CURRENCY    => $order->getCurrencyCode(),
187
            self::TOTALS_AMOUNT_GRAND_TOTAL => ceil($this->config->formatPrice($grandTotal)),
188
            self::TOTALS_AMOUNT_SUBTOTALS   => [
189
                self::TOTALS_AMOUNT_SUBTOTALS_SHIPPING => ceil($this->config->formatPrice(
190
                    $orderAdapter->getShippingAmount()
191
                )),
192
                self::TOTALS_AMOUNT_SUBTOTALS_DISCOUNT => ceil($this->config->formatPrice($discount)),
193
                self::TOTALS_AMOUNT_SUBTOTALS_ADDITION => ceil($this->config->formatPrice($addition)),
194
            ],
195
        ];
196
197
        return $result;
198
    }
199
200
    /**
201
     * Get Intereset Discount.
202
     *
203
     * @param $total
204
     * @param $interest
205
     *
206
     * @return string
207
     */
208
    public function getInterestDiscount($total, $interest)
209
    {
210
        if ($interest) {
211
            $taxa = $interest / 100;
212
            $valinterest = $total * $taxa;
213
214
            return $valinterest;
215
        }
216
217
        return 0;
218
    }
219
220
    /**
221
     * Get Intereset for Simple.
222
     *
223
     * @param $total
224
     * @param $interest
225
     *
226
     * @return float
227
     */
228
    public function getInterestSimple($total, $interest)
229
    {
230
        if ($interest) {
231
            $taxa = $interest / 100;
232
233
            return $total * $taxa;
234
        }
235
236
        return 0;
237
    }
238
239
    /**
240
     * Get Intereset for Compound.
241
     *
242
     * @param $total
243
     * @param $interest
244
     * @param $portion
245
     *
246
     * @return float
247
     */
248
    public function getInterestCompound($total, $interest, $portion)
249
    {
250
        if ($interest) {
251
            $taxa = $interest / 100;
252
            $calc = (($total * $taxa) * 1) / (1 - (pow(1 / (1 + $taxa), $portion)));
253
254
            return $total - $calc;
255
        }
256
257
        return 0;
258
    }
259
}
260