<?php
//++
function GET_SAL_INI($KLI,$FEC){
	$TOT_PRO=gn("SUM(tot_real)","cotizaciones",
			    "id_cliente=$KLI AND fec<'$FEC' AND anu='N'");
	$TOT_COZ=gn("SUM(cob_tot)","cobroz",
			    "cob_id_cliente=$KLI AND cob_fec<'$FEC' AND cob_anu='NO'");
	$TOT_COX=gn("SUM(cx_total)","cobrox",
			    "cx_id_cliente=$KLI AND cx_fec<'$FEC' AND cx_anu='NO'");

	$SALDO=round($TOT_PRO - ($TOT_COZ + $TOT_COX),1);
	return $SALDO;
}
function GEN_EDC($USU,$HWN,$KLI,$DEX,$HAX){
	$CXX=0;
	$s="DELETE FROM edc22 WHERE usu=$USU AND hw='$HWN'";
	ejt($s);
	//
	$s="SELECT * FROM cotizaciones 
		WHERE fec>='$DEX' AND fec<='$HAX' AND id_cliente=$KLI AND anu='N'";
    //echo $s."<br>";
	$rs=grs($s);
    while($r=$rs->fetch_assoc()){
		$CXX++;
		$MOD="PROFORMA";
		$MOD_ID=$r['id_cotizacion'];
		$MOD_FEC=$r['fec'];
		$MOD_GI=$r['gi'];
		$MOD_DOC_TIP="PROFORMA";
		$MOD_DOC_NUM=$r['ser']."-".$r['num'];
		$MOD_DOC_TDP=$r['id_tip_pag'];
			if($MOD_DOC_TDP=="CR")$MOD_DOC_TDP="CREDITO";
			if($MOD_DOC_TDP=="CO")$MOD_DOC_TDP="CONTADO";
		$MOD_DOC_OBS="";
		$MOD_TOT=$r['tot_real'];
		$MOD_DEB=$r['tot_real'];
		$MOD_HAB=0;
		if($MOD_DOC_TDP=="CONTADO"){
			$MOD_HAB=$MOD_DEB;
		}
		$MOD_SAL=0;
		//
		$s="INSERT INTO edc22 VALUES ($CXX,$USU,'$HWN','$MOD',
			$MOD_ID,'$MOD_FEC',$MOD_GI,'$MOD_DOC_TIP',
			'$MOD_DOC_NUM','$MOD_DOC_TDP','$MOD_DOC_OBS',
			$MOD_TOT,$MOD_DEB,$MOD_HAB,$MOD_SAL)";
		ejt($s);
	}
	//cobroz (regulares)
	$s="SELECT * FROM cobroz 
		WHERE cob_fec>='$DEX' AND cob_fec<='$HAX' AND cob_id_cliente=$KLI AND cob_anu='NO'";	
	$rs=grs($s);
    while($r=$rs->fetch_assoc()){
		$CXX++;
		$MOD="COBRO";
		$MOD_ID=$r['id_cobro'];
		$MOD_FEC=$r['cob_fec'];
		$MOD_GI=$r['cob_gi'];
		$MOD_DOC_TIP="COBRO(R)";
		$MOD_DOC_NUM=$r['cob_num'];
		$MOD_DOC_TDP=$r['cob_id_tdp'];
		$MOD_DOC_OBS="";
		$MOD_TOT=$r['cob_tot'];
		$MOD_DEB=0;
		$MOD_HAB=$r['cob_tot'];
		$MOD_SAL=0;
		//
		$s="INSERT INTO edc22 VALUES ($CXX,$USU,'$HWN','$MOD',
			$MOD_ID,'$MOD_FEC',$MOD_GI,'$MOD_DOC_TIP',
			'$MOD_DOC_NUM','$MOD_DOC_TDP','$MOD_DOC_OBS',
			$MOD_TOT,$MOD_DEB,$MOD_HAB,$MOD_SAL)";
		ejt($s);
	}
	//
	//cobroz (transferencias)
	$s="SELECT * FROM cobrox 
		WHERE cx_fec>='$DEX' AND cx_fec<='$HAX' AND cx_id_cliente=$KLI AND cx_anu='NO'";
	$rs=grs($s);
    while($r=$rs->fetch_assoc()){
		$CXX++;
		$MOD="COBRO";
		$MOD_ID=$r['id_cx'];
		$MOD_FEC=$r['cx_fec'];
		$MOD_GI=$r['cx_gi'];
		$MOD_DOC_TIP="COBRO(T)";
		$MOD_DOC_NUM=$r['cx_num'];
		$MOD_DOC_TDP=$r['cx_tipo'];
		$MOD_DOC_OBS="";
		$MOD_TOT=$r['cx_total'];
		$MOD_DEB=0;
		$MOD_HAB=$r['cx_total'];
		$MOD_SAL=0;
		//
		$s="INSERT INTO edc22 VALUES ($CXX,$USU,'$HWN','$MOD',
			$MOD_ID,'$MOD_FEC',$MOD_GI,'$MOD_DOC_TIP',
			'$MOD_DOC_NUM','$MOD_DOC_TDP','$MOD_DOC_OBS',
			$MOD_TOT,$MOD_DEB,$MOD_HAB,$MOD_SAL)";
		ejt($s);
	}
}
?>
