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

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

function cerrar() {
	global $empCod, $usuNom, $bitMod, $bitForm, $selBan, $numAno, $selMes, $selMesNom, $numAjus;

	$priv = new privileges();
	if (!$priv->modify($_SESSION['usuPriv'])) {
		return json_encode($priv->rErrorModify());
	}

	$selMes=$_POST["selMes"];
	$selMesNom=$_POST["selMesNom"];
	$numAno=$_POST["numAno"];
	$selBan=$_POST["selBan"];
	$numAjus=$_POST["numAjus"];

	if ($selMes == 12) {
	  $selMesi = 1;
	  $numAnoi = $numAno + 1;
	} else {
	  $selMesi = $selMes + 1;
	  $numAnoi = $numAno;
	}   
	$numDiai = 1;
	$Fecini = (strval($numAnoi)."-".strval($selMesi)."-".strval($numDiai));

	# QUERYS
	$sMova0cd = "SELECT ma_emp,ma_ban,
	            SUM(ma_debe) AS ma_sdebe, 
	            SUM(ma_haber) AS ma_shaber 
	            FROM mov_acu WHERE ma_emp='$empCod' AND ma_ban='$selBan' AND ma_fec<'$Fecini' AND ma_stat='C' GROUP BY '$empCod','$selBan'";

	$sMovAcu0 = "SELECT ma_emp,ma_ban,
	            SUM(ma_debe) AS ma_sdebe, 
	            SUM(ma_haber) AS ma_shaber 
	            FROM mov_acu WHERE ma_emp='$empCod' AND ma_ban='$selBan' AND ma_fec>='$Fecini' GROUP BY '$empCod','$selBan'";

	$sMovAcu = "SELECT *,
	            TRIM(ma_bene) AS ma_bene,
	            TRIM(ma_con) AS ma_con 
	            FROM mov_acu WHERE ma_emp='$empCod' AND ma_ban='$selBan' AND ma_tipo='004' AND ma_fec<'$Fecini' AND ma_stat='C' AND ma_haber!=0.00 ORDER BY ma_fec,ma_tipo";
	    
	$sSaldob = "SELECT * FROM saldob WHERE salb_emp='$empCod' AND salb_num=$selBan";

	$sBancos = "SELECT *,
	            TRIM(ban_nom) AS ban_nom,
	            TRIM(ban_ctab) AS ban_ctab 
	            FROM bancos WHERE ban_emp='$empCod' AND ban_num=$selBan";

	$dConcilia = "DELETE FROM concilia WHERE con_emp='$empCod' AND con_ban=$selBan AND con_ano=$numAno AND con_mes='$selMes'";
	$dConci_det = "DELETE FROM conci_det WHERE cd_emp='$empCod' AND cd_ban=$selBan AND cd_ano=$numAno AND cd_mes='$selMes'"; 

	function iConcilia($salban, $cir, $depcir, $salem) { 
		global $empCod, $usuNom, $selBan, $numAno, $selMes, $numAjus;
		return "INSERT INTO concilia(con_emp, con_ban, con_ano, con_mes, con_salban, con_cir, con_depcir, con_ajus, con_salem) VALUES('$empCod', '$selBan', $numAno, '$selMes', $salban, $cir, $depcir, $numAjus, $salem)";	    
	}
	function iConci_det($tipo, $fec, $num, $bene, $con, $haber, $stat) { 
		global $empCod, $usuNom, $selBan, $numAno, $selMes;
		return "INSERT INTO conci_det(cd_emp, cd_ban, cd_ano, cd_mes, cd_tipo, cd_fec, cd_num, cd_bene, cd_con, cd_haber, cd_stat,cd_saldo) VALUES('$empCod', $selBan, $numAno, $selMes, '$tipo', '$fec', $num, '$bene', '$con', $haber, '$stat',0)";	    
	} 

	# EJECUCION DE QUERYS
	$db = new dbExec();
	$select["mov_acu"] = $db->query($sMovAcu0);
	$recma0 = $db->fetchAssoc($select['mov_acu']);
	if ($db->numRows($select["mov_acu"]) == 0) {
		$sDebemov=0;
		$sHabermov=0;
	} else {
		$sDebemov=$recma0["ma_sdebe"];
		$sHabermov=$recma0["ma_shaber"];
	}

	$select["mov_acu"] = $db->query($sMova0cd);
	$recma0 = $db->fetchAssoc($select['mov_acu']);
	if ($db->numRows($select["mov_acu"]) == 0) {
		$sumCheques=0;
		$sumMovi=0;
	} else {
		$sumCheques=$recma0["ma_shaber"];
		$sumMovi=$recma0["ma_sdebe"];
	}

	$select["bancos"]=$db->query($sBancos);
	$recban = $db->fetchAssoc($select['bancos']);
	$banNom=trim($recban["ban_nom"]);
	$banCtab=$recban["ban_ctab"];

	$select["saldob"]=$db->query($sSaldob);
	$recsal = $db->fetchAssoc($select['saldob']);
	$Saldo=$recsal["salb_saldo"];

	$salConta=($Saldo-$sDebemov)+$sHabermov;
	$salCuenta=($salConta+$sumCheques)-($sumMovi+$numAjus);

	# Borra y vuelve a grabar conciliacion
	$db->query($dConcilia);
	$db->query($dConci_det);

	$resp = $db->iEx(iConcilia($salCuenta, $sumCheques, $sumMovi, $salConta));

	$select["mov_acu"] = $db->query($sMovAcu);
	while ($rec = $db->fetchAssoc($select['mov_acu'])) {
	    $tipo = $rec["ma_tipo"];
	    $fec = $rec["ma_fec"];
	    $num = $rec["ma_num"];
	    $bene = $rec["ma_bene"];
	    $con = $rec["ma_con"];
	    $haber = $rec["ma_haber"];
	    $stat = $rec["ma_stat"];
		$db->query(iConci_det($tipo, $fec, $num, $bene, $con, $haber, $stat));
	}

	$func = new functions();
	$db->iEx($func->iBitacora(utf8_decode("Se Cerró Conciliación Banco: $selBan $banNom Año: $numAno  Mes: $selMesNom Usuario: ").$usuNom, $bitMod, $bitForm));

	$db->close();
	$resp["msg"]["title"] = "Mes Cerrado";
	return json_encode($resp);
}

function abrir() {
	global $empCod, $usuNom, $bitMod, $bitForm, $selBan, $numAno, $selMes, $selMesNom, $numAjus;

	$priv = new privileges();
	if (!$priv->consult($_SESSION['usuPriv'])) {
		return json_encode($priv->rErrorConsult());
	}

	$selMes=$_POST["selMes"];
	$selMesNom=trim($_POST["selMesNom"]);
	$numAno=$_POST["numAno"];
	$selBan=$_POST["selBan"];
	$numAjus=$_POST["numAjus"];


	$dConcilia = "DELETE FROM concilia WHERE con_emp='$empCod' AND con_ban=$selBan AND con_ano=$numAno AND con_mes='$selMes'";
	$dConci_det = "DELETE FROM conci_det WHERE cd_emp='$empCod' AND cd_ban=$selBan AND cd_ano=$numAno AND cd_mes='$selMes'";

	# EJECUCION DE QUERYS
	$db = new dbExec();

	# Borra la conciliacion para que se habra nuevamente
	$db->query($dConcilia);
	$resp = $db->dEx($dConci_det);

	$func = new functions();
	$db->iEx($func->iBitacora(utf8_decode("Se Abrió Conciliacion Banco: $selBan  Año: $numAno  Mes: $selMesNom  Usuario: ").$usuNom, $bitMod, $bitForm));

	$db->close();
	$resp["msg"]["title"] = "Mes Abierto";
	return json_encode($resp);
}

?>