<?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";
	}
	//-------------------------
	$StrAlm=V($_POST['CboAlm']);
	//------------------------
	$StrPro_ID=V($_POST['Pro_TxtId']);
	$StrPro_DE=V($_POST['Pro_TxtDes']);
	//----------------------
	$StrIdPro=$StrPro_ID;
	$StrCod=V($_POST['TxtCod']);
	$StrDes=V($_POST['TxtDes']);
	//------------------------
	if(strlen($StrPro_ID)==0 || !is_numeric($StrPro_ID)) $StrPro_ID="0";
	//FUNCIONES DE DESZPLAZAMIENTO
	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="";
	}
	$cx=0;		
	//FUNCIONES DE ACTUALIZACION
	if($StrCmd=="G" && strlen($StrGT)==0){		
		$StrIdAlm=gn("id_almacen","almacenes","alm_nombre='$StrAlm'");
		ejt("DELETE FROM kardex WHERE hwnd='$StrHWND_K'");
		//INI
		$_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;
		//////////////////////////////////////////////////////////////		
		$s="SELECT FEC,COM,NUM,PROV,CA,GI 
			FROM vkar_com WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm
			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,GI 
			FROM vkar_tra WHERE ID_PRO=$StrIdPro AND ID_ALM_D=$StrIdAlm AND VAL='S'
			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,DES,CA,GI 
			FROM vkar_tra WHERE ID_PRO=$StrIdPro AND ID_ALM_O=$StrIdAlm AND VAL='S'
			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,'AJUSTE' as COM,NUM,ACC,CA,GI 
			FROM vkar_aju WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND ACC='Q' 
			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,'0' as GI 
			FROM ajustex WHERE id_producto=$StrIdPro AND id_almacen=$StrIdAlm AND accion='Q' 
			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="";
			$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,'0' as GI 
			FROM ajustex WHERE id_producto=$StrIdPro AND id_almacen=$StrIdAlm AND accion='A' 
			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="";
			$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,GI 
			FROM vkar_aju WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm AND ACC='A' 
			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,CONCAT(SER,'-',NUM),CLI,CA,GI 
			FROM vkar_ven WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm
			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,GI 
			FROM vkar_gui WHERE ID_PRO=$StrIdPro AND ID_ALM=$StrIdAlm
			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);			
		}
		//////////////////////////////////////////////////////////////		
		$K_SI=0;		
		if($_STOCK_ACTUAL==$_CC){
			$K_SI=0;
		}		
		if($_CC>$_STOCK_ACTUAL){
			$K_SI=$_STOCK_ACTUAL - $_CC;
		}		
		if($_STOCK_ACTUAL>$_CC){
			$K_SI=$_CC-$_STOCK_ACTUAL;
		}		

		$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('CboAlm').focus();";	
		$LStrMsg="onload=\"$LStrFoc;\"";	
	}
?>