<?php
include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
ejt("DELETE FROM stocks_pre_par");
$s="SELECT * FROM stocks_presentaciones ORDER BY sp_id_almacen,sp_id_producto";
$rs = grs($s);
while ($r = $rs->fetch_assoc()){
	$_ALM=$r['sp_id_almacen'];
	$_PRO=$r['sp_id_producto'];
	$_PIE=$r['sp_presentacion'];
	$_STO=$r['sp_cantidad'];
	$_PRV=$r['sp_provicional'];
	//
	$I_DEVO=gn("SUM(CA)","vkar_devoluciones","ID_ALM=$_ALM AND ID_PRO=$_PRO AND PIE=$_PIE");
	$I_TRW1=gn("SUM(GE_KA)","vkar_transformaciones","ID_ALM=$_ALM AND ID_PRO=$_PRO AND GE_PIE=$_PIE");
	$I_TRW2=gn("SUM(SU_KA)","vkar_transformaciones","ID_ALM=$_ALM AND ID_PRO=$_PRO AND SU_PIE=$_PIE");
	$I_COMP=gn("SUM(CA)","vkar_com","ID_ALM=$_ALM AND ID_PRO=$_PRO AND UNI=$_PIE AND ET='NO'");
	$I_TRAS=gn("SUM(CA)","vkar_tra","ID_ALM_D=$_ALM AND ID_PRO=$_PRO AND UNI=$_PIE AND VAL='S'");
	$I_AJUP=gn("SUM(CA)","vkar_ajup","ID_ALM=$_ALM AND ID_PRO=$_PRO AND PIE_N=$_PIE AND ACC='Q'");
	$I_AJUX=gn("SUM(cant)","ajustex","id_almacen=$_ALM AND id_producto=$_PRO AND piezas=$_PIE AND accion='A'");
	$I_AJUS=gn("SUM(CA)","vkar_aju","ID_ALM=$_ALM AND ID_PRO=$_PRO AND UNI=$_PIE AND ACC='A'");
	//
	$E_TRAS=gn("SUM(CA)","vkar_tra","ID_ALM_O=$_ALM AND ID_PRO=$_PRO AND UNI=$_PIE AND VAL='S'");
	$E_AJUP=gn("SUM(CA)","vkar_ajup","ID_ALM=$_ALM AND ID_PRO=$_PRO AND PIE=$_PIE AND ACC='Q'");
	$E_AJUS=gn("SUM(CA)","vkar_aju","ID_ALM=$_ALM AND ID_PRO=$_PRO AND UNI=$_PIE AND ACC='Q'");
	$E_AJUX=gn("SUM(cant)","ajustex","id_almacen=$_ALM AND id_producto=$_PRO AND piezas=$_PIE AND accion='Q'");
	$E_GINT=gn("SUM(CA)","vkar_gui_int","ID_ALM=$_ALM AND ID_PRO=$_PRO AND PIE=$_PIE");
	$E_TRW1=gn("SUM(SA_KA)","vkar_transformaciones","ID_ALM=$_ALM AND ID_PRO=$_PRO AND SA_PIE=$_PIE");
	$E_TRW2=gn("SUM(RE_KA)","vkar_transformaciones","ID_ALM=$_ALM AND ID_PRO=$_PRO AND RE_PIE=$_PIE");
	$E_VENT=gn("SUM(CA)","vkar_ven","ID_ALM=$_ALM AND ID_PRO=$_PRO AND UNI=$_PIE");
	$E_GUIR=gn("SUM(CA)","vkar_gui","ID_ALM=$_ALM AND ID_PRO=$_PRO AND UNI=$_PIE");
	//
	$STOCAL=($I_DEVO+$I_TRW1+$I_TRW2+$I_COMP+$I_TRAS+$I_AJUP+$I_AJUX+$I_AJUS)-($E_TRAS+$E_AJUP+$E_AJUS+$E_AJUX+$E_GINT+$E_TRW1+$E_TRW2+$E_VENT+$E_GUIR);
	$DIFERE=$_STO - $STOCAL;	
	$s="INSERT INTO stocks_pre_par VALUES ($_ALM,$_PRO,$_PIE,$_STO,$STOCAL,$DIFERE,$_PRV)";
	ejt($s);	
}
echo "OK"."<br>";
?>