wirecardBrasil /
magento2
| 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 | declare(strict_types=1); |
||
| 10 | |||
| 11 | namespace Moip\Magento2\Model; |
||
| 12 | |||
| 13 | use Exception; |
||
| 14 | use Magento\Directory\Model\PriceCurrency; |
||
| 15 | use Magento\Framework\Exception\CouldNotSaveException; |
||
| 16 | use Magento\Framework\Exception\NoSuchEntityException; |
||
| 17 | use Magento\Quote\Api\CartRepositoryInterface; |
||
| 18 | use Magento\Quote\Api\Data\CartInterface as QuoteCartInterface; |
||
| 19 | use Magento\Quote\Api\Data\TotalsInterface as QuoteTotalsInterface; |
||
| 20 | use Moip\Magento2\Api\Data\MoipInterestInterface; |
||
| 21 | use Moip\Magento2\Api\MoipInterestManagementInterface; |
||
| 22 | use Moip\Magento2\Gateway\Config\Config; |
||
| 23 | use Moip\Magento2\Gateway\Config\ConfigCc; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Class MoipInterestManagement - Calc Insterest by Installment. |
||
| 27 | */ |
||
| 28 | class MoipInterestManagement implements MoipInterestManagementInterface |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Quote repository. |
||
| 32 | * |
||
| 33 | * @var CartRepositoryInterface |
||
| 34 | */ |
||
| 35 | protected $quoteRepository; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var PriceCurrency |
||
| 39 | */ |
||
| 40 | protected $priceCurrency; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var total |
||
| 44 | */ |
||
| 45 | protected $total; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var Config |
||
| 49 | */ |
||
| 50 | private $config; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var configCc |
||
| 54 | */ |
||
| 55 | private $configCc; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * MoipInterestManagement constructor. |
||
| 59 | * |
||
| 60 | * @param CartRepositoryInterface $quoteRepository |
||
| 61 | * @param PriceCurrency $priceCurrency, |
||
| 62 | * @param Config $config, |
||
| 63 | * @param ConfigCC $configCc |
||
| 64 | */ |
||
| 65 | public function __construct( |
||
| 66 | CartRepositoryInterface $quoteRepository, |
||
| 67 | PriceCurrency $priceCurrency, |
||
| 68 | Config $config, |
||
| 69 | ConfigCc $configCc |
||
| 70 | ) { |
||
| 71 | $this->quoteRepository = $quoteRepository; |
||
| 72 | $this->priceCurrency = $priceCurrency; |
||
| 73 | $this->config = $config; |
||
| 74 | $this->configCc = $configCc; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Save moip interest number in the quote. |
||
| 79 | * |
||
| 80 | * @param int $cartId |
||
| 81 | * @param MoipInterestInterface $moipInterest |
||
| 82 | * |
||
| 83 | * @throws CouldNotSaveException |
||
| 84 | * @throws NoSuchEntityException |
||
| 85 | * |
||
| 86 | * @return null|string |
||
| 87 | */ |
||
| 88 | public function saveMoipInterest( |
||
| 89 | $cartId, |
||
| 90 | MoipInterestInterface $moipInterest |
||
| 91 | ) { |
||
| 92 | $moip = []; |
||
| 93 | $quote = $this->quoteRepository->getActive($cartId); |
||
| 94 | if (!$quote->getItemsCount()) { |
||
| 95 | throw new NoSuchEntityException(__('Cart %1 doesn\'t contain products', $cartId)); |
||
| 96 | } |
||
| 97 | $installment = $moipInterest->getInstallmentForInterest(); |
||
| 98 | $moipInterestValue = $this->calcInterest($quote, $installment); |
||
| 99 | |||
| 100 | try { |
||
| 101 | $quote->setData(MoipInterestInterface::MOIP_INTEREST_AMOUNT, $moipInterestValue); |
||
| 102 | $quote->setData(MoipInterestInterface::BASE_MOIP_INTEREST_AMOUNT, $moipInterestValue); |
||
| 103 | $this->quoteRepository->save($quote); |
||
| 104 | } catch (Exception $e) { |
||
| 105 | throw new CouldNotSaveException(__('The moip interest # number could not be saved')); |
||
| 106 | } |
||
| 107 | |||
| 108 | $moip = [ |
||
| 109 | 'interest_by_installment' => [ |
||
| 110 | 'installment' => $installment, |
||
| 111 | 'interest' => $moipInterestValue, |
||
| 112 | ], |
||
| 113 | ]; |
||
| 114 | |||
| 115 | return $moip; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Calc value Interest. |
||
| 120 | * |
||
| 121 | * @param $quote |
||
| 122 | * @param $installment |
||
| 123 | * |
||
| 124 | * @return $installmentInterest |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 125 | */ |
||
| 126 | public function calcInterest($quote, $installment) |
||
| 127 | { |
||
| 128 | $storeId = $quote->getData(QuoteCartInterface::KEY_STORE_ID); |
||
| 129 | $grandTotal = $quote->getData(QuoteTotalsInterface::KEY_GRAND_TOTAL); |
||
| 130 | $total = $grandTotal - $quote->getData(MoipInterestInterface::MOIP_INTEREST_AMOUNT); |
||
| 131 | $installmentInterest = 0; |
||
| 132 | |||
| 133 | if ($installment) { |
||
| 134 | $typeInstallment = $this->configCc->getTypeInstallment($storeId); |
||
| 135 | $interest = $this->configCc->getInfoInterest($storeId); |
||
| 136 | if ($interest[$installment] > 0) { |
||
| 137 | $installmentInterest = $this->getInterestCompound($total, $interest[$installment], $installment); |
||
| 138 | if ($typeInstallment === 'simple') { |
||
| 139 | $installmentInterest = $this->getInterestSimple($total, $interest[$installment], $installment); |
||
| 140 | } |
||
| 141 | } elseif ($interest[$installment] < 0) { |
||
| 142 | $installmentInterest = $this->getInterestSimple($total, $interest[$installment]); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this->priceCurrency->round($installmentInterest); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get Intereset for Simple. |
||
| 151 | * |
||
| 152 | * @param $total |
||
| 153 | * @param $interest |
||
| 154 | * |
||
| 155 | * @return float |
||
| 156 | */ |
||
| 157 | public function getInterestSimple($total, $interest) |
||
| 158 | { |
||
| 159 | if ($interest) { |
||
| 160 | $taxa = $interest / 100; |
||
| 161 | |||
| 162 | return $total * $taxa; |
||
| 163 | } |
||
| 164 | |||
| 165 | return 0; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get Intereset for Compound. |
||
| 170 | * |
||
| 171 | * @param $total |
||
| 172 | * @param $interest |
||
| 173 | * @param $portion |
||
| 174 | * |
||
| 175 | * @return float |
||
| 176 | */ |
||
| 177 | public function getInterestCompound($total, $interest, $portion) |
||
| 178 | { |
||
| 179 | if ($interest) { |
||
| 180 | $taxa = $interest / 100; |
||
| 181 | $calc = (($total * $taxa) * 1) / (1 - (pow(1 / (1 + $taxa), $portion))); |
||
| 182 | |||
| 183 | return $total - $calc; |
||
| 184 | } |
||
| 185 | |||
| 186 | return 0; |
||
| 187 | } |
||
| 188 | } |
||
| 189 |