<?php
include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
//204945 , 238438 , 227625
function FxCOT_PARSE_CANTIDADES_XX($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'];
		echo $PRO_KOD."<br>";
		$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");
		echo $GR_CAN." aa<br>";
		$GI_CAN=gn("SUM(CAN)","vix23_ginterna_det",
				   "COT_KOD=$COT_KOD AND PRO_KOD=$PRO_KOD AND PIE=$PIE AND ANU='N'");
		echo $GI_CAN." bb<br>";
		$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'");
		echo $DJ_CAN." cc<br>";
		$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'");
		echo $DV_CAN." dd<br>";
		$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'");
		echo $VE_CAN." ee<br>";
		$VK_CAN=gn("SUM(CAN)","vix23_ventas_kill",
				   "REF_KOD=$COT_KOD AND PRO_KOD=$PRO_KOD AND PIE=$PIE AND ANU='N'");
		echo $VK_CAN." ff<br>";
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
		$CA_CONTROL_CAL=$CAN - ($GI_CAN+$GR_CAN);
		$VE_CONTROL_CAL=$CAN - $VE_CAN + $VK_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);
			//
			$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);
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			
		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);
		}
		//
		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);
			$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);
		}
		//
		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);
			$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);
		}
		//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++			
	}
}

$KOD=204945;
FxCOT_PARSE_CANTIDADES_XX($KOD);
echo "OK";
/*
$KOD=238438;
FxCOT_PARSE_CANTIDADES($COT_KOD);
echo "OK";

$KOD=227625;
FxCOT_PARSE_CANTIDADES($COT_KOD);
echo "OK";
*/
?>