<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.2
 *
 * Copyright (C) 2006-2015  Sylvain FATOME
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program
 * (in the directory docs);
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Contact : galawa@free.fr
 *
 * Web : http://smi.no-ip.org
 ****************************************************************************************/


// Vérification des tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], "inc.php");
if ($pos === FALSE)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");

include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_mque.inc.php");
include_once("../inc/fic_pdt.inc.php");
include_once("../inc/fic_prm.inc.php");
include_once("../inc/fic_rdv.inc.php");
include_once("../inc/fic_rel.inc.php");
include_once("../inc/fic_sig.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_statut.inc.php");
include_once("../inc/fic_tec.inc.php");
include_once("../inc/fic_tra.inc.php");
include_once("../inc/fic_txt.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_time.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../classes/ezSQL/shared/ez_sql_core.php");
include_once("../classes/ezSQL/mysqli/ez_sql_mysqli.php");
include_once("../inc/func_sql.inc.php");
Archivelog(@$_SESSION["code_log"]);
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$pbm_etat = FALSE;

$cr = $_GET['cr']; // Récupération code fiche Relevé
$te = $_GET['te']; // Récupération type Etat (m : matériel, l : licence)
if (($te <> "m") AND ($te <> "l"))
    $pbm_etat = TRUE;
$pbm = intval($_GET['pbm']); // Récupération type Relevé (0 : sans détail problèmes, 1 : avec détail problèmes)

if ($pbm_etat == FALSE) {
    if ($cr <> "") {
        switch ($te) {
            case "m" :
                // Récupération des informations fiche Relevé produit
                $row = lit_enr_rpdt($cr);
                if (is_array($row)) {
                    $row = encode_str($row);
                    extract($row);

                    // Récupération des données client
                    $row = lit_enr_cli($rpdt_codecli, TRUE);
                    if (is_array($row))
                        extract($row);
                    else
                        $pbm_etat = TRUE;
                } else
                    $pbm_etat = TRUE;
                break;
            case "l" :
                // Récupération des informations fiche Relevé licence
                $row = lit_enr_rlic($cr);
                if (is_array($row)) {
                    $row = encode_str($row);
                    extract($row);

                    // Récupération des données client
                    $row = lit_enr_cli($rlic_codecli, TRUE);
                    if (is_array($row)) {
                        $row = encode_str($row);
                        extract($row);
                    } else
                        $pbm_etat = TRUE;
                } else
                    $pbm_etat = TRUE;
                break;
        }

        if ($pbm_etat == FALSE) {
            include_once('../classes/tcpdf/config/tcpdf_config.php');
            include_once('../classes/tcpdf/tcpdf.php');
            include_once('../inc/tcpdf_ext.inc.php');

            $pdf = new PDF_SMI('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
            $pdf->setFontLng('fr');
            $pdf->set_add_margin();
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor('SMI');

            switch ($te) {
                case "m" :
                    $pdf->SetTitle(_("Dossier N°") . $rpdt_code);
                    $pdf->SetSubject(_("Dossier N°") . $rpdt_code);
                    $pdf->settgen();
                    $pdf->settheader("rpdt");
                    break;
                case "l" :
                    $pdf->SetTitle(_("Dossier N°") . $rlic_code);
                    $pdf->SetSubject(_("Dossier N°") . $rlic_code);
                    $pdf->settgen();
                    $pdf->settheader("rlic");
                    break;
            }

            // 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);

            $pdf->SetLeftMargin(10);
            $pdf->SetLineWidth(0.2);
            $pdf->AddPage();

            // Récupération Coordonnées sous entête
            $Pos_Entete = $pdf->GetY();

            // Impression du détail client
            $pdf->SetFont('', 'BU', 12); // Titre
            $pdf->Cell(138, 5, _("CLIENT"), 0, 1, 'C');
            $ent_cli = ""; // Détail
            if ($cli_ste <> "")
                $ent_cli .= $cli_ste . "\n";
            else
                $ent_cli .= $cli_civilite . " " . $cli_prenom . " " . $cli_nom . "\n";
            $pdf->SetFont('', 'B', 10);
            $pdf->MultiCell(138, 4, $ent_cli, 0, 'L');

            $ent_cli = $cli_adr1 . "\n"; // Suite...
            if (trim($cli_adr2) <> "")
                $ent_cli .= $cli_adr2 . "\n";
            $ent_cli .= Retournecp_Id($cli_ville, TRUE) . " " . Retourneville_Id($cli_ville, TRUE) . "\n";
            $ent_cli .= _("Tel") . ' : ' . $cli_telf . "\n";
            $ent_cli .= _("Fax") . ' : ' . $cli_fax;
            if (trim($cli_tvai) <> "")
                $ent_cli .= "\n" . _("TVA") . ' : ' . $cli_tvai;
            $pdf->SetFont('', '', 10);
            $pdf->MultiCell(138, 4, $ent_cli, 0, 'L');

            // Récupération Coordonnées
            $Pos_EntCli = $pdf->GetY();

            // Impression du détail Relevé
            $pdf->SetY($Pos_Entete + 1); // Renvoie le pointeur SOUS l'entête agence

            $pdf->Cell(138);

            $pdf->SetFont('', '', 8);
            $ent_rel = _("Dossier Relevé") . " :\n";
            $pdf->MultiCell(30, 4, $ent_rel, 0, 'R');

            $pdf->SetY($Pos_Entete + 1); // Renvoie le pointeur SOUS l'entête agence + 5mm (cellule niveau urgence)
            $pdf->Cell(168);

            $pdf->SetFont('', '', 8);
            if ($te == "m")
                $ent_rel = $rpdt_coderel . "\n";
            else
                $ent_rel = $rlic_coderel . "\n";
            $pdf->MultiCell(109, 4, $ent_rel, 0, 'L');

            // Récupération Coordonnées
            $Pos_EntDetail = $pdf->GetY();

            $PosY = max($Pos_EntCli, $Pos_EntDetail);
            $pdf->SetY($PosY);
            $pdf->ln(1);
            $pdf->SetDrawColor(125);
            $pdf->SetLineWidth(0.4);
            $pdf->SetLineStyle(['dash' => 2]);
            $pdf->Line(10, $PosY, 200 + $pdf->add_margin, $PosY);
            $pdf->SetDrawColor(0);
            $pdf->SetLineWidth(0.2);
            $pdf->SetDrawColor(220);
            $pdf->SetLineStyle(['dash' => 0]);
            $pdf->ln(1);

            $pdf->Ln(2);
            $PosY = $pdf->GetY();
            $pdf->Line(10, $PosY, 287, $PosY);
            $pdf->SetDrawColor(0);
            $pdf->SetLineWidth(0.2);

            switch ($te) {
                /************************************/
                /* Impression de la fiche Matériel  */
                /************************************/
                case "m" :
                    include_once("../inc/rel_pdf_pdt.inc.php");

                    $url_pdf = $url_gfc . "/pj/" . _("releve_materiel").'_'.$rpdt_code;
                    $pdf->Output($url_pdf, 'FI');
                    @unlink($url_pdf);
                    break;

                /**********************************/
                /* Impression de la fiche licence */
                /**********************************/
                case "l" :
                    include_once("../inc/rel_pdf_lic.inc.php");

                    $url_pdf = $url_gfc . "/pj/" . _("releve_licence").'_'.$rlic_code;
                    $pdf->Output($url_pdf, 'FI');
                    @unlink($url_pdf);
                    break;
            }
        }
    }
}
