<?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;
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, $cantidad)
{
$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->validacionTrm($moneda_destino);
$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;
}
public function validacionTrm($moneda_destino) {
//Obtiene la tasa de cambio
$trmModel = new TrmModel();
$financialTrm = $this->webService->callWebService('GENERALLAVE', 'dummy|http://www.aviatur.com.co/dummy/', $trmModel->getTasaCambio());
// if (!$financialTrm || !isset($financialTrm->MENSAJE->TASAS_CAMBIO->ELEMENTO_TASA_CAMBIO)) {
// return false;
// }
foreach ($financialTrm->MENSAJE->TASAS_CAMBIO->ELEMENTO_TASA_CAMBIO as $elemento) {
if ((string) $elemento->TIPO_TASA_CAMBIO === "FIV") {
$monedaOrigen = (string) $elemento->MONEDA_ORIGEN;
$monedaDestino = (string) $elemento->MONEDA_DESTINO;
$valor = (string) $elemento->VALOR;
}
}
//Obtiene las tasas de cambio de la bases de datos
$tasaCambioBD = $this->em->getRepository(\Aviatur\TrmBundle\Entity\WorldPayCurrency::class)->tasaCambiosWorldPay($moneda_destino);
if(!$tasaCambioBD["Succeeded"]) {
return [
'Succeeded' => false
];
}
$FinancialExchangeRate = $valor;
if($valor > $tasaCambioBD["OfficialExchangeRate"]) {
$FinancialExchangeRate = $tasaCambioBD["OfficialExchangeRate"];
}
return [
'FinancialExchangeRate' => $FinancialExchangeRate,
'OfficialExchangeRate' => $tasaCambioBD["OfficialExchangeRate"],
'CurrencyValue' => $tasaCambioBD["CurrencyValue"],
'Succeeded' => true,
];
}
}