<?php
//include "clase.conexion.php";
class clsAsignacionReporteDocumentos extends clsConexion{
	public $varTitulo = "PACKING DE DOCUMENTOS LIQUIDADOS";
	public $varMyTabla  = "_sv_asigncion";
	public $varMyID  	  = "id_asignacion";
	function FormuBusqueda() {
		$varTabla=array("id"=>"tabla", "name"=>"table", "tamano"=>"430", "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),
									   array("tipo"=>"oculto", "id"=>"opt", "name"=>"opt", "valor"=>$Opcion)
									  ),
						"conte"=>array(array("titulo"=>"Codigo :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"cod", "name"=>"cod", 
																					 "alinear"=>"center", "ancho"=>"80"),
																			   array("tipo"=>"simple", "valor"=>"Nro. Asignacion"))
														 	)
											 ),									   
									   array("titulo"=>"Vehiculo :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"text", "id"=>"vcod1", "name"=>"vcod1", "valor"=>NULL, 
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "blur"=>"xajax_seleccionar_vehiculo(this.value,'vcod1','vnom1')", 
																					 "dclick"=>"fn_cargar_listado_vehiculo('vcod1', 'vnom1')"),
																			   array("tipo"=>"text", "id"=>"vnom1", "name"=>"vnom1", "valor"=>NULL, 
																					 "ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			   )
															)
											 ),	
									   array("titulo"=>"Conductor :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											"campo"=>array("tipo"=>"varios",
														   "contenido"=>array(array("tipo"=>"text", "id"=>"ccod1", "name"=>"ccod1", "valor"=>NULL, 
																					"ancho"=>"50",  "alinear"=>"center", 
																					"blur"=>"xajax_seleccionar_conductor(this.value, 'ccod1', 'cnom1')", 
																					"dclick"=>"fn_cargar_listado_conductor('ccod1', 'cnom1')"),
																			  array("tipo"=>"text", "id"=>"cnom1", "name"=>"cnom1", 
																					"ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			  )
														   )
										   ),
									 array("titulo"=>"F.Inicial :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"fini", "name"=>"fini", "alinear"=>"center"))
														 	)
											 ),
									   array("titulo"=>"F.Final :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"texto", "id"=>"ffin", "name"=>"ffin", "alinear"=>"center"))
														 	)
											 ),					   
								   ),
					  "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()")
									 )
					);
		return $this->setTablaForm($varTabla);
		}
	function GridDatos($varForm  = NULL, 
					   $varField = NULL, 
					   $varOrder = NULL
					   )  {
						  
		$varFie = (isset($varField) && $varField<>'')? "'$varField'":"NULL";
		$varOrd = (isset($varOrder) && $varOrder<>'')? "'$varOrder'":"NULL";
		$varCod = (isset($varForm["cod"]) && $varForm["cod"]<>'')?$varForm["cod"]:"NULL";
		$varVeh = (isset($varForm["vcod1"]) && $varForm["vcod1"]<>'')?$varForm["vcod1"]:"NULL";
		$varCon = (isset($varForm["ccod1"]) && $varForm["ccod1"]<>'')?$varForm["ccod1"]:"NULL";
		$varIni = (isset($varForm["fini"]) && $varForm["fini"]<>'')?"'".FechaMySql($varForm["fini"])."'":"NULL";
		$varFin = (isset($varForm["ffin"]) && $varForm["ffin"]<>'')?"'".FechaMySql($varForm["ffin"])."'":"NULL";
		
		$sql = "call spQuery_Asignacion($varCod, $varVeh, $varCon, $varIni, $varFin, $varFie, $varOrd);";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"1000", "sql_cab"=>$sql,	"pages"=>20,
					    "conte"=>array(array("titulo"=>"IDE",    "ancho"=>"30" , "alinear"=>"center"), 
									   array("titulo"=>"FECHA", "ancho"=>"80", "fijo"  =>"SI"),
									   array("titulo"=>"CVEH",  "ancho"=>"30", "alinear"=>"center"),
									   array("titulo"=>"PLACA", "ancho"=>"50"),
									   array("titulo"=>"VEHICULO", "ancho"=>"200", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"CCON", "ancho"=>"30", "alinear"=>"center"),
									   array("titulo"=>"CONDUCTOR","ancho"=>"200", "fijo"=>"SI"),
									   array("titulo"=>"DOC'S", "ancho"=>"50", "alinear"=>"right", "decimal"=>"2"),
									   array("titulo"=>"MONTO", "ancho"=>"50", "alinear"=>"right", "decimal"=>"2", "funcion"=>array("autosuma")),
									   array("titulo"=>"FCREADO", "ancho"=>"100", "fijo"=>"SI"),
									   array("titulo"=>"FUPDATE", "ancho"=>"100", "oculto"=>"SI"),
									   array("titulo"=>"UCREADO", "ancho"=>"100", "alinear"=>"center", "fijo"=>"SI"),
									   array("titulo"=>"UUPDATE", "ancho"=>"100", "alinear"=>"center", "oculto"=>"SI")
									   ), 
						"boton"=>array(array("tipo"=>"img", "img"=>"images/16x16/printer.png", 
											 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>IMPRIMIR EL PACKING DE DOCUMENTOS LIQUIDADOS</b>"),
											 				 array("valor"=>"<br><b>Nro. Asignacion :</b> "),
															 array("valor"=>0, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Fecha : </b>"),
															 array("valor"=>1, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Monto : </b>"),
															 array("valor"=>8, "tipo"=>"INDEX"),
															 ),
											 "click"=>array("funcion"=>"xImprimirJS",
															"param"=>array(array("valor"=>0, "tipo"=>"INDEX"))
															)
											),
									   array("tipo"=>"img", "img"=>"images/16x16/export_excel.png", 
											 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>EXPORTAR EL PACKING DE DOCUMENTOS LIQUIDADOS</b>"),
											 				 array("valor"=>"<br><b>Nro. Asignacion :</b> "),
															 array("valor"=>0, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Fecha : </b>"),
															 array("valor"=>1, "tipo"=>"INDEX"),
															 array("valor"=>"<br><b>Monto : </b>"),
															 array("valor"=>8, "tipo"=>"INDEX"),
															 ),
											 "click"=>array("funcion"=>"xajax_xExportarPacking",
															"param"=>array(array("valor"=>0, "tipo"=>"INDEX"))
															)
											)
										)
							);
		return $this->SetGridTabla2($varTabla);
		}
	function Imprimir($varAsignacion = NULL)  {
		$sqlcab = "call spQuery_Asignacion($varAsignacion, NULL, NULL, NULL, NULL, NULL, NULL);";
		$this->AbrirConexion();
		$rscab = $this->Query($sqlcab);
		$this->CerrarConexion();
		$filacab = $this->FetchArray($rscab);
		
		
		$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.='</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>Asignaci&oacute;n</b></td>';
			$html.='<td style="width: 1%; vertical-align: middle; text-align:left;"><b>:</b></td>';
			$html.='<td style="width: 39%; vertical-align: middle; text-align:left;">';
			$html.='<b>N&deg; '.str_pad($filacab["ASIG"], 5, "0", STR_PAD_LEFT).'&nbsp;&nbsp;&nbsp; '.$filacab["FECHA"].'</b></td>';
			$html.='<td style="width: 50%; vertical-align: middle; text-align:right;">'.MostrarFechaActual().'</td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="vertical-align: middle; text-align:left;" nowrap="nowrap"><b>Vehiculo</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>:</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;">'.$filacab["PLACA"]." - ".$filacab["VEHICULO"].'</td>';
			$html.='<td style="vertical-align: middle; text-align:right;"></td>';
			$html.='</tr>';
			$html.='<tr>';
			$html.='<td style="vertical-align: middle; text-align:left;" nowrap="nowrap"><b>Conductor</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;"><b>:</b></td>';
			$html.='<td style="vertical-align: middle; text-align:left;">'.$filacab["CCON"]." - ".$filacab["CONDUCTOR"].'</td>';
			$html.='<td style="vertical-align: middle; text-align:right;"></td>';
			$html.='</tr>';
			
			$html.='<tr>';
			$html.='<td style="vertical-align:top; text-align:left;" nowrap="nowrap"><b>Reparto</b></td>';
			$html.='<td style="vertical-align:top; text-align:left;"><b>:</b></td>';
			$html.='<td colspan="2" style="vertical-align: middle; text-align:left;">';
			$sqlrep = "CALL spQuery_Asignacion_Repartidor($varAsignacion);";
			$this->AbrirConexion();
			$rsrep = $this->Query($sqlrep);
			$this->CerrarConexion();
			$j = 0;
			while ($filarep = $this->FetchArray($rsrep)) {
				$j++;
				if ($j==1) $html.= $filarep["CODIGO"]." - ".$filarep["REPARTIDOR"];
				else $html.= " / ".$filarep["CODIGO"]." - ".$filarep["REPARTIDOR"];
				}
			$html.='</td>';
			$html.='</tr>';
			
			$html.='<tr>';
			$html.='<td style="vertical-align:top; text-align:left;" nowrap="nowrap"><b>Vendedores</b></td>';
			$html.='<td style="vertical-align:top; text-align:left;"><b>:</b></td>';
			$html.='<td colspan="2" style="vertical-align: middle; text-align:left;">';
			$sqlven = "select V.id_vendedor as CVEN, T.nombre as VENDEDOR
					   from _sv_venta V 
					   inner join _sv_vendedor T ON V.id_vendedor = T.id_vendedor
					   where id_asignacion = $varAsignacion
					   group by V.id_vendedor, T.nombre
					   order by T.nombre;";
			$this->AbrirConexion();
			$rsven = $this->Query($sqlven);
			$this->CerrarConexion();
			$j = 0;
			while ($filaven = $this->FetchArray($rsven)) {
				$j++;
				if ($j==1) $html.= $filaven["CVEN"]." - ".$filaven["VENDEDOR"];
				else $html.= " / ".$filaven["CVEN"]." - ".$filaven["VENDEDOR"];
				}
			$html.='</td>';
			$html.='</tr>';
			
			$html.='<tr>';
			$html.='<td style="vertical-align:top; text-align:left;" nowrap="nowrap"><b>Rutas</b></td>';
			$html.='<td style="vertical-align:top; text-align:left;"><b>:</b></td>';
			$html.='<td colspan="2" style="vertical-align: middle; text-align:left;">';
			$sqlrut = "select V.id_ruta as CRUT, R.nombre as RUTA
					   from _sv_venta V 
					   inner join _sv_ruta R ON V.id_ruta = R.id_ruta
					   where id_asignacion = $varAsignacion
					   group by V.id_ruta, R.nombre
					   order by R.nombre;";
			$this->AbrirConexion();
			$rsrut = $this->Query($sqlrut);
			$this->CerrarConexion();
			$j = 0;
			while ($filarut = $this->FetchArray($rsrut)) {
				$j++;
				if ($j==1) $html.= $filarut["CRUT"]." - ".$filarut["RUTA"];
				else $html.= " / ".$filarut["CRUT"]." - ".$filarut["RUTA"];
				}
			$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">CCLI</td>';
			$html.='<td width="100">CLIENTE</td>';
			$html.='<td width="100">RUTA</td>';
			$html.='<td width="100">DIRECCION</td>';
			$html.='<td width="50">DOC.VENTA</td>';
			$html.='<td width="30">CONDICION</td>';
			$html.='<td width="25" class="right">TOTAL</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.='<td width="150">MOTIVO RECHA.</td>';
			$html.='</tr>';
			$html.='</thead>';
			$sql = "CALL spQuery_LiqReparto_Documentos_Liquidados($varAsignacion, 'V.id_vendedor, V.id_tipo_doc, V.nro_documento', NULL);";
			$this->AbrirConexion();
			$rs = $this->Query($sql);
			$this->CerrarConexion();
			$varTotal = 0;
			$varConta = 0;
			$varCredi = 0;
			$varParci = 0;
			$varRecha = 0;
			$varNumDoc = 0;
			while($fila = $this->FetchArray($rs))	{
				$varTotal = $varTotal + $fila["TOTAL_GRAL"];
				$varConta = $varConta + $fila["CONTADO"];
				$varCredi = $varCredi + $fila["CREDITO"];
				$varParci = $varParci + $fila["PARCIAL"];
				$varRecha = $varRecha + $fila["RECHAZO"];
				$varVenAct = $fila["CVEN"];
				$varRutAct = $fila["CRUT"];
				$varNumDoc++;
				if($varVenAct <> $varVenAnt) {
					$varVenAnt = $fila["CVEN"];
					$varRutAnt = $fila["CRUT"];
					$html.='<tr class="list_row">';
					$html.='<td class="right"><b>'.$fila["CVEN"].'</b></td>';
					$html.='<td colspan="12"><b>'.$fila["VENDEDOR"].'</b></td>';
					$html.='</tr>';
					/*
					$html.='<tr class="list_row">';
					$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="list_row">';
					$html.='<td></td>';
					$html.='<td class="center"><b>'.$fila["CRUT"].'</b></td>';
					$html.='<td colspan="10"><b>'.$fila["RUTA"].'</b></td>';
					$html.='</tr>';
					}
					*/
				$html.='<tr class="list_row">
						<td class="center">&nbsp;</td>
						<td class="center">'.$fila["CCLI"].'</td>
						<td NOWRAP>'.$fila["CLIENTE"].'</td>
						<td NOWRAP>'.$fila["RUTA"].'</td>
						<td NOWRAP>'.substr($fila["DIRECCION"],0,35).'</td>
						<td NOWRAP>'.$fila["DOCUMENTO"].'</td>
						<td NOWRAP>'.$fila["CONDICION"].'</td>
						<td class="right">'.number_format($fila["TOTAL_GRAL"],"2",".",",").'</td>';
				$html.='<td class="right">&nbsp;';
				if(!is_null($fila["CONTADO"]) && $fila["CONTADO"]>0)
						$html.= number_format($fila["CONTADO"],"2",".",",");
				$html.='</td>';
				$html.='<td class="right">&nbsp;';
				if(!is_null($fila["CREDITO"]) && $fila["CREDITO"]>0)
						$html.= number_format($fila["CREDITO"],"2",".",",");
				$html.='</td>';
				$html.='<td class="right">&nbsp;';
				if(!is_null($fila["PARCIAL"]) && $fila["PARCIAL"]>0)
						$html.= number_format($fila["PARCIAL"],"2",".",",");
				$html.='</td>';
				$html.='<td class="right">&nbsp;';
				if(!is_null($fila["RECHAZO"]) && $fila["RECHAZO"]>0)
						$html.= number_format($fila["RECHAZO"],"2",".",",");
				$html.='</td>';
				$html.='<td>'.$fila["MOTIVO"].'</td>';
				$html.='</tr>';
				}
			$html.='<tr class="head">';
			$html.='<td colspan="4" class="left"><strong>Totales :</strong></td>';
			$html.='<td class="left" colspan="3">'.number_format($varNumDoc,"0",".",",").' Documentos</td>';
			$html.='<td class="right">'.number_format($varTotal,"2",".",",").'</td>';
			$html.='<td class="right">'.number_format($varConta,"2",".",",").'</td>';
			$html.='<td class="right">'.number_format($varCredi,"2",".",",").'</td>';
			$html.='<td class="right">'.number_format($varParci,"2",".",",").'</td>';
			$html.='<td class="right">'.number_format($varRecha,"2",".",",").'</td>';
			$html.='<td class="right"></td>';
			$html.='</tr>';
			$html.='</table>';
		$html.='</td>';
		$html.='</tr>';
				
		$html.='</table>		
				</body>
				</html>';
		return $html;
		}
	}
	
?>