<?php
//include "clase.conexion.php";
class clsPackingMercaderiaDevolucion extends clsConexion{
	public $varTitulo 	= "PACKING DE DEVOLUCION DE MERCADERIA";
	function FormuBusqueda() {
		$sqlven = "call spQuery_Lista_Vendedor('','','');";
		$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"=>"Fecha Venta :", "clase1"=>"FormCampo1", "clase2"=>"interior", "tamano"=>100,
											 "campo"=>array("tipo"=>"fecha", "id"=>"eini", "name"=>"eini", "alinear"=>"center",
											 				"valor"=>$varFINI) 
											 ),
									   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($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_Devolucion($varDia,$varVen,$varRut)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"700", "sql_cab"=>$sql,	"pages"=>100,
						"conte"=>array(array("titulo"=>"TIPO",    	"ancho"=>"20",  "alinear"=>"center"),
									   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"=>"PDV",  		"ancho"=>"60",  "alinear"=>"center"),
									   array("titulo"=>"CANTIDAD", 	"ancho"=>"60",  "alinear"=>"right", "clase"=>"sortable-numeric"),
									   array("titulo"=>"MONTO.S/.", "ancho"=>"60",  "alinear"=>"right", "clase"=>"sortable-numeric", "autosuma"=>"SI")
									   )
						);
		return $this->SetGridTabla($varTabla);
		}
	function Imprimir($varDia = NULL, 
					  $varVen = NULL, 
					  $varRut = NULL
					  )  {
						  
		$varFech = (isset($varDia) && $varDia<>"")?"'".FechaMySql($varDia)."'":"NULL";
		$varVend  = ($varVen<>"")?"\"(".$varVen.")\"":"NULL";
		$varRuta  = ($varRut<>"")?"\"(".str_replace("\\","",$varRut).")\"":"NULL";
		
		$sql = "call spQuery_Packing_Devolucion($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%;">';
			$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 Emision: '.$varDia.'</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="right">PDV.</td>';
			$html.='<td width="25" class="right">CANTIDAD</td>';
			$html.='<td width="25" class="right" nowrap="nowrap">MONTO S/.</td>';
			$html.='</tr>';
			$html.='</thead>';
			//$sql = "call spQuery_Packing_Devolucion($varFech,$varVen,$varRut)";
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$varTipo_Ant = "";
			$varTipo_Act = "";
			$varTotal1 = 0;
			$varTotal2 = 0;
			$varNumDoc = 0;
			while($fila = $this->FetchArray($rs))	{
				$varTipo_Act = $fila["TIPO"];
				$varTotal1 = $varTotal1 + $fila["CANTIDAD"];
				$varTotal2 = $varTotal2 + $fila["MONTO"];
				$varNumDoc++;
				if($varTipo_Act <> $varTipo_Ant) {
					$varTipo_Ant = $fila["TIPO"];
					$html.='<tr class="head">';
					$html.='<td class="left" colspan="7"><b>DEVOLUCION DE SALIDA POR '.$varTipo_Act.'</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="right">'.number_format($fila["PDV"],"0",".",",").'</td>
						<td class="right">'.number_format($fila["CANTIDAD"],"2",".",",").'</td>
						<td class="right">'.number_format($fila["MONTO"],"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;
		}
	}
?>