<?php
$LIMPIAR=0;
$SYS_FOC="Fin";
//-------------------------------------------------------------------------
$Fer=V($_POST['Fer']);
$Fin=V($_POST['Fin']);
$Cob=V($_POST['Cob']);
$Sal=V($_POST['Sal']);
$Obs=V($_POST['Obs']);
//-------------------------------------------------------------------------++
function RECALCULAR_KOD($KOD){
	$COB=gn("fp_cob","fechas_plazos","id_fp=$KOD");
	$SAL=gn("fp_sal","fechas_plazos","id_fp=$KOD");
	//
	$s="SELECT * FROM cotizaciones WHERE fv_id=".$KOD." ORDER BY id_cotizacion";
	$rs=grs($s);
	while($r=$rs->fetch_assoc()){
		$FEC=$r['fec'];
		$KOT=$r['id_cotizacion'];
		//+++++++++++
		$FEC_COB=FUN_FEC_ADD_EX($FEC,$COB);
		$FEC_SAL=FUN_FEC_ADD_EX($FEC,$SAL);
		$s="UPDATE cotizaciones 
			SET fv_cob='$FEC_COB',fv_sal='$FEC_SAL' 
			WHERE id_cotizacion=$KOT";
		ejt($s);
	}
	
}
//
function RECALCULAR_FEC($FEC,$COB,$SAL){
	$s="SELECT * FROM cotizaciones WHERE fec='$FEC' ORDER BY id_cotizacion";
	$rs=grs($s);
	while($r=$rs->fetch_assoc()){
		$FEC=$r['fec'];
		$KOT=$r['id_cotizacion'];
		//
		$FEC_COB=FUN_FEC_ADD_EX($FEC,$COB);
		$FEC_SAL=FUN_FEC_ADD_EX($FEC,$SAL);
		$s="UPDATE cotizaciones 
			SET fv_cob='$FEC_COB',fv_sal='$FEC_SAL' 
			WHERE id_cotizacion=$KOT";
		ejt($s);
	}
}
//-------------------------------------------------------------------------
PID($StrId,V($_GET['Id']),$StrCmd,$LStrPag,V($_GET['Pag']));
//-------------------------------------------------------------------------
if(strlen($StrId)>0 && $StrCmd=="AR"){
	$Id=$StrId;
	$r=gr("vis_fechas_plazos","ID=".$Id);	
	$Fer=$r['FER_'];
	$Fin=$r['FIN_'];
	$Cob=$r['COB'];
	$Sal=$r['SAL'];
	$Obs=$r['OBS'];
	//
	$SYS_FOC=$SYS_FOC;
}
//-------------------------------------------------------------------------

if($StrCmd=="G" || $StrCmd=="M"){
	$Fer_=ToDate($Fer);
	$Fin_=ToDate($Fin);
	$Cob_=vbval($Cob);
	$Sal_=vbval($Sal);
}
//-------------------------------------------------------------------------

if($StrCmd=="G"){
	if(gn("count(*)","fechas_plazos","fp_fin='$Fin_'")>0){
		$SYS_MSG="Fecha de inicio ya registrada";
		$SYS_FOC=$SYS_FOC;
		$OK=0;
	}
	if($OK==1){
		$SYS_FEC=date("Y-m-d");
		$SYS_HOR=GetHoraAP();
		$SYS_TIM=time();
		//
		$Id=GenId("id_fp","fechas_plazos");
		$s="INSERT INTO fechas_plazos 
			VALUES ($Id,'$SYS_FEC','$SYS_HOR',
			$SYS_TIM,'$Fer_','$Fin_',$Cob_,$Sal_,'$Obs',$UZU)";
		ejt($s);
		//BIT
		$_BIT_MOD_ID=$Id;
		$_BIT_SQL_CAB=$s;
		$_BIT_SQL_DET="";
		$_BIT_PHP="sis_fec_ven.php";
		$_BIT_ID=BIT_ADD("MANTENIMIENTO","FECHAS/PLAZOS","GUARDAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,$s);
		//BIT	
		RECALCULAR_FEC($Fin_,$Cob_,$Sal_);
		//
		$SYS_MSG="Datos Guardados";
		$SYS_FOC=$SYS_FOC;
		$LIMPIAR=1;	
	}	
}
//-------------------------------------------------------------------------

if($StrCmd=="M"){
	if(gn("count(*)","fechas_plazos","fp_fin='$Fin_' AND id_fp<>$StrId")>0){
		$SYS_MSG="Fecha de inicio ya registrada";
		$SYS_FOC=$SYS_FOC;
		$OK=0;
	}
	if($OK==1){
		$s="UPDATE fechas_plazos 
			SET fp_fer='$Fer_',
				fp_fin='$Fin_',
				fp_cob=$Cob_,
				fp_sal=$Sal_,
				fp_obs='$Obs',fp_usu=$UZU 
			WHERE id_fp=$StrId";
		ejt($s);
		//BIT
		$_BIT_MOD_ID=$StrId;
		$_BIT_SQL_CAB=$s;
		$_BIT_SQL_DET="";
		$_BIT_PHP="sis_fec_ven.php";
		$_BIT_ID=BIT_ADD("MANTENIMIENTO","FECHAS/PLAZOS","MODIFICAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,$s);
		//BIT	
		RECALCULAR_KOD($StrId);
		//
		$SYS_MSG="Datos Actualizados";
		$SYS_FOC=$SYS_FOC;
		$LIMPIAR=1;
	}
}
//-------------------------------------------------------------------------
if($StrCmd=="E"){
	if($OK==1){
		$s="DELETE FROM fechas_plazos WHERE id_fp=$StrId";	
		ejt($s);
		//
		$s="UPDATE cotizaciones 
			SET fv_cob=fec,fv_sal=fec,fv_id=0 
			WHERE fv_id=$StrId ";
		//BIT
		$_BIT_MOD_ID=$StrId;
		$_BIT_SQL_CAB=$s;
		$_BIT_SQL_DET="";
		$_BIT_PHP="sis_fec_ven.php";
		$_BIT_ID=BIT_ADD("MANTENIMIENTO","FECHAS/PLAZOS","ELIMINAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,$s);
		//BIT	
		$SYS_MSG="Datos Eliminado";
		$SYS_FOC=$SYS_FOC;
		$LIMPIAR=1;
	}		
}
//-------------------------------------------------------------------------
if($LIMPIAR==1){
	$StrId="";		
	$Fer="";
	$Fin="";
	$Cob="";
	$Sal="";
	$Obs="";
	$OK=0;
}
//-------------------------------------------------------------------------
?>