<?php
include_once('../../../php/sessionStart.php');
include_once('../../../php/db/dbExec.php');
include_once('../../../php/functions.php');
include_once('../../../php/timezone.php');
include_once('../../../php/privileges.php');
include_once('../../../php/maxExecTime.php');

$bitForm = "inv4070";
$bitMod = "inv";
$empCod = $_SESSION['empCod'];
$usuCod = utf8_decode($_SESSION['usuCod']);
$usuNom = utf8_decode($_SESSION['usuNom']);

switch ($_POST['action']) {
	case 'proceso':
		echo proceso();
		break;
}

function proceso() {
	global $empCod, $usuNom, $bitMod, $bitForm, $dtFeci;

	$func = new functions();

	# QUERYS
	/*
	$sArtic = "SELECT * FROM artic WHERE art_emp='$empCod' AND art_bienser!='S'";

	$uExiart00 = "UPDATE exiart set ea_exist=0 WHERE ea_emp='$empCod'";  */

	$uExiart00peps = "UPDATE exiart_peps set ea_exist=0 WHERE ea_emp='$empCod'";

	$sExiartpeps = "SELECT * FROM exiart_peps WHERE ea_emp='$empCod'";

//	$sBodegas = "SELECT * FROM bodegas WHERE bod_emp='$empCod'";

/*
	function sMoviie0($codd, $bodega) { 
	    global $empCod, $usuNom;
	    return "SELECT mov_emp,mov_cod,mov_bodent,mov_bodsal,
	            SUM(mov_entra) AS mov_sentra
	            FROM movii WHERE mov_emp='$empCod' AND mov_cod='$codd' AND mov_bodent='$bodega' GROUP BY '$empCod','$bodega','$codd'";
	}

	function sMoviis0($codd, $bodega) { 
	    global $empCod, $usuNom;
	    return "SELECT mov_emp,mov_cod,mov_bodent,mov_bodsal,
	            SUM(mov_sale) AS mov_ssale
	            FROM movii WHERE mov_emp='$empCod' AND mov_cod='$codd' AND mov_bodsal='$bodega' GROUP BY '$empCod','$bodega','$codd'";
	}

*/
	function sMoviie0peps($codd, $bodega, $fvence) { 
	    global $empCod, $usuNom;
	    return "SELECT mov_emp,mov_cod,mov_bodent,mov_bodsal,
	            SUM(mov_entra) AS mov_sentrapeps
	            FROM movii_peps WHERE mov_emp='$empCod' AND mov_cod='$codd' AND mov_bodent='$bodega' AND mov_fvence='$fvence' GROUP BY '$empCod','$bodega','$codd','$fvence'";
	}

	function sMoviis0peps($codd, $bodega, $fvence) { 
	    global $empCod, $usuNom;
	    return "SELECT mov_emp,mov_cod,mov_bodent,mov_bodsal,
	            SUM(mov_sale) AS mov_ssalepeps
	            FROM movii_peps WHERE mov_emp='$empCod' AND mov_cod='$codd' AND mov_bodsal='$bodega' AND mov_fvence='$fvence' GROUP BY '$empCod','$bodega','$codd','$fvence'";
	}

/*
	function sExiart($codd, $bodega) { 
	    global $empCod, $usuNom;
	    return "SELECT * FROM exiart WHERE ea_emp='$empCod' AND ea_cod='$codd' AND ea_bod='$bodega'";
	}

	function uExiart($codd, $bodega, $nexist) { 
		global $empCod, $usuNom;
		return "UPDATE exiart SET ea_exist='$nexist' WHERE ea_emp='$empCod' AND ea_cod='$codd' AND ea_bod='$bodega'";
	}
	function iExiart($codd, $bodega, $nexist) { 
		global $empCod, $usuNom;
		return "INSERT INTO exiart(ea_emp,ea_cod,ea_bod,ea_exist) VALUES('$empCod','$codd','$bodega','$nexist')";
	}

*/
/*
	function sExiartpeps($codd,$bodega,$fvence) { 
		global $empCod, $usuNom;
		return "SELECT * FROM exiart_peps WHERE ea_emp='$empCod' AND ea_cod='$codd' AND ea_bod='$bodega' AND ea_fvence='$fvence'";
	}
*/
	function uExiartpeps($codd,$bodega,$nexist,$fvence) { 
		global $empCod, $usuNom;
		return "UPDATE exiart_peps SET ea_exist='$nexist' WHERE ea_emp='$empCod' AND ea_cod='$codd' AND ea_bod='$bodega' AND ea_fvence='$fvence'";
	}
/*
	function iExiartpeps($codd,$bodega,$nexist,$lote,$fvence,$cosu) { 
		global $empCod, $usuNom;
		return "INSERT INTO exiart_peps(ea_emp,ea_cod,ea_bod,ea_exist,ea_lote,ea_fvence,ea_cosu,ea_codp) VALUES('$empCod','$codd','$bodega','$nexist','','$fvence',$cosu,'')";
	}

*/
	# EJECUCION DE QUERYS 
	$db = new dbExec();
	// Para mandar todo a cero
	$resp = $db->query($uExiart00peps);

	$select["exiart_peps"] = $db->query($sExiartpeps);
	while ($rec = $db->fetchAssoc($select["exiart_peps"])) {
	    $codd=$rec["ea_cod"];
	    $bodega=$rec["ea_bod"];
	    $fvence=$rec["ea_fvence"];

        $select["movii"] = $db->query(sMoviie0peps($codd,$bodega,$fvence));
        $recme0 = $db->fetchAssoc($select['movii']);
        $sEntramov=$recme0["mov_sentrapeps"];

        $select["movii"] = $db->query(sMoviis0peps($codd,$bodega,$fvence));
        $recms0 = $db->fetchAssoc($select['movii']);
        $sSalemov=$recms0["mov_ssalepeps"];

        $nexist=($sEntramov-$sSalemov);

		$resp = $db->uEx(uExiartpeps($codd,$bodega,$nexist,$fvence));
	}

	$db->close();

	//$func->unlinkEmpCod($empCod, $txtCod, '../imgArticulos');

	return json_encode($resp);
}
?>