<?php
//============================================================+
// File name   : example_011.php
// Begin       : 2008-03-04
// Last Update : 2013-05-14
//
// Description : Example 011 for TCPDF class
//               Colored Table (very simple table)
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
//               Tecnick.com LTD
//               www.tecnick.com
//               info@tecnick.com
//============================================================+

/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: Colored Table
 * @author Nicola Asuni
 * @since 2008-03-04
 */


//echo "<head>";
//echo "<title>Servicios de Internet</title>";
//echo "<link rel='shortcut icon' href='logo.ico'>"; 
//echo "</head>"; 

// Include the main TCPDF library (search for installation path).
//require_once('tcpdf_include.php');

$output = isset($_POST['output']) ? $_POST["output"]  : "browser";
$mode = isset($_POST['mode']) ? $_POST["mode"]  : "monthly";
$informe = isset($_POST['informe']) ? $_POST["informe"]  : "altas";

//$mode = "monthly";
//$output = "email";
//$informe = "reclamos";

define('K_PATH_IMAGES', '/var/www/html/cooperativa/'); 
require_once('tcpdf/tcpdf.php');

include_once('db_config.php');
ini_set('max_execution_time', 120);



// extend TCPF with custom functions
class MYPDF extends TCPDF {

    protected $title = "";

   //Page header
    public function Header() {
        // Logo
        /*
        $image_file = K_PATH_IMAGES . 'images/logo.png';
        $this->Image($image_file, 10, 10, 45, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
        //                        x    y   w   h   type          resize   DPI  align  imask imgmask border  fitbox, hidden fitonpage [alt alimgs]
        */

        // Set font
        $this->SetFont('helvetica', 'B', 12);

        // Title
        $tyhis->SetY(20);
        $this->Cell(0, 10, $this->title, 0, false, 'C', 0, '', 0, false, 'M', 'M');
    }

    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', '', 8);
        // Page number
        $this->Cell(0, 10, 'Pagina '.$this->getAliasNumPage().'-'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
    }

   // Load table data from file
   public function LoadData($file) {
      // Read file lines
      $lines = file($file);
      $data = array();
      foreach($lines as $line) {
         $data[] = explode(';', chop($line));
      }
      return $data;
   }

   // Colored table
   public function ColoredTable($header,$data,$conn,$mode,$informe) {

      $count = 0;

      // Colors, line width and bold font
      /*
      $this->SetFillColor(255, 0, 0);
      $this->SetTextColor(255);
      $this->SetDrawColor(128, 0, 0);
      $this->SetLineWidth(0.3);
//      $this->SetFont('', 'B');
*/

      $this->SetY(30);

      $this->SetFillColor(255, 255, 255);
      $this->SetTextColor(0);
      $this->SetDrawColor(255,255,255);
      $this->SetLineWidth(0);
      $this->SetFont('Courier','B',8);

      switch( $informe ) {
         case "reclamos":

            $this->SetFont('Courier','B',10);
            $this->SetY( $this->GetY() + 5 );
            $this->Cell($w[0], 6, "Reclamos", 'LR', 0, 'L', $fill);
            $this->SetY( $this->GetY() + 8 );
            $this->SetFont('Courier','B',8);

            $tipo = " and tickets.estado <> 'Resuelto' and tickets.estado <> 'Cerrado' ";
            $sql = "select sql_no_cache  DISTINCT id_ticket, ag_users.username, srvname, velocidad, situacion, calidad, fecha_resolucion, phone, mobile, tickets.observaciones as observa, diagnostico_reclamo, address, phone, mobile, servicio, concat(ag_users.lastname,' ',ag_users.firstname) as name, concat(ag_users.lastname,', ',ag_users.firstname) as uname, fecha_reclamo, descripcion, tickets.estado as status, atendido_por, tickets.email as temail, ag_users.email as email from tickets left join ag_users on ag_users.customer = tickets.servicio left join causas on causas.id_causal = causal_reclamo left join rm_services on rm_services.srvid = ag_users.srvid where tickets.servicio <> 0 $tipo order by fecha_reclamo";
            $result = $conn->query($sql);
            if ($result->num_rows > 0) {

               // Header
               $header = array('Fecha', 'Cliente', 'Causa', 'Observaciones');
               $w = array(20, 60, 30, 60);
               $num_headers = count($header);
               for($i = 0; $i < $num_headers; ++$i) {
                  $this->Cell($w[$i], 7, $header[$i], 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill
               }
               $this->Ln();
               $this->SetFont('Courier','',8);

               $x = 0;
               while( $row = $result->fetch_assoc() ) {

                  // Verifico si es Moroso
                  $moroso = false;
                  $sql1 = "select sql_no_cache cliente from ag_cortes where cliente = '" . $row["servicio"] . "' and activo = 1";
                  $result1 = $conn->query($sql1);
                  if ($result1->num_rows > 0) {
                     mysqli_free_result( $result1 );
                     continue;
                  }
   
                  $name = $row["name"];
                  if( strlen( $name ) <= 0 ) $name = "Cliente desconocido";

                  $date_reclamo = date_create($row["fecha_reclamo"]);
//                  $formatted = date_format($date_reclamo, 'd-m-Y H:i');          // formatted, name, descripcion, observa
                  $formatted = date_format($date_reclamo, 'd-m-Y');          // formatted, name, descripcion, observa

                  $descripcion = $row["descripcion"];
                  $address = $row["address"];
                  $phone = $row["phone"];
                  $mobile = $row["mobile"];
                  if( $mobile == "" && $phone != "" ) $telefonos = $phone;  
                  else if( $mobile != "" && $phone == "" ) $telefonos = $mobile;  
                  else $telefonos = "$mobile $phone"; 

                  $observa = $row["observa"];
                  $estado = $row["status"];

                  $fill = 0;
      
                  // Celdas
                  $this->Cell($w[0], 6, $formatted, 'LR', 0, 'L', $fill);
                  $this->Cell($w[1], 6, $name, 'LR', 0, 'L', $fill);
                  $this->Cell($w[2], 6, $descripcion, 'LR', 0, 'L', $fill);

                  if( strlen( $observa ) > 40 ) {
                     
                     $n = 1;                        
                     $part = substr($observa,0,40);
                     $this->Cell($w[3], 6, $part, 'LR', 0, 'L', $fill);
                     $observa = substr($observa,40,strlen($observa));
                     $len = strlen( $observa );
                     while($len > 40 ) {
                        $this->Ln();
                        $part = substr($observa,0,40);
                        $this->Cell($w[0], 6, "", 'LR', 0, 'L', $fill);

                        // Direccion o en blanco
                        if( $n == 1 )      $this->Cell($w[1], 6, $address, 'LR', 0, 'L', $fill);   
                        else if( $n == 2 ) $this->Cell($w[1], 6, $telefonos, 'LR', 0, 'L', $fill); 
                            else           $this->Cell($w[1], 6, "", 'LR', 0, 'L', $fill);
                        
                        $this->Cell($w[2], 6, "", 'LR', 0, 'L', $fill);
                        $this->Cell($w[3], 6, $part, 'LR', 0, 'L', $fill);
                        $observa = substr($observa,40,strlen($observa));
                        $part = substr($observa,0,40);
                        $len = strlen( $observa );
                        ++$n;
                     }
                     if( $len > 0 ) {
                        $this->Ln();
                        $part = substr($observa,0,40);
                        $this->Cell($w[0], 6, "", 'LR', 0, 'L', $fill);

                        // Direccion o en blanco
                        if( $n == 1 )      $this->Cell($w[1], 6, $address, 'LR', 0, 'L', $fill);   
                        else if( $n == 2 ) $this->Cell($w[1], 6, $telefonos, 'LR', 0, 'L', $fill); 
                            else           $this->Cell($w[1], 6, "", 'LR', 0, 'L', $fill);

                        $this->Cell($w[2], 6, "", 'LR', 0, 'L', $fill);
                        $this->Cell($w[3], 6, $part, 'LR', 0, 'L', $fill);
                        $this->Ln();
                     }

                  }
                  else {
                     $this->Cell($w[3], 6, $observa, 'LR', 0, 'L', $fill);
                     $this->Ln();

                     if( $address != "" ) {
                        $this->Cell($w[0], 6, "", 'LR', 0, 'L', $fill);
                        $this->Cell($w[1], 6, $address, 'LR', 0, 'L', $fill);
                        $this->Ln();
                     }

                     if( $telefonos != "" ) {
                        $this->Cell($w[0], 6, "", 'LR', 0, 'L', $fill);
                        $this->Cell($w[1], 6, $telefonos, 'LR', 0, 'L', $fill);
                        $this->Ln();
                     }
                  }
                  $this->Ln();
                  $fill=!$fill;
                  ++$count;

               }
            }            
            else {
               $this->Cell($w[$i], 7, "No se registran reclamos", 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill
            }
            if( $result && is_resource( $result ) ) mysqli_free_result( $result );


            $this->SetFont('Courier','B',10);
            $this->SetY( $this->GetY() + 5 );

            $senial = utf8_encode( "Seņal" );
            $this->Cell($w[0], 6, "ONUs con Baja " . $senial, 'LR', 0, 'L', $fill);
            $this->SetY( $this->GetY() + 8 );
            $this->SetFont('Courier','B',8);

            $sql = "select sql_no_cache *, ag_signalfo.mac as onu_mac from ag_signalfo left join ag_users on ag_users.username = ag_signalfo.mac where ag_signalfo.mac <> '' and onu_signal > '-23' and onu_signal  < '-38' and onu_signal <> 'N/A' $filter order by onu_signal desc";
            $result = $conn->query($sql);
            if ($result->num_rows > 0) {

               // Header
               $header = array('Cliente', 'Direccion', $senial );
               $w = array(70, 80, 30);
               $num_headers = count($header);
               for($i = 0; $i < $num_headers; ++$i) {
                  $this->Cell($w[$i], 7, $header[$i], 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill
               }
               $this->Ln();
               $this->SetFont('Courier','',8);

               $x = 0;
               while( $row = $result->fetch_assoc() ) {

                  $mac = $row["onu_mac"];
                  $intface = $row["interface"];
                  $signal = $row["onu_signal"];
                  $data = explode("(dbm)",$signal);
                  $customer = "";
                  $name = "";
   
                  $lastname = $row["lastname"];
                  $firstname = $row["firstname"];
                  $address = $row["address"];
                  $customer = $row["customer"];
                  $name = $lastname . " " . $firstname;
   
                  $num = ($Offset + $n + 1);
                  $color = "";

                  if(intval($data[0])  < -23 ) {
                     $part = substr($observa,0,40);
                     $this->Cell($w[0], 6, $name, 'LR', 0, 'L', $fill);
                     $this->Cell($w[1], 6, $address, 'LR', 0, 'L', $fill);
                     $this->Cell($w[2], 6, $signal, 'LR', 0, 'L', $fill);
                     $this->Ln();
                  }
               }
            }
            else {
               $this->Cell($w[$i], 7, "No se registran onus con problema", 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill
            }
            break;

         case "altas":
         /*
            // Header
            $header = array('Cliente', 'Nombre', 'Servicio Instalado', 'Fecha');
            $w = array(15, 70, 70, 25);
            $num_headers = count($header);
            for($i = 0; $i < $num_headers; ++$i) {
               $this->Cell($w[$i], 7, $header[$i], 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill
            }
            $this->Ln();
            */
      
      
            // Color and font restoration
            /*
            $this->SetFillColor(224, 235, 255);
            $this->SetTextColor(0);
            $this->SetFont('');
            */
      
            // Data
      
            switch( $mode ) {
               case "daily":
                  $sql = "select sql_no_cache * from ag_users left join rm_services on rm_services.srvid = ag_users.srvid left join cooperativa_clientes on cooperativa_clientes.servicio = ag_users.customer where situacion='1' and borrado=0 and instalacion>=date_sub(date(now()),interval 1 day) order by concat(year(instalacion),month(instalacion)),lastname, firstname";
                  break;
      
               default:
                  $sql = "select sql_no_cache * from ag_users left join rm_services on rm_services.srvid = ag_users.srvid left join cooperativa_clientes on cooperativa_clientes.servicio = ag_users.customer where situacion='1' and borrado=0 and instalacion>=date_sub(now(),interval 1 month) order by concat(year(instalacion),month(instalacion)),instalacion,lastname, firstname";
                  $sql = "select sql_no_cache * from ag_users left join rm_services on rm_services.srvid = ag_users.srvid left join cooperativa_clientes on cooperativa_clientes.servicio = ag_users.customer where situacion='1' and borrado=0 and month(instalacion)=month(now()) and year( instalacion ) = year(now()) order by instalacion,lastname, firstname";
                  break;
                 
            }
            $result = $conn->query($sql);
            $count = 0;

            $this->SetFont('Courier','B',10);
            $this->SetY( $this->GetY() + 5 );
            $this->Cell($w[0], 6, "Altas", 'LR', 0, 'L', $fill);
            $this->SetY( $this->GetY() + 8 );
            $this->SetFont('Courier','B',8);

            if ($result->num_rows > 0) {


               // Header
               $header = array('Cliente', 'Nombre', 'Servicio Instalado', 'Fecha');
               $w = array(15, 70, 70, 25);
               $num_headers = count($header);
               for($i = 0; $i < $num_headers; ++$i) {
                  $this->Cell($w[$i], 7, $header[$i], 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill
               }
               $this->Ln();
               $this->SetFont('Courier','',8);

               while( $row = $result->fetch_assoc() ) {
                  $customer = $row["customer"];
                  $fecha_instalacion = $row["instalacion"];
                  if( $fecha_instalacion == "0000-00-00" ) $instalado = "";
                  else $instalado = date("d-m-Y", strtotime( $row["instalacion"] ) );  
      
                  $nombre = $row["lastname"] . " " . $row["firstname"];
                  $direccion = $row["address"] . " " . $row["calle_num"];
                  $srvname = $row["srvname"];
      
                  $nombre = utf8_encode( $nombre );
                  $nombre = ucwords( strtolower ( $nombre ) );
                  $direccion = utf8_encode( $direccion );
      
                  if( $mobile == "" && $phone != "" ) $telefonos = $phone;  
                  else if( $mobile != "" && $phone == "" ) $telefonos = $mobile;  
                  else $telefonos = "$mobile / $phone"; 
               
                  $fill = 0;
      
                  // Celdas
                  $this->Cell($w[0], 6, $customer, 'LR', 0, 'L', $fill);
                  $this->Cell($w[1], 6, $nombre, 'LR', 0, 'L', $fill);
                  $this->Cell($w[2], 6, $srvname, 'LR', 0, 'L', $fill);
                  $this->Cell($w[3], 6, $instalado, 'LR', 0, 'L', $fill);
                  $this->Ln();
                  $fill=!$fill;
                  ++$count;
               } 
            }
            else {
               $this->Cell($w[$i], 7, "No se registran altas en este periodo", 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill
            }

      //      $this->Cell(array_sum($w), 0, '', 'T');
            if( $result && is_resource( $result ) ) mysqli_free_result( $result );


            // Upgrades
            switch( $mode ) {
               case "daily":
                  $sql = "select sql_no_cache * from ag_tareas left join ag_users on ag_users.customer = ag_tareas.id_customer left join rm_services on rm_services.srvid = ag_users.srvid where situacion='1' and borrado=0 and fecha >=date_sub(date(now()),interval 1 day) and Tipo = 115 order by fecha,lastname, firstname";
                  break;
      
               default:
                  $sql = "select sql_no_cache * from ag_tareas left join ag_users on ag_users.customer = ag_tareas.id_customer left join rm_services on rm_services.srvid = ag_tareas.srvid where situacion='1' and borrado=0 and month(fecha)=month(now()) and year( fecha) = year(now()) and Tipo = 115 order by fecha,lastname, firstname";
                  break;
                 
            }
            $result = $conn->query($sql);
            $count = 0;

            $this->SetFont('Courier','B',10);
            $this->SetY( $this->GetY() + 5 );
            $this->Cell($w[0], 6, "Upgrades", 'LR', 0, 'L', $fill);
            $this->SetY( $this->GetY() + 8 );
            $this->SetFont('Courier','B',8);

            if ($result->num_rows > 0) {


               // Header
               $header = array('Cliente', 'Nombre', 'Servicio Instalado', 'Fecha');
               $w = array(15, 70, 70, 25);
               $num_headers = count($header);
               for($i = 0; $i < $num_headers; ++$i) {
                  $this->Cell($w[$i], 7, $header[$i], 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill
               }
               $this->Ln();
               $this->SetFont('Courier','',8);

               while( $row = $result->fetch_assoc() ) {
                  $customer = $row["customer"];
                  $fecha_instalacion = $row["Fecha"];
                  if( $fecha_instalacion == "0000-00-00" ) $instalado = "";
                  else $instalado = date("d-m-Y", strtotime( $row["Fecha"] ) );  
      
                  $nombre = $row["lastname"] . " " . $row["firstname"];
                  $direccion = $row["address"] . " " . $row["calle_num"];
                  $srvname = $row["srvname"];
      
                  $nombre = utf8_encode( $nombre );
                  $nombre = ucwords( strtolower ( $nombre ) );
                  $direccion = utf8_encode( $direccion );
      
                  if( $mobile == "" && $phone != "" ) $telefonos = $phone;  
                  else if( $mobile != "" && $phone == "" ) $telefonos = $mobile;  
                  else $telefonos = "$mobile / $phone"; 
               
                  $fill = 0;
      
                  // Celdas
                  $this->Cell($w[0], 6, $customer, 'LR', 0, 'L', $fill);
                  $this->Cell($w[1], 6, $nombre, 'LR', 0, 'L', $fill);
                  $this->Cell($w[2], 6, $srvname, 'LR', 0, 'L', $fill);
                  $this->Cell($w[3], 6, $instalado, 'LR', 0, 'L', $fill);
                  $this->Ln();
                  $fill=!$fill;
                  ++$count;
               } 
            }
            else {
               $this->Cell($w[$i], 7, "No se registran upgrades en este periodo", 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill
            }

      //      $this->Cell(array_sum($w), 0, '', 'T');
            if( $result && is_resource( $result ) ) mysqli_free_result( $result );


            // Bajas
            switch( $mode ) {
               case "daily":
                  $sql = "select sql_no_cache * from ag_users left join rm_services on rm_services.srvid = ag_users.srvid left join cooperativa_clientes on cooperativa_clientes.servicio = ag_users.customer where situacion='1' and borrado=0 and instalacion>=date_sub(now(),interval 1 day) order by concat(year(instalacion),month(instalacion)),lastname, firstname";
                  break;
      
               default:
                  $sql = "select sql_no_cache * from ag_users left join rm_services on rm_services.srvid = ag_users.srvid left join cooperativa_clientes on cooperativa_clientes.servicio = ag_users.customer where situacion='1' and borrado=0 and instalacion>=date_sub(now(),interval 1 month) order by concat(year(instalacion),month(instalacion)),instalacion,lastname, firstname";
                  $sql = "select sql_no_cache * from ag_users left join rm_services on rm_services.srvid = ag_users.srvid left join cooperativa_clientes on cooperativa_clientes.servicio = ag_users.customer where month(fecha_baja)=month(now()) and year( fecha_baja ) = year(now()) order by instalacion,lastname, firstname";
                  break;
                 
            }

            $result = $conn->query($sql);

            $this->SetFont('Courier','B',10);
            $this->SetY( $this->GetY() + 10 );
            $this->Cell($w[0], 6, "Bajas", 'LR', 0, 'L', $fill);
            $this->SetY( $this->GetY() + 8 );
            $this->SetFont('Courier','B',8);

            if ($result->num_rows > 0) {


               // Header
               $header = array('Cliente', 'Nombre', 'Servicio Instalado', 'Fecha Baja');
               $w = array(15, 70, 70, 25);
               $num_headers = count($header);
               for($i = 0; $i < $num_headers; ++$i) {
                  $this->Cell($w[$i], 7, $header[$i], 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill
               }
               $this->Ln();
               $this->SetFont('Courier','',8);

               while( $row = $result->fetch_assoc() ) {
                  $customer = $row["customer"];
                  $fecha_instalacion = $row["fecha_baja"];
                  if( $fecha_instalacion == "0000-00-00" ) $instalado = "";
                  else $instalado = date("d-m-Y", strtotime( $row["fecha_baja"] ) );  
      
                  $nombre = $row["lastname"] . " " . $row["firstname"];
                  $direccion = $row["address"] . " " . $row["calle_num"];
                  $srvname = $row["srvname"];
      
                  $nombre = utf8_encode( $nombre );
                  $nombre = ucwords( strtolower ( $nombre ) );
                  $direccion = utf8_encode( $direccion );
      
                  if( $mobile == "" && $phone != "" ) $telefonos = $phone;  
                  else if( $mobile != "" && $phone == "" ) $telefonos = $mobile;  
                  else $telefonos = "$mobile / $phone"; 
               
                  $fill = 0;
      
                  // Celdas
                  $this->Cell($w[0], 6, $customer, 'LR', 0, 'L', $fill);
                  $this->Cell($w[1], 6, $nombre, 'LR', 0, 'L', $fill);
                  $this->Cell($w[2], 6, $srvname, 'LR', 0, 'L', $fill);
                  $this->Cell($w[3], 6, $instalado, 'LR', 0, 'L', $fill);
                  $this->Ln();
                  $fill=!$fill;
                  ++$count;
               } 
            }
            else {
               $this->Cell($w[$i], 7, "No se registran bajas en este periodo", 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill

            }
      //      $this->Cell(array_sum($w), 0, '', 'T');
            if( $result && is_resource( $result ) ) mysqli_free_result( $result );
            break;

         case "diario":
            // Header
            $header = array('Nro.', 'Fecha', 'Cuenta', 'Concepto', 'Debe', 'Haber');
            $w = array(14,55,60,60);
            $num_headers = count($header);
            for($i = 0; $i < $num_headers; ++$i) {
               $this->Cell($w[$i], 7, $header[$i], 0, 0, 'L', 1);    // Width, Height, Text, Border, ??, Align. Fill
            }
            $this->Ln();
      
            // Data
            $this->SetFont('Courier','',8);
      
//          $sql = "select sql_no_cache * from ag_users left join rm_services on rm_services.srvid = ag_users.srvid left join cooperativa_clientes on cooperativa_clientes.servicio = ag_users.customer where situacion='1' and borrado=0 and instalacion>=date_sub(now(),interval 1 month) order by concat(year(instalacion),month(instalacion)),instalacion,lastname, firstname";
            $sql = "select sql_no_cache * from ag_users left join rm_services on rm_services.srvid = ag_users.srvid left join cooperativa_clientes on cooperativa_clientes.servicio = ag_users.customer where situacion='1' and borrado=0 order by lastname, firstname";
            $result = $conn->query($sql);
            $count = 0;
            if ($result->num_rows > 0) {
               while( $row = $result->fetch_assoc() ) {

                  $customer = $row["customer"];
                  $fecha_instalacion = $row["instalacion"];
                  if( $fecha_instalacion == "0000-00-00" ) $instalado = "";
                  else $instalado = date("d-m-Y", strtotime( $row["instalacion"] ) );  
      
                  $nombre = $row["lastname"] . " " . $row["firstname"];
                  $direccion = $row["address"] . " " . $row["calle_num"];
                  $srvname = $row["srvname"];
                  $srvcoope = $row["id_tar_base"];
      
                  $nombre = utf8_encode( $nombre );
                  $nombre = ucwords( strtolower ( $nombre ) );
                  $direccion = utf8_encode( $direccion );
      
                  if( $mobile == "" && $phone != "" ) $telefonos = $phone;  
                  else if( $mobile != "" && $phone == "" ) $telefonos = $mobile;  
                  else $telefonos = "$mobile / $phone"; 
               
                  // Verifico los conceptos que se le factura al cliente
                  $sql1 = "select sql_no_cache * from cooperativa_conceptos where servicio='$customer'";
                  $result1 = $conn->query($sql1);
                  if ($result1->num_rows > 0 ) {
                     $id_concepto = "";
                     $bad = true;
         
                     while( $row1 = $result1->fetch_assoc() ) {
                        $concepto = $row1["concepto"];   // Concepto a facturar del cliente
                        $bypass = false;
                        $concepto = $row1["concepto"];   // Concepto a facturar del cliente
                        switch( $concepto ) {
                           case "70":       // Instalacion 2 cuotas
                           case "10":       // IVA 21
                           case "20":       // Servicios Sociales
                           case "25":
                           case "100":      // Instalacion cuotas
                           case "902":      // Cargo de reconexion
                              $bypass = true;
                              break;
         
                           default:
                              // Veo si la conversion existe   
                              $bypass = false;
                              $descripcion_concepto = $row1["descripcion"];   // Concepto a facturar del cliente
                              $id_concepto = $row1["concepto"];   // Concepto a facturar del cliente
         
                              $sql2 = "select sql_no_cache * from cooperativa_conversion_recursos where srvid=$servicio and recurso=$concepto";
                              $result2 = $conn->query($sql2);
                              if ($result2->num_rows > 0 ) {
                                 // El recurso corresponde
                                 $bad = false;
                              }
                              else {
                                 // No esta apareado
                                 $bad = true;
                              }
                              break;
                        }
                        
                        if( $bypass == false ) {
            
                           if( $bad == true ) {
                              $conError++;
                           }
         
                           $fill = 0;
      
                           // Celdas
                           $this->Cell($w[0], 6, $customer, 'LR', 0, 'L', $fill);
                           $this->Cell($w[1], 6, $nombre, 'LR', 0, 'L', $fill);
                           $this->Cell($w[2], 6, substr( $srvname,0,32 ) , 'LR', 0, 'L', $fill);
                           $this->Cell($w[3], 6, substr( $descripcion_concepto,0,32), 'LR', 0, 'L', $fill);
                           $this->Ln();
                           $fill=!$fill;
                           ++$count;
                           $found = true;
                        }
                     }
                     if( $found == false ) {

                        $fill = 0;
      
                        // Celdas
                        $this->Cell($w[0], 6, $customer, 'LR', 0, 'L', $fill);
                        $this->Cell($w[1], 6, $nombre, 'LR', 0, 'L', $fill);
                        $this->Cell($w[2], 6, $srvname, 'LR', 0, 'L', $fill);
                        $this->Cell($w[3], 6, " ", 'LR', 0, 'L', $fill);
                        $this->Ln();
                        $fill=!$fill;
                     }
                  }
                  if( $result1 && is_resource( $result1 ) ) mysqli_free_result( $result1 );
               } 
            }
            if( $result && is_resource( $result ) ) mysqli_free_result( $result );
            break;
      }
      return $count;
   }
}

// create new PDF document

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//                      P                 mm          A$


// print colored table
switch( $informe ) {
   case "altas":
      $title = 'Informe de Altas, Bajas & Upgrades';
      $filename = "altas.pdf";
      break;

   case "diario":
      $title = 'Servicios Interactivos SRL';
      $filename = "diario.pdf";
      break;

   case "reclamos":
      $title = 'Informe de Reclamos';
      $filename = "reclamos.pdf";
      break;
}

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Servicios Interactivos');
$pdf->SetTitle($title);
$pdf->SetSubject('Servicio de Internet');
$pdf->SetKeywords('diario');

// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 011', PDF_HEADER_STRING);
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 011', PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/tcpdf/lang/spa.php')) {
   require_once(dirname(__FILE__).'/tcpdf/lang/spa.php');
   $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', '', 12);

// add a page
$pdf->AddPage();

// column titles
$header = array('Nro.', 'Fecha', 'Cuenta', 'Concepto', 'Debe', 'Haber');

// data loading
//$data = $pdf->LoadData('tcpdf/data/table_data_demo.txt');


$n = $pdf->ColoredTable($header, $data, $conn, $mode, $informe );

// ---------------------------------------------------------

// close and output PDF document
   switch( $output ) {
      case 'browser':
         $pdf->Output($filename, 'I');
         break;

      case 'email':
         if( intval( $n ) > 0 ) {
            $pdf->Output('/var/www/html/impuestos/pdf/'. $filename, 'F');
            $errorText = "{ \"errno\": 0, \"count\": \"" . $n . "\" }";
         }
         echo $errorText;
         break;
   }

mysqli_close($conn);


//============================================================+
// END OF FILE
//============================================================+
