<?php
//include "clase.conexion.php";
class clsAsignacionCostoReparto extends clsConexion{
	public $varTitulo = "COSTO DE REPARTO";
	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"=>"vcod", "name"=>"vcod", "valor"=>$_POST[vcod], 
																					 "ancho"=>"50", "alinear"=>"center", 
																					 "blur"=>"xajax_seleccionar_vehiculo(this.value)", 
																					 "dclick"=>"fn_cargar_listado_vehiculo('vcod', 'vnom')"),
																			   array("tipo"=>"text", "id"=>"vnom", "name"=>"vnom", "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"=>"ccod", "name"=>"ccod", "valor"=>$_POST["ccod"], 
																					"ancho"=>"50",  "alinear"=>"center", 
																					"blur"=>"xajax_seleccionar_conductor(this.value)", 
																					"dclick"=>"fn_cargar_listado_conductor('ccod', 'cnom')"),
																			  array("tipo"=>"text", "id"=>"cnom", "name"=>"cnom", 
																					"ancho"=>"250", "clase"=>"inputDisable", "locked"=>"SI")
																			  )
														   )
										   ),
									 array("titulo"=>"F.Inicial :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"fecha", "id"=>"fini", "name"=>"fini", "alinear"=>"center"))
														 	)
											 ),
									   array("titulo"=>"F.Final :", "clase1"=>"FormCampo1", "clase2"=>"interior",
											 "campo"=>array("tipo"=>"varios",
															"contenido"=>array(array("tipo"=>"fecha", "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()"),
									 array("tipo"=>"button", "id"=>"bimp", "name"=>"bimp", "ancho"=>"100", "texto"=>"Imprimir", 
										   "clase"=>"boton_imprimir", "click"=>"javascript:xImprimirJS()")
									 )
					);
		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[vcod]) && $varForm[vcod]<>'')?$varForm[vcod]:"NULL";
		$varCon = (isset($varForm[ccod]) && $varForm[ccod]<>'')?$varForm[ccod]:"NULL";
		$varIni = (isset($varForm[fini]) && $varForm[fini]<>'')?"'".FechaMySql($varForm[fini])."'":"NULL";
		$varFin = (isset($varForm[ffin]) && $varForm[ffin]<>'')?"'".FechaMySql($varForm[ffin])."'":"NULL";
		
		$sql = "call spQuery_Costo_Reparto($varCod, $varVeh, $varCon, $varIni, $varFin, $varFie, $varOrd)";
		$varTabla=array("id"=>"theTable", "name"=>"theTable", "tamano"=>"1000", "sql_cab"=>$sql,	"pages"=>20,
					    "conte"=>array(array("titulo"=>"CASIG",    "ancho"=>"30" , "alinear"=>"center"), 
									   array("titulo"=>"FECHA", "ancho"=>"80", "fijo"  =>"SI", "alinear"=>"center"),
									   array("titulo"=>"CVEH",  "ancho"=>"30", "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"VEHICULO", "ancho"=>"200", "fijo"=>"SI", "funcion"=>array("contar")),
									   array("titulo"=>"CCON", "ancho"=>"30", "alinear"=>"center", "oculto"=>"SI"),
									   array("titulo"=>"CONDUCTOR","ancho"=>"200", "fijo"=>"SI"),
									   array("titulo"=>"VENTA", "ancho"=>"50", "alinear"=>"right", "decimal"=>"2", "funcion"=>array("autosuma")),
									   array("titulo"=>"M_VEH", "ancho"=>"50", "alinear"=>"right", "decimal"=>"2", "funcion"=>array("autosuma")),
									   array("titulo"=>"M_CON", "ancho"=>"100", "alinear"=>"right", "decimal"=>"2", "funcion"=>array("autosuma")),
									   array("titulo"=>"M_REP", "ancho"=>"100", "alinear"=>"right", "decimal"=>"2", "funcion"=>array("autosuma")),
									   array("titulo"=>"M_GAS", "ancho"=>"100", "alinear"=>"right", "decimal"=>"2", "funcion"=>array("autosuma")),
									   array("titulo"=>"M_TOTAL", "ancho"=>"100", "alinear"=>"right", "decimal"=>"2", "funcion"=>array("autosuma")),
									   array("titulo"=>"INDICE", "ancho"=>"100", "alinear"=>"right", "decimal"=>"2", 
									   							 "funcion"=>array("porc","11","6"), "tipodat"=>"porc")
									   ), 
						"boton"=>array(array("tipo"=>"img", "id"=>"bimp", "name"=>"bimp", "img"=>"images/b_printer.png", 
										   		 "toltip"=>array(array("valor"=>"HAGA CLICK PARA <b>IMPRIMIR EL DETALLE</b> DE LA ASIGNACION<br>"),
												 				 array("valor"=>"<b>Numero : </b>"),
																 array("valor"=>0, "tipo"=>"INDEX"),
																 array("valor"=>"<br><b>Fecha : </b>"),
																 array("valor"=>1, "tipo"=>"INDEX")
												   				),
												 "click"=>array("funcion"=>"ImprimirAsignacion",
																"param"=>array(array("valor"=>0, "tipo"=>"INDEX"))
																)
											 ),
										)
							);
		return $this->SetGridTabla2($varTabla);
		}
	
	function Imprimir($varIde = NULL)  
		{
		$varFech = (isset($varDia) && $varDia<>"")?"'".FechaMySql($varDia)."'":"NULL";
		$sql = "CALL spQuery_Asignacion_Detalle($varIde);";
		$this->AbrirConexion();
		$rs = $this->Query($sql);
		$this->CerrarConexion();
		if(isset($varIde) && $varIde<>"") {
			if($filac = $this->FetchArray($rs)) {
				$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:100%;vertical-align:middle;text-align:center;font-size:14px" colspan="4">';
					$html.='<b>REPORTE DE '.$this->varTitulo.'</b></td>';
					$html.='</tr>';
					$html.='<tr>';
					$html.='<td style="width: 10%; vertical-align: middle; text-align:left;" nowrap="nowrap"><b>Asignacion</b></td>';
					$html.='<td style="width: 1%; vertical-align: middle; text-align:center;" nowrap="nowrap"><b>:</td>';
					$html.='<td style="width: 39%; vertical-align: middle; text-align:left;" nowrap="nowrap">'.$filac["ASIG"].'</b></td>';
					$html.='<td style="width: 50%; vertical-align: middle; text-align:right;"><b>'.Config("SYSEMP").'</b></td>';
					$html.='</tr>';
					$html.='<tr>';
					$html.='<td style="vertical-align: middle; text-align:left;" nowrap="nowrap"><b>Fecha Asig.</b></td>';
					$html.='<td style="vertical-align: middle; text-align:center;" nowrap="nowrap"><b>:</b></td>';
					$html.='<td style="vertical-align: middle; text-align:left;" nowrap="nowrap">'.$filac["FECHA"].'</b></td>';
					$html.='<td style="vertical-align: middle; text-align:right;"><b>'.MostrarFechaActual().'</b></td>';
					$html.='</tr>';
					$html.='<tr>';
					$html.='<td style="vertical-align: middle; text-align:left;" nowrap="nowrap"><b>Veh&iacute;culo</b></td>';
					$html.='<td style="vertical-align: middle; text-align:center;" nowrap="nowrap"><b>:</b></td>';
					$html.='<td style="vertical-align: middle; text-align:left;" nowrap="nowrap">';
					$html.=$filac["CVEH"].' - '.$filac["PLACA"].' - '.$filac["VEHICULO"].'</b></td>';
					$html.='<td style="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>';
					$html.='<td width="48%" class="center">';
						$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
						$html.='<tr class="head">';
						$html.='<td width="100%" align="center" style="font-size:9px;">DETALLE DE INGRESOS</td>';
						$html.='</tr>';
						$html.='</table>';
					$html.='</td>';
					$html.='<td width="2%">&nbsp;</td>';
					$html.='<td width="48%" class="center">';
						$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
						$html.='<tr class="head">';
						$html.='<td width="100%" align="center" style="font-size:9px;">DETALLE DE EGRESOS</td>';
						$html.='</tr>';
						$html.='</table>';
					$html.='</td>';
					$html.='</tr>';
					$html.='</thead>';
					$html.='<tr>';
					$html.='<td valign="top">';
					$varIng = 0;
					$varEgr = 0;
					//Detalle de Ingresos
						$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
						$html.='<tr class="head">';
						$html.='<td width="100%" style="text-align:left" colspan="3">INGRESO POR VENTAS</td>';
						$html.='</tr>';
						$html.='<tr class="head">';
						$html.='<td width="20%">CVEN</td>';
						$html.='<td width="70%">VENDEDOR</td>';
						$html.='<td width="20%" align="right">MONTO</td>';
						$html.='</tr>';
							$sql = "select C.id_vendedor as CVEN, V.nombre as VENDEDOR, SUM(C.total) AS VENTA
									from _sv_venta C
									inner join _sv_vendedor V ON C.id_vendedor = V.id_vendedor
									where C.id_asignacion = $varIde
									and C._estado = 'A'
									and C.estado<>'AN'
									GROUP BY C.id_vendedor, V.nombre";
							$this->AbrirConexion();
							$rsv = $this->Query($sql);
							$this->CerrarConexion();
							while($filav = $this->FetchArray($rsv)) {
								$html.='<tr class="list_row">';
									$html.='<td>'.$filav["CVEN"].'</td>';
									$html.='<td style="text-align:left">'.$filav["VENDEDOR"].'</td>';
									$html.='<td style="text-align:right">'.number_format($filav["VENTA"],"2",".",",").'</td>';
									$varIng = $varIng + $filav["VENTA"];
								$html.='</tr>';	
								}
						$html.='</table>';
					$html.='</td>';
					$html.='<td>&nbsp;</td>';
					$html.='<td valign="top">';
						//====================================================
						//Detalle de Egresos
						//====================================================
						//Detalle del Vehiculo
						$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
						$html.='<tr class="head">';
						$html.='<td width="100%" style="text-align:left" colspan="4">COSTO DE VEHICULO</td>';
						$html.='</tr>';
						$html.='<tr class="head">';
						$html.='<td width="15%">CVEH</td>';
						$html.='<td width="15%">PLACA</td>';
						$html.='<td width="50%">DESCRIPCION</td>';
						$html.='<td width="20%" align="right">MONTO</td>';
						$html.='</tr>';
						$html.='<tr class="list_row">';
						$html.='<td>'.$filac["CVEH"].'</td>';
						$html.='<td style="text-align:left">'.$filac["PLACA"].'</td>';
						$html.='<td style="text-align:left">'.$filac["VEHICULO"].'</td>';
						$html.='<td style="text-align:right">'.number_format($filac["MONTO_VEHICULO"],"2",".",",").'</td>';
						$varEgr = $varEgr + $filac["MONTO_VEHICULO"];
						$html.='</tr>';	
						$html.='</table>';
						//Detalle del Conductor
						$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
						$html.='<tr class="head" style="vertical-align:top">';
						$html.='<td width="100%" style="text-align:left" colspan="4">COSTO DE CONDUCTOR</td>';
						$html.='</tr>';
						$html.='<tr class="head">';
						$html.='<td width="15%">CCON</td>';
						$html.='<td width="65%">NOMBRE CONDUCTOR</td>';
						$html.='<td width="20%" align="right">MONTO</td>';
						$html.='</tr>';
						$html.='<tr class="list_row">';
						$html.='<td>'.$filac["CCON"].'</td>';
						$html.='<td style="text-align:left">'.$filac["CONDUCTOR"].'</td>';
						$html.='<td style="text-align:right">'.number_format($filac["MONTO_CONDUCTOR"],"2",".",",").'</td>';
						$varEgr = $varEgr + $filac["MONTO_CONDUCTOR"];
						$html.='</tr>';	
						$html.='</table>';
						//Detalle del Personal del Reparto
						$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
						$html.='<tr class="head" style="vertical-align:top">';
						$html.='<td width="100%" style="text-align:left" colspan="4">COSTO DE PERSONAL DE REPARTO</td>';
						$html.='</tr>';
						$html.='<tr class="head">';
						$html.='<td width="15%">CREP</td>';
						$html.='<td width="65%">NOMBRE REPARTIDOR</td>';
						$html.='<td width="20%" align="right">MONTO</td>';
						$html.='</tr>';
						$sql = "select A.id_repartidor as CREP, R.nombre as REPARTIDOR, A.MONTO
								from _sv_asignacion_repartidor A
								inner join _sv_repartidor R ON A.id_repartidor = R.id_repartidor
								where A._estado='A'
								and A.id_asignacion=$varIde;";
						$this->AbrirConexion();
						$rsr = $this->Query($sql);
						$this->CerrarConexion();
						while($filar = $this->FetchArray($rsr)) {
							$html.='<tr class="list_row">';
							$html.='<td>'.$filar["CREP"].'</td>';
							$html.='<td style="text-align:left">'.$filar["REPARTIDOR"].'</td>';
							$html.='<td style="text-align:right">'.number_format($filar["MONTO"],"2",".",",").'</td>';
							$varEgr = $varEgr + $filar["MONTO"];
							$html.='</tr>';	
							}
						$html.='</table>';
						//Detalle del Gasto Operativo
						$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
						$html.='<tr class="head" style="vertical-align:top">';
						$html.='<td width="100%" style="text-align:left" colspan="4">COSTO OPERATIVO DE REPARTO</td>';
						$html.='</tr>';
						$html.='<tr class="head">';
						$html.='<td width="15%">CGAS</td>';
						$html.='<td width="65%">DESCRIPCION</td>';
						$html.='<td width="20%" align="right">MONTO</td>';
						$html.='</tr>';
						$sql = "select A.id_gasto as CGAS, G.nombre as GASTO, A.MONTO
								from _sv_asignacion_gasto A
								inner join _sv_gasto G ON A.id_gasto = G.id_gasto
								where A._estado = 'A'
								and A.id_asignacion=$varIde;";
						$this->AbrirConexion();
						$rsg = $this->Query($sql);
						$this->CerrarConexion();
						while($filag = $this->FetchArray($rsg)) {
							$html.='<tr class="list_row">';
							$html.='<td>'.$filag["CGAS"].'</td>';
							$html.='<td style="text-align:left">'.$filag["GASTO"].'</td>';
							$html.='<td style="text-align:right">'.number_format($filag["MONTO"],"2",".",",").'</td>';
							$varEgr = $varEgr + $filag["MONTO"];
							$html.='</tr>';	
							}
						$html.='</table>';
					$html.='</td>';
					$html.='</tr>';
					$html.='<tr>';
					$html.='<td class="center">';
						$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
						$html.='<tr class="head" style="font-size:9px;">';
						$html.='<td width="80%" colspan="3" align="left">TOTAL DE INGRESOS</td>';
						$html.='<td width="20%" align="right">'.number_format($varIng,"2",".",",").'</td>';
						$html.='</tr>';
						$html.='</table>';
					$html.='</td>';
					$html.='<td>&nbsp;</td>';
					$html.='<td class="center">';
						$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
						$html.='<tr class="head" style="font-size:9px;">';
						$html.='<td width="80%" colspan="3" align="left">TOTAL DE EGRESOS</td>';
						$html.='<td width="20%" align="right">'.number_format($varEgr,"2",".",",").'</td>';
						$html.='</tr>';
						$html.='</table>';
					$html.='</td>';
					$html.='</tr>';
					//Costo de Reparto
					$html.='<tr>';
					$html.='<td class="center">';
						
					$html.='</td>';
					$html.='<td>&nbsp;</td>';
					$html.='<td class="center">';
						$html.='<table style="width: 100%;padding: 0; margin-top:0px; margin-bottom:0px; border-collapse:collapse;">';
						$html.='<tr class="head" style="font-size:9px;">';
						$html.='<td width="80%" colspan="3" align="left">COSTO DE REPARTO (EGRESOS/INGRESOS)</td>';
						$html.='<td width="20%" align="right">'.number_format($varEgr/$varIng*100,"2",".",",").'%</td>';
						$html.='</tr>';
						$html.='</table>';
					$html.='</td>';
					$html.='</tr>';
					/*
					$html.='<tr class="head">';
					$html.='<td colspan="4" class="left"><strong>Totales :</strong></td>';
					$html.='<td class="left" colspan="2">'.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>';	
				}
			else {
				$html = $this->ErrorConsulta();
				}
			}
		else {
			$html =  $this->ErrorConsulta();
			}
		return $html;
		}
		
	}
	
?>