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

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

switch ($_POST['action']) {

	case 'guardar':
		echo guardar();
		break;

	case 'consultarCta':
		echo consultarCta();
		break;

	case 'corrPoliza':
		echo corrPoliza();
		break;

	case 'mCerrado':
		echo mCerrado();
		break;

	case 'generarPartida':
		echo generarPartida();
		break;

	case 'consultarPoliza':
		echo consultarPoliza();
		break;
}

function guardar() {
	global $empCod, $usuNom, $bitMod, $bitForm, $selBanCod, $selTipoCod, $numNum, $dtFec;

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

	$func = new functions();

	$numNum=$_POST['numPol'];
	$nuevocorr=$numNum+1;
	$selTipoCod="016";
	$selBanCod=0;
	$dtFec=$_POST['dtFec'];
	$dtDel=$_POST['dtDel'];
	$dtAl=$_POST['dtAl'];
	$crTrepo=$_POST['crTrepo'];
	$txtCon="Poliza de Sueldos Correspondiente del ".strftime("%d/%m/%Y", strtotime($dtDel))." al ".strftime("%d/%m/%Y", strtotime($dtAl))."  ".$crTrepo;
	$jsonPol=json_decode($_POST['tblPol']);
	$numAnio=date("Y", strtotime($dtFec));
	$numMes=date("n", strtotime($dtFec));
	$dtGrabado=date("Y-m-d H:i:s", STRTOTIME(date('h:i:sa')));

	# QUERYS PARA UN REGISTRO

	$iPolizas = "INSERT INTO polizas(pol_emp, pol_fec, pol_ban, pol_tipo, pol_num, pol_con, pol_ulcam, pol_grabado) VALUES ('$empCod','$dtFec','$selBanCod','$selTipoCod','$numNum','$txtCon','$usuNom','$dtGrabado')";

	$uPolizas = "UPDATE polizas SET pol_fec='$dtFec', pol_con='$txtCon', pol_ulcam='$usuNom', pol_grabado='$dtGrabado' WHERE pol_emp='$empCod' AND pol_ban='$selBanCod' AND pol_tipo='$selTipoCod' AND pol_num='$numNum'";

	$uCorrpol = "UPDATE corrpol SET cp_num=$nuevocorr WHERE cp_emp='$empCod' AND cp_tipo='$selTipoCod'";

	function sCuentas($cta) { 
		global $empCod;
		return "SELECT * FROM cuentas WHERE cta_emp='$empCod' AND cta='$cta'";
	}

	function sCierres($cta, $numAnio, $numMes) { 
		global $empCod;
		return "SELECT * FROM cierres WHERE cie_emp='$empCod' AND cie_ano=$numAnio AND cie_mes=$numMes AND cie_cta='$cta'";
	}

	function uCierres($cta, $numAnio, $numMes, $mdebe, $mhaber) { 
		global $empCod, $usuNom;
		return "UPDATE cierres SET cie_mdebe=$mdebe, cie_mhaber=$mhaber, cie_ulcam='$usuNom' WHERE cie_emp='$empCod' AND cie_ano=$numAnio AND cie_mes=$numMes AND cie_cta='$cta'";
	}

	function iCierres($cta, $numAnio, $numMes, $mdebe, $mhaber) { 
		global $empCod, $usuNom;
		return "INSERT INTO cierres(cie_emp, cie_ano, cie_mes, cie_cta, cie_mdebe, cie_mhaber, cie_ulcam) VALUES('$empCod',$numAnio,$numMes,'$cta',$mdebe,$mhaber,'$usuNom')";	    
	}

	# QUERYS PARA VARIOS REGISTROS
	$iPoli_det = "INSERT INTO poli_det (pold_emp, pold_fec, pold_ban, pold_tipo, pold_num, pold_cta, pold_ctan, pold_debe, pold_haber, pold_tipoi, pold_nit) VALUES ";
	foreach ($jsonPol as $obj) {
		$obj->ctaNom = utf8_decode($obj->ctaNom);
		$iPoli_det = $iPoli_det."('$empCod','$dtFec','$selBanCod','$selTipoCod','$numNum','$obj->cta','$obj->ctaNom','$obj->debe','$obj->haber','','')".$func->addValues($obj, $jsonPol);
	}

	$dPolizas = "DELETE FROM polizas WHERE pol_emp='$empCod' AND pol_ban='$selBanCod' AND pol_tipo='$selTipoCod' AND pol_num='$numNum'";

	$dPoli_det = "DELETE FROM poli_det WHERE pold_emp='$empCod' AND pold_ban='$selBanCod' AND pold_tipo='$selTipoCod' AND pold_num='$numNum'";

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

	// Revisa si hay algun problema en la partida, para ver si permite grabar o no
	$probpol1="X";
	$probpol2="X";	
	$probpol3="X";	
	foreach ($jsonPol as $obj) {
		$ctapol = trim($obj->cta);
		$haber = trim($obj->haber);

		$select["cuentas"] = $db->query(sCuentas($ctapol));
		$fetchAssoc["cuentas"] = $db->fetchAssoc($select["cuentas"]);
		//$ctaC = trim($fetchAssoc["cuentas"]["cta"]);
		if ($db->numRows($select["cuentas"]) == 0) {
			$probpol1 = "A";
		}
	}
	//

	if ($probpol1 == "X") {
		//
		$resp = $db->uEx($uCorrpol);
		$db->iEx($func->iBitacora(utf8_decode("Se creó Registro Poliza de Sueldos No.: $numNum  Fecha: $dtFec Usuario: ").$usuNom, $bitMod, $bitForm));

		$db->dEx($dPolizas);
		$db->dEx($iPolizas);

		$db->dEx($dPoli_det);
		$resp = $db->iEx($iPoli_det);

		// PARA APLICAR LOS CIERRES
		foreach ($jsonPol as $obj) {
			$cta = $obj->cta;
			while (!empty($cta)) {
				$select["cuentas"] = $db->query(sCuentas($cta));
				$select["cierres"] = $db->query(sCierres($cta, $numAnio, $numMes));

				$fetchAssoc["cuentas"] = $db->fetchAssoc($select["cuentas"]);
				$ctaActa = trim($fetchAssoc["cuentas"]["cta_acta"]);

				if ($db->numRows($select["cierres"]) > 0) {
					$fetchAssoc["cierres"] = $db->fetchAssoc($select["cierres"]);
					$mdebe=$fetchAssoc["cierres"]["cie_mdebe"]+$obj->debe;
					$mhaber=$fetchAssoc["cierres"]["cie_mhaber"]+$obj->haber;

					$db->query(uCierres($cta, $numAnio, $numMes, $mdebe, $mhaber));
				} else {
					$mdebe=$obj->debe;
					$mhaber=$obj->haber;
					$db->query(iCierres($cta, $numAnio, $numMes, $mdebe, $mhaber));
				}
				$cta = $ctaActa;
			}

		}
	}

	$db->close();
	
	$resp["dtGrabado"]=$dtGrabado;
	$resp["probpol1"]=$probpol1;
	$resp["probpol2"]=$probpol2;
	$resp["probpol3"]=$probpol3;
	return json_encode($resp);
}

function corrPoliza() {
	global $empCod;

	$func = new functions();

	$tipopol="016";

	# QUERYS
	$sCorrpol = "SELECT cp_num FROM corrpol WHERE cp_emp='$empCod' AND cp_tipo='$tipopol'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();

	$select["corrpol"] = $db->query($sCorrpol);
	if ($db->numRows($select["corrpol"])>0) {
		$numDoc = ($db->fetchAssoc($select["corrpol"])["cp_num"]);
	} else {
		$numDoc = 1;
		$r["corrpol"] = $db->query("INSERT INTO corrpol (cp_emp, cp_tipo, cp_num) VALUES('$empCod', '$tipopol', $numDoc);");
	}

	$resp["corrpol"]["cp_num"] = $numDoc;
	$db->close();

	$func = new functions();
	return $func->jsonEncodeArray($resp, "corrpol");
}

function mCerrado() {
	global $empCod, $usuNom;

	$func = new functions();
	$dtFec=$_POST['dtFec'];
	$nAnio=date("Y", strtotime($dtFec));
	$nMes=date("n", strtotime($dtFec));

	# QUERYS
	$sCiemes = "SELECT ciem_stat FROM ciemes WHERE ciem_emp='$empCod' AND ciem_ano=$nAnio AND ciem_mes=$nMes";
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp["ciemes"] = $db->sfetchAssoc($sCiemes);
	$db->close();
	$func = new functions();
	return $func->jsonEncodeArray($resp, "ciemes");
}

function consultarCta() {
	global $empCod;
	$txtCod=utf8_decode($_POST['txtCod']);

	# QUERYS
	$sCuentas = "SELECT c.*, TRIM(cta) AS cta, TRIM(cta_nom) AS cta_nom
				FROM cuentas c WHERE cta='$txtCod' AND cta_emp='$empCod'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sCuentas, "cuentas");
	$db->close();

	$func = new functions();
	return $func->jsonEncodeArray($resp, "cuentas");
}

function consultarPoliza() {
	global $empCod;

	$numPol=$_POST['numPol'];
	$tipopol="016";

	# QUERYS
	$sPoliza = "SELECT *
				FROM polizas WHERE pol_emp='$empCod' AND pol_tipo='$tipopol' AND pol_num='$numPol'";
	
	# EJECUCION DE QUERYS
	$db = new dbExec();
	$resp = $db->sEx($sPoliza, "polizas");
	$db->close();

	$func = new functions();
	return $func->jsonEncodeArray($resp, "polizas");
}

function generarPartida() {
	global $empCod, $empCtaic;

	$func = new functions();

	$dtDel = utf8_decode($_POST["dtDel"]);
	$dtAl = utf8_decode($_POST["dtAl"]);
	$crTrepo = utf8_decode($_POST["crTrepo"]);

	# QUERYS
	/*function sCuentas($cta,$debe,$haber) {
		global $empCod;
		return "SELECT TRIM(cta) AS cta, TRIM(cta_nom) AS cta_nom, $debe AS cta_debe, $haber AS cta_haber
				FROM cuentas c WHERE cta='$cta' AND cta_emp='$empCod'";
	}*/
	function sCuentas($cta,$valor,$valorh) {
		global $empCod;
		return "SELECT TRIM(cta) AS cta, TRIM(cta_nom) AS cta_nom, $valor AS cta_valor, $valorh AS cta_valorh 
				FROM cuentas c WHERE cta='$cta' AND cta_emp='$empCod'";
	}
		// Se quito el calculo de provisiones, segun requerimiento
/*		$sNomper = "SELECT SUM(np.np_sord+np.np_ssept) AS np_sumsord, SUM(np.np_sexts+np.np_sextd) AS np_sumsext,
					SUM(np.np_boni) AS np_sumboni, SUM(np.np_comi) AS np_sumcomi, SUM(np.np_bonii+np.np_sferi) AS np_sumbonii,
					SUM(np.np_bonoe) AS np_sumbonoe, SUM(np.np_depre) AS np_sumdepre, SUM(np.np_viati) AS np_sumviati,
					SUM(np.np_isrdevo) AS np_sumisrdevo, SUM(np.np_otrp) AS np_sumotrp,
		            SUM(np.np_igss) AS np_sumigss, SUM(np.np_isr) AS np_sumisr, SUM(np.np_preb) AS np_sumpreb, 
		            SUM(np.np_pree) AS np_sumpree, SUM(np.np_desj) AS np_sumdesj,
		            SUM(np.np_unif+np.np_lent+np.np_anti+np.np_otrd) AS np_sumotrd, 
		            SUM(np.np_liqrec) AS np_sumliqrec, SUM(np.np_sord)*9.72/100 AS np_suminde, 
		            SUM(np.np_sord)*8.33/100 AS np_sumbono14, SUM(np.np_sord)*8.33/100 AS np_sumagui, 
		            SUM(np.np_sord)*4.17/100 AS np_sumvaca, 
		            SUM(np.np_sord+np.np_sexts+np.np_sextd+np.np_comi+np.np_otrp)*12.67/100 AS np_sumcuotapat, 
		            0 AS np_cero,  
		            dep.*
		            FROM nomper np
		            LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
		            LEFT JOIN dep dep ON dep.dep_emp=mae.mae_emp AND dep.dep_cod = mae.mae_dep
		            WHERE np_emp='$empCod' AND np_ffin>='$dtDel' AND np_ffin<='$dtAl' GROUP BY np_emp,mae_gru,mae_dep";  */

	if ($crTrepo=='T') {
		$sNomper = "SELECT SUM(np.np_sord+np.np_ssept) AS np_sumsord, SUM(np.np_sexts+np.np_sextd) AS np_sumsext,
					SUM(np.np_boni) AS np_sumboni, SUM(np.np_comi) AS np_sumcomi, SUM(np.np_bonii+np.np_sferi+np.np_difprod) AS np_sumbonii,
					SUM(np.np_bonoe) AS np_sumbonoe, SUM(np.np_depre) AS np_sumdepre, SUM(np.np_viati) AS np_sumviati,
					SUM(np.np_isrdevo) AS np_sumisrdevo, SUM(np.np_otrp) AS np_sumotrp,
		            SUM(np.np_igss) AS np_sumigss, SUM(np.np_isr) AS np_sumisr, SUM(np.np_preb) AS np_sumpreb, 
		            SUM(np.np_pree) AS np_sumpree, SUM(np.np_desj) AS np_sumdesj,
		            SUM(np.np_unif) AS np_sumunif, SUM(np.np_lent) AS np_sumlent,
					SUM(np.np_anti) AS np_sumanti, SUM(np.np_otrd) AS np_sumotrd, 
		            SUM(np.np_pagado) AS np_sumliqrec, 0 AS np_suminde, 
		            0 AS np_sumbono14, 0.00 AS np_sumagui, 
		            0 AS np_sumvaca, 
		            0 AS np_sumcuotapat, 
		            0 AS np_cero,  
		            dep.*
		            FROM nomper np
		            LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
		            LEFT JOIN dep dep ON dep.dep_emp=mae.mae_emp AND dep.dep_cod = mae.mae_dep
		            WHERE np_emp='$empCod' AND np_ffin>='$dtDel' AND np_ffin<='$dtAl' GROUP BY np_emp,mae_gru,mae_dep";
	}
	if ($crTrepo=='S') {
		$sNomper = "SELECT SUM(np.np_sord+np.np_ssept) AS np_sumsord, SUM(np.np_sexts+np.np_sextd) AS np_sumsext,
					SUM(np.np_boni) AS np_sumboni, SUM(np.np_comi) AS np_sumcomi, SUM(np.np_bonii++np.np_difprod) AS np_sumbonii,
					SUM(np.np_bonoe) AS np_sumbonoe, SUM(np.np_depre) AS np_sumdepre, SUM(np.np_viati) AS np_sumviati,
					SUM(np.np_isrdevo) AS np_sumisrdevo, SUM(np.np_otrp) AS np_sumotrp,
		            SUM(np.np_igss) AS np_sumigss, SUM(np.np_isr) AS np_sumisr, SUM(np.np_preb) AS np_sumpreb, 
		            SUM(np.np_pree) AS np_sumpree, SUM(np.np_desj) AS np_sumdesj,
		            SUM(np.np_unif) AS np_sumunif, SUM(np.np_lent) AS np_sumlent,
					SUM(np.np_anti) AS np_sumanti, SUM(np.np_otrd) AS np_sumotrd, 
		            SUM(np.np_pagado) AS np_sumliqrec, 0 AS np_suminde, 
		            0 AS np_sumbono14, 0.00 AS np_sumagui, 
		            0 AS np_sumvaca, 
		            0 AS np_sumcuotapat, 
		            0 AS np_cero,  
		            dep.*
		            FROM nomper np
		            LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
		            LEFT JOIN dep dep ON dep.dep_emp=mae.mae_emp AND dep.dep_cod = mae.mae_dep
		            WHERE np_emp='$empCod' AND np_ffin>='$dtDel' AND np_ffin<='$dtAl' AND mae_igsssn='$crTrepo' GROUP BY np_emp,mae_gru,mae_dep";
	}	
	if ($crTrepo=='N') {
		$sNomper = "SELECT SUM(np.np_sord+np.np_ssept) AS np_sumsord, SUM(np.np_sexts+np.np_sextd) AS np_sumsext,
					SUM(np.np_boni) AS np_sumboni, SUM(np.np_comi) AS np_sumcomi, SUM(np.np_bonii+np.np_sferi+np.np_difprod) AS np_sumbonii,
					SUM(np.np_bonoe) AS np_sumbonoe, SUM(np.np_depre) AS np_sumdepre, SUM(np.np_viati) AS np_sumviati,
					SUM(np.np_isrdevo) AS np_sumisrdevo, SUM(np.np_otrp) AS np_sumotrp,
		            SUM(np.np_igss) AS np_sumigss, SUM(np.np_isr) AS np_sumisr, SUM(np.np_preb) AS np_sumpreb, 
		            SUM(np.np_pree) AS np_sumpree, SUM(np.np_desj) AS np_sumdesj,
		            SUM(np.np_unif) AS np_sumunif, SUM(np.np_lent) AS np_sumlent,
					SUM(np.np_anti) AS np_sumanti, SUM(np.np_otrd) AS np_sumotrd, 
		            SUM(np.np_pagado) AS np_sumliqrec, 0 AS np_suminde, 
		            0 AS np_sumbono14, 0.00 AS np_sumagui, 
		            0 AS np_sumvaca, 
		            0 AS np_sumcuotapat, 
		            0 AS np_cero, dep.*
		            FROM nomper np
		            LEFT JOIN maestro mae ON np.np_emp=mae.mae_emp AND np.np_cod = mae.mae_cod 
		            LEFT JOIN dep dep ON dep.dep_emp=mae.mae_emp AND dep.dep_cod = mae.mae_dep
		            WHERE np_emp='$empCod' AND np_ffin>='$dtDel' AND np_ffin<='$dtAl' AND mae_igsssn='$crTrepo' GROUP BY np_emp,mae_gru,mae_dep";
	}
    $sNp1 = "SELECT np.*
                FROM nomper np
                WHERE np_emp='$empCod' AND np_ffin>='$dtDel' AND np_ffin<='$dtAl' AND np_gru=20";

    $uNp1 = "UPDATE nomper SET np_difprod=0, np_pagado=np_liqrec WHERE np_emp='$empCod' AND np_ffin>='$dtDel' AND np_ffin<='$dtAl'";

	function uNp2($numpla, $cod, $difprod, $pagado){
	    global $empCod;
	    return "UPDATE nomper SET np_difprod=$difprod, np_pagado=$pagado WHERE np_emp='$empCod' AND np_numpla='$numpla' AND np_cod='$cod'";
	}

	function sNomper_prod($cod,$del,$al){
	    global $empCod, $dtDel, $dtAl;
	    return "SELECT *, SUM(npp_cant) AS npp_uniprod, SUM(npp_tot) AS npp_valprod, TRIM(fed.fesd_desc) AS fesd_desc
	    FROM nomper_prod npp 
	    LEFT JOIN festilos_d fed ON npp.npp_emp=fed.fesd_emp and npp.npp_cod=fed.fesd_cod and npp.npp_linea=fed.fesd_linea 
	    WHERE npp_emp='$empCod' AND npp_codemple='$cod' AND npp_fec>='$del' AND npp_fec<='$al' GROUP BY npp_emp,npp_codemple";
	}

	# EJECUCION DE QUERYS
	$db = new dbExec();
//  Aqui hay que recalcular planilla de produccion para que asigne bien los datos
	$db->uEx($uNp1);
	$select["np1"] = $db->query($sNp1);
	while ($recp = $db->fetchAssoc($select["np1"])) {
	    $numpla=$recp["np_numpla"];
	    $cod=$recp["np_cod"];
	    $del=$recp["np_fini"];
	    $al=$recp["np_ffin"];
	    $uniprod=0;
	    $valprod=0;
	    $select["nomper_prod"] = $db->query(sNomper_prod($cod,$del,$al));
	    if ($db->numRows($select["nomper_prod"]) > 0) {
	       $fetchAssoc["nomper_prod"] = $db->fetchAssoc($select["nomper_prod"]);
	       $uniprod=$fetchAssoc["nomper_prod"]["npp_uniprod"];
	       $valprod=$fetchAssoc["nomper_prod"]["npp_valprod"]+$recp["np_bonii"];
	    }
	    $ingrepla=($recp["np_sord"]+$recp["np_sexts"]+$recp["np_sextd"]+$recp["np_ssept"]+$recp["np_bonii"]+$recp["np_bonoe"]+$recp["np_depre"]+$recp["np_viati"]+$recp["np_comi"]+$recp["np_otrp"]+$recp["np_boni"]+$recp["np_sferi"]+$recp["np_bono14"]+$recp["np_agui"]+$recp["np_vaca"]);
	    if ($ingrepla>=$valprod) {
	        $subtotal=$ingrepla;
	        $ajuste=$ingrepla-$valprod;
	        $difprod=0;
	    } else {
	        $subtotal=$valprod;
	        $ajuste=0;
	        $difprod=round(($valprod-$ingrepla),2);
	    }   
	    $descu=($recp["np_igss"]+$recp["np_isr"]+$recp["np_anti"]+$recp["np_unif"]+$recp["np_lent"]+$recp["np_preb"]+$recp["np_pree"]+$recp["np_desj"]+$recp["np_otrd"]);
	    $liquido=($subtotal-$descu);
		$db->uEx(uNp2($numpla,$cod,$difprod,$liquido));
	}

	// Para las cuentas de Cada departamento
	$arrayCtaComp = array(
		array('cta' => 'dep_ctaord', 'valor' => 'np_sumsord', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctahext', 'valor' => 'np_sumsext', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctabonil', 'valor' => 'np_sumboni', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctacomi', 'valor' => 'np_sumcomi', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctabonii', 'valor' => 'np_sumbonii', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctabonoe', 'valor' => 'np_sumbonoe', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctadepre', 'valor' => 'np_sumdepre', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctaviati', 'valor' => 'np_sumviati', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctadevisr', 'valor' => 'np_sumisrdevo', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctaotrp', 'valor' => 'np_sumotrp', 'valorh' => 'np_cero'),

		array('cta' => 'dep_ctaind', 'valor' => 'np_suminde', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctabon', 'valor' => 'np_sumbono14', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctaag', 'valor' => 'np_sumagui', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctavac', 'valor' => 'np_sumvaca', 'valorh' => 'np_cero'),
		array('cta' => 'dep_ctacpi', 'valor' => 'np_sumcuotapat', 'valorh' => 'np_cero'),

		array('cta' => 'dep_ctaigss', 'valor' => 'np_cero', 'valorh' => 'np_sumigss'),
		array('cta' => 'dep_ctaisr', 'valor' => 'np_cero', 'valorh' => 'np_sumisr'),
		array('cta' => 'dep_ctapreb', 'valor' => 'np_cero', 'valorh' => 'np_sumpreb'),
		array('cta' => 'dep_ctapree', 'valor' => 'np_cero', 'valorh' => 'np_sumpree'),
		array('cta' => 'dep_ctadesj', 'valor' => 'np_cero', 'valorh' => 'np_sumdesj'),
		array('cta' => 'dep_ctaunif', 'valor' => 'np_cero', 'valorh' => 'np_sumunif'),
		array('cta' => 'dep_ctalent', 'valor' => 'np_cero', 'valorh' => 'np_sumlent'),
		array('cta' => 'dep_ctaanti', 'valor' => 'np_cero', 'valorh' => 'np_sumanti'),
		array('cta' => 'dep_ctaotrd', 'valor' => 'np_cero', 'valorh' => 'np_sumotrd'),
		array('cta' => 'dep_ctasueldos', 'valor' => 'np_cero', 'valorh' => 'np_sumliqrec'),

		array('cta' => 'dep_ctaproind', 'valor' => 'np_cero', 'valorh' => 'np_suminde'),
		array('cta' => 'dep_ctaprobon', 'valor' => 'np_cero', 'valorh' => 'np_sumbono14'),
		array('cta' => 'dep_ctaproag', 'valor' => 'np_cero', 'valorh' => 'np_sumagui'),
		array('cta' => 'dep_ctaprovac', 'valor' => 'np_cero', 'valorh' => 'np_sumvaca'),
		array('cta' => 'dep_ctaprocpi', 'valor' => 'np_cero', 'valorh' => 'np_sumcuotapat')
	);

	$arrayIva_comp = $db->sFetchAll($sNomper);
	$hayFac = (sizeof($arrayIva_comp) != 0) ? 'S' : 'N';
	$resp["ctaGasto"]=array();
	$j=0;
	foreach ($arrayIva_comp as $rec) {
		foreach ($arrayCtaComp as $recCta) {
			// $rec[$recCta["cta"]]!="" && 
			if (($rec[$recCta["valor"]]!=0 || $rec[$recCta["valorh"]]!=0)) {
				$i = $func->multiSearch($resp["ctaGasto"], array('cta' => (string)$rec[$recCta["cta"]]));
				if ($i >= 0) {
					$resp["ctaGasto"][$i]["cta_valor"] += $arrayIva_comp[$j][$recCta["valor"]];
					$resp["ctaGasto"][$i]["cta_valorh"] += $arrayIva_comp[$j][$recCta["valorh"]];
				} else {
					$fa["ctaGasto"] = $db->sFetchAssoc(sCuentas((string)$rec[$recCta["cta"]],$rec[$recCta["valor"]],$rec[$recCta["valorh"]]));
				    array_push($resp["ctaGasto"], array("cta"=>$rec[$recCta["cta"]],"cta_nom"=>$fa["ctaGasto"]["cta_nom"],"cta_valor"=>$rec[$recCta["valor"]],"cta_valorh"=>$rec[$recCta["valorh"]]));
				}
			}
		}

		unset($arrayIva_comp[$j]);
		$j++;
	}
    $resp["ctaRisr"] = $db->sFetchAssoc(sCuentas((string)$_SESSION["empCtarisr"],0,0));
	$db->close();

	$resp["hayFac"]=$hayFac;
	return $func->jsonEncodeArray($resp, 'ctaRisr');
}

?>