<?php
//include "clase.conexion.php";
class clsInventario extends clsConexion{
	public $varTitulo 	= "TOMA DE INVEVENTARIOS";
	public $varMyTabla  = "_sv_inventario_cab";
	public $varMyID  	= "id_inventario";
	function FormuBusqueda() {
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"430", "clase"=>"FormTitulo", "titulo"=>$this->varTitulo." - BUSQUEDA",
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST", "onsubmit"=>"return false;"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo),
									   array("tipo"=>"oculto", "id"=>"opt", "name"=>"opt", "valor"=>$Opcion)
									  ),
						"conte"=>array(array("titulo"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"100",
										 "campo" =>array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "valor"=>$_POST["cod"], 
										 "alinear"=>"center", "ancho"=>"70")),								   
								   array("titulo"=>"Descripci&oacute;n :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo" =>array("tipo"=>"texto", "id"=>"nam", "name"=>"nam", "valor"=>$_POST["nam"], "ancho"=>"306"))								   
								   ),
					  "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"=>"bnew", "name"=>"bnew", "ancho"=>"100", "texto"=>"Nuevo", 
										   "clase"=>"boton_nuevo", "click"=>"javascript:FormuRegistroJS('', 'NEW')"),
									 )
					);
		return $this->setTablaForm($varTabla);
		}
 	function GridDatos($varForm  = NULL, 
					   $varField = NULL, 
					   $varOrder = NULL
					   ) {
		$varCod = (isset($varForm["cod"]) && $varForm["cod"]<>"")?$varForm["cod"]:"NULL";
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		
		$sql = "call spQuery_Inventario($varCod, '$varForm[nam]', $varFie, $varOrd)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"800", "sql_cab"=>$sql,	"pages"=>20,
					    "conte"=>array(array("titulo"=>"CINV",     "ancho"=>"40" , "alinear"=>"center", "clase"=>"sortable-numeric", "tipo"=>"texto"), 
									   array("titulo"=>"DESCRIPCION",    "ancho"=>"300", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"FECHA",  "ancho"=>"100", "alinear"=>"center", "fijo"=>"SI"),
									   array("titulo"=>"FCREADO",  "ancho"=>"100", "alinear"=>"center", "fijo"=>"SI"),
									   array("titulo"=>"FUPDATE",  "ancho"=>"100", "alinear"=>"center", "fijo"=>"SI"),
									   array("titulo"=>"UCREADO",  "ancho"=>"50", "alinear"=>"center"),
									   array("titulo"=>"UUPDATE",  "ancho"=>"50", "alinear"=>"center")
									   ), 
						"boton"=>array(array("tipo"=>"img", "id"=>"bbus", "name"=>"bbus", "img"=>"images/b_table_edit.png", 
												 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>EDITAR</b> EL REGISTRO<br><b>"),
																	 array("valor"=>0, "tipo"=>"INDEX"),
																	 array("valor"=>" - "),
																	 array("valor"=>1, "tipo"=>"INDEX"),
																	 array("valor"=>"</b>")
																	),
												 "click"=>array("funcion"=>"FormuRegistroJS",
																"param"=>array(array("valor"=>0, 	  "tipo"=>"INDEX"),
																			   array("valor"=>"EDIT", "tipo"=>"TEXTO")
																			  )
																)
												 
												),
										   array("tipo"=>"img", "id"=>"badd", "name"=>"badd", "img"=>"images/b_table_delete.png", 
												 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>ELIMINAR</b> EL REGISTRO<br><b>"),
																	 array("valor"=>0, "tipo"=>"INDEX"),
																	 array("valor"=>" - "),
																	 array("valor"=>1, "tipo"=>"INDEX"),
																	 array("valor"=>"</b>")
																	),
												 "click"=>array("funcion"=>"xEliminarJS",
																"param"=>array(array("valor"=>0, "tipo"=>"INDEX"),
																			   array("valor"=>1, "tipo"=>"INDEX"),
																			   array("valor"=>"this", "tipo"=>"NUM"),
																			   )
																)
												 )
										)
							);
		return $this->SetGridTabla2($varTabla);
		}
	function FormuRegistro($varCod=NULL,$varOpt=NULL) {
		if($varOpt=="NEW") {
			$SubTit = $this->varTitulo." - NUEVO";
			$Permite = $_SESSION["_FINS"];
			$varLock = "NO";
			$Fila = NULL;
			$Fila["CDEP"] = "14";
			$Fila["CPRO"] = "1401";
			$Fila["CDIS"] = "140101";
			}
		else {
			$SubTit =  $this->varTitulo." - EDITAR";
			$varLock = "SI";
			
			$sql = "select * from ".$this->varMyTabla." where ".$this->varMyID." = $varCod";
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$Fila = $this->FetchArray($rs);
			}
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"450", "clase"=>"FormTitulo", "titulo"=>$SubTit,
						"formu"=>array("id"=>"form", "name"=>"form", "metodo"=>"POST"),						
						"ocult"=>array(array("tipo"=>"oculto", "id"=>"titu", "name"=>"titu", "valor"=>$this->varTitulo),
									   array("tipo"=>"oculto", "id"=>"opt", "name"=>"opt", "valor"=>$varOpt)
									  ),
						"conte"=>array(array("titulo"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>"100",
										 "campo" =>array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", "clase"=>"inputDisable",
										 "valor"=>$Fila[$this->varMyID], "alinear"=>"center", "locked"=>"SI", "ancho"=>"70")),								   
								   array("titulo"=>"Nombre :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
										 "campo" =>array("tipo"=>"texto", "id"=>"nam", "name"=>"nam", "valor"=>$Fila["nombre"], "ancho"=>"306")),
								   ),
						"boton"=>array(array("tipo"=>"button", "id"=>"bsav", "name"=>"bsav", "ancho"=>"100",
											 "clase"=>"boton_guardar", "texto"=>"Grabar", "click"=>"javascrip:xGrabarJS()"),			  
									   array("tipo"=>"button", "id"=>"breg", "name"=>"breg", "ancho"=>"100",
											 "clase"=>"boton_cancelar", "texto"=>"Cancelar", "click"=>"javascrip:xajax_xRegresar()"),
									)
						);
		$kul_with_progress = new KoolUploader("kul_with_progress");
		$kul_with_progress->scriptFolder = "clases/KoolUploader";
		$kul_with_progress->handlePage = "subirAsistencia.php";	
		$kul_with_progress->styleFolder="clases/KoolUploader/styles/default";
		$kul_with_progress->height="200px";
		$kul_with_progress->width="270px";
		$kul_with_progress->allowedExtension = "txt, TXT";
		$kul_with_progress->maxFileSize = 512*1024; //500KB
		$kul_with_progress->progressTracking = true;
		
		$kul_with_progress->texts["BUTTON_UPLOAD"]="Subir";
		$kul_with_progress->texts["BUTTON_REMOVE"]="Eliminar";
		$kul_with_progress->texts["MESSAGE_UPLOAD_FAILED"]	  = "Error al Subir Archivo";
		$kul_with_progress->texts["MESSAGE_FILE_NOT_ALLOWED"] = "Archivo no Soportado";
		$kul_with_progress->texts["MESSAGE_UPLOAD_SUCCESSFUL"]= "Archivo Subido Correctamente";
		
		//echo $kul_with_progress->Render();
		return $this->setTablaForm($varTabla).$kul_with_progress->Render();	
		}
	
	function Imprimir($varIde = NULL,
					  $varTit = NULL,
					  $varFec = NULL)  {	
		$sql = "call spQuery_Logisitica_Inventario_Toma($varIde)";
		$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>'.$varFec.' => '.$varTit.'</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">&nbsp;</td>';
			$html.='<td width="2" class="left">CPRO</td>';
			$html.='<td width="200" class="left">PRODUCTO</td>';
			$html.='<td width="50" NOWRAP>UM</td>';
			$html.='<td width="50" class="right" NOWRAP>SISTEMA</td>';
			$html.='<td width="50" class="right" NOWRAP>FISICO</td>';
			$html.='<td width="50" class="right" NOWRAP>DIFERE.</td>';
			$html.='<td width="50" class="right" NOWRAP>PRECIO</td>';
			$html.='<td width="50" class="right" NOWRAP>MONTO</td>';
			$html.='</tr>';
			$html.='</thead>';
			
			$varPro_Ant = $varPro_Act = "";
			$varSub_Ant = $varSub_Act = "";
			$varSub_Tot = 0;
			$varPro_Tot = 0;
			while($fila = $this->FetchArray($rs))	{
				$varPro_Act = $fila["CLIN"];
				$varSub_Act = $fila["CSUB"];
				$varPro_Tot = $varPro_Tot + $fila["MONTO"];			
				
				$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">&nbsp;</td>
								<td class="right">&nbsp;</td>
								<td class="right"><b>'.number_format($varSub_Tot,"2",".",",").'</b></td>';
						$html.='</tr>';	
						}
					$varSub_Tot=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">&nbsp;</td>
							<td class="right">&nbsp;</td>
							<td class="right"><b>'.number_format($varSub_Tot,"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 = $fila["MONTO"];
					}
				else {
					$varSub_Tot = $varSub_Tot + $fila["MONTO"];	
					}
				$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["QSISTEMA"],"1",".",",").'</td>
						<td class="right">'.number_format($fila["QFISICO"],"1",".",",").'</td>
						<td class="right">'.number_format($fila["QDIFERENCIA"],"1",".",",").'</td>
						<td class="right">'.number_format($fila["PRECIO"],"5",".",",").'</td>
						<td class="right">'.number_format($fila["MONTO"],"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,"2",".",",").'</b></td>';
			$html.='</tr>';	

			$html.='<tr class="head">';
			$html.='<td class="center"><b></b></td>';			
			$html.='<td class="left" colspan="7"><b>TOTAL VALORIZADO</b></td>';
			$html.='<td class="right"><b>'.number_format($varPro_Tot,"2",".",",").'</b></td>';
			$html.='</tr>';
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>		
				</body>
				</html>';
		return $html;
		}
	
	function ImprimirSobFal($varIde = NULL,
						    $varTit = NULL,
						    $varFec = NULL)  {	
		$sql = "call spQuery_Logisitica_Inventario_Toma_SobFal($varIde)";
		$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>'.$varFec.' => '.$varTit.'</b></td>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b>'.MostrarFechaActual().'</b></td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:left;"><b>FALTANTES / SOBRANTES DE MERCADERIA</b></td>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b></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">&nbsp;</td>';
			$html.='<td width="2" class="left">CPRO</td>';
			$html.='<td width="200" class="left">PRODUCTO</td>';
			$html.='<td width="50" NOWRAP>UM</td>';
			$html.='<td width="50" class="right" NOWRAP>SISTEMA</td>';
			$html.='<td width="50" class="right" NOWRAP>FISICO</td>';
			$html.='<td width="50" class="right" NOWRAP>DIFERE.</td>';
			$html.='<td width="50" class="right" NOWRAP>PRECIO</td>';
			$html.='<td width="50" class="right" NOWRAP>MONTO</td>';
			$html.='</tr>';
			$html.='</thead>';
			
			$varPro_Ant = $varPro_Act = "";
			$varSub_Ant = $varSub_Act = "";
			$varSub_Tot = 0;
			$varPro_Tot = 0;
			while($fila = $this->FetchArray($rs))	{
				$varPro_Act = $fila["CTIP"];
				$varSub_Act = $fila["CLIN"];
				$varPro_Tot = $varPro_Tot + $fila["MONTO"];			
				
				$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">&nbsp;</td>
								<td class="right">&nbsp;</td>
								<td class="right"><b>'.number_format($varSub_Tot,"2",".",",").'</b></td>';
						$html.='</tr>';	
						}
					$varSub_Tot=0;
					$varPro_Ant = $fila["CTIP"];
					$varSub_Ant = $fila["CLIN"];
					
					$html.='<tr class="head">';
					$html.='<td class="center"><b>'.$fila["CTIP"].'</b></td>';
					$html.='<td class="left" colspan="8"><b>'.$fila["TIPO"].'</b></td>';
					$html.='</tr>';
					$html.='<tr class="head">';
					$html.='<td class="center"><b></b></td>';
					$html.='<td class="center"><b>'.$fila["CLIN"].'</b></td>';
					$html.='<td class="left" colspan="7"><b>'.$fila["LINEA"].'</b></td>';
					$html.='</tr>';
					}
				if($varSub_Act <> $varSub_Ant) {
					$varSub_Ant = $fila["CLIN"];
					$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">&nbsp;</td>
							<td class="right">&nbsp;</td>
							<td class="right"><b>'.number_format($varSub_Tot,"2",".",",").'</b></td>';
					$html.='</tr>';		
					$html.='<tr class="head">';
					$html.='<td class="center"><b></b></td>';
					$html.='<td class="center"><b>'.$fila["CLIN"].'</b></td>';
					$html.='<td class="left" colspan="7"><b>'.$fila["LINEA"].'</b></td>';
					$html.='</tr>';
					$varSub_Tot = $fila["MONTO"];
					}
				else {
					$varSub_Tot = $varSub_Tot + $fila["MONTO"];	
					}
				$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["QSISTEMA"],"1",".",",").'</td>
						<td class="right">'.number_format($fila["QFISICO"],"1",".",",").'</td>
						<td class="right">'.number_format($fila["QDIFERENCIA"],"1",".",",").'</td>
						<td class="right">'.number_format($fila["PRECIO"],"5",".",",").'</td>
						<td class="right">'.number_format($fila["MONTO"],"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">&nbsp;</td>
					<td class="right">&nbsp;</td>
					<td class="right"><b>'.number_format($varSub_Tot,"2",".",",").'</b></td>';
			$html.='</tr>';	

			$html.='<tr class="head">';
			$html.='<td class="center"><b></b></td>';			
			$html.='<td class="left" colspan="7"><b>TOTAL FALTANTE / SOBRANTE</b></td>';
			$html.='<td class="right"><b>'.number_format($varPro_Tot,"2",".",",").'</b></td>';
			$html.='</tr>';
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>		
				</body>
				</html>';
		return $html;
		}
				
	function ProcesoGrabar($varForm = NULL) {		
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$varCod = ($varForm["cod"]=="") ? "NULL" : $varForm["cod"];		
		$sql = "call spMante_Producto_Marca('$varForm[opt]', $varCod, '$varForm[nam]',$varUsr)";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		$Fila = $this->FetchArray($rs);
		return $Fila[0];
		}
		
	function ProcesoEliminar($varCod) {
		$varUsr = (isset($_SESSION["_CUSE"]))?$_SESSION["_CUSE"]:1;
		$sql = "update ".$this->varMyTabla." set _estado = 'X', _date_update=NOW(), _user_update=$varUsr where ".$this->varMyID."= '".$varCod."';";
		$this->AbrirConexion();
		$this->Query($sql);
		$this->CerrarConexion();
		}
	}
	
?>