<?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 = "inv4085";
$bitMod = "inv";
$empCod = $_SESSION['empCod'];
$usuCod = utf8_decode($_SESSION['usuCod']);
$usuNom = utf8_decode($_SESSION['usuNom']);
$pIva = $_SESSION['pIva'];

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

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

	$func = new functions();

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

	$uArtict = "UPDATE artic set art_cosp=0.00,art_cosa=0.00,art_cosr=0.00 WHERE art_emp='$empCod'";

	function sMovii($codd) { 
	    global $empCod, $usuNom;
	    return "SELECT * FROM movii m LEFT JOIN tipodoci td ON m.mov_tipo=td.td_cod 
	    		WHERE mov_emp='$empCod' AND mov_cod='$codd' ORDER BY mov_emp,mov_cod,mov_fec";
	}

	function sArtic($codd) { 
	    global $empCod, $usuNom;
	    return "SELECT * FROM artic WHERE art_emp='$empCod' AND art_cod='$codd'";
	}

	function sDoce_det($codd, $tipo, $num, $fec) { 
		global $empCod, $usuNom;
		return "SELECT * FROM doce_det WHERE ded_emp='$empCod' AND ded_cod='$codd' AND ded_tipo='$tipo' AND ded_num='$num' AND ded_fec='$fec'";
	}

	function uArtic($codd, $cosp, $cosr, $cosa) { 
		global $empCod, $usuNom;
		return "UPDATE artic SET art_cosp='$cosp',art_cosa='$cosa',art_cosr='$cosr' WHERE art_emp='$empCod' AND art_cod='$codd'";
	}

	function uMovii($codd, $cosp, $tipo, $num, $fec) { 
		global $empCod, $usuNom;
		return "UPDATE movii SET mov_cosu='$cosp' WHERE mov_emp='$empCod' AND mov_tipo='$tipo' AND mov_num='$num' AND mov_fec='$fec' AND mov_cod='$codd'";
	}
	function uDocs_det($codd, $cosp, $tipo, $num, $fec) { 
		global $empCod, $usuNom;
		return "UPDATE docs_det SET dsd_cosu='$cosp' WHERE dsd_emp='$empCod' AND dsd_tipo='$tipo' AND dsd_num='$num' AND dsd_fec='$fec' AND dsd_cod='$codd'";
	}

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

	$select["artict"] = $db->query($sArtict);
	while ($rec = $db->fetchAssoc($select["artict"])) {
	    $codd=$rec["art_cod"];
	    $cospant=$rec["art_cosp"];
	    $cosrant=$rec["art_cosr"];
	    $cosaant=$rec["art_cosa"];

	    $sini=0;
	    $sinicon=0;
	    $exiact=0;
	    $select["movii"] = $db->query(sMovii($codd));
		while ($recmov = $db->fetchAssoc($select["movii"])) {
			$fec=$recmov["mov_fec"];
		   	$entra=$recmov["mov_entra"];
		   	$sale=$recmov["mov_sale"];
		   	$tipo=$recmov["mov_tipo"];
		   	$num=$recmov["mov_num"];
		   	$bodent=$recmov["mov_bodent"];
		   	$bodsal=$recmov["mov_bodsal"];
		   	$cosu=$recmov["mov_cosu"];
		   	$preu=$recmov["mov_preu"];
		   	$eos=$recmov["td_eos"];
		   	$inveini=$recmov["td_inveini"];
		   	$afecos=$recmov["td_afecos"];

		   	$exiant=$exiact;
		   	$exiact=($exiact+$entra)-$sale;


	        $select["artic"] = $db->query(sArtic($codd));
	        $recartic = $db->fetchAssoc($select['artic']);
	        $cospant=$recartic["art_cosp"];
		    $cosrant=$recartic["art_cosr"];
		    $cosaant=$recartic["art_cosa"];
			$costota=ROUND(($exiant*$cospant),4);

			if ($eos=='E' && $afecos=='S') {
		        $select["doce_det"] = $db->query(sDoce_det($codd,$tipo,$num,$fec));
		        $recded = $db->fetchAssoc($select['doce_det']);
		        if (($inveini=='S' || $inveini=='P')) {
		        	$cosu=$recded["ded_cosu"];
		        } else {
		        	$cosu=$recded["ded_cosu"];
					$cosu=ROUND(($recded["ded_cosu"]/(($pIva/100)+1)),4);
		        }
		        $tcosr=$cosu;
		        if ($exiant<0) {
		        	$costota=0;
		        	$tcosa=$cospant;
		        	$tcosp=$cosu;
		        } else {
		        	$tcostot=round(($entra*$cosu),4);
		        	if (($exiact>0)) {
		        		$tcosp=round((($tcostot+$costota)/$exiact),4);
		        	} else {
		        		$tcosp=$cosu;
		        	}
		        	$tcosa=$cospant;
		        }
			} else {
				$tcosa=$cosaant;
				$tcosr=$cosrant;
				$tcosp=$cospant;
			}
	   		if ($tcosp<0 && $cosu<0) {
	   			$tcosp=0;
	   		}
	   		if ($tcosp<0 && $cosu>0) {
	   			$tcosp=$cosu;
	   		}
	   		if ($tcosa<0) {
	   			$tcosa=0;
	   		}
	   		if ($tcosr<0) {
	   			$tcosr=0;
	   		}

			$resp = $db->uEx(uArtic($codd, $tcosp, $tcosr, $tcosa));
			$resp = $db->uEx(uMovii($codd, $tcosp, $tipo, $num, $fec));
			$resp = $db->uEx(uDocs_det($codd, $tcosp, $tipo, $num, $fec));
		}
	}

	$db->close();

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

	return json_encode($resp);
}
?>