<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Aviatur\GeneralBundle\Services;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Aviatur\TrmBundle\Models\TrmModel;
// use Aviatur\GeneralBundle\Services\AviaturWebService;
use DateTime;
/**
* Description of AviaturChangeCoin.
*
* @author sergio.amaya
*/
class AviaturChangeCoin
{
private \Doctrine\Persistence\ManagerRegistry $managerRegistry;
private \Symfony\Component\HttpFoundation\Session\SessionInterface $session;
private \Aviatur\GeneralBundle\Services\AviaturLogSave $logSave;
private \Aviatur\GeneralBundle\Services\AviaturWebService $webService;
private $em;
public function __construct(ManagerRegistry $managerRegistry, SessionInterface $session, AviaturLogSave $logSave, AviaturWebService $webService)
{
$this->managerRegistry = $managerRegistry;
$this->em = $this->managerRegistry->getManager();
$this->session = $session;
$this->logSave = $logSave;
$this->webService = $webService;
}
public function conversor_monedas($moneda_origen, $moneda_destino)
{
$trmValue = null;
$get = 1;
$getFinancial = 1;
//Obtiene las monedas activas en WorldPay
$modenasWorldPay = $this->em->getRepository(\Aviatur\TrmBundle\Entity\WorldPayCurrency::class)->worldPayMonedas();
$isActiveCurrency = array_map(function ($item) {
return $item['iata'];
}, $modenasWorldPay);
if ($moneda_origen != $moneda_destino) {
//Valida la trm de aviatur y la compára con la que esta guardada en BD y trae la mas alta
$tasasDeCambio = $this->getExchangeRate($moneda_origen, $moneda_destino, 'FIV');
$this->session->set('tasasDeCambio', $tasasDeCambio);
if ($tasasDeCambio['Succeeded']) {
if (!$this->session->has('financialValue')) {
//La tasa financiera y la oficial deben de ser igual 09-01-2025
$financialValue = strtok($tasasDeCambio["OfficialExchangeRate"], '.');
$trmValue = strtok($tasasDeCambio["OfficialExchangeRate"], '.');
$this->session->set('financialValue', $financialValue);
$this->session->set('trmValue', $trmValue);
} else {
$financialValue = $this->session->get('financialValue');
$trmValue = $this->session->get('trmValue');
}
if (isset($financialValue)) {
$RateChange = 0;
if (!$this->session->has('RateChange')) {
$RateChange = floatval($tasasDeCambio["CurrencyValue"]);
$ExchangeRate = str_replace('.', '', $tasasDeCambio["CurrencyValue"]);
$this->session->set('RateChange', $RateChange);
if ($moneda_destino == "USD") {
$ExchangeRate = "10000000";
}
$get = ($ExchangeRate / 10_000_000) / $trmValue;
$getFinancial = ($ExchangeRate / 10_000_000) / $financialValue;
$finantial = (float) (1 / $financialValue);
$trm = (float) (1 / $trmValue);
$diff = (float) (($finantial - $trm) * $RateChange);
if ($diff < 0) {
$diff = 0;
}
$this->session->set('RateDiff', $diff);
if (0 == $RateChange) {
$get = 1;
$getFinancial = 1;
$this->session->set('typeCoin', 'COP');
}
} else {
$get = $this->session->get('RateChange');
}
}
}
}
$this->session->set('isActiveCurrency', $isActiveCurrency);
$this->session->set('CoinValueFinancial', $getFinancial);
$this->session->set('CoinValue', $get);
$this->session->set('CurrencyFrom', $moneda_origen);
$this->session->set('CurrencyTo', $moneda_destino);
$this->session->set('typeCountry', ('COP' == $this->session->get('typeCoin')) ? 'co' : $this->session->get('typeCountry'));
return $get;
}
public function InfoCoin($currencyTo, $coin = null, $fee = false)
{
$convertionPrice = 1;
if ($this->session->get('WorldChangeCoin')) {
if ($this->session->has('CoinValue')) {
$convertionPrice = $this->session->get('CoinValue');
} else {
$convertionPrice = $this->conversor_monedas($currencyTo, $this->session->get('typeCoin'), 1);
}
if (null != $coin) {
$initialCoin = $coin;
$coin = ($convertionPrice * $coin);
if ($fee && $this->session->has('CoinValueFinancial')) {
$coin = ($this->session->get('CoinValueFinancial') * $initialCoin);
}
}
} else {
$this->removeSessionValues();
}
return $coin;
}
public function setSessionValues($sessionValues)
{
foreach ($sessionValues as $sessionKey => $sessionValue) {
$this->session->set($sessionKey, $sessionValue);
}
}
public function removeSessionValues()
{
$this->session->remove('CoinValue');
$this->session->remove('financialValue');
$this->session->remove('ExchangeRates');
$this->session->remove('RateChange');
$this->session->remove('typeCoin');
$this->session->remove('isActiveCurrency');
$this->session->remove('tasasDeCambio');
}
public function validateChangeCoin($route)
{
if (!$route) {
return false;
}
$this->session->remove('WorldChangeCoin');
$paymentMethodAgency = $this->em->getRepository(\Aviatur\GeneralBundle\Entity\PaymentMethodAgency::class)->findWorldPayAgency($this->session->get('agencyId'));
$validateAgency = false;
$validateProduct = false;
if ((is_countable($paymentMethodAgency) ? count($paymentMethodAgency) : 0) > 0) {
$paymentWorldPay = json_decode($this->em->getRepository(\Aviatur\GeneralBundle\Entity\Parameter::class)->findOneByName('aviatur_payment_world_pay')->getDescription(), true);
$HistoricalRate = $this->em->getRepository(\Aviatur\TrmBundle\Entity\HistoricalRate::class)->findCurrency();
if ((is_countable($HistoricalRate) ? count($HistoricalRate) : 0) == 0) {
return false;
}
$validateAgency = true;
foreach ($paymentWorldPay as $key => $payments) {
foreach ($payments as $keyId => $payment) {
switch ($key) {
case 'productDisabled':
if (strstr($route, (string) $keyId) && $payment) {
$validateProduct = true;
}
break;
default:
return false;
}
}
}
if ($validateAgency && $validateProduct) {
$this->session->set('WorldChangeCoin', true);
return true;
}
}
if (!$validateAgency || !$validateProduct) {
$this->removeSessionValues();
}
return false;
}
public function storeCoinParams($product = null) {}
public function getCoinParams()
{
return;
}
// Obtiene la tasa de cambio
public function getExchangeRate($moneda_origen, $moneda_destino, $tipo_moneda)
{
$exchangeRateModel = new TrmModel();
$exchangeRates = $this->webService->callWebService('GENERALLAVE', 'dummy|http://www.aviatur.com.co/dummy/', $exchangeRateModel->getTasaCambio());
$exchangeRateRS = json_decode(json_encode($exchangeRates), true);
$exchangeRateValue = 0;
if (!empty($exchangeRateRS) && $exchangeRateRS['RESULTADO'] == "EXITO") {
$arrayExchangeRates = $exchangeRateRS['MENSAJE']['TASAS_CAMBIO']['ELEMENTO_TASA_CAMBIO'];
// Filtrar las tasas de cambio
$filteredExchangeRates = array_filter($arrayExchangeRates, function ($exchangeRate) use ($tipo_moneda, $moneda_origen, $moneda_destino) {
return $exchangeRate['TIPO_TASA_CAMBIO'] == $tipo_moneda &&
$exchangeRate['MONEDA_ORIGEN'] == $moneda_origen &&
$exchangeRate['MONEDA_DESTINO'] == $moneda_destino;
});
// Obtener el primer valor filtrado
$filteredExchangeRate = reset($filteredExchangeRates);
if ($filteredExchangeRate) {
$exchangeRateValue = $filteredExchangeRate['VALOR'];
}
}
// Obtener la tasa de cambio en la BD de acuerdo al tipo de moneda
switch ($tipo_moneda) {
case 'FIV':
$exchangeRatesBD = $this->em->getRepository(\Aviatur\TrmBundle\Entity\WorldPayCurrency::class)->tasaCambiosWorldPay($moneda_destino);
$exchangeRateBD = isset($exchangeRatesBD["OfficialExchangeRate"]) ? $exchangeRatesBD["OfficialExchangeRate"] : 0;
break;
default:
$exchangeRatesBD = $this->em->getRepository(\Aviatur\TrmBundle\Entity\Trm::class)->recentTrm($moneda_origen);
$exchangeRateBD = isset($exchangeRatesBD[0]['value']) ? $exchangeRatesBD[0]['value'] : 0;
break;
}
// Verificar si la tasa de cambio es 0, devolver respuesta de error
if ($exchangeRateBD == 0) {
return [
'Succeeded' => false,
'ErrorMessage' => 'No se pudo obtener la tasa de cambio.'
];
}
// Determinar la tasa de cambio financiera
$FinancialExchangeRate = $exchangeRateBD > $exchangeRateValue ? $exchangeRateBD : $exchangeRateValue;
return [
'FinancialExchangeRate' => $FinancialExchangeRate,
'OfficialExchangeRate' => $exchangeRateBD,
'CurrencyValue' => $exchangeRatesBD["CurrencyValue"] ?? $tipo_moneda,
'Succeeded' => true,
];
}
}