<?php
function COSTEO_GET_FACTOR($REF_KOD){
	$SUM_FOB=gn("SUM(pro_fob)","ges_xml_costeo_det_pro","id_ref=$REF_KOD");
	//+++++
	$SUM_DOL=gn("SUM(tot_dol)","ges_xml_costeo_det","id_ref=$REF_KOD");		
	$COM_EXP=round($SUM_FOB*0.03,2);
	$SUM_FOB=round($SUM_FOB,2);		
	$TOTAL= round($SUM_DOL + $COM_EXP + $SUM_FOB,2);
	//
	$FACTOR=round($TOTAL / $SUM_FOB,3);
	if(is_nan($FACTOR)){
		$FACTOR=0;
	}
	if(is_infinite($FACTOR)){
	  $FACTOR=0;
	}
	return $FACTOR;
}
//
function COSTEO_CONCEPTOS_ACT_SOL_DOL($REF_KOD,$TCAM){
	$s="SELECT * FROM ges_xml_costeo_det WHERE id_ref=$REF_KOD ";
	$rs=grs($s);
	while($r=$rs->fetch_assoc()){
		$KEY=$r['id'];
		$TOT=$r['tot'];
		$MND=$r['mnd'];
		//
		if($MND=="S"){
			$SOL=$TOT;
			$DOL=round($SOL / $TCAM,2);				
		}else{
			$DOL=$TOT;
			$SOL=round($DOL * $TCAM,2);				
		}
		$s="UPDATE ges_xml_costeo_det SET tot_sol=$SOL,tot_dol=$DOL WHERE id=$KEY";
		ejt($s);
	}
}
//
function PRODUCTOS_ACTUALIZAR($REF_KOD){
	$FACTOR=COSTEO_GET_FACTOR($REF_KOD);
	$s="SELECT * FROM ges_xml_costeo_det_pro WHERE id_ref=$REF_KOD";
	$rs=grs($s);
	while($r=$rs->fetch_assoc()){
		$KEY=$r['id'];
		$CTN=$r['pro_ctn'];
		$SET=$r['pro_set'];
		$PRE=$r['pro_pre'];
		//
		$ProFob=round($SET * $PRE,3);
		$ProExt1=round($ProFob * $FACTOR,3);
		$ProExt2=round($ProExt1 * 1.03,3);
		//
		$s="UPDATE ges_xml_costeo_det_pro 
			SET 
			pro_fob=$ProFob,
			pro_ext1=$ProExt1,
			pro_ext2=$ProExt2
			WHERE id=$KEY";
		ejt($s);
	}
}
?>

