<?php
//include "clase.conexion.php";
class clsInventarioHistorico extends clsConexion{
	public $varTitulo 	= "INVENTARIO HISTORICO";
	function FormuBusqueda() {
		$Array = array("T" => "TODOS LOS PRODUCTOS", "S" => "SOLO PRODUCTOS CON STOCK");
		$sql = "select id_almacen, nombre from _sv_almacen where _estado = 'A'";
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"450", "clase"=>"Formtitulo", "titulo"=>$this->varTitulo,
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST", "onsubmit"=>"return false;"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo)
									  ),
						"conte"=>array(array("titulo"=>"Tipo Reporte :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										     "campo"=>array("tipo"=>"select_simple", "id"=>"tipo", "name"=>"tipo", "valor"=>$varTipo, "ancho"=>"100",
														    "combo"=>array("sql"=>$Array, "sincod"=>"SI", "ancho"=>"307"))),
									   array("titulo"=>"Almacen :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										     "campo"=>array("tipo"=>"select", "id"=>"alm", "name"=>"alm", "valor"=>$_POST["alm"], "ancho"=>"100",
														    "combo"=>array("sql"=>$sql, "sincod"=>"SI", "ancho"=>"307"))),									   
									   array("titulo"=>"Fecha Inv. :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										     "campo"=>array("tipo"=>"fecha", "id"=>"femi", "name"=>"femi", "valor"=>$varFecha, "ancho"=>"80",
														  )
										    ),
									   array("titulo"=>"Proveedor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"pcod", "name"=>"pcod", "valor"=>$_POST["pcod"], 
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "blur"=>"xajax_seleccionar_proveedor(this.value)", 
																					 "dclick"=>"fn_cargar_listado_proveedor('pcod', 'pnom')"),
																			   array("tipo"=>"text", "id"=>"pnom", "name"=>"pnom", "valor"=>$_POST["pnom"], 
																					 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
															)
											 )
									   ),
						"boton"=>array(array("tipo"=>"submit", "id"=>"bcon", "name"=>"bcon", "ancho"=>"100", "texto"=>"Buscar",
					  					   "clase"=>"boton_buscar", "click"=>"javascript:xBuscarJS()"),
									 array("tipo"=>"button", "id"=>"bexp", "name"=>"bexp", "ancho"=>"100", "texto"=>"Exportar",
									 	   "clase"=>"boton_exportar", "click"=>"javascript:xExportarJS()"),								
									 array("tipo"=>"button", "id"=>"bimp", "name"=>"bimp", "ancho"=>"100", "texto"=>"Imprimir", 
										   "clase"=>"boton_imprimir", "click"=>"javascript:xImprimirJS()"),
									 )
						);
		return $this->setTablaForm($varTabla);
		}
 	function GridDatos($varForm = NULL
					  )  {						  
		$varPro  = ($varForm["pcod"]<>"")?$varForm["pcod"]:"NULL";
		$varFec  = ($varForm["femi"]<>"")?"'".FechaMySql($varForm["femi"])."'":"NULL";
		$sql = "call spQuery_Logisitica_Inventario_Historico('$varForm[tipo]', $varForm[alm], $varFec, $varPro)";
		
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"800", "sql_cab"=>$sql,	"pages"=>50,
						"conte"=>array(array("tamano"=>20, "titulo"=>"CPRO", "alinear"=>"center", "oculto"=>"SI"),
									   array("tamano"=>50, "titulo"=>"PROVEEDOR", "fijo"=>"SI"),
									   array("tamano"=>20, "titulo"=>"CTIP", "alinear"=>"center", "oculto"=>"SI"),
									   array("tamano"=>50, "titulo"=>"TIPO", "oculto"=>"SI"),
									   array("tamano"=>20, "titulo"=>"CFAM", "alinear"=>"center", "oculto"=>"SI"),
									   array("tamano"=>50, "titulo"=>"FAMILIA", "oculto"=>"SI"),
									   array("tamano"=>20, "titulo"=>"CSUB", "alinear"=>"center", "oculto"=>"SI"),
									   array("tamano"=>50, "titulo"=>"SUB FAMILIA", "oculto"=>"SI"),
									   array("tamano"=>20, "titulo"=>"CART", "alinear"=>"center"),
									   array("tamano"=>250, "titulo"=>"ARTICULO", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("tamano"=>50, "titulo"=>"UM_STOCK", "alinear"=>"center"),
									   array("tamano"=>50, "titulo"=>"PRECIO", "alinear"=>"right", "decimal"=>"2"),
									   array("tamano"=>50, "titulo"=>"STOCK ACTUAL", "alinear"=>"right", "decimal"=>"2"),									   
									   array("tamano"=>50, "titulo"=>"VALORIZA ACTUAL", "alinear"=>"right", "decimal"=>"2", "funcion"=>array("autosuma")),
									   array("tamano"=>50, "titulo"=>"STOCK HISTO", "alinear"=>"right", "decimal"=>"2"),									   
									   array("tamano"=>50, "titulo"=>"VALORIZA HISTO", "alinear"=>"right", "decimal"=>"2", "funcion"=>array("autosuma"))
									   )
						);
		
		return $this->SetGridTabla($varTabla);
		}
	function Imprimir($varTip = NULL,
					  $varAlm = NULL,
					  $varPro = NULL,
					  $varFec = NULL
					  )  {	
		$varPro  = ($varPro<>"")?$varPro:"NULL";
		$varEmi  = ($varFec<>"")?"'".FechaMySql($varFec)."'":"NULL";
		$sql = "call spQuery_Logisitica_Inventario_Historico('$varTip', $varAlm, $varEmi, $varPro)";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$varNumCol = $this->NumField($rs);
		$varNumCom = 
		$html.='<table style="width: 95%; padding: 0; margin-top:10px; margin-bottom:10px; border-collapse:collapse;" align="center">';
		$html.='<thead>';
		$html.='<tr>';
		$html.='<td>';
			$html.='<table style="width: 100%;">';
			$html.='<tr>';
			$html.='<td style="font-size:11px;width:50%;vertical-align:middle;text-align:left;"><b>'.Config("SYSEMP").'</b></td>';
			$html.='<td style="font-size:11px;width:50%;vertical-align:middle;text-align:right;"><b>'.$this->varTitulo.'</b></td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:left;"><b></b></td>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b>'.MostrarFechaActual().'</b></td>';
			$html.='</tr>';
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
		$html.='</thead>';	
		$html.='<tr>';	
		$html.='<td>';
			$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
			$html.='<thead>';
			$html.='<tr class="head">';			
			$html.='<td width="2" 	class="center"	rowspan="2">&nbsp;</td>';
			$html.='<td width="2" 	class="left"	rowspan="2">CPRO</td>';
			$html.='<td width="200" class="left"	rowspan="2">PRODUCTO</td>';
			$html.='<td width="50" 					rowspan="2">UM</td>';
			$html.='<td width="50" 	class="right" 	rowspan="2">PRECIO</td>';
			$html.='<td width="100" class="center" 	colspan="2">ACTUAL</td>';			
			$html.='<td width="100" class="center" 	colspan="2">AL '.$varFec.' </td>';
			$html.='</tr>';
			$html.='<tr class="head">';			
			$html.='<td width="100" class="right" NOWRAP>STOCK</td>';			
			$html.='<td width="100" class="right" NOWRAP>VALOR.</td>';
			$html.='<td width="100" class="right" NOWRAP>STOCK</td>';			
			$html.='<td width="100" class="right" NOWRAP>VALOR.</td>';
			$html.='</tr>';
			$html.='</thead>';
			
			$varPro_Ant = $varPro_Act = "";
			$varSub_Ant = $varSub_Act = "";
			$varSub_Tot_Act = 0;
			$varSub_Tot_His = 0;
			$varPro_Tot_Act = 0;
			$varPro_Tot_His = 0;
			while($fila = $this->FetchArray($rs))	{
				$varPro_Act = $fila["CLIN"];
				$varSub_Act = $fila["CSUB"];
				$varPro_Tot_Act = $varPro_Tot_Act + $fila["VALORIZA_ACTUAL"];
				$varPro_Tot_His = $varPro_Tot_His + $fila["VALORIZA_HISTO"];
				$varNumDoc++;
				if($varPro_Act <> $varPro_Ant) {
					if($varPro_Ant<>"") {
						$html.='<tr class="list_row">
								<td class="center">&nbsp;</td>
								<td class="left">&nbsp;</td>
								<td NOWRAP>&nbsp;</td>
								<td NOWRAP>&nbsp;</td>						
								<td class="right">&nbsp;</td>
								<td class="right">&nbsp;</td>
								<td class="right"><b>'.number_format($varSub_Tot_Act,"2",".",",").'</b></td>
								<td class="right">&nbsp;</td>
								<td class="right"><b>'.number_format($varSub_Tot_His,"2",".",",").'</b></td>';
						$html.='</tr>';	
						}
					$varSub_Tot_Act=0;	
					$varSub_Tot_His=0;
					$varPro_Ant = $fila["CLIN"];
					$varSub_Ant = $fila["CSUB"];
					$html.='<tr class="head">';
					$html.='<td class="center"><b>'.$fila["CLIN"].'</b></td>';
					$html.='<td class="left" colspan="8"><b>'.$fila["LINEA"].'</b></td>';
					$html.='</tr>';
					$html.='<tr class="head">';
					$html.='<td class="center"><b></b></td>';
					$html.='<td class="center"><b>'.$fila["CSUB"].'</b></td>';
					$html.='<td class="left" colspan="7"><b>'.$fila["TIPO"].' - '.$fila["FAMILIA"].' - '.$fila["SUB_FAMILIA"].'</b></td>';
					$html.='</tr>';
					}
				if($varSub_Act <> $varSub_Ant) {
					$varSub_Ant = $fila["CSUB"];
					$html.='<tr class="list_row">
							<td class="center">&nbsp;</td>
							<td class="left">&nbsp;</td>
							<td NOWRAP>&nbsp;</td>
							<td NOWRAP>&nbsp;</td>						
							<td class="right">&nbsp;</td>
							<td class="right">&nbsp;</td>
							<td class="right"><b>'.number_format($varSub_Tot_Act,"2",".",",").'</b></td>
							<td class="right">&nbsp;</td>
							<td class="right"><b>'.number_format($varSub_Tot_His,"2",".",",").'</b></td>';
					$html.='</tr>';				
					$html.='<tr class="head">';
					$html.='<td class="center"><b></b></td>';
					$html.='<td class="center"><b>'.$fila["CSUB"].'</b></td>';
					$html.='<td class="left" colspan="7"><b>'.$fila["TIPO"].' - '.$fila["FAMILIA"].' - '.$fila["SUB_FAMILIA"].'</b></td>';
					$html.='</tr>';
					$varSub_Tot_Act = $fila["VALORIZA_ACTUAL"];
					$varSub_Tot_His = $fila["VALORIZA_HISTO"];
					}
				else {
					$varSub_Tot_Act = $varSub_Tot_Act + $fila["VALORIZA_ACTUAL"];
					$varSub_Tot_His = $varSub_Tot_His + $fila["VALORIZA_HISTO"];
					}
				$html.='<tr class="list_row">
						<td class="center">&nbsp;</td>
						<td class="left">'.$fila["CPRO"].'</td>
						<td NOWRAP>'.$fila["PRODUCTO"].'</td>
						<td NOWRAP>'.$fila["UM"].'</td>						
						<td class="right">'.number_format($fila["PRECIO"],"5",".",",").'</td>
						<td class="right">'.number_format($fila["STOCK_ACTUAL"],"2",".",",").'</td>
						<td class="right">'.number_format($fila["VALORIZA_ACTUAL"],"2",".",",").'</td>
						<td class="right">'.number_format($fila["STOCK_HISTO"],"2",".",",").'</td>
						<td class="right">'.number_format($fila["VALORIZA_HISTO"],"2",".",",").'</td>';						
				$html.='</tr>';
				}
			$html.='<tr class="list_row">
					<td class="center">&nbsp;</td>
					<td class="left">&nbsp;</td>
					<td NOWRAP>&nbsp;</td>
					<td NOWRAP>&nbsp;</td>						
					<td class="right">&nbsp;</td>
					<td class="right">&nbsp;</td>
					<td class="right"><b>'.number_format($varSub_Tot_Act,"2",".",",").'</b></td>
					<td class="right">&nbsp;</td>
					<td class="right"><b>'.number_format($varSub_Tot_His,"2",".",",").'</b></td>';
			$html.='</tr>';
			$html.='<tr class="head">';
			$html.='<td class="center"><b></b></td>';			
			$html.='<td class="left" colspan="5"><b>TOTAL VALORIZADO</b></td>';
			$html.='<td class="right"><b>'.number_format($varPro_Tot_Act,"2",".",",").'</b></td>
					<td class="right">&nbsp;</td>
					<td class="right"><b>'.number_format($varPro_Tot_His,"2",".",",").'</b></td>';
			$html.='</tr>';
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>		
				</body>
				</html>';
		return $html;
		}
	}
?>