<!DOCTYPE html>
<html lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Orden de Mantenimiento</title>
<link href="../../maincss.css" rel="stylesheet" type="text/css" />
<link href="../../printcss.css" rel="stylesheet" type="text/css" media="print" />

</head>
<?php
include "../../conn.php";
include "../../general.php";
include "../../token.php";

$rep=$_GET["rep"];
$tipo=$_GET["tipo"];

//index.php?pkyMaintenanceOrder=3&user=Luigi&token=123456
$cod_orden_mant = $_GET["pkyMaintenanceOrder"];
//$usuario = $_GET["user"];
$manual = $_GET["manual"];
//$n_decimales = 6;

?>
<body>

<?php

// Inicio impresion orden de mantenimiento

$result3 = mysqli_query($link, "SELECT a.pkyMaintenanceOrder AS cod_orden_mant, a.fldDocumentNumber AS nro_doc, a.fldDate AS fecha, a.fldTime AS hora, a.codResponsible AS cod_resp, case when b.fldMiddleName is NULL then concat(b.fldLastName,', ',b.fldFirstName) else concat(b.fldLastName,' ',b.fldMiddleName,', ',b.fldFirstName) end as responsable, a.codVehicle AS cod_vehi, c.fldName AS vehiculo, a.fldFuel AS combustible, a.fldMileage AS kilometraje, a.codCompanyBranch AS cod_sucursal, a.codExchangeRate AS cod_tc, a.txtExtraData AS glosa, a.sysEnabled as enabled, a.sysAuthorized as authorized, g.fldName as usuario_reg, a.sysContext as cod_contexto, s.fldName as contexto  
FROM vhcMaintenanceOrder a, agnPerson b, invItem c, accGroup g, srvContext s 
WHERE 
a.codResponsible = b.pkyPerson AND 
a.codVehicle = c.pkyItem AND 
a.sysAuthorizationGroup = g.pkyGroup AND 
a.sysContext = s.pkyContext AND 
a.pkyMaintenanceOrder = '$cod_orden_mant';");
	while ($row = mysqli_fetch_array($result3)) {
	$nro_doc = $row['nro_doc'];
	$fecha = $row['fecha'];
	$hora = $row['hora'];
	$cod_resp = $row['cod_resp'];
	$responsable = $row['responsable'];
	$cod_vehi = $row['cod_vehi'];
	$vehiculo = $row['vehiculo'];
	$combustible = $row['combustible'];
	$kilometraje = $row['kilometraje'];
	$cod_sucursal = $row['cod_sucursal'];
	$cod_tc = $row['cod_tc'];
	$glosa = $row['glosa'];
	$glosa = nl2br($glosa);
	$glosa = str_replace('\n','<br />',$glosa);
	$usuario_reg = $row['usuario_reg'];
	$enabled = $row['enabled'];
	$authorized = $row['authorized'];
	$cod_contexto = $row['cod_contexto'];
	$contexto = $row['contexto'];
	}
	mysqli_free_result($result3);

$resultsuc = mysqli_query($link, "select fldName as sucursal
from
agnCompanyBranch
where
pkyCompanyBranch = '$cod_sucursal';");
while ($row = mysqli_fetch_array($resultsuc)) {
	$sucursal = $row['sucursal'];
}
mysqli_free_result($resultsuc);

//detalle orden de mantenimiento
$result2 = mysqli_query($link, "SELECT a.pkyMaintenanceOrderDetail AS cod_mant_det, a.codMaintenanceOrder AS cod_orden_mant, b.codCategory AS cod_cat, c.fldName AS categoria, a.codConcept AS cod_concepto, b.fldName AS concepto, a.fldStatus AS estado, a.fldLevel AS nivel, a.txtNotes AS notas
FROM vhcMaintenanceOrderDetail a, vhcMaintenanceConcept b, vhcMaintenanceConceptCategory c 
WHERE 
a.codConcept = b.pkyMaintenanceConcept AND 
b.codCategory = c.pkyMaintenanceConceptCategory AND 
a.codMaintenanceOrder = '$cod_orden_mant' 
ORDER BY c.fldName, b.fldName;");


?>
<table border="0" align="left" width="100%" cellpadding="5">
<tr><td><?php echo $mostrar_logo_reportes; ?>
</td><td align="right"><b>Fecha: <?php echo date("Y-m-d"); ?><br/>Hora: <?php echo date("H:i:s"); ?><br/>Usuario: <?php echo $usuario; ?></b>
</td></tr></table>
<p><br /><center><font color="#000099" size="+1"><b>Nota Orden de Mantenimiento</b>&nbsp;&nbsp;
	<?php if($manual == 1){ //if manual1 ?>
		<a href="../../ModuleVehicles/VoucherMaintenanceOrder/index.php?pkyMaintenanceOrder=<?php echo $cod_orden_mant; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>&manual=0" title="Llenado Manual"><img src="../../Images/penshe.png"></a>
	<?php } else { //else if manual1 ?>
		<a href="../../ModuleVehicles/VoucherMaintenanceOrder/index.php?pkyMaintenanceOrder=<?php echo $cod_orden_mant; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>&manual=1" title="Llenado Manual"><img src="../../Images/penshe.png"></a>
	<?php } //fin if manual1 ?>
	</font></center></p>
<table border="0" cellpadding="2" align="center" width="100%">
<tr>
<td>	
    <table border="1" align="center" cellpadding="2" width="100%">
        <TR> 
        <TD width="25%"><b>Cod. Reg.: </b><?php echo $cod_orden_mant; ?></TD>
        <TD width="25%"><b>Nro. Doc.: </b><?php echo $nro_doc; ?></TD>
        <TD width="25%"><b>Fecha de Reg: </b><?php echo $fecha; ?></TD>
        <TD width="25%"><b>Hora de Reg.: </b><?php echo $hora; ?></TD>
        </TR>
        <TR> 
        <TD><b>Sucursal: </b><?php echo $sucursal; ?></TD>
		<TD><b>Responsable: </b><?php echo $responsable; ?></TD>
		<TD><b>Kilometraje: </b><?php echo $kilometraje; ?></TD>
		<TD><b>Combustible: </b><?php echo $combustible; ?></TD>
        </TR>
		<TR> 
        <TD colspan="2"><b>Vehículo: </b><a href="../../ModuleInventory/VoucherItem/index.php?pkyItem=<?php echo $cod_vehi; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Nota de Item"><?php echo $cod_vehi; ?></a> - <?php echo $vehiculo; ?></TD>
		<TD colspan="2"><b>Contexto: </b><?php echo $cod_contexto; ?> - <?php echo $contexto; ?></TD>
        </TR>
		
    </table>

	<?php if($manual == 1){ //if manual2 ?>
		<br /> <font color="#000099"><b>Llenado manual datos del mismo vehículo para el nuevo mantenimiento:</b></font>
		<table border="1" align="center" cellpadding="2" width="100%">
			<TR> 
			<TD width="25%"><b>Cod. Reg.: </b></TD>
			<TD width="25%"><b>Nro. Doc.: </b></TD>
			<TD width="25%"><b>Fecha de Reg: </b></TD>
			<TD width="25%"><b>Hora de Reg.: </b></TD>
			</TR>
			<TR> 
			<TD><b>Sucursal: </b></TD>
			<TD><b>Responsable: </b></TD>
			<TD><b>Kilometraje: </b></TD>
			<TD><b>Combustible: </b></TD>
			</TR>
			
		</table>
	<?php } //fin if manual2 ?>

</td>
<!--
<td valign="top" width="150">

    <table border="1" align="center" cellpadding="2" width="100%">
    <tr>
    <TD><b>Tipo de Cambio:</b></TD>
    </tr><tr>
    <TD align="center"><a href="../../ModuleAccounting/VoucherExchangeRate/index.php?pkyExchangeRate=<?php echo $cod_tc; ?>&user=<?php echo $usuario; ?>&token=<?php echo $token; ?>" title="Tipo de Cambio"><?php echo $cod_tc; ?></a> (<?php echo $tc_fecha; ?> - <?php echo $tc_hora; ?>)<br/ >
        <table border="0" cellpadding="2" align="center">
            <?php
            for($m = 1; $m <= $fin_m; $m++) {
            printf("<tr><td align='left'>1 %s</td>", $ma_tc [$m][1]);//moneda base
            printf("<td align='center'>%s</td>", ' = ');//letra a
            printf("<td align='left'>%s %s</td></tr>", number_format($ma_tc [$m][3],$n_tipocambio), $ma_tc [$m][2]);//moneda target y tasa
            //printf("<td align='right'>%s</td></tr>", number_format($ma_tc [$m][3],$n_tipocambio));//tasa
            }
            ?>
        </table>
    </TD>
    </tr>
	</table>
    
</td>
-->
</tr>
</table>
<br />
<table border="1" align="center" cellpadding="2" width="100%">
 	<TR> 
    <TD align="center"><b>Cod.</b></TD>
    <TD align="center"><b>Categoría</b></TD>
	<TD align="center"><b>Concepto</b></TD>
    <TD align="center"><b>¿En Buen Estado?</b></TD>
    <TD align="center"><b>Nivel (%)</b></TD>
    <TD align="center"><b>Notas</b></TD>
	<?php if($manual == 1){ //if manual3 ?>
		<TD align="center"><b>¿En Buen Estado?</b></TD>
		<TD align="center"><b>Nivel (%)</b></TD>
		<TD align="center" width="300"><b>Notas</b></TD>
	<?php } //fin if manual3 ?>

    </TR>
    <?php
    $color = 2;
	$suma_nivel = 0;
	$cant_det = 0;
		
	while ($row = mysqli_fetch_array($result2)) { //while
		$cod_cxc_det = $row['cod_cxc'];
		if($color % 2 == 0) { //if color
		printf("<tr BGCOLOR='#e2e7fa'><td align='center'>%s</td>", $row['cod_mant_det']);//cod orden mant detalle
		} else {
		printf("<tr><td align='center'>%s</td>", $row['cod_mant_det']);//cod orden mant detalle
		} //cierra if color
		if($cant_det == 0){
			printf("<td align='center'>%s</td>", $row['categoria']);//categoria
			$cod_cat_ant = $row['cod_cat']; //guardo la categoria para preguntar
		} else {
			if($cod_cat_ant != $row['cod_cat']){ //si es diferente imprimo
				printf("<td align='center'>%s</td>", $row['categoria']);//categoria
				$cod_cat_ant = $row['cod_cat']; //guardo la categoria para preguntar
			} else {
				printf("<td align='center'>%s</td>", '');//categoria vacia
			}
		}
		printf("<td align='left'>%s</td>", $row['concepto']);//concepto
		if($row['estado'] == 0) {
			printf("<td align='center'><font color='#F00'>%s</font></td>", 'No');//estado
		} else {
			printf("<td align='center'><font color='#009011'><b>%s</b></font></td>", 'Si');//estado
		}
		printf("<td align='right'>%s %%</td>", number_format($row['nivel'], 0));//nivel (%)
		if($row['nivel'] >= 0 && $row['nivel'] <= 29) {
			printf("<td align='left'><font color='#FF0000'>%s</font></td>", $row['notas']);//notas rojo 0-29
		} else {
			if($row['nivel'] > 29 && $row['nivel'] <= 59) {
				printf("<td align='left'><font color='#FFC300'><b>%s</b></font></td>", $row['notas']);//notas amarillo 30-59
			} else {
				printf("<td align='left'><font color='#009011'><b>%s</b></font></td>", $row['notas']);//notas verde 60-100
			}
		}

		if($manual == 1){ //if manual4
			printf("<td>%s</td>", '');//estado vacio
			printf("<td>%s</td>", '');//nivel vacio
			printf("<td>%s</td>", '');//notas vacio
		} //fin if manual4

		echo "</tr>";

		$cant_det ++;
		$suma_nivel = $suma_nivel + $row['nivel'];
		$color++;
	}//fin while
	mysqli_free_result($result2);
	if($cant_det > 0){
		$promedio = $suma_nivel / $cant_det;
	} else {
		$promedio = 0;
	}	
	?>
	<tr BGCOLOR='#ffcccc'><td colspan="4"><b>Total </b></td>
    <td align="right"><b><?php echo number_format($promedio, 2).' %'; ?></b></td>
	<td align="right"><b><?php echo number_format($cant_det, 0); ?></b></td>
	<?php if($manual == 1){ //if manual5 ?>	
		<td colspan="3">&nbsp;</td>
	<?php } //fin if manual5 ?>
	</tr>
    
</table>
<br />

<?php
    if($enabled == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>REGISTRO ANULADO</font></td></tr></table>");
			}
	if($authorized == 0){
				printf("<table border='0' align='center' cellpadding='2' width='600'><tr><td align='center'><font color='#FF0000' size='+1'>REGISTRO PENDIENTE DE AUTORIZACI&Oacute;N</font></td></tr></table>");
			}
?>

<p>&nbsp;</p>
<br /><br /><br />
<table border="0" align="center" cellpadding="2" width="90%">
<tr>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
<td align="center">----------------------------------------</td>
</tr><tr>
<td align="center">Elaborado por: <?php echo $usuario_reg; ?></td>
<td align="center"><?php echo $firma_contabilidad; ?></td>
<td align="center"><?php echo $firma_gerencia; ?></td>
<td align="center"><?php echo $firma_interesado; ?></td>
</tr><tr>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
<td align="left">Nombre:__________________________</td>
</tr><tr>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
<td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C.I.:__________________________</td>
</tr>
</table>

</body>
</html>
