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

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.     public function __construct(ManagerRegistry $managerRegistrySessionInterface $sessionAviaturLogSave $logSaveAviaturWebService $webService)
  26.     {
  27.         $this->managerRegistry $managerRegistry;
  28.         $this->em $this->managerRegistry->getManager();
  29.         $this->session $session;
  30.         $this->logSave $logSave;
  31.         $this->webService $webService;
  32.     }
  33.     public function conversor_monedas($moneda_origen$moneda_destino$cantidad)
  34.     {
  35.         $trmValue null;
  36.         $get 1;
  37.         $getFinancial 1;
  38.         //Obtiene las monedas activas en WorldPay
  39.         $modenasWorldPay $this->em->getRepository(\Aviatur\TrmBundle\Entity\WorldPayCurrency::class)->worldPayMonedas();
  40.         $isActiveCurrency array_map(function($item) {
  41.             return $item['iata'];
  42.         }, $modenasWorldPay);
  43.         
  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->validacionTrm($moneda_destino);
  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.     
  56.                 } else {
  57.     
  58.                     $financialValue $this->session->get('financialValue');
  59.                     $trmValue $this->session->get('trmValue');
  60.                 }
  61.     
  62.                 if(isset($financialValue)) {
  63.                     $RateChange 0;
  64.                     if (!$this->session->has('RateChange')) {
  65.                         $RateChange floatval($tasasDeCambio["CurrencyValue"]);
  66.                         $ExchangeRate str_replace('.'''$tasasDeCambio["CurrencyValue"]);
  67.                         $this->session->set('RateChange'$RateChange);
  68.     
  69.                         if($moneda_destino == "USD") {
  70.                             $ExchangeRate "10000000";
  71.                         }
  72.     
  73.                         $get = ($ExchangeRate 10_000_000) / $trmValue;
  74.                         $getFinancial = ($ExchangeRate 10_000_000) / $financialValue;
  75.                         
  76.                         $finantial = (float) ($financialValue);
  77.                         $trm = (float) ($trmValue);
  78.                         $diff = (float) (($finantial $trm) * $RateChange);
  79.                         if ($diff 0) {
  80.                             $diff 0;
  81.                         }
  82.                         $this->session->set('RateDiff'$diff);
  83.                         if (== $RateChange) {
  84.                             $get 1;
  85.                             $getFinancial 1;
  86.                             $this->session->set('typeCoin''COP');
  87.                         }
  88.                     } else {
  89.                         $get $this->session->get('RateChange');
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.         $this->session->set('isActiveCurrency'$isActiveCurrency);
  95.         $this->session->set('CoinValueFinancial'$getFinancial);
  96.         $this->session->set('CoinValue'$get);
  97.         $this->session->set('CurrencyFrom'$moneda_origen);
  98.         $this->session->set('CurrencyTo'$moneda_destino);
  99.         $this->session->set('typeCountry', ('COP' == $this->session->get('typeCoin')) ? 'co' $this->session->get('typeCountry'));
  100.         return $get;
  101.     }
  102.     public function InfoCoin($currencyTo$coin null$fee false)
  103.     {
  104.         $convertionPrice 1;
  105.         if ($this->session->get('WorldChangeCoin')) {
  106.             if ($this->session->has('CoinValue')) {
  107.                 $convertionPrice $this->session->get('CoinValue');
  108.             } else {
  109.                 $convertionPrice $this->conversor_monedas($currencyTo$this->session->get('typeCoin'), 1);
  110.             }
  111.             if (null != $coin) {
  112.                 $initialCoin $coin;
  113.                 $coin = ($convertionPrice $coin);
  114.                 if ($fee && $this->session->has('CoinValueFinancial')) {
  115.                     $coin = ($this->session->get('CoinValueFinancial') * $initialCoin);
  116.                 }
  117.             }
  118.         } else {
  119.             $this->removeSessionValues();
  120.         }
  121.         return $coin;
  122.     }
  123.     public function setSessionValues($sessionValues)
  124.     {
  125.         foreach ($sessionValues as $sessionKey => $sessionValue) {
  126.             $this->session->set($sessionKey$sessionValue);
  127.         }
  128.     }
  129.     public function removeSessionValues()
  130.     {
  131.         $this->session->remove('CoinValue');
  132.         $this->session->remove('financialValue');
  133.         $this->session->remove('ExchangeRates');
  134.         $this->session->remove('RateChange');
  135.         $this->session->remove('typeCoin');
  136.         $this->session->remove('isActiveCurrency');
  137.         $this->session->remove('tasasDeCambio');
  138.     }
  139.     public function validateChangeCoin($route)
  140.     {
  141.         if (!$route) {
  142.             return false;
  143.         }
  144.         $this->session->remove('WorldChangeCoin');
  145.         $paymentMethodAgency $this->em->getRepository(\Aviatur\GeneralBundle\Entity\PaymentMethodAgency::class)->findWorldPayAgency($this->session->get('agencyId'));
  146.         $validateAgency false;
  147.         $validateProduct false;
  148.         if ((is_countable($paymentMethodAgency) ? count($paymentMethodAgency) : 0) > 0) {
  149.             $paymentWorldPay json_decode($this->em->getRepository(\Aviatur\GeneralBundle\Entity\Parameter::class)->findOneByName('aviatur_payment_world_pay')->getDescription(), true);
  150.             $HistoricalRate $this->em->getRepository(\Aviatur\TrmBundle\Entity\HistoricalRate::class)->findCurrency();
  151.             if ((is_countable($HistoricalRate) ? count($HistoricalRate) : 0) == 0) {
  152.                 return false;
  153.             }
  154.             $validateAgency true;
  155.             foreach ($paymentWorldPay as $key => $payments) {
  156.                 foreach ($payments as $keyId => $payment) {
  157.                     switch ($key) {
  158.                         case 'productDisabled':
  159.                             if (strstr($route, (string) $keyId) && $payment) {
  160.                                 $validateProduct true;
  161.                             }
  162.                             break;
  163.                         default:
  164.                             return false;
  165.                     }
  166.                 }
  167.             }
  168.             if ($validateAgency && $validateProduct) {
  169.                 $this->session->set('WorldChangeCoin'true);
  170.                 return true;
  171.             }
  172.         }
  173.         if (!$validateAgency || !$validateProduct) {
  174.             $this->removeSessionValues();
  175.         }
  176.         return false;
  177.     }
  178.     public function storeCoinParams($product null)
  179.     {
  180.     }
  181.     public function getCoinParams()
  182.     {
  183.         return;
  184.     }
  185.     public function validacionTrm($moneda_destino) {
  186.         //Obtiene la tasa de cambio
  187.         $trmModel = new TrmModel();
  188.         $financialTrm $this->webService->callWebService('GENERALLAVE''dummy|http://www.aviatur.com.co/dummy/'$trmModel->getTasaCambio());
  189.         // if (!$financialTrm || !isset($financialTrm->MENSAJE->TASAS_CAMBIO->ELEMENTO_TASA_CAMBIO)) {
  190.         //     return false;
  191.         // }
  192.         foreach ($financialTrm->MENSAJE->TASAS_CAMBIO->ELEMENTO_TASA_CAMBIO as $elemento) {
  193.             if ((string) $elemento->TIPO_TASA_CAMBIO === "FIV") {
  194.                 $monedaOrigen = (string) $elemento->MONEDA_ORIGEN;
  195.                 $monedaDestino = (string) $elemento->MONEDA_DESTINO;
  196.                 $valor = (string) $elemento->VALOR;
  197.             }
  198.         }
  199.         //Obtiene las tasas de cambio de la bases de datos
  200.         $tasaCambioBD $this->em->getRepository(\Aviatur\TrmBundle\Entity\WorldPayCurrency::class)->tasaCambiosWorldPay($moneda_destino);
  201.         if(!$tasaCambioBD["Succeeded"]) {
  202.             return [
  203.                 'Succeeded' => false
  204.             ];
  205.         }
  206.         $FinancialExchangeRate $valor;
  207.         if($valor $tasaCambioBD["OfficialExchangeRate"]) {
  208.             $FinancialExchangeRate $tasaCambioBD["OfficialExchangeRate"];
  209.         }
  210.         return [
  211.             'FinancialExchangeRate' => $FinancialExchangeRate,
  212.             'OfficialExchangeRate' => $tasaCambioBD["OfficialExchangeRate"],
  213.             'CurrencyValue' => $tasaCambioBD["CurrencyValue"],
  214.             'Succeeded' => true,
  215.         ];
  216.     }
  217. }