<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {
	function __construct() {
		parent::__construct();
		# Hace falta configurar con cual ConfigSolucion no se visualizara el sitio por default todos los sitios se deshabilitan para crm posvente
		if ($this->config->item('ConfigSolucion') !== 'crm posventa' && strpos(current_url(), 'forbidden') === false ) {
			redirect('forbidden','refresh');
		}
	}
	public function versionIntelisis($sucursal)
	{
		try{
			$this->load->library('ConfigWriter', array(
				'file'          => '/config.php' ,
				'variable_name' => 'config'
			));
			$int = $this->load->database("other", TRUE);
			// Obtener versión intelisis
			$version = $int->select('version')->from('Version')->get()->row_array();
			$version = isset($version['version']) ? $version : 4000;
			$version = $version['version'] < 6000 ? 'V4000' : 'V6000';
			$this->config->set_item('ConfigVersionIntelisis', $version);
			$this->configwriter->write('ConfigVersionIntelisis', $version);
			// Obtener marca
			$marca = $int->select('Version')->from('Sucursal')->where(['Estatus' => 'ALTA', 'Sucursal' => $sucursal])->get()->row_array();
			$marca = isset($marca['Version']) ? $marca['Version'] : null;
			$this->config->set_item('ConfigMarca', $marca);
			$this->configwriter->write('ConfigMarca', $marca);

			// Obtener empresa
			$empresa = $int->select('Empresa')->from('Empresa')->where(['Estatus' => 'ALTA'])->get()->row_array();
			$empresa = isset($empresa['Empresa']) ? $empresa['Empresa'] : null;
			$this->config->set_item('ConfigEmpresa', $empresa);
			$this->configwriter->write('ConfigEmpresa', $empresa);
			$grupo = $int->select('Grupo')->from('Empresa')->where(['Estatus' => 'ALTA'])->get()->row_array();
			$grupo = isset($grupo['Grupo']) ? $grupo['Grupo'] : null;
			$this->config->set_item('ConfigGrupo', $grupo);
			$this->configwriter->write('ConfigGrupo', $grupo);
			$nombre = $int->select('Nombre')->from('Empresa')->where(['Estatus' => 'ALTA'])->get()->row_array();
			$nombre = isset($nombre['Nombre']) ? $nombre['Nombre'] : null;
			$this->configwriter->write('ConfigNombre', $nombre);
			$suc = $int->select('Nombre')->from('Sucursal')->where(['Estatus' => 'ALTA', 'Sucursal' => $sucursal])->get()->row_array();
			$suc = isset($suc['Nombre']) ? $suc['Nombre'] : null;
			$this->configwriter->write('ConfigSucursal', $suc);
		}catch(Exception $ex){
			/*$this->config->set_item('ConfigVersionIntelisis', getenv("VERSION"));
			$this->config->set_item('ConfigMarca', getenv("MARCA"));
			$this->config->set_item('ConfigEmpresa', getenv("EMPRESA"));
			$this->configwriter->write('ConfigMarca', getenv("MARCA"));
			$this->configwriter->write('ConfigVersionIntelisis', getenv("VERSION"));
			$this->configwriter->write('ConfigEmpresa', getenv("EMPRESA"));*/
		}
	}
}