<?php require 'funcs/conexion.php';
require 'funcs/funcs.php';
require('fpdf/fpdf.php');
date_default_timezone_set('America/Mexico_City');
header("Content-Type: text/html;charset=utf-8");


if (!empty($_GET)) {
    $fi = $_GET['fi'];
    $ff = $_GET['ff'];
    $sql = "SELECT * FROM `ie_detalle`,ie_concepto WHERE `ie_det_fecha` BETWEEN '$fi' and '$ff' and `ie_det_activo` and `ie_det_con_id` = `ie_con_id` = 1 order by ie_det_fecha";
    $resultado = mysqli_query($mysqli, $sql);
    class PDF extends FPDF
    {

        function Header()
        {
            //$this->Image("img/fi.jpg", 5, null, 200,30);
            $this->Image('img/logo.png', 150, 5, 40);
            $this->Image('img/fondo.png', 10, 160, 180, 90);

            $this->Ln(20);
        }
        function Footer()
        {
            $this->SetXY(120, 238);
            $this->SetFont('Arial', 'B', 10);
            $this->Cell(13, 30, utf8_decode('Dra. Nelly Eugenia Robles Luján. C.P. 4412680'), 0, 0, 'L');

            //Go to 1.5 cm from bottom
            $this->SetY(-40);
            //this->Image('img/fi.png', 5, 270, 200 );     


            $this->Image("img/fi.jpg", 5, null, 200, 30);
            //Select Arial italic 8
            $this->SetFont('Arial', 'I', 8);
            //Print current and total page numbers
            $this->Cell(0, 10, utf8_decode('Página ') . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }
    }

    $pdf = new PDF('P', 'mm', 'A4');

    $pdf->AliasNbPages();
    $pdf->AddPage();
    $pdf->SetAutoPageBreak(TRUE, 44);
    $pdf->SetXY(10, 5);
    $pdf->SetFont('Arial', 'B', 18);
    $pdf->Cell(45, 30, utf8_decode('Balance: '), 0, 0, 'L');
    $pdf->SetXY(10, 15);
    $pdf->SetFont('Arial', 'B', 12);
    $fi = date_create($fi);
    $fi= date_format($fi, "d/m/Y");
    $ff = date_create($ff);
    $ff= date_format($ff, "d/m/Y");
    $pdf->Cell(50, 30, 'Periodo: del ' . $fi . ' al ' . $ff, 0, 1, 'L');
    $pdf->SetXY(10, 45);
    $pdf->SetFont('Arial', 'B', 14);
    $pdf->Cell(7, 10, 'N', 1, 0, 'L');
    $pdf->Cell(23, 10, 'FECHA', 1, 0, 'L');
    $pdf->Cell(50, 10, 'CONCEPTO', 1, 0, 'L');
    $pdf->Cell(50, 10, 'DESCRIPCION', 1, 0, 'L');
    $pdf->Cell(30, 10, 'INGRESO $', 1, 0, 'L');
    $pdf->Cell(30, 10, 'EGRESO $', 1, 1, 'L');
    $pdf->SetFont('Arial', '', 12);
    $n = 0;
    $L = 0;
    while ($row = mysqli_fetch_array($resultado)) {
        $l = $l + 1;
        $pdf->Cell(7, 10, $l, 1, 0, 'L');
        $fechaie = date_create($row['ie_det_fecha']);
        $fechaie = date_format($fechaie, "d/m/Y");
        $pdf->Cell(23, 10, $fechaie, 1, 0, 'L');
        $pdf->Cell(50, 10, $row['ie_con_descripcion'], 1, 0, 'L');
        $pdf->Cell(50, 10, $row['ie_det_descripcion'], 1, 0, 'L');
        $ingreso = '';
        $egreso = '';
        if ($row['ie_con_tipo'] == 'Egreso') {
            $egreso = $row['ie_det_importe'];
            $totalegreso = $totalegreso + $egreso;
        }
        if ($row['ie_con_tipo'] == 'Ingreso') {
            $ingreso = $row['ie_det_importe'];
            $totalingreso = $totalingreso + $ingreso;
        }
        $pdf->Cell(30, 10, $ingreso, 1, 0, 'R');
        $pdf->Cell(30, 10, $egreso, 1, 1, 'R');
    }


    $pdf->SetFont('Arial', 'B', 14);
    $pdf->Cell(130, 10, 'TOTAL', 1, 0, 'L');
    $pdf->Cell(30, 10, '$ '.$totalingreso.'.00', 1, 0, 'R');
    $pdf->Cell(30, 10, '$ '.$totalegreso.'.00', 1, 1, 'R');

    $pdf->SetFont('Arial', 'B', 14);
    $pdf->Cell(130, 10, 'CAJA', 1, 0, 'L');
    $total=$totalingreso-$totalegreso;
    $pdf->Cell(60, 10, '$ ' . $total . '.00', 1, 1, 'R');


    $pdf->Output();
}
