<?php
//include "clase.conexion.php";
class clsPackingDocumento extends clsConexion{
	public $varTitulo 	= "PACKING DE DOCUMENTOS";
	function FormuBusqueda() {
		$sqlven = "call spQuery_Lista_Trabajador('V',NULL,NULL, NULL);";
		$sqlrut = "call spQuery_Lista_Ruta('','%');";
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"650", "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 Emi. :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"femi", "name"=>"femi", "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, 
					   $varDia = NULL, 
					   $varVen = NULL, 
					   $varRut = NULL
					   )  {
						  
		$varDia = (isset($varDia) && $varDia<>"")?"'".FechaMySql($varDia)."'":"NULL";
		$varVen  = ($varVen<>"")?"\"(".$varVen.")\"":"NULL";
		$varRut  = ($varRut<>"")?"\"(".str_replace("\\","",$varRut).")\"":"NULL";
		
		$sql = "call spQuery_Packing_Documentos($varEmp, $varDia, $varVen, $varRut)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"1100", "sql_cab"=>$sql,	"pages"=>100,
						"conte"=>array(array("titulo"=>"CVEN",    	"ancho"=>"20",  "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"VENDEDOR", 	"ancho"=>"200", "clase"  =>"sortable", "fijo"=>"SI"),
									   array("titulo"=>"CCLI",      "ancho"=>"40" , "alinear"=>"center", "clase"=>"sortable-numeric"), 
									   array("titulo"=>"CLIENTE",  	"ancho"=>"200", "funcion"=>array("contar"), "fijo"=>"SI"),
									   array("titulo"=>"DIRECCION",	"ancho"=>"250", "fijo"=>"SI"), 
									   array("titulo"=>"CRUT",    	"ancho"=>"20",  "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"RUTA",  	"ancho"=>"100", "clase"  =>"sortable", "fijo"=>"SI"),
									   array("titulo"=>"CTIP",    	"ancho"=>"20",  "alinear"=>"center"),
									   array("titulo"=>"NDOC",    	"ancho"=>"20",  "alinear"=>"center", "fijo"=>"SI"),
									   array("titulo"=>"NPED",    	"ancho"=>"20",  "alinear"=>"center"),
									   array("titulo"=>"CCON",    	"ancho"=>"20",  "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"CONDICION",	"ancho"=>"80", "clase"  =>"sortable"),
									   array("titulo"=>"LP",	"ancho"=>"20", "alinear"  =>"center"),
									   array("titulo"=>"MONTO.S/.", "ancho"=>"60",  "alinear"=>"right", "clase"=>"sortable-numeric", "autosuma"=>"SI")
									   )
						);
		return $this->SetGridTabla($varTabla);
		}
	function Imprimir ($varEmp = NULL, 
					   $varDia = NULL, 
					   $varVen = NULL, 
					   $varRut = NULL
					   )  {
		$sql_emp = "select concat(id_empresa, ' - ', nombre) as empresa from _sv_empresa where id_empresa=$varEmp;";
		$this->AbrirConexion();
		$rs_emp = $this->Query($sql_emp);
		$this->CerrarConexion();
		$fila_emp = $this->FetchArray($rs_emp);
		$varCadEmp = $fila_emp[0];
		
		// Listamos Vendedores seleccionados
		if ($varVen == "") {
			$varCadVen = "Todos - TODOS LOS VENDEDORES";
			}
		else {
			$varVend = "(".$varVen.")";
			$sql_ven = "select id_trabajador, concat(apellido_paterno, ' ', apellido_materno, ' ', nombre) as nombre from _sv_trabajador where id_trabajador in ".$varVend;
			$this->AbrirConexion();
			$rs_ven = $this->Query($sql_ven);
			$this->CerrarConexion();
			while($fila_ven = $this->FetchArray($rs_ven)) {
				if ($varCadVen=="")	$varCadVen = $fila_ven[0]." - ".$fila_ven[1];
				else $varCadVen.= " / ".$fila_ven[0]." - ".$fila_ven[1];			
				}
			}
		
		// Listamos Rutas seleccionados
		if ($varRut == "") {
			$varCadRut = "Todos - TODAS LAS RUTAS";
			}
		else {
			$varRuta = "(".str_replace("\\","",$_GET["rcod"]).")";
			$sql_rut = "select id_ruta, nombre from _sv_ruta where id_ruta in ".$varRuta;
			$this->AbrirConexion();
			$rs_rut = $this->Query($sql_rut);
			$this->CerrarConexion();
			$varCadRut = "";
			while($fila_rut = $this->FetchArray($rs_rut)) {
				if ($varCadRut=="")	$varCadRut = $fila_rut[0]." - ".$fila_rut[1];
				else $varCadRut.= " / ".$fila_rut[0]." - ".$fila_rut[1];			
				}
			}				  
		$varFech = (isset($varDia) && $varDia<>"")?"'".FechaMySql($varDia)."'":"NULL";
		$varVend  = ($varVen<>"")?"\"(".$varVen.")\"":"NULL";
		$varRuta  = ($varRut<>"")?"\"(".str_replace("\\","",$varRut).")\"":"NULL";
		
		$sql = "call spQuery_Packing_Documentos($varEmp, $varFech,$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%;" cellspacing="0" cellpadding="0">';
			$html.='<tr class="titu_sup" height="25px">';
			$html.='<td colspan="2"><b>'.$this->varTitulo.'</b></td>';
			$html.='</tr>';
			$html.='<tr class="titu_inf">';
			$html.='<td colspan="2">';
			$html.='<div align="left" style=" width:50%;float:left;">'.$_SESSION["_CABR"].' : '.$_SESSION["_NSYS"].'</div>';
    		$html.='<div align="right" style="width:50%;float:left">'.MostrarFechaActual().'</div>';
			$html.='</td>';
			$html.='</tr>';
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';		
		$html.='<tr>';
		$html.='<td>';
			$html.='<table style="width: 100%;" cellspacing="0" cellpadding="0">';
			$html.='<tr>';
			$html.='<td style="width:80px; vertical-align: middle; text-align:left;"><b>Empresa </b></td>';
			$html.='<td style="width:10px; vertical-align: middle; text-align:left;"><b>:</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;">'.$varCadEmp.'</td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>F. Emision</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>:</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;" nowrap>'.$varDia.'</td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>Vendedor(es) </b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>:</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;" nowrap>'.$varCadVen.'</td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>Ruta(s) </b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>:</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;" nowrap>'.$varCadRut.'</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="titu_tabla">';
			$html.='<td width="2" class="center">&nbsp;</td>';
			$html.='<td width="2" class="center">CCLI</td>';
			$html.='<td width="150">CLIENTE</td>';
			$html.='<td width="150">DIRECCION</td>';
			$html.='<td width="50">DOC.VENTA</td>';
			$html.='<td width="30">CONDICION</td>';
			$html.='<td width="5">LP</td>';
			$html.='<td width="25" class="right" nowrap>MONTO S/.</td>';
			$html.='<td width="25" class="right">CONTADO</td>';
			$html.='<td width="25" class="right">CREDITO</td>';
			$html.='<td width="25" class="right">PARCIAL</td>';
			$html.='<td width="25" class="right">ANULAD.</td>';
			$html.='</tr>';
			$html.='</thead>';
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$varTotal1 = 0;
			$varTotal2 = 0;
			$varNumDoc = 0;
			while($fila = $this->FetchArray($rs))	{
				$varTotal1 = $varTotal1 + $fila["MONTO"];
				$varVenAct = $fila["CVEN"];
				$varRutAct = $fila["CRUT"];
				$varNumDoc++;
				if($varVenAct <> $varVenAnt) {
					$varVenAnt = $fila["CVEN"];
					$varRutAnt = $fila["CRUT"];
					$html.='<tr class="cont_tabla">';
					$html.='<td class="right"><b>'.$fila["CVEN"].'</b></td>';
					$html.='<td colspan="10"><b>'.$fila["VENDEDOR"].'</b></td>';
					$html.='</tr>';
					$html.='<tr class="cont_tabla">';
					$html.='<td></td>';
					$html.='<td class="center"><b>'.$fila["CRUT"].'</b></td>';
					$html.='<td colspan="9"><b>'.$fila["RUTA"].'</b></td>';
					$html.='</tr>';
					}
				if($varRutAct <> $varRutAnt) {
					$varRutAnt = $fila["CRUT"];
					$html.='<tr class="cont_tabla">';
					$html.='<td></td>';
					$html.='<td class="center"><b>'.$fila["CRUT"].'</b></td>';
					$html.='<td colspan="9"><b>'.$fila["RUTA"].'</b></td>';
					$html.='</tr>';
					}
				$html.='<tr class="cont_tabla">
						<td class="center">&nbsp;</td>
						<td class="center">'.$fila["CCLI"].'</td>
						<td NOWRAP>'.$fila["CLIENTE"].'</td>
						<td NOWRAP>'.$fila["DIRECCION"].'</td>
						<td NOWRAP>'.$fila["CTIP"].' '.$fila["NDOC"].'</td>
						<td>'.$fila["CONDICION"].'</td>
						<td>'.$fila["LP"].'</td>
						<td class="right">'.number_format($fila["MONTO"],"2",".",",").'</td>
						<td class="right"></td>
						<td class="right"></td>
						<td class="right"></td>
						<td class="right"></td>
						</tr>';
				}
			$html.='<tr class="titu_tabla">';
			$html.='<td colspan="4" class="left"><strong>Totales :</strong></td>';
			$html.='<td class="left">'.number_format($varNumDoc,"0",".",",").' Documentos</td>';
			$html.='<td class="right"></td>';
			$html.='<td class="right"></td>';
			$html.='<td class="right">'.number_format($varTotal1,"2",".",",").'</td>';
			$html.='<td class="right"></td>';
			$html.='<td class="right"></td>';
			$html.='<td class="right"></td>';
			$html.='<td class="right"></td>';
			$html.='</tr>';
	
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>';
		return $html;
		}
	}
?>