<?php
	$StrHWND_K=$_SESSION['SYS_HW']."_K";
	//-------------------------
	$StrId=V($_POST['TxtId']);
	$StrCmd=V($_POST['TxtAc']);
	if(strlen($StrCmd)==0){
		$StrCmd=V($_GET['TxtAc']);
		if($StrCmd=="X")$StrCmd="C";
	}
	//-------------------------
	$StrEmp=V($_POST['CboEmp']);
	$StrAlm=V($_POST['CboAlm']);
	//
	if(strlen($StrEmp)==0)$StrEmp=GE("N");
	if(strlen($StrAlm)==0)$StrAlm=GA($StrEmp,"N");	
	//	
	$StrPro_ID=vbval(V($_POST['Pro_TxtId']));	
	$StrPro_DE=V($_POST['Pro_TxtDes']);
	//----------------------
	$StrIdPro=$StrPro_ID;
	$StrCod=V($_POST['TxtCod']);
	$StrDes=V($_POST['TxtDes']);
	$StrDex=V($_POST['TxtDex']);
	//
	$StrDex_=val_dex($StrDex);
	//
	if(strlen($LStrPag)==0)$LStrPag="0";
	if($StrCmd=="MSig")$LStrPag=($LStrPag+1);
	if($StrCmd=="MAnt")$LStrPag=($LStrPag-1);
	if($StrCmd=="MPri")$LStrPag=0;
	if($StrCmd=="MUlt")$LStrPag=(floor($LStrTR / $VP_RegXPag) );	
	//
	if($StrCmd=="C"){
		ejt("DELETE FROM kardex WHERE hwnd='$StrHWND_K'");
		$StrCod="";
		$StrDes="";
		$StrPro_ID="";
		$StrPro_DE="";
		$StrDex="";
	}
	$cx=0;		
	//FUNCIONES DE ACTUALIZACION
	if($StrCmd=="G" && strlen($StrGT)==0){
		//
		$StrEmp_=gn("id_emp","empresas","emp_nomc='$StrEmp'");
		$StrIdPro=gn("id_producto","productos","pro_codigo='$StrCod'");	
		$StrIdAlm=gn("id_almacen","almacenes","alm_obs='$StrAlm' AND alm_id_empresa=$StrEmp_");
		ejt("DELETE FROM kardex WHERE hwnd='$StrHWND_K'");
		//
		$_STOCK_ACTUAL=gn("stock","stocks","id_almacen=$StrIdAlm AND id_producto=$StrIdPro");
		$_CC=0;
		//COMPRAS
		$K_FE="";
		$K_CO="";
		$K_NU="";
		$K_CP="";
		$K_CA="";
		$K_GI="";		
		$K_SI=0;
		$K_SF=0;
		//
		$TOT_SAI=0;
		$TOT_COM=gn("SUM(CA*UNI)","vkar_com","ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm and ET='NO' AND FEC<'$StrDex_'");
		$TOT_TRI=gn("SUM(CA*UNI)","vkar_tra","ID_PRO=$StrIdPro AND ID_ALM_D=$StrIdAlm AND VAL='S' AND FEC<'$StrDex_'");
		$TOT_AJA=gn("SUM(CA*UNI)","vkar_aju","ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND ACC='A' AND FEC<'$StrDex_'");
		$TOT_AXA=gn("SUM(cant * piezas)","ajustex","id_producto=$StrIdPro AND id_almacen=$StrIdAlm AND accion='A' AND fecha<'$StrDex_'");		
		$TOT_DEV=gn("SUM(CA*PIE)","vkar_devoluciones","ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm  AND FEC<'$StrDex_'");
		//
		$TOT_TRS=gn("SUM(CA*UNI)","vkar_tra","ID_PRO=$StrIdPro AND ID_ALM_O=$StrIdAlm AND VAL='S' AND FEC<'$StrDex_'");
		$TOT_AJP=gn("SUM(CA*PIE_AJ)","vkar_ajup","ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND ACC='Q' AND FEC<'$StrDex_'");
		$TOT_AJQ=gn("SUM(CA*UNI)","vkar_aju","ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND ACC='Q' AND FEC<'$StrDex_'");		
		$TOT_AXQ=gn("SUM(cant * piezas)","ajustex","id_producto=$StrIdPro AND id_almacen=$StrIdAlm AND accion='Q' AND fecha<'$StrDex_'");		
		$TOT_GIN=gn("SUM(CA * PIE)","vkar_gui_int","ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND FEC<'$StrDex_'");
		$TOT_VEN=gn("SUM(CA * UNI)","vkar_ven","ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND FEC<'$StrDex_'");
		$TOT_GIA=gn("SUM(CA * UNI)","vkar_gui","ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND FEC<'$StrDex_'");
		//
		$TOT_SAI=$TOT_SAI +	($TOT_COM+$TOT_TRI+$TOT_AJA+$TOT_AXA+$TOT_DEV) - ($TOT_TRS+$TOT_AJP+$TOT_AJQ+$TOT_AXQ+$TOT_GIN+$TOT_VEN+$TOT_GIA);
		//
		$s="SELECT FEC,COM,NRO,CONCAT(REF_COM,' ',REF_SER,'-',REF_NUM) as REF,(CA*PIE) as TOT_PIE,GI 
			FROM vkar_devoluciones 
			WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm  AND FEC>='$StrDex_'
			ORDER BY FEC,GI";
		$result = grs($s);						
		while ($row = $result->fetch_assoc()){
			$cx++;
			$K_FE=$row['FEC'];
			$K_CO=$row['COM'];
			$K_NU=$row['NRO'];
			$K_CP=$row['REF'];
			$K_CA=$row['TOT_PIE'];
			$K_GI=$row['GI'];
			$_CC=$_CC+$K_CA;			
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,$K_CA,0,0,$K_GI)";
			ejt($s);			
		}
		//////////////////////////////////////////////////////////////		
		$s="SELECT FEC,COM,NUM,PROV,CA*UNI,GI 
			FROM vkar_com 
			WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm and ET='NO' AND FEC>='$StrDex_' 
			ORDER BY FEC,GI";
		$result = grs($s);						
		while ($row = $result->fetch_row()){
			$cx++;
			$K_FE=$row[0];
			$K_CO=$row[1];
			$K_NU=$row[2];
			$K_CP=$row[3];
			$K_CA=$row[4];
			$K_GI=$row[5];
			$_CC=$_CC+$K_CA;			
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,$K_CA,0,0,$K_GI)";
			ejt($s);			
		}
		//////////////////////////////////////////////////////////////		
		$s="SELECT FEC,'TRANSFERENCIA' as COM,NUM,ORI,CA*UNI,GI,TSER,TNUM
			FROM vkar_tra 
			WHERE ID_PRO=$StrIdPro AND ID_ALM_D=$StrIdAlm AND VAL='S' AND FEC>='$StrDex_'
			ORDER BY FEC,GI";			
		$result = grs($s);					
		while ($row = $result->fetch_row()){
			$cx++;
			$K_FE=$row[0];
			$K_CO=$row[1];
			$K_NU=$row[6]."-".$row[7];
			$K_CP=$row[3];
			$K_CA=$row[4];
			$K_GI=$row[5];
			$_CC=$_CC+$K_CA;						
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,$K_CA,0,0,$K_GI)";
			ejt($s);			
		}
		//////////////////////////////////////////////////////////////		
		$s="SELECT FEC,'TRANSFERENCIA' as COM,NUM,DES,CA*UNI,GI,TSER,TNUM
			FROM vkar_tra 
			WHERE ID_PRO=$StrIdPro AND ID_ALM_O=$StrIdAlm AND VAL='S' AND FEC>='$StrDex_'
			ORDER BY FEC,GI";			
		$result = grs($s);					
		while ($row = $result->fetch_row()){
			$cx++;
			$K_FE=$row[0];
			$K_CO=$row[1];
			$K_NU=$row[6]."-".$row[7];
			$K_CP=$row[3];
			$K_CA=$row[4];
			$K_GI=$row[5];		
			$_CC=$_CC-$K_CA;				
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,0,$K_CA,0,$K_GI)";
			ejt($s);			
		}
		////
		$s="SELECT FEC,COM,NUM,ACC,(CA*PIE_AJ) as TOT_PIE,GI 
			FROM vkar_ajup 
			WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND ACC='Q' AND FEC>='$StrDex_'
			ORDER BY FEC,GI";				
		$result = grs($s);					
		while ($row = $result->fetch_assoc()){
			$cx++;
			$K_FE=$row['FEC'];
			$K_CO=$row['COM'];
			$K_NU=$row['NUM'];
			$K_CP="";
			$K_CA=$row['TOT_PIE'];
			$K_GI=$row['GI'];	
			$_CC=$_CC-$K_CA;					
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,0,$K_CA,0,$K_GI)";
			ejt($s);			
		}				
		//**
		$s="SELECT FEC,'AJUSTE' as COM,NUM,ACC,CA*UNI,GI 
			FROM vkar_aju 
			WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND ACC='Q' AND FEC>='$StrDex_'
			ORDER BY FEC,GI";				
		$result = grs($s);					
		while ($row = $result->fetch_row()){
			$cx++;
			$K_FE=$row[0];
			$K_CO=$row[1];
			$K_NU=$row[2];
			$K_CP="";
			$K_CA=$row[4];
			$K_GI=$row[5];	
			$_CC=$_CC-$K_CA;					
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,0,$K_CA,0,$K_GI)";
			ejt($s);			
		}				
		
		$s="SELECT fecha,'AJUXTE' as COM,id_ajuste,accion,cant * piezas,gi,obs 
			FROM ajustex 
			WHERE id_producto=$StrIdPro AND id_almacen=$StrIdAlm AND accion='Q' AND fecha>='$StrDex_'
			ORDER BY fecha";		
		$result = grs($s);					
		while ($row = $result->fetch_row()){
			$cx++;
			$K_FE=$row[0];
			$K_CO=$row[1];
			$K_NU=$row[2];
			$K_CP=$row[6];
			$K_CA=$row[4];
			$K_GI=$row[5];	
			$_CC=$_CC-$K_CA;					
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,0,$K_CA,0,$K_GI)";
			ejt($s);			
		}				
		//////////////////////////////////////////////////////////////		
		$s="SELECT fecha,'AJUXTE' as COM,id_ajuste,accion,cant * piezas,gi,obs 
			FROM ajustex 
			WHERE id_producto=$StrIdPro AND id_almacen=$StrIdAlm AND accion='A' AND fecha>='$StrDex_' 
			ORDER BY fecha";	
		$result = grs($s);					
		while ($row = $result->fetch_row()){
			$cx++;
			$K_FE=$row[0];
			$K_CO=$row[1];
			$K_NU=$row[2];
			$K_CP=$row[6];
			$K_CA=$row[4];
			$K_GI=$row[5];		
			$_CC=$_CC+$K_CA;				
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,$K_CA,0,0,$K_GI)";
			ejt($s);			
		}				
		//
		$s="SELECT FEC,'AJUSTE' as COM,NUM,ACC,CA * UNI,GI 
			FROM vkar_aju 
			WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND ACC='A' AND FEC>='$StrDex_'
			ORDER BY FEC,GI";
		$result = grs($s);					
		while ($row = $result->fetch_row()){
			$cx++;
			$K_FE=$row[0];
			$K_CO=$row[1];
			$K_NU=$row[2];
			$K_CP="";
			$K_CA=$row[4];
			$K_GI=$row[5];		
			$_CC=$_CC+$K_CA;				
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,$K_CA,0,0,$K_GI)";
			ejt($s);			
		}				
		//////////////////////////////////////////////////////////////
		$s="SELECT FEC,COM,NUM,CLI,(CA * PIE) as TOT_PIE,GI,CONCAT(COT_SER,'-',COT_NUM) as REF
			FROM vkar_gui_int 
			WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND FEC>='$StrDex_'
			ORDER BY FEC,GI";
		$result = grs($s);					
		while ($row = $result->fetch_assoc()){
			$cx++;
			$K_FE=$row['FEC'];
			$K_CO=$row['COM'];
			$K_NU=$row['NUM'];
			$K_CP=$row['REF']." | ".$row['CLI'];
			$K_CA=$row['TOT_PIE'];
			$K_GI=$row['GI'];	
			$_CC=$_CC-$K_CA;					
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,0,$K_CA,0,$K_GI)";
			ejt($s);			
		}	
		//////////////////////////////////////////////////////////////
		$s="SELECT FEC,COM,CONCAT(SER,'-',NUM),CLI,CA * UNI,GI 
			FROM vkar_ven 
			WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND FEC>='$StrDex_'
			ORDER BY FEC,GI";
		$result = grs($s);					
		while ($row = $result->fetch_row()){
			$cx++;
			$K_FE=$row[0];
			$K_CO=$row[1];
			$K_NU=$row[2];
			$K_CP=$row[3];
			$K_CA=$row[4];
			$K_GI=$row[5];	
			$_CC=$_CC-$K_CA;					
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,0,$K_CA,0,$K_GI)";
			ejt($s);			
		}
		//////////////////////////////////////////////////////////////
		$s="SELECT FEC,COM,CONCAT(SER,'-',NUM),CLI,CA * UNI,GI 
			FROM vkar_gui 
			WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND FEC>='$StrDex_'
			ORDER BY FEC,GI";
		$result = grs($s);				
		while ($row = $result->fetch_row()){
			$cx++;
			$K_FE=$row[0];
			$K_CO=$row[1];
			$K_NU=$row[2];
			$K_CP=$row[3];
			$K_CA=$row[4];
			$K_GI=$row[5];	
			$_CC=$_CC-$K_CA;					
			$s="INSERT INTO kardex VALUES ($cx,'$StrHWND_K',
				'$K_FE','$K_CO','$K_NU','$K_CP',0,0,$K_CA,0,$K_GI)";
			ejt($s);			
		}
		//
		$s="INSERT INTO kardex VALUES (0,'$StrHWND_K','$StrDex_','SALDO INICIAL','','',0,0,0,$TOT_SAI,0)";
		ejt($s);
		//
		$K_SI=$TOT_SAI;
		$K_SF=0;
		$s="SELECT id,ing,sal FROM kardex 
			WHERE hwnd='$StrHWND_K' ORDER BY fec,gi";
		$result = grs($s);			
		$KK_ING=0;
		$KK_SAL=0;
		$KK_ID=0;
		while ($row = $result->fetch_row()){
			$KK_ID=$row[0];
			$KK_ING=$row[1];
			$KK_SAL=$row[2];
			$K_SF=$K_SI + $KK_ING - $KK_SAL;
			$s="UPDATE kardex SET fin=$K_SF,ini=$K_SI 
				WHERE hwnd='$StrHWND_K' AND id=$KK_ID";
			ejt($s);			
			$K_SI=$K_SF;			
		}
		//////////////////////////////////////////////////////////////		
		$LStrMsg="Proceso terminado";
		$LStrFoc="document.getElementById('TxtCod').focus();";						
		$StrGT="S";		
	}	
	//////////////////////////////////
	if($StrGT=="S"){
		if(strlen($LStrMsg)>0){
			if(strlen($LStrFoc)==0){
				$LStrMsg="onload=\"alert('$LStrMsg');\"";
			}else{
				$LStrMsg="onload=\"alert('$LStrMsg');$LStrFoc;\"";
			}
		}else{
			if(strlen($LStrFoc)>0){
				$LStrMsg="onload=\"$LStrFoc;\"";
			}
		}
	}else{
		$LStrFoc="document.getElementById('CboEmp').focus();";	
		$LStrMsg="onload=\"$LStrFoc;\"";	
	}
?>