<?php
session_start(); 
////
/// Acciones dentro del tallet
///

//$file = fopen("log.txt", "w");   /// BORRAR LOG
//fwrite($file, "$sql". PHP_EOL);
//fclose($file);


header("Content-Type: acciones_taller/json");

//------------------------------------------------ Prepara la primera pantalla -----------------------------------------------------------------------------------------
$view='jsonfiles/acciones_taller.json';


$taller=$_GET["taller"];
$user=$_GET["user"];

$data1=array();
$grid=array();
$output = array();

$config = require ('config.php');
$conexion=$config['conexion']; 

$conn =  pg_pconnect("$conexion");

$data1['taller']=$taller;
$data1['user']=$user;
$data1['url']= $config['URL'];  
$data1['extjs']='jscss/acciones_taller.js?'.trim(date_timestamp_get(date_create()));

if (!isset($_POST['mode'])) {
	cargar_taller($conn, $taller);	
	$output = array('data' => $data1, 'view' => $view, 'screen' => 'fmt01');
}
if (isset($_POST['mode']) && $_POST['mode'] == 'salir') { 
	$user=$_POST['user'];
	$controler= $data1['url']."phpfiles/consolida_rf.php?user=$user";
	$output = array('controller' => $controler);
}
if (isset($_POST['mode']) && $_POST['mode'] == 'taller'){
	$data1['mode']=$_POST['mode'];
	cargar_taller($conn, $taller);	
	$output = array('data' => $data1, 'view' => $view, 'screen' => 'fmt01');
}
if (isset($_POST['mode']) && $_POST['mode'] == 'soporte') {
	$data1['sselect']=$_POST['sselect'];
	$data1['sselect2']=$_POST['sselect'];
	$data1['zona2']=$_POST['zona2'];
	$data1['mode']=$_POST['mode'];
	$hay=cargar_soporte($conn, $data1['sselect']);
	if ($hay=='S') {	
		$output = array('data' => $data1, 'view' => $view, 'screen' => 'fmt01');
	} else {
		$data1['mode']='taller';
		cargar_taller($conn, $taller);	
		$output = array('data' => $data1, 'view' => $view, 'screen' => 'fmt01');	
	}			
}


utf8_encode_deep($output);
echo  json_encode($output);	

//
//------------------------ Carga ultimas peticiones 
// 

function cargar_taller($conn,$taller) {
	
global $data1;	

	$grid = array();

	$sql="SELECT A.ras, A.zona, COALESCE ( A.tienda, '-' ) tienda, 
				COALESCE ((SELECT b.tienda_descripcion FROM items b WHERE b.tienda_code = A.tienda LIMIT 1),'-') item_descripcion,
				COALESCE(a.soporte_activo,'0') soporte_activo
			FROM zonas A where ras=$taller 	ORDER BY zona";
			
	$result = pg_query($conn, $sql) or die('La consulta fallo: ' . pg_last_error());
	while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC)) {	
		array_push($grid, array(
			'zona' => $row['zona'],
			'sselect' => $row['soporte_activo'],
			'tienda' => trim($row['tienda']).' '.trim($row['item_descripcion'])));	
	}		

	$data1['grid1'] = $grid;

	$conn=null;


}
//
//------------------------ Carga ultimas peticiones 
// 

function cargar_soporte($conn,$soporte) {
	
global $data1;	

	$grid = array();
	$bultos=0;
	
	$hay='N';

	$sql="SELECT * FROM soportes_det WHERE soporte='$soporte' order by mfin_clasifica";

			
	$result = pg_query($conn, $sql) or die('La consulta fallo: ' . pg_last_error());
	while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC)) {
		$iresult='-';
		$hay='S';
		if ($row['vcheck_status']==0) {
			$iresult='images/interrogacion.png';
		}		
		if ($row['vcheck_status']==1) {
			$iresult='images/ajusteverde.png';
		}
		if ($row['vcheck_status']==2) {
			$iresult='images/alertat.png';
		}		
			
		array_push($grid, array(
			'iresult' => $iresult,
			'item' => $row['item_code']));
			
		$bultos=$bultos+1;	
	}		

	$data1['grid2'] = $grid;
	$data1['bultos'] = $bultos.' Bultos';

	$conn=null;

	return($hay);

}

function utf8_encode_deep(&$input) {
	
	if (is_string($input)) {
		$input = utf8_encode($input);
	} else if (is_array($input)) {
		foreach ($input as &$value) {
			utf8_encode_deep($value);
		}
		
		unset($value);
	} else if (is_object($input)) {
		$vars = array_keys(get_object_vars($input));
		
		foreach ($vars as $var) {
			utf8_encode_deep($input->$var);
		}
	}
}

?>