<?php
	$StrHWND_CH=$_SESSION['SYS_HW']."_CH";
	//-------------------------
	$StrId=V($_POST['TxtId']);
	$StrCmd=V($_POST['TxtAc']);
	//-------------------------
	$StrIdPro=V($_POST['TxtIdPro']);
	$StrPro=V($_POST['TxtPro']);
	$StrMon=V($_POST['CboMon']);
	//ACTIVAR DESDE OTRA PAGINA
	if(strlen($StrId)==0){
		$StrId=V($_GET['Id']);
		if(is_numeric($StrId)==false){
			$StrId="";
		}else{
			$StrCmd="AR";
			$LStrPag=V($_GET['Pag']);
			if(!is_numeric($LStrPag)){
				$LStrPag="";
			}
		}
	}
	//FUNCIONES DE DESZPLAZAMIENTO
	if(strlen($LStrPag)==0)	$LStrPag="0";
	if($StrCmd=="MSig")$LStrPag=($LStrPag+1);
	if($StrCmd=="MAnt")	$LStrPag=($LStrPag-1);
	if($StrCmd=="MPri")	$LStrPag=0;
	if($StrCmd=="MUlt")	$LStrPag=(floor($LStrTR / $VP_RegXPag) );
	//////////////////////////////////
	$EDC_HW=$StrHWND_CH;
	$EDC_FE="";
	$EDC_DO="";
	$EDC_NU="";
	$EDC_TO=0;
	$EDC_AC=0;
	$EDC_SA=0;
	$EDC_GI=0;
	$EDC_OB="";	
	//FUNCION PARA ACTIVAR UN REGISTRO
	if($StrCmd=="Procesar"){
		if(strlen($StrIdPro)==0) $StrIdPro="0";
		if(!is_numeric($StrIdPro)) $StrIdPro="0";
		if(strlen($StrMon)==0) $StrMon="SOLES";
		$StrMon_=substr($StrMon,0,1);
		ejt("DELETE FROM edc WHERE hwnd='$StrHWND_CH'");
		$cx=0;
		$s="SELECT FEC,COM,NUM,TOT,GI FROM vventas WHERE 
			ID_CLI=$StrIdPro AND MON='$StrMon_' AND TDP='CR' AND 
			ANU='N' ORDER BY FEC,GI";	
		$result = grs($s);
		while ($row = $result->fetch_row()){
			$cx=$cx+1;
			$EDC_FE=$row[0];
			$EDC_DO=$row[1];
			$EDC_NU=$row[2];
			$EDC_TO=$row[3];
			$EDC_GI=$row[4];
			$s="INSERT INTO edc VALUES ($cx,'$StrHWND_CH','$EDC_FE',
				'$EDC_DO','$EDC_NU',$EDC_TO,$EDC_AC,$EDC_SA,$EDC_GI,'$EDC_OB')";
			ejt($s);
		}
		//
		$s="SELECT NUM,FEC,TOT,FPAGO,NUMF,DATE_FORMAT(FECVEN,'%d-%m-%y'),COM,SERR,NUMR,PAG,
			FECPAG,GI,GI2 
			FROM vcobros WHERE 
			ID_CLI=$StrIdPro AND MON='$StrMon_' AND ANU='N' ORDER BY FEC,GI,GI2";
		$result = grs($s);
		while ($row = $result->fetch_row()){
			$cx=$cx+1;
			$EDC_NU=$row[0];
			$EDC_FE=$row[1];
			$EDC_TO=$row[2];
			$EDC_FPP=$row[3];
			$EDC_NF=$row[4];
			$EDC_FV=$row[5];
			$EDC_CR=$row[6];
			$EDC_SR=$row[7];
			$EDC_NR=$row[8];
			$EDC_PA=$row[9];
			$EDC_FP=$row[10];
			$EDC_G1=$row[11];
			$EDC_G2=$row[12];
			if($EDC_FPP=="EFECTIVO"){
				$s="INSERT INTO edc VALUES ($cx,'$StrHWND_CH','$EDC_FE',
					'COBRO','$EDC_NU',0,$EDC_TO,$EDC_SA,$EDC_G1,'$EDC_OB')";
				ejt($s);
			}else{
				if($EDC_PA=="N"){
					$EDC_OB=$EDC_FPP." | N&deg;: ".$EDC_NF." | TOT: ".$EDC_TO." | FV: ";
					$EDC_OB=$EDC_OB.$EDC_FV;
					$s="INSERT INTO edc VALUES ($cx,'$StrHWND_CH','$EDC_FE',
						'EMISION','$EDC_NU',0,0,$EDC_SA,$EDC_G1,'$EDC_OB')";
					ejt($s);					
				}else{
					$EDC_OB=$EDC_FPP." | N&deg;: ".$EDC_NF." | TOT: ";
					$EDC_OB=$EDC_OB.$EDC_TO." | FV: ".$EDC_FV;
					$EDC_OB=$EDC_OB." | FE: ". date("d-m-y",strtotime($EDC_FE));
					$s="INSERT INTO edc VALUES ($cx,'$StrHWND_CH','$EDC_FE',
						'COBRO','$EDC_NU',0,$EDC_TO,$EDC_SA,$EDC_G2,'$EDC_OB')";
					ejt($s);					
				}
			}			
		}
		//
		$EDC_ID=0;
		$EDC_TO=0;
		$EDC_AC=0;
		$EDC_SA=0;
		$s="SELECT id_edc,tot,acuenta FROM edc WHERE hwnd='$StrHWND_CH' ORDER BY FEC,GI";
		$result = grs($s);
		while ($row = $result->fetch_row()){
			$EDC_ID=$row[0];
			$EDC_TO=$row[1];
			$EDC_AC=$row[2];
			$EDC_SA=$EDC_SA + $EDC_TO - $EDC_AC;
			$s="UPDATE edc SET saldo=$EDC_SA WHERE hwnd='$StrHWND_CH' AND id_edc=$EDC_ID";
			ejt($s);
		}
	}
	//////////////////////////////////
	if($StrCmd=="C"){
		$LStrFoc="document.getElementById('TxtPro').focus();";
		$StrPro="";
		$StrIdPro="";
		$StrMon="";
		$StrGT="S";
	}	
	//////////////////////////////////	
	if($StrGT=="S"){
		if(strlen($LStrMsg)>0){
			if(strlen($LStrFoc)==0){
				$LStrMsg="onload=\"alert('$LStrMsg');\"";
			}else{
				$LStrMsg="onload=\"alert('$LStrMsg');$LStrFoc;\"";
			}
		}else{
			if(strlen($LStrFoc)>0){
				$LStrMsg="onload=\"$LStrFoc;\"";
			}
		}
	}
	
?>