<?php
//include "clase.conexion.php";
class clsPackingMercaderia extends clsConexion{
	public $varTitulo 	= "PACKING DE MERCADERIA DE CAMBIOS";
	function FormuBusqueda() {
		$sqlven = "call spQuery_Lista_Trabajador('V',NULL,NULL, NULL);";
		$sqlrut = "call spQuery_Lista_Ruta('','%');";
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"660", "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"=>"Empresa :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"ecod", "name"=>"ecod", "valor"=>$_POST["ecod"], 
																					 "ancho"=>"60", "alinear"=>"center", 
																					 "blur"=>"xajax_seleccionar_empresa(this.value)", 
																					 "dclick"=>"fn_cargar_listado_empresa('ecod', 'enom')"),
																			   array("tipo"=>"text", "id"=>"enom", "name"=>"enom", "valor"=>$_POST["enom"], 
																					 "ancho"=>"400", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
															)
											 ),
									   array("titulo"=>"Fecha Inicial :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"fini", "name"=>"fini", "alinear"=>"center",
																					 "ancho"=>"130", "valor"=>date("01/m/Y"))
																			   )
														 	)
											 ),
									   array("titulo"=>"Fecha Final :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"ffin", "name"=>"ffin", "alinear"=>"center", 
																					 "ancho"=>"130", "valor"=>date("d/m/Y"))
																			   )
														 	)
											 ),
									   array("titulo"=>"Vendedor :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo"=>array("tipo"=>"lista_doble", "filas"=>8, "ancho"=>"250", "sql"=>$sqlven,
											 				"id1"=>"ven", "name1"=>"ven", "id2"=>"vcod", "name2"=>"vcod",
															"dclick1"=>"mover_lista('right','ven','vcod')", "dclick2"=>"mover_lista('left','ven','vcod')"															
															)
											),									   
									   array("titulo"=>"Ruta :", "clase1"=>"FormCampo1", "clase2"=>"interior", 
											 "campo"=>array("tipo"=>"lista_doble", "filas"=>8, "ancho"=>"250", "sql"=>$sqlrut,
											 				"id1"=>"rut", "name1"=>"rut", "id2"=>"rcod", "name2"=>"rcod",
															"dclick1"=>"mover_lista('right','rut','rcod')", "dclick2"=>"mover_lista('left','rut','rcod')"															
															)
											),
									   ),
						"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('', 'NEW')"),
									 )
						);
		return $this->setTablaForm($varTabla);
		}
 	function GridDatos($varEmp = NULL,
					   $varIni = NULL,
					   $varFin = NULL,
					   $varVen = NULL, 
					   $varRut = NULL
					   )  {
		$varFIni = (isset($varIni) && $varIni<>"")?"'".FechaMySql($varIni)."'":"NULL";
		$varFFin = (isset($varFin) && $varFin<>"")?"'".FechaMySql($varFin)."'":"NULL";
		$varVen  = ($varVen<>"")?"\"(".$varVen.")\"":"NULL";
		$varRut  = ($varRut<>"")?"\"(".str_replace("\\","",$varRut).")\"":"NULL";
		
		$sql = "call spQuery_Packing_Mercaderia_Cambios($varEmp, $varFIni, $varFFin, $varVen, $varRut)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"1000", "sql_cab"=>$sql,	"pages"=>100,
						"conte"=>array(array("titulo"=>"CVEN",      "ancho"=>"40" , "alinear"=>"center"), 
									   array("titulo"=>"VENDEDOR",  "ancho"=>"200", "fijo"=>"SI"),
									   array("titulo"=>"CCLI",      "ancho"=>"40" , "alinear"=>"center"), 
									   array("titulo"=>"CLIENTE",  	"ancho"=>"200", "fijo"=>"SI"),
									   array("titulo"=>"DIRECCION", "ancho"=>"200", "oculto"=>"SI"),
									   array("titulo"=>"FEMI",      "ancho"=>"40" , "alinear"=>"center", "fijo"=>"SI"), 
									   array("titulo"=>"CTIP",      "ancho"=>"40" , "oculto"=>"SI"), 
									   array("titulo"=>"NDOC",  	"ancho"=>"40",  "alinear"=>"center", "fijo"=>"SI"),
									   array("titulo"=>"CPRO",      "ancho"=>"40" , "alinear"=>"center", "clase"=>"sortable-numeric"), 
									   array("titulo"=>"PRODUCTO",  "ancho"=>"200", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"UM",    	"ancho"=>"60",  "alinear"=>"center"),
									   array("titulo"=>"CANTIDAD", 	"ancho"=>"60",  "alinear"=>"right", "clase"=>"sortable-numeric"),
									   array("titulo"=>"FVEN",      "ancho"=>"40" , "alinear"=>"center", "fijo"=>"SI"), 
									   array("titulo"=>"LOTE",      "ancho"=>"40" , "alinear"=>"center", "fijo"=>"SI"), 
									   )
						);
		return $sql.$this->SetGridTabla($varTabla);
		}
	function Imprimir($varEmp = NULL,
					  $varIni = NULL,
					  $varFin = NULL, 
					  $varVen = NULL, 
					  $varRut = NULL
					  )  {
						  
		$varFIni = (isset($varIni) && $varIni<>"")?"'".FechaMySql($varIni)."'":"NULL";		
		$varVend  = ($varVen<>"")?"\"(".$varVen.")\"":"NULL";
		$varRuta  = ($varRut<>"")?"\"(".str_replace("\\","",$varRut).")\"":"NULL";
		
		$sql = "call spQuery_Packing_Mercaderia_Cambios($varFIni, $varVend, $varRuta)";
		
		$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="width: 50%; vertical-align: middle; text-align:left;"><b>'.Config("SYSEMP").'</b></td>';
			$html.='<td style="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>Fecha Pedido: '.$varIni.' - '.$varFin.'</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.='<tr>';
		$html.='<td>';
			$html.='<table style="width: 100%;">';
			$html.='<tr>';
			$html.='<td style="width: 10%; vertical-align: middle; text-align:left;" nowrap="nowrap"><b>Vendedor(es)</b></td>';
			$html.='<td style="width: 90%; vertical-align: middle; text-align:left;"><b> : </b>';
			if($varVen=="") {
				$html.= "Todos - TODOS LOS VENDEDORES";
				}
			else {
				$varVend  = ($varVen<>"")?"(".$varVen.")":"NULL";
				$sql1 = "select id_vendedor, nombre from _sv_vendedor where id_vendedor in ".$varVend;
				$this->AbrirConexion();
				$rs_1 = $this->Query($sql1);
				$this->CerrarConexion();
				$varTemp = "";
				while($fila_1 = $this->FetchArray($rs_1)) {
					if($varTemp=="")
						$varTemp = $fila_1[0]." - ".$fila_1[1];
					else	
						$varTemp.= " / ".$fila_1[0]." - ".$fila_1[1];
					}
				}
			$html.=$varTemp;
			$html.='</td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>Ruta(as)</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b> : </b>';
			if($varRut=="") {
				$html.= "Todos - TODOS LAS RUTAS";
				}
			else {
				$varRuta  = ($_GET["rcod"]<>"")?"(".str_replace("\\","",$_GET["rcod"]).")":"NULL";
				$sql1 = "select id_ruta, nombre from _sv_ruta where id_ruta in ".$varRuta;
				$this->AbrirConexion();
				$rs_1 = $this->Query($sql1);
				$this->CerrarConexion();
				$varTemp = "";
				while($fila_1 = $this->FetchArray($rs_1)) {
					if($varTemp=="")
						$varTemp = $fila_1[0]." - ".$fila_1[1];
					else	
						$varTemp.= " / ".$fila_1[0]." - ".$fila_1[1];
					}
				}
			$html.=$varTemp;
			$html.='</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="center">CPRO</td>';
			$html.='<td width="150">PRODUCTO</td>';
			$html.='<td width="50">U.M.</td>';
			$html.='<td width="25" class="center">LOTE</td>';
			$html.='<td width="25" class="center">F.VENC.</td>';
			$html.='<td width="25" class="right" nowrap="nowrap">CANTIDAD</td>';
			$html.='</tr>';
			$html.='</thead>';
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$varCli_Ant = "";
			$varCli_Act = "";
			$varVen_Ant = "";
			$varVen_Act = "";
			$varTotal1 = 0;
			$varTotal2 = 0;
			$varNumDoc = 0;
			while($fila = $this->FetchArray($rs))	{
				$varCli_Act = $fila["CCLI"];
				$varVen_Act = $fila["CVEN"];
				$varTotal1 = $varTotal1 + $fila["CANTIDAD"];
				$varNumDoc++;
				if($varVen_Act <> $varVen_Ant) {
					$varVen_Ant = $fila["CVEN"];
					$html.='<tr class="head">';
					$html.='<td class="right"><b>'.$fila["CVEN"].'</b></td>';
					$html.='<td class="left" colspan="6"><b>'.$fila["VENDEDOR"].'</b></td>';
					$html.='</tr>';
					}
				if($varCli_Act <> $varCli_Ant) {
					$varCli_Ant = $fila["CCLI"];
					$html.='<tr class="head">';
					$html.='<td class="right"></td>';
					$html.='<td class="left" colspan="4"><b>'.str_pad($fila["CCLI"],5,'0',STR_PAD_LEFT).' - '.$fila["CLIENTE"].' / '.$fila["DIRECCION"].'</b></td>';
					$html.='<td class="right" colspan="2"><b>'.$fila["CTIP"].'/'.$fila["NDOC"].'</b></td>';
					$html.='</tr>';
					}
				$html.='<tr class="list_row">
						<td class="center">&nbsp;</td>
						<td class="center">'.$fila["CPRO"].'</td>
						<td NOWRAP>'.$fila["PRODUCTO"].'</td>
						<td NOWRAP>'.$fila["UM"].'</td>
						<td class="center">'.$fila["LOTE"].'</td>
						<td class="center">'.$fila["FVEN"].'</td>
						<td class="right">'.number_format($fila["CANTIDAD"],"2",".",",").'</td>
						</tr>';
				}
			$html.='<tr class="head">';
			$html.='<td colspan="2" class="left"><strong>Totales :</strong></td>';
			$html.='<td class="left">'.number_format($varNumDoc,"0",".",",").' Productos</td>';
			$html.='<td class="right"></td>';
			$html.='<td class="right"></td>';
			$html.='<td class="right">'.number_format($varTotal1,"2",".",",").'</td>';
			$html.='<td class="right">'.number_format($varTotal2,"2",".",",").'</td>';
			$html.='</tr>';
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>		
				</body>
				</html>';
		return $html;
		}
	}
?>