<?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"]  : "iva_compras";
$Ejercicio = isset($_POST['id_ejercicio']) ? $_POST["id_ejercicio"]  : "28"; 
$Desde  = isset($_POST['desde']) ? $_POST["desde"]  : "01-07-2017";
$Hasta  = isset($_POST['hasta']) ? $_POST["hasta"]  : "30-06-2018";

if( empty($Desde) ) {        
   console.log( "Empty desde" );
   if( $_SESSION["Desde"] != "" ) $Desde = $_SESSION["Desde"];
   else $Desde = date("01-01-Y");   
}

if( empty($Hasta) ) {        
   console.log( "Empty hasta" );
   if( $_SESSION["Hasta"] != "" ) $Hasta = $_SESSION["Hasta"];
   else $Hasta = date("31-12-Y");   
}

$D = substr( $Desde, 6, 4 ) . "/" . substr( $Desde, 3, 2 ) . "/" . substr( $Desde, 0, 2 );
$H = substr( $Hasta, 6, 4 ) . "/" . substr( $Hasta, 3, 2 ) . "/" . substr( $Hasta, 0, 2 ); 
$Total_Debe = 0;
$Total_Haber = 0;
$Subtotal = 0;
$Total_Iva = 0;
$Total_Neto = 0;
$first_page = true;

//$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() {

        global $informe;
        global $first_page;
        global $Total_Debe, $Total_Haber;

        // 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
        switch( $informe ) {
            case "diario":
               $this->SetY(20);
               $this->Cell(0, 10, $this->title, 0, false, 'C', 0, '', 0, false, 'M', 'M');


               $header = array('Nro.', 'Fecha', 'Cuenta', 'Concepto', 'Debe', 'Haber');
               $align = array('L', 'L', 'L', 'L', 'R', 'R');
               
               $this->SetY(30);
               
               $w = array(14,20,20,70,30,30);
               $num_headers = count($header);
               $this->SetFont('helvetica', 'B', 8);
               for($i = 0; $i < $num_headers; ++$i) {
                  $this->Cell($w[$i], 7, $header[$i], '', 0, $align[$i], 0);    // Width, Height, Text, Border, ??, Align. Fill
               }         
               $this->Ln();

               if( $first_page == true ) {


               }
               else {
                  $this->Cell(40, 7, 'Transporte', '', 0, 'L', 0);    // Width, Height, Text, Border, ??, Align. Fill
                  $this->Cell(14, 7, '', '', 0, 'L', 0);  
                  $this->Cell(70, 7, '', '', 0, 'L', 0);  

                  $fDebe = number_format ( $Total_Debe, 2, ',' ,  '.' );
                  $fHaber = number_format ( $Total_Haber, 2, ',' ,  '.' );

                  $this->Cell(30, 6, $fDebe, '', 0, 'R', $fill);
                  $this->Cell(30, 6, $fHaber, '', 0, 'R', $fill);
                  $this->Ln();
               }
               //
               break;

         case "iva_ventas":            
         case "iva_compras":            
               $this->SetY(20);
               $this->Cell(0, 10, $this->title, 0, false, 'C', 0, '', 0, false, 'M', 'M');

               $header = array('Fecha', 'TC', 'Numero', 'CUIT', 'Nombre', 'Tasa', 'Subtotal', 'Importe IVA', 'Total Neto');
               $align = array('L', 'L', 'L', 'L', 'L', 'R', 'R', 'R', 'R');

               $this->SetY(30);

               $w = array(20,8,25,25,60,8,25,25,25);
               $num_headers = count($header);
               $this->SetFont('helvetica', 'B', 8);
               for($i = 0; $i < $num_headers; ++$i) {
                  $this->Cell($w[$i], 7, $header[$i], 0, 0, $align[$i], 0);    // Width, Height, Text, Border, ??, Align. Fill
               }
               $this->Ln();
               break;
         }
    }

    // Page footer
    public function Footer() {
                  
        global $Total_Debe, $Total_Haber;
        global $Subtotal;
        global $Total_Iva;
        global $Total_Neto;
        global $informe;


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

        // Position at 35 mm from bottom
        $this->SetY(-30);

         switch( $informe ) {
            case "iva_compras":
            case "iva_ventas":

               $w = array(20,8,25,25,60,8,25,25,25);
               $fill = 0;
               $this->Cell($w[0], 6, "Totales", '', 0, 'L', 0, '', 0, false, 'T', 'M');
               $this->Cell($w[1], 6, "", '', 0, 'L', 0, '', 0, false, 'T', 'M');
               $this->Cell($w[2], 6, "", '', 0, 'L', 0, '', 0, false, 'T', 'M');
               $this->Cell($w[3], 6, "", '', 0, 'L', 0, '', 0, false, 'T', 'M');
               $this->Cell($w[4], 6, "", '', 0, 'L', 0, '', 0, false, 'T', 'M');
               $this->Cell($w[5], 6, "", '', 0, 'L', 0, '', 0, false, 'T', 'M');

               $this->Cell($w[6], 6, number_format ( $Subtotal, 2, ',' ,  '.' ), '', 0, 'R', 0, '', 0, false, 'T', 'M');
               $this->Cell($w[7], 6, number_format ( $Total_Iva, 2, ',' ,  '.' ), '', 0, 'R', 0, '', 0, false, 'T', 'M');
               $this->Cell($w[8], 6, number_format ( $Total_Neto, 2, ',' ,  '.' ), '', 0, 'R', 0, '', 0, false, 'T', 'M');
               break;

            case "diario":
               $w = array(14,20,20,70,30,30);
               $fill = 0;

               $this->Cell($w[0], 6, "Totales", '', 0, 'L', 0, '', 0, false, 'T', 'M');
               $this->Cell($w[1], 6, "", '', 0, 'L', 0, '', 0, false, 'T', 'M');
               $this->Cell($w[2], 6, "", '', 0, 'L', 0, '', 0, false, 'T', 'M');
               $this->Cell($w[3], 6, "", '', 0, 'L', 0, '', 0, false, 'T', 'M');
               
               
               $fDebe = number_format ( $Total_Debe, 2, ',' ,  '.' );
               $fHaber = number_format ( $Total_Haber, 2, ',' ,  '.' );

               $this->Cell($w[4], 6, $fDebe, '', 0, 'R', $fill);
               $this->Cell($w[5], 6, $fHaber, '', 0, 'R', $fill);
               
//             $this->Ln();
               break;
      }

        // Position at 15 mm from bottom
       // Page number
//        $this->SetY(-15);
//        $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,$sql) {

      global $Total_Debe, $Total_Haber;
      global $Subtotal;
      global $Total_Iva;
      global $Total_Neto;
      global $first_page;

      $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 "iva_compras":
         case "iva_ventas":

            // Header
            $new = true;
            $first_page = true;

//            $header = array('Fecha', 'TC', 'Numero', 'CUIT', 'Nombre', 'Tasa', 'Subtotal', 'Importe IVA', 'Total Neto');
            $align = array('L', 'L', 'L', 'L', 'L', 'R', 'R', 'R', 'R');

            $w = array(20,8,25,25,60,8,25,25,25);
//            $num_headers = count($header);
//            for($i = 0; $i < $num_headers; ++$i) {
//               $this->Cell($w[$i], 7, $header[$i], 0, 0, $align[$i], 1);    // Width, Height, Text, Border, ??, Align. Fill
  //          }
      
            // Data
            $this->SetFont('Courier','',8);
      
            $result = $conn->query($sql);
            $count = 0;
            if ($result->num_rows > 0) {
               while( $row = $result->fetch_assoc() ) {

                  $op = $row["operacion"];
                  $Fecha = $row["fecha"];
                  $numero = $row["numero"];
                  $nombre = $row["nombre"];
                  $cuit = $row["cuit"];
                  $iva_105 = $row["iva_105"];
                  $iva_21 = $row["iva_21"];
                  $iva_27 = $row["iva_27"];
                  $iva_mt = $row["iva_MT"];
                  $subtotal = $iva_105 + $iva_21 + $iva_27 + $iva_mt;
                  $iva = $row["iva"];
                  $total = $row["importe"];

                  $Fecha = substr( $Fecha, 8, 2 ) . "-" . substr( $Fecha, 5, 2 ) . "-" . substr( $Fecha, 0, 4 ); 
                  $fill = 0;
                  if( $first_page == true ) {
                     $this->SetY(40);
                     $first_page = false;
                  }

                  $i_mt = floatval( $iva_mt );
                  $i_105 = floatval( $iva_105 );
                  $i_21 = floatval( $iva_21 ); 
                  $i_27 = floatval( $iva_27 ); 
                  $t = floatval( $total ); 
                  $iva_mt = number_format ( $iva_mt, 2, ',' ,  '.' ); 
                  $iva_27 = number_format ( $iva_27, 2, ',' ,  '.' ); 
                  $iva_105 = number_format ( $iva_105, 2, ',' ,  '.' ); 
                  $iva_21 = number_format ( $iva_21, 2, ',' ,  '.' ); 
                  $subtotal = number_format ( $subtotal, 2, ',' ,  '.' ); 
                  $iva = number_format ( $iva, 2, ',' ,  '.' ); 
                  $total = number_format ( $total, 2, ',' ,  '.' ); 

                  // Celdas
//                  $this->Ln();

                  $this->Cell($w[0], 6, $Fecha, '', 0, 'L', $fill);

                  if( $op == "1" )
                     $this->Cell($w[1], 6, "FC A", '', 0, 'L', $fill);
                  else if( $op == "6" ) $this->Cell($w[1], 6, "NC A", '', 0, 'L', $fill); 

                  $this->Cell($w[2], 6, $numero, '', 0, 'L', $fill);
                  $this->Cell($w[3], 6, $cuit, '', 0, 'L', $fill);
                  $this->Cell($w[4], 6, substr( $nombre,0,40), '', 0, 'L', $fill);

                  if( $i_mt != 0 ) {
                     $iva = 0;
                     $iva = number_format ( $iva, 2, ',' ,  '.' ); 

                     $this->Cell($w[5], 6, "21%", '', 0, 'R', $fill);
                     $this->Cell($w[6], 6, $iva_mt, '', 0, 'R', $fill);
                     $this->Cell($w[7], 6, $iva, '', 0, 'R', $fill);

                     $this->Cell($w[8], 6, $total, '', 0, 'R', $fill);
                     $this->Ln();
                  }

                  $s = $e = "";
                  if( $op == 6 ) {
                     $s = "(";
                     $e = ")";
                  }
                  if( $i_21 != 0 ) {
                     $iva = $i_21 * 21 / 100;
                     $iva = number_format ( $iva, 2, ',' ,  '.' ); 

                     $this->Cell($w[5], 6, "21%", '', 0, 'R', $fill);
                     $this->Cell($w[6], 6, $s . $iva_21 . $e, '', 0, 'R', $fill);
                     $this->Cell($w[7], 6, $s . $iva . $e, '', 0, 'R', $fill);

                     if( $i_27 == 0 && $i_105 == 0 )
                        $this->Cell($w[8], 6, $s . $total . $e, '', 0, 'R', $fill);
                     $this->Ln();

                     if( $i_27 != 0 ) {
                        $iva = $i_27 * 27 / 100;
                        $iva = number_format ( $iva, 2, ',' ,  '.' ); 

                        $this->Cell($w[0], 6, "", '', 0, 'L', $fill);
                        $this->Cell($w[1], 6, "", '', 0, 'L', $fill);
                        $this->Cell($w[2], 6, "", '', 0, 'L', $fill);
                        $this->Cell($w[3], 6, "", '', 0, 'L', $fill);
                        $this->Cell($w[4], 6, "", '', 0, 'L', $fill);
                        $this->Cell($w[5], 6, "27%", '', 0, 'R', $fill);
                        $this->Cell($w[6], 6, $s . $iva_27 . $e, '', 0, 'R', $fill);
                        $this->Cell($w[7], 6, $s . $iva . $e, '', 0, 'R', $fill);
                        if( $i_21 == 0 && $i_105 == 0 )
                           $this->Cell($w[8], 6, $s . $total . $e, '', 0, 'R', $fill);
                        $this->Ln();
                     }

                     if( $i_105 != 0 ) {
                        $iva = $i_105 * 10.5 / 100;
                        $iva = number_format ( $iva, 2, ',' ,  '.' ); 

                        $this->Cell($w[0], 6, "", '', 0, 'L', $fill);
                        $this->Cell($w[1], 6, "", '', 0, 'L', $fill);
                        $this->Cell($w[2], 6, "", '', 0, 'L', $fill);
                        $this->Cell($w[3], 6, "", '', 0, 'L', $fill);
                        $this->Cell($w[4], 6, "", '', 0, 'L', $fill);
                        $this->Cell($w[5], 6, "10.5%", '', 0, 'R', $fill);
                        $this->Cell($w[6], 6, $s . $iva_105 . $e, '', 0, 'R', $fill);
                        $this->Cell($w[7], 6, $s . $iva . $e, '', 0, 'R', $fill);
                        $this->Cell($w[8], 6, $s . $total . $e, '', 0, 'R', $fill);
                        $this->Ln();
                     }

                  }
                  else {
                     if( $i_27 != 0 ) {
                        $iva = $i_27 * 27 / 100;
                        $iva = number_format ( $iva, 2, ',' ,  '.' ); 

                        $this->Cell($w[5], 6, "27%", '', 0, 'R', $fill);
                        $this->Cell($w[6], 6, $s . $iva_27 . $e, '', 0, 'R', $fill);
                        $this->Cell($w[7], 6, $s . $iva . $e, '', 0, 'R', $fill);
                        $this->Cell($w[8], 6, $s . $total . $e, '', 0, 'R', $fill);
                        $this->Ln();

                        if( $i_105 != 0 ) {
                           $iva = $i_105 * 10.5 / 100;
                           $iva = number_format ( $iva, 2, ',' ,  '.' ); 

                           $this->Cell($w[0], 6, "", '', 0, 'L', $fill);
                           $this->Cell($w[1], 6, "", '', 0, 'L', $fill);
                           $this->Cell($w[2], 6, "", '', 0, 'L', $fill);
                           $this->Cell($w[3], 6, "", '', 0, 'L', $fill);
                           $this->Cell($w[4], 6, "", '', 0, 'L', $fill);
                           $this->Cell($w[5], 6, "10.5%", '', 0, 'R', $fill);
                           $this->Cell($w[6], 6, $s . $iva_105 . $e, '', 0, 'R', $fill);
                           $this->Cell($w[7], 6, $s . $iva . $e, '', 0, 'R', $fill);
                           $this->Cell($w[8], 6, $s . $total . $e, '', 0, 'R', $fill);
                           $this->Ln();
                        }
                     }
                     else {
                        if( $i_105 != 0 ) {
                           $iva = $i_105 * 10.5 / 100;
                           $iva = number_format ( $iva, 2, ',' ,  '.' ); 

                           $this->Cell($w[5], 6, "10.5%", '', 0, 'R', $fill);
                           $this->Cell($w[6], 6, $s . $iva_105 . $e, '', 0, 'R', $fill);
                           $this->Cell($w[7], 6, $s . $iva . $e, '', 0, 'R', $fill);
                           $this->Cell($w[8], 6, $s . $total . $e, '', 0, 'R', $fill);
                           $this->Ln();
                        }
                     }
                  }
                  
                  if( $op == 1 ) {
                     $Subtotal += $i_27 + $i_105 + $i_21;
                     $Total_Iva += $i_27 * 27 / 100 + $i_105 * 10.5 / 100 + $i_21 * 21 /100;
                     $Total_Neto += $t;
                  }
                  else {
                     $Subtotal -= ( $i_27 + $i_105 + $i_21 );
                     $Total_Iva -= ( $i_27 * 27 / 100 + $i_105 * 10.5 / 100 + $i_21 * 21 /100 );
                     $Total_Neto -= $t;
                  }
                  ++$count;
                  $found = true;
                  $first = false;
                  
               } 
            }
            if( $result && is_resource( $result ) ) mysql_free_result( $result );
            break;


         case "diario":
            // Header
            $new = true;
            $first_page = true;

            $header = array('Nro.', 'Fecha', 'Cuenta', 'Concepto', 'Debe', 'Haber');
            $align = array('L', 'L', 'L', 'L', 'R', 'R');

            $w = array(14,20,20,70,30,30);
            $num_headers = count($header);
//            for($i = 0; $i < $num_headers; ++$i) {
//               $this->Cell($w[$i], 7, $header[$i], 0, 0, $align[$i], 1);    // Width, Height, Text, Border, ??, Align. Fill
//            }
      
            // 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 asientos.Asiento_Ordenado, asientos.ordenado, asientos.fecha as _fecha, asientos.asiento as _asiento, movimientos.movimiento , movimientos.cuenta as c, cuentas.Denominacion as d, concepto, detalle, movimientos.debe as suma_debe, movimientos.haber as suma_haber from movimientos left join asientos on asientos.asiento=movimientos.asiento left join cuentas on cuentas.cuenta=movimientos.Cuenta where movimientos.Empresa=1 and asientos.fecha is not null and asientos.fecha >='" . $D . "' and asientos.Fecha <='" . $H . "' and movimientos.ejercicio='" . $Ejercicio . "' order by asientos.fecha, asientos.asiento, movimiento";
//            echo $sql;
            $result = $conn->query($sql);
            $count = 0;
            if ($result->num_rows > 0) {
               while( $row = $result->fetch_assoc() ) {

                  $cuenta = $row["c"];
                  $asiento = $row["_asiento"];
                  $asiento_ordenado = $row["Asiento_Ordenado"];
                  if( $asiento_ordenado == 0 ) $asiento_ordenado = $asiento;
                  $ordenado = $row["ordenado"];
                  $concepto = $row["concepto"];
                  $d = $row["d"];

                  if( $asiento != $ActualAsiento ) $new = true;
                  else $new = false;
                  $ActualAsiento = $asiento;

                  $Debe = $row["suma_debe"];
                  $Haber = $row["suma_haber"];
                  $Fecha = $row["_fecha"];
                  $Fecha = substr( $Fecha, 8, 2 ) . "-" . substr( $Fecha, 5, 2 ) . "-" . substr( $Fecha, 0, 4 ); 

                  $concepto = utf8_encode( $concepto );
//                $concepto = ucwords( strtolower ( $concepto ) );
                  $fill = 0;
      
                  if( $first_page == true ) {
                     $this->SetY(45);
                     $first_page = false;
                  }

                  // Celdas
                  if( $new == true ) {
                     
                     $this->Ln();
                     $this->Cell($w[0], 6, $asiento_ordenado, 'LR', 0, 'L', $fill);
                     $this->Cell($w[1], 6, $Fecha, 'LR', 0, 'L', $fill);
                     $this->Cell($w[2], 6, "", 'LR', 0, 'L', $fill);
                     $this->Cell($w[3], 6, substr( $concepto,0,32), 'LR', 0, 'L', $fill);
                     $this->Cell($w[4], 6, "", 'LR', 0, 'L', $fill);
                     $this->Cell($w[5], 6, "", 'LR', 0, 'L', $fill);
                     $this->Ln();
                  }
//                  else {
                     $this->Cell($w[0], 6, "", 'LR', 0, 'L', $fill);
                     $this->Cell($w[1], 6, "", 'LR', 0, 'L', $fill);
                     $this->Cell($w[2], 6, $cuenta, 'LR', 0, 'L', $fill);
                     $this->Cell($w[3], 6, substr( $d,0,32), 'LR', 0, 'L', $fill);

                     if( $Debe > 0 ) {
                        $fDebe = number_format ( $Debe, 2, ',' ,  '.' ); 
                        $this->Cell($w[4], 6, $fDebe, 'LR', 0, 'R', $fill);
                        $this->Cell($w[5], 6, "", 'LR', 0, 'R', $fill);
                     }
               
                     if( $Haber > 0 ) {
                        $fHaber = number_format ( $Haber, 2, ',' ,  '.' );
                        $this->Cell($w[4], 6, "", 'LR', 0, 'R', $fill);
                        $this->Cell($w[5], 6, $fHaber, 'LR', 0, 'R', $fill);
                     }
//                  }

                  $this->Ln();
                  $fill=!$fill;
                  $Total_Debe += $Debe;
                  $Total_Haber += $Haber;


                  ++$count;
                  
                  $found = true;
                  $first = false;
               } 
            }
            if( $result && is_resource( $result ) ) mysql_free_result( $result );
            break;
      }
      return $count;
   }
}

// create new PDF document

//                      P                 mm          A$


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

   case "iva_compras":            
      $year = intval( substr( $Desde, 6, 4 ) );
      $month = intval( substr( $Desde, 3, 2 ) );
      $pdf = new MYPDF("L", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
      $title = 'Servicios Interactivos SRL';
      $filename = "iva_compras.pdf";
      $sql = "select sql_no_cache comprobantes.id as idc, operacion, fecha, numero, Etiquetas, empresas.id as code_empresa, empresas.nombre, empresas.cuit, iva_105, iva_21, iva_27, iva_MT, ( iva_105 + iva_21 +iva_27 + iva_MT ) as sin_iva, iva, importe, retenciones, retenciones_iva, op_exentas, ( retenciones_iva + op_exentas + otras_retenciones ) as otras, importe, retenciones_ganancias from comprobantes left join empresas on empresas.id = comprobantes.cliente where year(fecha)=$year and month(fecha)=$month and comprobantes.Borrado=0 and operacion=2 order by fecha";
      break;

   case "iva_ventas":
      $year = intval( substr( $Desde, 6, 4 ) );
      $month = intval( substr( $Desde, 3, 2 ) );
      $pdf = new MYPDF("L", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
      $title = 'Servicios Interactivos SRL';
      $filename = "iva_ventas.pdf";
      $sql = "select sql_no_cache comprobantes.id as idc, operacion, fecha, numero, Etiquetas, empresas.id as code_empresa, empresas.nombre, empresas.cuit, iva_105, iva_21, iva_27, iva_MT, ( iva_105 + iva_21 +iva_27 + iva_MT ) as sin_iva, iva, importe, retenciones, retenciones_iva, op_exentas, ( retenciones_iva + op_exentas + otras_retenciones ) as otras, importe, retenciones_ganancias from comprobantes left join empresas on empresas.id = comprobantes.cliente where year(fecha)=$year and month(fecha)=$month and comprobantes.Borrado=0 and (operacion=1 or operacion=6 )order by fecha";
      break;

   case "diario":
      $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
      $title = 'Servicios Interactivos SRL';
      $filename = "diario.pdf";
      $sql = "select asientos.Asiento_Ordenado, asientos.ordenado, asientos.fecha as _fecha, asientos.asiento as _asiento, movimientos.movimiento , movimientos.cuenta as c, cuentas.Denominacion as d, concepto, detalle, movimientos.debe as suma_debe, movimientos.haber as suma_haber from movimientos left join asientos on asientos.asiento=movimientos.asiento left join cuentas on cuentas.cuenta=movimientos.Cuenta where movimientos.Empresa=1 and asientos.fecha is not null and asientos.fecha >='" . $D . "' and asientos.Fecha <='" . $H . "' and movimientos.ejercicio='" . $Ejercicio . "' order by asientos.fecha, asientos.asiento, movimiento";
      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);
//$pdf->SetFooterMargin(20);

$pdf->SetHeaderMargin();

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

// 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, $sql );

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

// 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
//============================================================+
