<?php
//CASO 01 CA_CONTROL NEG
include("conexion_demo.php");
//+++
/*
FUN_COT_UPDATE($StrId);
*/
//////////////////////////////////////
function FxCOT_SUM_DET($COT_KOD){
	$X_IMPORTE=0;
	$s="SELECT * FROM cotizaciones_det 
		WHERE id_cotizacion=$COT_KOD";
	$rs=grs($s);
	while($r=$rs->fetch_assoc()){
		$X_IM=NF($r['im'],3);	
		$X_IMPORTE=$X_IMPORTE+$X_IM;
	}
	$N=round($X_IMPORTE,2);
	$M=round($X_IMPORTE,1);
	if($N>$M){
		$X_IMPORTE=$M+0.1;	
	}else{
		$X_IMPORTE=$M;	
	}
	return $X_IMPORTE;
}
//////////////////////////////////////
function LQ_COT_ERR_TOT_LOG($cot,$cot_fec,$cot_tot,$tot_rea,$tot_des,$tot_det,$tipo,$dif,$acc){
	$FEC=date("Y-m-d");
	$s="INSERT INTO lq_cot_err_tot (fec,cot,cot_fec,cot_tot,cot_tot_rea,cot_tot_des,cot_tot_det,cot_err_tip,cot_dif,acc) 
		VALUES ('$FEC',$cot,'$cot_fec',$cot_tot,$tot_rea,$tot_des,$tot_det,'$tipo',$dif,'$acc')";
	ejt($s);
	//echo $s."<br>";
}
//////////////////////////////////////

//////////////////////////////////////
function FxCOT_PARSE_TOT($COT_KOD,$ACC){
	$r=gr("cotizaciones","id_cotizacion=".$COT_KOD);
	$COT_FEC=$r['fec'];
	$TOTAL=round($r['tot'],1);
	$TOTAL_REA=round($r['tot_real'],1);
	$TOTAL_DES=round($r['tot_des'],1);
	$DET_SUM_TOT=round(FxCOT_SUM_DET($COT_KOD),1);
	//
	if($TOTAL!=round($TOTAL_REA + $TOTAL_DES,1)){
		LQ_COT_ERR_TOT_LOG($COT_KOD,$COT_FEC,$TOTAL,$TOTAL_REA,$TOTAL_DES,$DET_SUM_TOT,"INCOHERENCIA",0,$ACC);
		return false;
	}	
	$DIF=round($TOTAL - $DET_SUM_TOT,1);
	if($DIF==0){
		return true;
	}else{
		if(abs($DIF)==0.1){
			LQ_COT_ERR_TOT_LOG($COT_KOD,$COT_FEC,$TOTAL,$TOTAL_REA,$TOTAL_DES,$DET_SUM_TOT,"OX",0,$ACC);
			return true;
		}else{
			if(abs($DIF)==0.2){
				LQ_COT_ERR_TOT_LOG($COT_KOD,$COT_FEC,$TOTAL,$TOTAL_REA,$TOTAL_DES,$DET_SUM_TOT,"OZ",0,$ACC);
				return true;
			}else{
				LQ_COT_ERR_TOT_LOG($COT_KOD,$COT_FEC,$TOTAL,$TOTAL_REA,$TOTAL_DES,$DET_SUM_TOT,"OBS",$DIF,$ACC);
				return false;
			}
		}
	}
	return false;
}
//////////////////////////////////////
function FxCOT_PARSE($COT_KOD){
	$COT_FEC=gs("fec","cotizaciones","id_cotizacion=".$COT_KOD);
	$SYS_FEC=date("Y-m-d");
	$s="SELECT * FROM cotizaciones_det WHERE id_cotizacion=".$COT_KOD;
	$rs=grs($s);
	while($r=$rs->fetch_assoc()){
		$PRO_KOD=$r['id_producto'];
		$COD=$r['cod'];
		$CAN=$r['ca'];
		$PIE=$r['can_uni'];
		$VE_CONTROL=$r['ve_control'];
		$CA_CONTROL=$r['ca_control'];//DISMINUYE CON LAS GUIAS INTERNAS
		$DEJA=$r['dej'];//INICALMENT CERO
		$DEVO=$r['dev'];//INICALMENT CERO
		//
		$GR_CAN=gn("SUM(CAN)","vix23_guias_det",
				   "REF_KOD=$COT_KOD AND PRO_KOD=$PRO_KOD AND PIE=$PIE AND ANU='N' AND COM_REF_KOD=-3");
		$GI_CAN=gn("SUM(CAN)","vix23_ginterna_det",
				   "COT_KOD=$COT_KOD AND PRO_KOD=$PRO_KOD AND PIE=$PIE AND ANU='N'");
		$DJ_CAN=gn("SUM(CAN)","vix23_devoluciones",
				   "REF_COM_KOD=-3 AND REF_KOD=$COT_KOD AND PRO_KOD=$PRO_KOD AND PIE=$PIE AND ANU='N' AND TIPO='J'");
		$DV_CAN=gn("SUM(CAN)","vix23_devoluciones",
				   "REF_COM_KOD=-3 AND REF_KOD=$COT_KOD AND PRO_KOD=$PRO_KOD AND PIE=$PIE AND ANU='N' AND TIPO='D'");
		$VE_CAN=gn("SUM(CAN)","vix23_ventas",
				   "COM_REF_KOD=-3 AND REF_KOD=$COT_KOD AND PRO_KOD=$PRO_KOD AND PIE=$PIE AND ANU='N'");
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
		$CA_CONTROL_CAL=$CAN - ($GI_CAN+$GR_CAN);
		$VE_CONTROL_CAL=$CAN - $VE_CAN;
		$CAN_ASI=0;
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		if($VE_CONTROL!=$VE_CONTROL_CAL){			
			if($VE_CONTROL_CAL<0){
				$CAN_ASI=0;
			}else{
				$CAN_ASI=$VE_CONTROL_CAL;
			}
			$s="UPDATE cotizaciones_det 
				SET ve_control=".$CAN_ASI." 
				WHERE id_cotizacion=".$COT_KOD." AND id_producto=".$PRO_KOD." AND can_uni=".$PIE;
			ejt($s);
			echo $s."<br>";
			//
			$s="INSERT INTO lq_cot_err (fec,tip,cot,cot_fec,pro,pie,ci,cf,cod,can,can_asi) 
				VALUES ('$SYS_FEC','VE',$COT_KOD,'$COT_FEC',$PRO_KOD,$PIE,$VE_CONTROL,$VE_CONTROL_CAL,'$COD',$CAN,$CAN_ASI)";
			ejt($s);
			echo $s."<br>";
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			
		if($CA_CONTROL!=$CA_CONTROL_CAL){
			if($CA_CONTROL_CAL<0){
				$CAN_ASI=0;
			}else{
				$CAN_ASI=$CA_CONTROL_CAL;
			}
			$s="UPDATE cotizaciones_det 
				SET ca_control=".$CAN_ASI." 
				WHERE id_cotizacion=".$COT_KOD." AND id_producto=".$PRO_KOD." AND can_uni=".$PIE;
			ejt($s);
			echo $s."<br>";
			$s="INSERT INTO lq_cot_err (fec,tip,cot,cot_fec,pro,pie,ci,cf,cod,can,can_asi) 
				VALUES ('$SYS_FEC','CA',$COT_KOD,'$COT_FEC',$PRO_KOD,$PIE,$CA_CONTROL,$CA_CONTROL_CAL,'$COD',$CAN,$CAN_ASI)";
			ejt($s);
			echo $s."<br>";
		}
		//
		if($DJ_CAN!=$DEJA){
			$s="UPDATE cotizaciones_det 
				SET dej=".$DJ_CAN." 
				WHERE id_cotizacion=".$COT_KOD." AND id_producto=".$PRO_KOD." AND can_uni=".$PIE;
			ejt($s);
			echo $s."<br>";
			$s="INSERT INTO lq_cot_err (fec,tip,cot,cot_fec,pro,pie,ci,cf,cod,can,can_asi) 
				VALUES ('$SYS_FEC','DJ',$COT_KOD,'$COT_FEC',$PRO_KOD,$PIE,$DEJA,$DJ_CAN,'$COD',$CAN,$DJ_CAN)";
			ejt($s);
			echo $s."<br>";
		}
		//
		if($DV_CAN!=$DEVO){
			$s="UPDATE cotizaciones_det 
				SET dev=".$DV_CAN." 
				WHERE id_cotizacion=".$COT_KOD." AND id_producto=".$PRO_KOD." AND can_uni=".$PIE;
			ejt($s);
			echo $s."<br>";
			$s="INSERT INTO lq_cot_err (fec,tip,cot,cot_fec,pro,pie,ci,cf,cod,can,can_asi) 
				VALUES ('$SYS_FEC','DV',$COT_KOD,'$COT_FEC',$PRO_KOD,$PIE,$DEVO,$DV_CAN,'$COD',$CAN,$DV_CAN)";
			ejt($s);
			echo $s."<br>";
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
		
	}
}
//SELECT * FROM `cotizaciones` WHERE `fec`>='2023-05-01' AND `fec`>='2023-05-31' 
$s="SELECT * FROM cotizaciones 
	WHERE YEAR(fec)=2023 AND MONTH(fec)>=7 AND ANU='N' ORDER BY id_cotizacion";
//$s="SELECT * FROM cotizaciones WHERE id_cotizacion>242247";
$rs=grs($s);
while($r=$rs->fetch_assoc()){	
	$COT_KOD=$r['id_cotizacion'];
	//FxCOT_PARSE($COT_KOD);
	FxCOT_PARSE_TOT($COT_KOD,"DEP");
}
echo "FINX";
?>