<?php
	$LIMPIAR_DET=0;
	$LIMPIAR=0;
	$SYS_FOC="Prv";
	//-------------------------+++++++++++++++++
	$GII=0;
	$StrId=V($_POST['TxtId']);
	$StrCmd=V($_POST['TxtAc']);
	if(strlen($StrCmd)==0){
		$StrCmd=V($_GET['TxtAc']);
		if($StrCmd=="X"){
			$StrCmd="C";
			$StrFec=date("d-m-y");
		}
	}
	//-------------------------++++
	$Prv=V($_POST['Prv']);
	$PrvAni=V($_POST['PrvAni']);
	$ProSer=V($_POST['ProSer']);
	$ProNum=V($_POST['ProNum']);
	$InvSer=V($_POST['InvSer']);
	$InvNum=V($_POST['InvNum']);
	$Fec=V($_POST['Fec']);
	$Emp=V($_POST['Emp']);
	$PunOri=V($_POST['PunOri']);
	$PunDes=V($_POST['PunDes']);
	$Tot=V($_POST['Tot']);
	
	//------------------------
	$Cod=V($_POST['Cod']);
	$Des=V($_POST['Des']);
	$Pie=V($_POST['Pie']);
	$Caj=V($_POST['Caj']);
	$Und=V($_POST['Und']);
	$TotPie=V($_POST['TotPie']);
	$TotUni=V($_POST['TotUni']);
	$TotUwi=V($_POST['TotUwi']);
	$Pre=V($_POST['Pre']);
	$Imp=V($_POST['Imp']);
	//-------------------------
	$CheFle=vbval(V($_POST['CheFle']));
	$PreFle=V($_POST['PreFle']);
	$NumFle=V($_POST['NumFle']);
	$AmoFle=V($_POST['AmoFle']);
	//
	$PreFob=V($_POST['PreFob']);
	$NumFob=V($_POST['NumFob']);
	$AmoFob=V($_POST['AmoFob']);
	//
	$PreCfr=V($_POST['PreCfr']);
	$NumCfr=V($_POST['NumCfr']);
	$AmoCfr=V($_POST['AmoCfr']);
	//-------------------------
	$Per=V($_POST['Per']);
	$Per1=V($_POST['Per1']);
	$Per2=V($_POST['Per2']);
	$TotAmo=V($_POST['TotAmo']);
	$SayTot=V($_POST['SayTot']);
	$ExpAge=V($_POST['ExpAge']);
	$ExpAgeAcc=V($_POST['ExpAgeAcc']);
	$TheBan=V($_POST['TheBan']);
	$Add=V($_POST['Add']);
	$Swi=V($_POST['Swi']);
	//-------------------------
	$StrTC=1;//gs("valor","valores","dato='T_CAMBIO'");
	//-------------------------
	//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	
	//++
	//
	function GEN_P_NUM_L($PRV,$ANI){
		$CX=gn("MAX(in_pro_num_)","ges_invoice","in_id_prv=$PRV AND in_prv_ani=$ANI")+1;
		return vbr("000000".$CX,6);
		
	}
	function GEN_I_NUM_L($PRV,$ANI){
		$CX=gn("MAX(in_inv_num_)","ges_invoice","in_id_prv=$PRV AND in_prv_ani=$ANI")+1;
		return vbr("000000".$CX,6);
		
	}
	//
	if(($StrCmd=="G" || $StrCmd=="M") && $TODO_OK==1){
		$SYS_FEC=date("Y-m-d");
		$SYS_HOR=GetHoraAP();
		$SYS_TIM=time();
		$Prv_=gn("id_prv","ges_proveedores","prv_nom='$Prv'");
		$PrvAni_=vbval($PrvAni);
		$ProNum_=vbval($ProNum);
		$InvNum_=vbval($InvNum);
		$Fec_=ToDate($Fec);
		$Emp_=gn("id_emp","ges_empresas","emp_nom_cor='$Emp'");
		$PunOri_=gn("id_pto","ges_puertos","pto_nom='$PunOri' AND pto_tip='ORIGEN'");
		$PunDes_=gn("id_pto","ges_puertos","pto_nom='$PunDes' AND pto_tip='DESTINO'");
		$Tot_=vbval($Tot);
		//	
		$CheFle_=vbval($CheFle);
		$CheSel="N";
		if($CheFle_==1)$CheSel="S";
		//		
		$PreFle_=vbval($PreFle);
		$NumFle_=vbval($NumFle);
		$AmoFle_=vbval($AmoFle);
		//
		$PreFob_=vbval($PreFob);
		$NumFob_=vbval($NumFob);
		$AmoFob_=vbval($AmoFob);
		//
		$PreCfr_=vbval($PreCfr);
		$NumCfr_=vbval($NumCfr);
		$AmoCfr_=vbval($AmoCfr);
		//+++++
		$Per_=gn("id_per","ges_periodos","per_nom='$Per'");
		
		//
		$TotAmo_=vbval($TotAmo);	

	}
	if($StrCmd=="L"){
		$LIMPIAR=1;
	}	
	//
	if($StrCmd=="G" && $TODO_OK==1){
		$CX=gn("count(*)","ges_invoice","in_id_prv=$Prv_ AND in_inv_ser='$InvSer' AND in_inv_num_=$InvNum_");
		if($CX>0){
			$SYS_MSG="Proveedor, serie y numero de invoice ya registrado *";
			$TODO_OK=0;
		}
	}
	if($StrCmd=="M" && $TODO_OK==1){
		$CX=gn("count(*)","ges_invoice","in_id_prv=$Prv_ AND in_inv_ser='$InvSer' AND in_inv_num_=$InvNum_ AND id_in<>$StrId");
		if($CX>0){
			$SYS_MSG="Proveedor, serie y numero de invoice ya registrado **";
			$TODO_OK=0;
		}
	}
	//
	if($StrCmd=="M" && $TODO_OK==1){
		$ACC_INV_EDI=USU_PER_GET($IdUsu,"INV_EDI");
		if($ACC_INV_EDI=="NO"){
			$SYS_MSG="No tiene permisos para esta accion...";
		}else{
			//BIT
			$_BIT_MOD_ID=$StrId;
			$_BIT_SQL_CAB="SQL";
			$_BIT_SQL_DET="";
			$_BIT_PHP="ges_invoice.php";
			$_BIT_ID=BIT_ADD("PROCESOS","INVOICE","MODIFICAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
			BIT_DET_ADD($_BIT_ID,"");
			//BIT
			$qqq=ejt("DELETE FROM ges_invoice WHERE id_in=$StrId");
			BIT_DET_ADD($_BIT_ID,$qqq);
			//
			$qqq=ejt("DELETE FROM ges_invoice_det WHERE det_id_inv=$StrId");
			BIT_DET_ADD($_BIT_ID,$qqq);
			//
			$SYS_KOD=$StrId;
			$StrCmd="G";
		}
	}
	//
	if($StrCmd=="G" && $TODO_OK==1){		
		$USU_ID=0;
		if($SYS_KOD==0){
			$Id=GenId("id_in","ges_invoice");		
		}else{
			$Id=$SYS_KOD;
		}
		$s="INSERT INTO ges_invoice 
			VALUES ($Id,'$SYS_FEC','$SYS_HOR',
			$SYS_TIM,$Prv_,$PrvAni_,'$ProSer','$ProNum',
			$ProNum_,'$InvSer','$InvNum',$InvNum_,'$Fec_',
			$Emp_,$PunOri_,$PunDes_,$Tot_,'$CheSel',$PreFle_,$NumFle_,$AmoFle_,
			$PreFob_,$NumFob_,$AmoFob_,$PreCfr_,$NumCfr_,$AmoCfr_,$Per_,
			'$Per1','$Per2',$TotAmo_,'$SayTot',$USU_ID)";
		ejt($s);
		//BIT
		$_BIT_MOD_ID=$Id;
		$_BIT_SQL_CAB=$s;
		$_BIT_SQL_DET="";
		$_BIT_PHP="ges_invoice.php";
		$_BIT_ID=BIT_ADD("PROCESOS","INVOICE","GUARDAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,$s);
		//BIT
		$qqq=ejt("DELETE FROM ges_invoice_det WHERE id_det=$Id");
		BIT_DET_ADD($_BIT_ID,$qqq);
		//
		$PX=0;
		$s="SELECT * FROM ges_invoice_det_t WHERE hw='$HW' ORDER BY cx";		
		$rs=grs($s);
		while($r=$rs->fetch_assoc()){			
			$DET_PID=$r['dt_id_pro'];
			$DET_PIE=$r['dt_pie'];
			$DET_CAJ=$r['dt_caj'];
			$DET_UND=$r['dt_id_und'];
			$DET_TOT_PIE=$r['dt_tot_pie'];
			$DET_TOT_UNI=$r['dt_tot_uni'];
			$DET_TOT_UWI=$r['dt_tot_uwi'];
			$DET_PRE=$r['dt_pre'];
			$DET_IMP=$r['dt_imp'];
			//
			$DET_ID=GenId("id_det","ges_invoice_det");
			$PX++;
			//			
			$s="INSERT INTO ges_invoice_det 
				VALUES ($DET_ID,$PX,$Id,$DET_PID,$DET_PIE,
				$DET_CAJ,$DET_UND,$DET_TOT_PIE,
				$DET_TOT_UNI,$DET_TOT_UWI,$DET_PRE,$DET_IMP)";
			ejt($s);
			BIT_DET_ADD($_BIT_ID,$s);
		//
		}
		$SYS_FOC="Emp";
		if($SYS_KOD==0){
			$SYS_MSG="Datos registrados";
		}else{
			$SYS_MSG="Datos actualizados";
		}
		$StrId=$Id;
		$StrCmd="AR";
	}
	//
	if($StrCmd=="INVC_KIL" && $TODO_OK==1){
		$CX=vbval(V($_POST['PRO_KIL']));
		$s="DELETE FROM ges_invoice_det_t WHERE cx=$CX";
		ejt($s);
		$LIMPIAR_DET=1;
		$SYS_FOC="Cod";
	}
	//
	if($StrCmd=="INVC_ADD" && $TODO_OK==1){
		$PRO_KOD=gn("id_pro","ges_productos","pro_cod='$Cod' AND pro_des='$Des'");
		$PIE_=vbval($Pie);
		$CAJ_=vbval($Caj);
		$UNI_=gn("id_und","ges_unidades","und_abr='$Und'");
		$TOT_PIE_=vbval($TotPie);
		$TOT_UNI_=vbval($TotUni);
		$TOT_UWI_=vbval($TotUwi);
		$PRE_=vbval($Pre);
		$IMP_=vbval($Imp);
		//
		$CX=GenId("cx","ges_invoice_det_t");
		$s="INSERT INTO ges_invoice_det_t 
			VALUES ($CX,'$HW',$PRO_KOD,$PIE_,
			$CAJ_,$UNI_,$TOT_PIE_,$TOT_UNI_,$TOT_UWI_,$PRE_,$IMP_)";
		ejt($s);
		$LIMPIAR_DET=1;
		$SYS_FOC="Cod";
	}
	//
	if($StrCmd=="E" && $TODO_OK==1){
		$ACC_INV_ELI=USU_PER_GET($IdUsu,"INV_ELI");

		if($ACC_INV_ELI=="NO"){
			$SYS_MSG="No tiene permisos para esta accion";
		}else{
			$qqq=ejt("DELETE FROM ges_invoice WHERE id_in=$StrId");
			//BIT
			$_BIT_MOD_ID=$StrId;
			$_BIT_SQL_CAB=$qqq;
			$_BIT_SQL_DET="";
			$_BIT_PHP="ges_invoice.php";
			$_BIT_ID=BIT_ADD("PROCESOS","INVOICE","ELIMINAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
			BIT_DET_ADD($_BIT_ID,$qqq);
			//BIT	
			$qqq=ejt("DELETE FROM ges_invoice_det WHERE det_id_inv=$StrId");
			BIT_DET_ADD($_BIT_ID,$qqq);
			//BIT	
			$qqq=ejt("DELETE FROM ges_invoice_det_t WHERE hw='$HW'");
			BIT_DET_ADD($_BIT_ID,$qqq);
			//BIT
			$SYS_MSG="Registro eliminado";
			$LIMPIAR=1;
		}
	}	
	//
	if(strlen($StrId)>0 && $StrCmd=="AR" && $TODO_OK==1){
		$Id=$StrId;
		$r=gr("vis_ges_invoice","ID=$Id");
		$Prv=$r['PROVEEDOR'];
			$PRV_ORI=$Prv;		
		$ProSer=$r['PRO_SER'];
		$ProNum=$r['PRO_NUN'];
			$PRV_NUM=$ProNum;
		$InvSer=$r['INV_SER'];
		$InvNum=$r['INV_NUM'];
		$Fec=$r['FEC_'];
		$Emp=$r['EMP_NC'];
		$PunOri=$r['PTO_ORI'];
		$PunDes=$r['PTO_DES'];
		//
		$CheFle=0;
		if($r['FLE_SEL']=="S")$CheFle=1;
		$PreFle=$r['FLE_PRE'];
		$NumFle=$r['FLE_NUM'];
		$AmoFle=$r['FLE_AMO'];
		
		$PreFob=$r['FOB_PRE'];
		$NumFob=$r['FOB_NUM'];
		$AmoFob=$r['FOB_AMO'];
		
		$PreCfr=$r['CFR_PRE'];
		$NumCfr=$r['CFR_NUM'];
		$AmoCfr=$r['CFR_AMO'];
		
		$Per=$r['PERIODO'];
		$Per1=$r['PER_TEX1'];
		$Per2=$r['PER_TEX2'];
		$TotAmo=$r['TOT_AMO'];
		$SayTot=$r['SAY_TOT'];
		
		$ExpAgeAcc=$r['AGE'];
		$ExpAge=$r['EXP_AGE'];
		$TheBan=$r['BAN'];
		$Add=$r['BAN_DIR'];
		$Swi=$r['SWI'];
		
		//
		ejt("DELETE FROM ges_invoice_det_t WHERE hw='$HW'");
		$s="SELECT * FROM ges_invoice_det WHERE det_id_inv=$Id ORDER BY id_det";		
		$rs=grs($s);
		while($r=$rs->fetch_assoc()){			
			$PRO_KOD=$r['det_id_pro'];
			$PIE_=$r['dt_pie'];
			$CAJ_=$r['dt_caj'];
			$UNI_=$r['dt_id_und'];
			$TOT_PIE_=$r['dt_tot_pie'];
			$TOT_UNI_=$r['dt_tot_uni'];
			$TOT_UWI_=$r['dt_tot_uwi'];
			$PRE_=$r['dt_pre'];
			$IMP_=$r['dt_imp'];
			//
			$CX=GenId("cx","ges_invoice_det_t");
			$s="INSERT INTO ges_invoice_det_t 
				VALUES ($CX,'$HW',$PRO_KOD,$PIE_,
				$CAJ_,$UNI_,$TOT_PIE_,$TOT_UNI_,$TOT_UWI_,$PRE_,$IMP_)";
			ejt($s);
		}
	}
	//
	if($LIMPIAR==1){
		$StrId="";		
		$Prv="";
		$ProSer="";
		$ProNum="";
		$InvSer="";
		$InvNum="";
		$Fec="";
		$Emp="";
		$PunOri="";
		$PunDes="";
		//
		$CheFle=0;
		$PreFle="";
		$NumFle="";
		$AmoFle="";
		$PreFob="";
		$NumFob="";
		$AmoFob="";
		$PreCfr="";
		$NumCfr="";
		$AmoCfr="";
		//
		$Per="";
		$Per1="";
		$Per2="";
		$TotAmo="";
		$SayTot="";
		$ExpAge="";
		$ExpAgeAcc="";
		$TheBan="";
		$Add="";
		$Swi="";
		//
		ejt("DELETE FROM ges_invoice_det_t WHERE hw='$HW'");
		//
		$LIMPIAR_DET=1;
	}
	//
	if($LIMPIAR_DET==1){
		$Cod="";
		$Des="";
		$Pie="";
		$Caj="";
		$Und="";
		$TotPie="";
		$TotUni="";
		$TotUwi="";
		$Pre="";
		$Imp="";
	}
?>
