<?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'";

	$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 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')";
	}



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

	$select["artic"] = $db->query($sArtic);
	while ($rec = $db->fetchAssoc($select["artic"])) {
	    $codd=$rec["art_cod"];
	    $select["bodegas"] = $db->query($sBodegas);
		while ($recbod = $db->fetchAssoc($select["bodegas"])) {
			$bodega=$recbod["bod_cod"];

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

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

	        $nexist=($sEntramov-$sSalemov);

		    $select["exiart"] = $db->query(sExiart($codd,$bodega));
		    $fetchAssoc["exiart"] = $db->fetchAssoc($select["exiart"]);
	    	if ($db->numRows($select["exiart"]) > 0) {
				$resp = $db->uEx(uExiart($codd,$bodega,$nexist));
		    } else {
		    	if ($nexist!=0) {
				  $resp = $db->iEx(iExiart($codd,$bodega,$nexist));
				}
		    }
		}
	}

	$db->close();

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

	return json_encode($resp);
}
?>