src/Aviatur/GeneralBundle/Services/AviaturChangeCoin.php line 147

Open in your IDE?
  1. <?php
  2. /*
  3.  * To change this license header, choose License Headers in Project Properties.
  4.  * To change this template file, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7. namespace Aviatur\GeneralBundle\Services;
  8. use Doctrine\Bundle\DoctrineBundle\Registry;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Aviatur\TrmBundle\Models\TrmModel;
  12. // use Aviatur\GeneralBundle\Services\AviaturWebService;
  13. use DateTime;
  14. /**
  15.  * Description of AviaturChangeCoin.
  16.  *
  17.  * @author sergio.amaya
  18.  */
  19. class AviaturChangeCoin
  20. {
  21.     private \Doctrine\Persistence\ManagerRegistry $managerRegistry;
  22.     private \Symfony\Component\HttpFoundation\Session\SessionInterface $session;
  23.     private \Aviatur\GeneralBundle\Services\AviaturLogSave $logSave;
  24.     private \Aviatur\GeneralBundle\Services\AviaturWebService $webService;
  25.     private $em;
  26.     public function __construct(ManagerRegistry $managerRegistrySessionInterface $sessionAviaturLogSave $logSaveAviaturWebService $webService)
  27.     {
  28.         $this->managerRegistry $managerRegistry;
  29.         $this->em $this->managerRegistry->getManager();
  30.         $this->session $session;
  31.         $this->logSave $logSave;
  32.         $this->webService $webService;
  33.     }
  34.     public function conversor_monedas($moneda_origen$moneda_destino)
  35.     {
  36.         $trmValue null;
  37.         $get 1;
  38.         $getFinancial 1;
  39.         //Obtiene las monedas activas en WorldPay
  40.         $modenasWorldPay $this->em->getRepository(\Aviatur\TrmBundle\Entity\WorldPayCurrency::class)->worldPayMonedas();
  41.         $isActiveCurrency array_map(function ($item) {
  42.             return $item['iata'];
  43.         }, $modenasWorldPay);
  44.         if ($moneda_origen != $moneda_destino) {
  45.             //Valida la trm de aviatur y la compára con la que esta guardada en BD y trae la mas alta
  46.             $tasasDeCambio $this->getExchangeRate($moneda_origen$moneda_destino'FIV');
  47.             $this->session->set('tasasDeCambio'$tasasDeCambio);
  48.             if ($tasasDeCambio['Succeeded']) {
  49.                 if (!$this->session->has('financialValue')) {
  50.                     //La tasa financiera y la oficial deben de ser igual 09-01-2025
  51.                     $financialValue strtok($tasasDeCambio["OfficialExchangeRate"], '.');
  52.                     $trmValue strtok($tasasDeCambio["OfficialExchangeRate"], '.');
  53.                     $this->session->set('financialValue'$financialValue);
  54.                     $this->session->set('trmValue'$trmValue);
  55.                 } else {
  56.                     $financialValue $this->session->get('financialValue');
  57.                     $trmValue $this->session->get('trmValue');
  58.                 }
  59.                 if (isset($financialValue)) {
  60.                     $RateChange 0;
  61.                     if (!$this->session->has('RateChange')) {
  62.                         $RateChange floatval($tasasDeCambio["CurrencyValue"]);
  63.                         $ExchangeRate str_replace('.'''$tasasDeCambio["CurrencyValue"]);
  64.                         $this->session->set('RateChange'$RateChange);
  65.                         if ($moneda_destino == "USD") {
  66.                             $ExchangeRate "10000000";
  67.                         }
  68.                         $get = ($ExchangeRate 10_000_000) / $trmValue;
  69.                         $getFinancial = ($ExchangeRate 10_000_000) / $financialValue;
  70.                         $finantial = (float) ($financialValue);
  71.                         $trm = (float) ($trmValue);
  72.                         $diff = (float) (($finantial $trm) * $RateChange);
  73.                         if ($diff 0) {
  74.                             $diff 0;
  75.                         }
  76.                         $this->session->set('RateDiff'$diff);
  77.                         if (== $RateChange) {
  78.                             $get 1;
  79.                             $getFinancial 1;
  80.                             $this->session->set('typeCoin''COP');
  81.                         }
  82.                     } else {
  83.                         $get $this->session->get('RateChange');
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.         $this->session->set('isActiveCurrency'$isActiveCurrency);
  89.         $this->session->set('CoinValueFinancial'$getFinancial);
  90.         $this->session->set('CoinValue'$get);
  91.         $this->session->set('CurrencyFrom'$moneda_origen);
  92.         $this->session->set('CurrencyTo'$moneda_destino);
  93.         $this->session->set('typeCountry', ('COP' == $this->session->get('typeCoin')) ? 'co' $this->session->get('typeCountry'));
  94.         return $get;
  95.     }
  96.     public function InfoCoin($currencyTo$coin null$fee false)
  97.     {
  98.         $convertionPrice 1;
  99.         if ($this->session->get('WorldChangeCoin')) {
  100.             if ($this->session->has('CoinValue')) {
  101.                 $convertionPrice $this->session->get('CoinValue');
  102.             } else {
  103.                 $convertionPrice $this->conversor_monedas($currencyTo$this->session->get('typeCoin'), 1);
  104.             }
  105.             if (null != $coin) {
  106.                 $initialCoin $coin;
  107.                 $coin = ($convertionPrice $coin);
  108.                 if ($fee && $this->session->has('CoinValueFinancial')) {
  109.                     $coin = ($this->session->get('CoinValueFinancial') * $initialCoin);
  110.                 }
  111.             }
  112.         } else {
  113.             $this->removeSessionValues();
  114.         }
  115.         return $coin;
  116.     }
  117.     public function setSessionValues($sessionValues)
  118.     {
  119.         foreach ($sessionValues as $sessionKey => $sessionValue) {
  120.             $this->session->set($sessionKey$sessionValue);
  121.         }
  122.     }
  123.     public function removeSessionValues()
  124.     {
  125.         $this->session->remove('CoinValue');
  126.         $this->session->remove('financialValue');
  127.         $this->session->remove('ExchangeRates');
  128.         $this->session->remove('RateChange');
  129.         $this->session->remove('typeCoin');
  130.         $this->session->remove('isActiveCurrency');
  131.         $this->session->remove('tasasDeCambio');
  132.     }
  133.     public function validateChangeCoin($route)
  134.     {
  135.         if (!$route) {
  136.             return false;
  137.         }
  138.         $this->session->remove('WorldChangeCoin');
  139.         $paymentMethodAgency $this->em->getRepository(\Aviatur\GeneralBundle\Entity\PaymentMethodAgency::class)->findWorldPayAgency($this->session->get('agencyId'));
  140.         $validateAgency false;
  141.         $validateProduct false;
  142.         if ((is_countable($paymentMethodAgency) ? count($paymentMethodAgency) : 0) > 0) {
  143.             $paymentWorldPay json_decode($this->em->getRepository(\Aviatur\GeneralBundle\Entity\Parameter::class)->findOneByName('aviatur_payment_world_pay')->getDescription(), true);
  144.             $HistoricalRate $this->em->getRepository(\Aviatur\TrmBundle\Entity\HistoricalRate::class)->findCurrency();
  145.             if ((is_countable($HistoricalRate) ? count($HistoricalRate) : 0) == 0) {
  146.                 return false;
  147.             }
  148.             $validateAgency true;
  149.             foreach ($paymentWorldPay as $key => $payments) {
  150.                 foreach ($payments as $keyId => $payment) {
  151.                     switch ($key) {
  152.                         case 'productDisabled':
  153.                             if (strstr($route, (string) $keyId) && $payment) {
  154.                                 $validateProduct true;
  155.                             }
  156.                             break;
  157.                         default:
  158.                             return false;
  159.                     }
  160.                 }
  161.             }
  162.             if ($validateAgency && $validateProduct) {
  163.                 $this->session->set('WorldChangeCoin'true);
  164.                 return true;
  165.             }
  166.         }
  167.         if (!$validateAgency || !$validateProduct) {
  168.             $this->removeSessionValues();
  169.         }
  170.         return false;
  171.     }
  172.     public function storeCoinParams($product null) {}
  173.     public function getCoinParams()
  174.     {
  175.         return;
  176.     }
  177.     // Obtiene la tasa de cambio
  178.     public function getExchangeRate($moneda_origen$moneda_destino$tipo_moneda)
  179.     {
  180.         $exchangeRateModel = new TrmModel();
  181.         $exchangeRates $this->webService->callWebService('GENERALLAVE''dummy|http://www.aviatur.com.co/dummy/'$exchangeRateModel->getTasaCambio());
  182.         $exchangeRateRS json_decode(json_encode($exchangeRates), true);
  183.         $exchangeRateValue 0;
  184.         if (!empty($exchangeRateRS) && $exchangeRateRS['RESULTADO'] == "EXITO") {
  185.             $arrayExchangeRates $exchangeRateRS['MENSAJE']['TASAS_CAMBIO']['ELEMENTO_TASA_CAMBIO'];
  186.             
  187.             // Filtrar las tasas de cambio
  188.             $filteredExchangeRates array_filter($arrayExchangeRates, function ($exchangeRate) use ($tipo_moneda$moneda_origen$moneda_destino) {
  189.                 return $exchangeRate['TIPO_TASA_CAMBIO'] == $tipo_moneda &&
  190.                     $exchangeRate['MONEDA_ORIGEN'] == $moneda_origen &&
  191.                     $exchangeRate['MONEDA_DESTINO'] == $moneda_destino;
  192.             });
  193.             // Obtener el primer valor filtrado
  194.             $filteredExchangeRate reset($filteredExchangeRates);
  195.             if ($filteredExchangeRate) {
  196.                 $exchangeRateValue $filteredExchangeRate['VALOR'];
  197.             }
  198.         }
  199.         // Obtener la tasa de cambio en la BD de acuerdo al tipo de moneda
  200.         switch ($tipo_moneda) {
  201.             case 'FIV':
  202.                 $exchangeRatesBD $this->em->getRepository(\Aviatur\TrmBundle\Entity\WorldPayCurrency::class)->tasaCambiosWorldPay($moneda_destino);
  203.                 $exchangeRateBD = isset($exchangeRatesBD["OfficialExchangeRate"]) ? $exchangeRatesBD["OfficialExchangeRate"] : 0;
  204.                 break;
  205.             default:
  206.                 $exchangeRatesBD $this->em->getRepository(\Aviatur\TrmBundle\Entity\Trm::class)->recentTrm($moneda_origen);
  207.                 $exchangeRateBD = isset($exchangeRatesBD[0]['value']) ? $exchangeRatesBD[0]['value'] : 0;
  208.                 break;
  209.         }
  210.         // Verificar si la tasa de cambio es 0, devolver respuesta de error
  211.         if ($exchangeRateBD == 0) {
  212.             return [
  213.                 'Succeeded' => false,
  214.                 'ErrorMessage' => 'No se pudo obtener la tasa de cambio.'
  215.             ];
  216.         }
  217.         // Determinar la tasa de cambio financiera
  218.         $FinancialExchangeRate $exchangeRateBD $exchangeRateValue $exchangeRateBD $exchangeRateValue;
  219.         return [
  220.             'FinancialExchangeRate' => $FinancialExchangeRate,
  221.             'OfficialExchangeRate' => $exchangeRateBD,
  222.             'CurrencyValue' => $exchangeRatesBD["CurrencyValue"] ?? $tipo_moneda,
  223.             'Succeeded' => true,
  224.         ];
  225.     }
  226. }