<?php require_once('../Connections/sicot.php'); ?>
<?php
 
$hoymismo = date("d-m-Y");
$por_hoy = date('Y-m-d');
$fecha_final=strtotime($row_trabajos['Fecha_entrega']);
 
function calcularFecha($dias){
							  $calculo = strtotime("$dias days");
    						  return date("Y-m-d", $calculo);
    							 }
    					  $calculo= strtotime("-2 days", $por_hoy); //Le restamos x dias
    					  $monito = date("Y-m-d", $calculo);
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
mysql_select_db($database_sicot, $sicot);
$query_atrazadas = "SELECT hi.id_history , hi.id_trabajos , hi.observaciones , hi.fecha , tjs.* FROM history_order hi INNER JOIN (SELECT MAX(fecha) TER, id_trabajos FROM history_order GROUP BY id_trabajos)t1 ON t1.id_trabajos = hi.id_trabajos AND  t1.TER = hi.fecha  INNER JOIN trabajos tjs ON tjs.id_trabajos = hi.id_trabajos WHERE tjs.condicion = '2' AND Fecha_entrega < '$por_hoy' AND NOT tjs.area = 'Ventas' AND NOT tjs.area = 'Despacho' ORDER BY tjs.area DESC";
$atrazadas = mysql_query($query_atrazadas, $sicot) or die(mysql_error());
$row_atrazadas = mysql_fetch_assoc($atrazadas);
$totalRows_atrazadas = mysql_num_rows($atrazadas);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Informe General OT Atrasadas</title>
 
</head>
 
<body onload="window.print()"><div id="rounded-box2">
<table width="98%" border="0">
  <tr>
    <td align="right"><input type="button" value="Cerrar" class="nover" onclick="cerrarse()" /></td>
  </tr>
  <tr>
    <td align="center" class="titulo_GRANDE_NEGRO">Informe General OT Atrasadas</td>
  </tr>
  <tr>
    <td align="center" class="texto_descripcion_r" >Informe no incluye Ordenes de Trabajo al día y a 2 días de término</td>
  </tr>
  <tr>
    <td align="center" ><table width="98%" border="0">
      <tr class="texto-secciones">
        <td> </td>
        <td> </td>
        <td align="right"> </td>
        <td> </td>
      </tr>
      <tr class="texto_descripcion_r">
        <td width="21%" align="right"><?php echo "$dia $dia2 de $mes de $ano";
?>   </td>
        <td width="23%">Hora: 
          <?php
date_default_timezone_set("America/Santiago");
$hora = time(); 
echo date ("H:i", $hora);
?></td>
        <td width="48%" align="right">Imprenta de la Armada</td>
        <td width="8%"> </td>
      </tr>
      <tr class="texto-secciones">
        <td> </td>
        <td> </td>
        <td align="right"> </td>
        <td> </td>
      </tr>
      <tr class="texto-secciones">
        <td colspan="4"> </td>
      </tr>
    </table></td>
  </tr>
</table>
<p> </p>
<table width="1231" border="0" align="center" style="background-color:#CCC;">
  <tr class="texto_descripcion">
    <td align="center" scope="col"><div style="width:150px;">Nº OT</div></td>
    <td align="center" scope="col"><div style="width:170px;">Cliente</div></td>
    <td align="center" scope="col"><div style="width:170px;">Tipo de Trabajo</div></td>
    <td align="center" scope="col"><div style="width:96px;">Cantidad</div></td>
    <td align="center" scope="col"><div style="width:90px;">Total</div></td>
    <td align="center" scope="col"><div style="width:96px;"> Fecha Inicio</div></td>
    <td align="center" scope="col"><div style="width:96px;"> Fecha Entrega</div></td>
    <td align="center" scope="col"><div style="width:116px;">Área actual</div></td>
    <td align="center" scope="col"><div style="width:86px;">Estado</div></td>
    <td align="center" scope="col">Observaciones</td>
  </tr>
 
 <?php do { 
	  if ($colorfila==0){ 
       $color= "#FFFFFF"; 
       $colorfila=1; 
    }else{ 
       $color="#E8E8E8"; 
       $colorfila=0; 
    } ?>
    <tr bgcolor="<? echo $color;?>">
      <td width="244" height="40" align="center" valign="top" class="texto-precio-new"><?php echo $row_atrazadas['id_trabajos']; ?>/13</td>
      <td width="208" align="center" valign="top" class="texto_descripcion_r"><?php echo $row_atrazadas['rut_cliente']; ?><br />
        <br /> 
        <br /></td>
      <td width="134" align="center" valign="top" class="texto_descripcion_r"><?php echo $row_atrazadas['trabajo_1'].'<br />';
          if ($row_atrazadas['trabajo_2']!= ""){
             echo $row_atrazadas['trabajo_2'].'<br />';}
		   elseif ($row_atrazadas['trabajo_3']!= ""){
             echo $row_atrazadas['trabajo_3'].'<br />';}
			elseif ($row_atrazadas['trabajo_4']!= ""){
             echo $row_atrazadas['trabajo_4'].'<br />';}
			elseif ($row_atrazadas['trabajo_5']!= ""){
             echo $row_atrazadas['trabajo_5'].'<br />';}
			elseif ($row_atrazadas['trabajo_6']!= ""){
             echo $row_atrazadas['trabajo_6'].'<br />';}
			 else{
				 }?></td>
      <td width="145" align="center" valign="top" class="texto_descripcion_r"><?php echo $row_atrazadas['cantidad_1'].'<br />';
          if ($row_atrazadas['cantidad_2']!= ""){
             echo $row_atrazadas['cantidad_2'].'<br />';}
		   elseif ($row_atrazadas['cantidad_3']!= ""){
             echo $row_atrazadas['cantidad_3'].'<br />';}
			elseif ($row_atrazadas['cantidad_4']!= ""){
             echo $row_atrazadas['cantidad_4'].'<br />';}
			elseif ($row_atrazadas['cantidad_5']!= ""){
             echo $row_atrazadas['cantidad_5'].'<br />';}
			elseif ($row_atrazadas['cantidad_6']!= ""){
             echo $row_atrazadas['cantidad_6'].'<br />';}
			 else{
				 }?></td>
      <td width="74" align="center" valign="top" class="producto-descripcion">        <span class="texto_descripcion_r">
        <?php 
 
		$todo_ot = $row_atrazadas['valor_6'] + $row_atrazadas['valor_5'] + $row_atrazadas['valor_4'] + $row_atrazadas['valor_3'] + $row_atrazadas['valor_2'] + + $row_atrazadas['valor_1']; 
		$numero_entero = $todo_ot;
		number_format ($numero_entero , 0, ",",".");
		$finales = number_format ($numero_entero ,0, ",",".");
		echo $finales; ?>      
      </span></td>
      <td width="101" align="center" valign="top" class="texto_descripcion_r"><span class="texto_descripcion">
        <?php 
 
 
    //formato fecha americana
    $fecha1=$row_atrazadas['fecha_inicio'];
    $fecha2=date("d-m-Y",strtotime($fecha1));
	$fecha_entre=$row_atrazadas['Fecha_entrega'];
	$fecha_final=date("d-m-Y",strtotime($fecha_entre));
	$nuevafecha = strtotime ( '+1 day' , strtotime ( $fecha1 ) ) ;
	$nuevafecha = date ( 'd-m-Y' , $nuevafecha );
	$para_viern = strtotime ( '+3 day' , strtotime ( $fecha1 ) ) ;
	$para_viern= date ( 'd-m-Y' , $para_viern );
	$hora = date("H:i:s",strtotime($fecha1));
	$dia_fe = date("l",strtotime($fecha1));
 
 
	if (($hora >= '14:00:00') && ($dia_fe == 'Friday')) { echo $para_viern;} elseif ($hora >= '14:00:00'){ echo $nuevafecha;} else {
    //El nuevo valor de la variable: $fecha2="20-10-2008"
    echo $fecha2; }
 
	?>
      </span></td>
      <td width="56" align="center" valign="top" class="texto_descripcion_r"><?php echo $fecha_final; ?></td>
      <td width="109" align="center" valign="top" class="texto_descripcion_r"><?php echo $row_atrazadas['area']; ?></td>
      <td width="59" align="center" valign="top" class="texto_descripcion_r">
        <?php 
 
	$hoymismo= strtotime(date("d-m-Y"));
	$fecha_final=strtotime($row_atrazadas['Fecha_entrega']);
 
 
    					  $calculo= strtotime("-2 days", $fecha_final); //Le restamos x dias
 
	if ( $hoymismo == $calculo) {
		echo '<img src="../images/yellow_light.png" width="28" height="26"/></br>2 Días para Termino';
	}
	elseif ($fecha_final > $hoymismo)   {
		echo '<img src="../images/green_light.png" width="28" height="26"/></br>AL DIA';
	}
	elseif ($fecha_final < $hoymismo)  {
		echo '<img src="../images/red_light.png" width="28" height="26"/></br><span class="carro-total">ATRASADA</span>';
	}
	elseif ($fecha_final == $hoymismo)  {
		echo '<img src="../images/yellow_light.png" width="28" height="26"/></br><span class="regalo">EN FECHA</span>';
	}
	else {
 
	}?>
      </td>
      <td width="59" align="center" valign="top" class="texto_descripcion_r"><?php echo $row_atrazadas['observaciones']; ?></td>
    </tr>
    <?php } while ($row_atrazadas = mysql_fetch_assoc($atrazadas)); ?>
 
</table>
</div>
</body>
</html>
<?php
mysql_free_result($atrazadas);
?>
 este es mi código para obtener los trabajos atrasados y un detalle alijado en el historial de trabajos, solo el ultimo ingresado, ahora esta consulta es muy lenta o aveces envía el mensaje Maximum execution....
 
alguien sabe como puedo mejorar este código para optimizarlo 
  
 

