<?php
	//-------------------------
	$StrId=V($_POST['TxtId']);
	$StrCmd=V($_POST['TxtAc']);
	//-------------------------
	$StrKil=V($_POST['TxtKill']);
	$StrAlm=V($_POST['CboAlm']);
	$StrFec=V($_POST['TxtFec']);
	$StrIdPro=V($_POST['TxtIdPro']);
	$StrPro=V($_POST['TxtPro']);
	$StrMon=V($_POST['CboMon']);
	$StrTot=V($_POST['TxtTot']);
	$StrFdp=V($_POST['CboFdp']);
	$StrBan=V($_POST['CboBan']);
	$StrNdp=V($_POST['TxtNdp']);
	$StrFecVen=V($_POST['TxtFecVen']);
	$StrCom=V($_POST['CboCom']);
	$StrSer=V($_POST['TxtSer']);
	$StrNum=V($_POST['TxtNum']);
	$StrFecPag=V($_POST['TxtFecPag']);
	//	
	$StrTOT=V($_POST['TxtTOT']);
	//ACTIVAR DESDE OTRA PAGINA
	if(strlen($StrFec)==0)$StrFec=date("d-m-y");
	
	//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) );
	//FUNCION PARA ACTIVAR UN REGISTRO
	if(strlen($StrId)>0 && $StrCmd=="AR"){
		$Id=$StrId;
		$StrFec=gs("DATE_FORMAT(FEC,'%d-%m-%y')","vcobros","ID=$Id");		
		$StrPro=gs("CLI","vcobros","ID=$Id");				
		$StrMon=gs("MON","vcobros","ID=$Id");		
		if($StrMon=="S")$StrMon="SOLES";	
		if($StrMon=="D")$StrMon="DOLARES";	
		$StrTot=gn("TOT","vcobros","ID=$Id");		
		$StrFdp=gs("FPAGO","vcobros","ID=$Id");		
		$StrNdp=gs("NUMF","vcobros","ID=$Id");		
		$StrFecVen=gs("DATE_FORMAT(FECVEN,'%d-%m-%y')","vcobros","ID=$Id");	
		if($StrFecVen=="01-01-00") $StrFecVen="";	
		$StrCom=gs("COM","vcobros","ID=$Id");		
		$StrSer=gs("SERR","vcobros","ID=$Id");		
		$StrNum=gs("NUMR","vcobros","ID=$Id");		
		$StrAlm=gs("ALM","vcobros","ID=$Id");		
		$StrBan=gs("BAN","vcobros","ID=$Id");		
		$StrFecPag=gs("DATE_FORMAT(FECPAG,'%d-%m-%y')","vcobros","ID=$Id");	
		if($StrFecPag=="01-01-00") $StrFecPag="";
		//
		ejt("DELETE FROM cobros_ventas_t WHERE hw='$StrHW'");
		$s="SELECT cv_id_venta ".
		   "FROM cobros_ventas ".
		   "WHERE cv_id_cobro=$Id ORDER BY id_cobven";
		$result = grs($s);
		while ($row = $result->fetch_row()){
			$T_IV=$row[0];
			$cx=GenId("item","cobros_ventas_t");
			$s="INSERT INTO cobros_ventas_t ".
			   "VALUES ('$StrHW',$cx,$T_IV)";
			ejt($s);
		}
		//
		$LStrFoc="document.getElementById('TxtFec').focus();";		
	}
	if($StrCmd=="KILLER"){
		$StrKil=vbval($StrKil);
		ejt("DELETE FROM cobros_ventas_t WHERE item=$StrKil");
		$LStrFoc="document.getElementById('CboCom').focus();";	
	}
	$ADD_OK=0;
	if($StrCmd=="G_T" || $StrCmd=="ADD"){
		$T_MO=substr($StrMon,0,1);
		$T_IC=$StrIdPro;
		$T_DO=$StrCom;
		$T_SE=$StrSer;
		$T_NU=$StrNum;
		$T_IV=gn("ID","vventas",
				 "ID_CLI=$T_IC AND COM='$T_DO' AND ".
				 "SER='$T_SE' AND NUM='$T_NU' AND MON='$T_MO'");
		if($T_IV==0){
			$LStrMsg="Cliente, comprobante, serie, numero y moneda no registrado";
			$LStrFoc="document.getElementById('CboCom').focus();";						
			$StrGT="S";
		}
		if(strlen($StrGT)==0){
			$T_AN=gs("ANU","vventas","ID=$T_IV");
			if($T_AN=="S"){
				$LStrMsg="El comprobante se encuentra anulado";
				$LStrFoc="document.getElementById('CboCom').focus();";						
				$StrGT="S";
			}
		}
		if(strlen($StrGT)==0){
			$T_TP=gs("TDP_","vventas","ID=$T_IV");
			if($T_TP!="CREDITO"){
				$LStrMsg="El tipo de pago del presente comprobante deberia ser 'CREDITO'";
				$LStrFoc="document.getElementById('CboCom').focus();";						
				$StrGT="S";
			}
		}
		$StrTOT="";
		if(strlen($StrGT)==0){
			$T_CA=gs("ven_cancelado","ventas","id_venta=$T_IV");
			if($T_CA=="S"){
				$LStrMsg="El presente documento ya ha sido cancelado";
				$LStrFoc="document.getElementById('CboCom').focus();";						
				$StrGT="S";
			}else{
				$StrTOT=NF(gn("TOT","vventas","ID=$T_IV"),2);	
				$LStrFoc="document.getElementById('CmdAdd').focus();";						
				$StrGT="S";	
				$ADD_OK=1;
			}
		}
	}
	if($StrCmd=="ADD" && $ADD_OK==1){
		$StrGT="";
		$c=gn("count(*)","cobros_ventas_t",
			  "hw='$StrHW' AND id_venta=$T_IV");
		if($c>0){
			$LStrMsg="Comprobante, serie y numero ya se encuentra en la lista";
			$LStrFoc="document.getElementById('CboCom').focus();";						
			$StrGT="S";
		}
		if(strlen($StrGT)==0){
			$cx=GenId("item","cobros_ventas_t");
			$s="INSERT INTO cobros_ventas_t ".
			   "VALUES ('$StrHW',$cx,$T_IV)";
			ejt($s);
			$StrCom="";
			$StrSer="";
			$StrNum="";
			$StrTOT="";
			$LStrFoc="document.getElementById('CboCom').focus();";
			$StrGT="S";						
		}
	}
	//
	if($StrCmd=="DESHACER"){
		$StrFecPag_=ToDate("");
		ejt("UPDATE cobros SET gi2=0,PAG='N',fecpag='$StrFecPag_' WHERE id_cobro=$StrId");
		$LStrMsg="Proceso completado";
		$LStrFoc="document.getElementById('TxtFec').focus();";
		$StrId="";
		$StrFec="";
		$StrPro="";
		$StrIdPro="";
		$StrMon="";
		$StrTot="";
		$StrFdp="";
		$StrNdp="";
		$StrFecVen="";
		$StrCom="";
		$StrSer="";
		$StrNum="";
		$StrFecPag="";
		$StrGT="S";
	}
	if($StrCmd=="Pagar"){
		$GI=GenGI();
		$StrFecPag_=ToDate($StrFecPag);
		$_TC=GetTC();
		$s="UPDATE cobros 
			SET tcam=$_TC,gi2=$GI,PAG='S',fecpag='$StrFecPag_' 
			WHERE id_cobro=$StrId";
		ejt($s);
		$LStrMsg="El pago ha sido registrado";
		$LStrFoc="document.getElementById('TxtFec').focus();";
		$StrId="";
		$StrFec="";
		$StrPro="";
		$StrIdPro="";
		$StrMon="";
		$StrTot="";
		$StrFdp="";
		$StrNdp="";
		$StrFecVen="";
		$StrCom="";
		$StrSer="";
		$StrNum="";
		$StrFecPag="";
		$StrGT="S";
	}
	//FUNCIONES DE ACTUALIZACION
	if($StrCmd=="G" || $StrCmd=="M"){
		$StrAlm_=gn("id_almacen","almacenes","alm_nombre='$StrAlm'");
		$StrMon_=substr($StrMon,0,1);
		$StrFdp_=gn("id_fpago","fpagos","nombre='$StrFdp'");
		$StrCom_=gn("id_comprobante","comprobantes","com_nombre='$StrCom'");
		$StrFec_=ToDate($StrFec);
		$StrFecVen_=ToDate($StrFecVen);
		$StrPag="N";
		$StrFecPag=ToDate("");
		if($StrFdp=="EFECTIVO")	{
			$StrPag="S";
			$StrFecPag=$StrFec_;
		}
	}
	//
	if($StrCmd=="G" && "TODO_OK"=="TODO_OK" && strlen($StrGT)==0){			
		$cx=gn("count(*)","cobros_ventas_t","hw='$StrHW'");
		$ct=gn("count(*)","vis_cobros_ventas_temp",
			   "MON='$StrMon_' AND TDP='CR' ".
			   "AND IDC=$StrIdPro AND CAN='N' AND HW='$StrHW'");
		if($ct!=$cx){
			$LStrMsg="Uno o mas comprobantes no coinciden con los criterios.";
			$LStrFoc="document.getElementById('CboCom').focus();";						
			$StrGT="S";	
		}
	}
	//
	if($StrCmd=="G" && "TODO_OK"=="TODO_OK" && strlen($StrGT)==0){
			$StrBan_=gn("id_banco","bancos","ban_nombre='$StrBan'");		
			$StrPendiente=0;
			if($StrMon_=="S"){
				$TOT=gn("SUM(tot)","ventas","id_cliente=$StrIdPro AND 
				  		  anu='N' AND id_tip_pag='CR' AND mon='S'");
				$PAE=gn("SUM(total)","cobros",
						  "id_cliente=$StrIdPro AND anu='N' AND mon='S'");
				$StrPendiente=round($TOT - $PAE,2);
			}else{
				$TOT=gn("SUM(tot)","ventas","id_cliente=$StrIdPro AND 
				  		  anu='N' AND id_tip_pag='CR' AND mon='D'");
				$PAE=gn("SUM(total)","cobros",
						  "id_cliente=$StrIdPro AND anu='N' AND mon='D'");
				$StrPendiente=round($TOT - $PAE,2);
			}
			$GI=GenGI();
			$Id=GenId("id_cobro","cobros");
			if(strlen($Id)==4)$Id="0".$Id;
			if(strlen($Id)==3)$Id="00".$Id;
			if(strlen($Id)==2)$Id="000".$Id;
			if(strlen($Id)==1)$Id="0000".$Id;			
			$_TCAM=GetTC();
			$s="INSERT INTO cobros VALUES ($Id,'$Id','$StrFec_',$StrIdPro,
				$StrPendiente,$StrTot,'$StrMon_','N',$GI,$StrFdp_,'$StrNdp',
				'$StrFecVen_',$StrCom_,'$StrSer','$StrNum','$StrPag',
				'$StrFecPag',0,$StrAlm_,$_TCAM,$StrBan_)";
			ejt($s);
			//			
			$s="SELECT id_venta FROM cobros_ventas_t ".
			   "WHERE hw='$StrHW' ORDER BY item";
			$result = grs($s);
			while ($row = $result->fetch_row()){
				$T_IDV=$row[0];
				$cx=GenId("id_cobven","cobros_ventas");
				$s="INSERT INTO cobros_ventas VALUES ($cx,$Id,$T_IDV)";
				ejt($s);
				$s="UPDATE ventas SET ven_cancelado='S' ".
				   "WHERE id_venta=$T_IDV";				
				ejt($s);
			}			
			//
			$LStrMsg="Datos registrados";
			$LStrFoc="document.getElementById('TxtFec').focus();";
			$StrId="";
			$StrFec=date("d-m-y");
			$StrFec="";
			$StrPro="";
			$StrIdPro="";
			$StrMon="";
			$StrTot="";
			$StrFdp="";
			$StrNdp="";
			$StrFecVen="";
			$StrCom="";
			$StrSer="";
			$StrNum="";
			$StrTOT="";
			$StrFecPag="";
			ejt("DELETE FROM cobros_ventas_t WHERE hw='$StrHW'");
			$StrGT="S";
	}
	if($StrCmd=="C"){
		$LStrFoc="document.getElementById('TxtFec').focus();";
		ejt("DELETE FROM cobros_ventas_t WHERE hw='$StrHW'");
		$StrId="";
		$StrFec=date("d-m-y");
		$StrPro="";
		$StrIdPro="";
		$StrMon="";
		$StrTot="";
		$StrFdp="";
		$StrNdp="";
		$StrFecVen="";
		$StrCom="";
		$StrSer="";
		$StrNum="";
		$StrTOT="";
		$StrBan="";
		$StrFecPag="";
		$StrGT="S";
	}
	if($StrCmd=="E"){
		$s="SELECT cv_id_venta FROM cobros_ventas ".
		   "WHERE cv_id_cobro=$StrId ORDER BY id_cobven";
		$result = grs($s);
		while ($row = $result->fetch_row()){
			$T_IDV=$row[0];
			ejt("UPDATE ventas SET ven_cancelado='N' ".
			    "WHERE id_venta=$T_IDV");							
		}
		ejt("DELETE FROM cobros_ventas WHERE cv_id_cobro=$StrId");
		ejt("DELETE FROM cobros WHERE id_cobro=$StrId");
		ejt("DELETE FROM cobros_ventas_t WHERE hw='$StrHW'");		
		//		
		$StrId="";
		$StrFec="";
		$StrPro="";
		$StrIdPro="";
		$StrMon="";
		$StrTot="";
		$StrFdp="";
		$StrNdp="";
		$StrFecVen="";
		$StrCom="";
		$StrSer="";
		$StrNum="";
		$StrTOT="";
		$StrFecPag="";
		$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;\"";
			}
		}
	}else{
		if(strlen($LStrFoc)>0){
			$LStrMsg="onload=\"$LStrFoc;\"";
		}else{
			$LStrFoc="document.getElementById('CboAlm').focus();";
			$LStrMsg="onload=\"$LStrFoc;\"";
		}
	}
	
?>