<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  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);

$cr = $_GET['cr']; // Récupération code Relevé
$pbm = intval($_GET['pbm']); // Récupération type Relevé (0 : sans détail problèmes, 1 : avec détail problèmes)

// On vérifie s'il y a un filtrage par site (si oui, on le récupère)
if (isset($_GET['site']))
    $idsite = intval($_GET['site']);
else
    $idsite = -1;

if ($cr <> "") {
    // Récupération des informations dossier Relevé
    $row = lit_enr_rel($cr);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
    }

    $rel_datecreaf = datetolocal($rel_datecrea);
    $rel_datemodf = datetolocal($rel_datemod);

    // Récupération des données agence
    $row = lit_enr_age($rel_codeage, TRUE);
    $row = encode_str($row);
    extract($row);

    $cp_age = Retournecp_Id($age_ville, TRUE);
    $ville_age = Retourneville_Id($age_ville, TRUE);

    // Récupération des données client
    $row = lit_enr_cli($rel_codecli, TRUE);
    $row = encode_str($row);
    extract($row);

    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');
    $pdf->SetTitle(_("Dossier N°") . $rel_code);
    $pdf->SetSubject(_("Dossier N°") . $rel_code);
    $pdf->settgen();
    $pdf->settheader("rel");

    // 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(95, 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(95, 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(95, 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 = _("Racine codage") . " :\n";
    $ent_rel .= _("Dernier N° machine") . " :\n";
    if ($idsite <> -1)
        $ent_rel .= _("Filtré par") . " :\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);
    $ent_rel = $rel_rnum . "\n";
    $ent_rel .= $rel_num . "\n";
    if ($idsite <> -1) {
        $tab_scli = explode("<br />", nom_scli($idsite, TRUE, FALSE, FALSE));
        foreach ($tab_scli as $key => $value) {
            if (trim($value) <> "")
                $ent_rel .= $value . "\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);

    //Notes
    $pdf->SetFont('', 'UI', 8);
    $pdf->MultiCell(277, 4, 'Notes :', '', 'L');

    $pdf->SetFont('', 'I', 8);
    $ent_rel = $rel_nota . "\n";

    $pdf->MultiCell(277, 4, $ent_rel, '', 'L');

    $pdf->SetDrawColor(125);
    $pdf->SetLineWidth(0.4);
    $PosY = $pdf->GetY();
    $pdf->Line(10, $PosY, 287, $PosY);


    /**********************************/
    /* Impression des fiches licences */
    /**********************************/

    $pdf->Ln(2);
    $pdf->SetFont('', 'BU', 12);
    $pdf->Cell(277, 5, _("LICENCES"), '', 1, 'C');
    $pdf->Ln(2);

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


    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT * FROM `" . TBRLIC . "` WHERE `rlic_codecli` = '" . $rel_codecli . "'";
    if ($idsite != -1)
        $rech .= " AND (rlic_idsite = '" . $idsite . "' OR rlic_idsite = '-1')";
    $rech .= " ORDER BY rlic_code";
    $rows = $db->get_results($rech, ARRAY_A);
    $db->disconnect();

    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);

            include_once("../inc/rel_pdf_lic.inc.php");
        }
        $db->disconnect();
    } else {
        $pdf->Ln(1);
        $pdf->SetFont('', 'I', 8);
        $pdf->Cell(277, 4, _("Aucune fiche Relevé licence créée"), 0, 1, 'L');
    }

    $pdf->AddPage();
    $pdf->SetDrawColor(125);
    $pdf->SetLineWidth(0.4);
    $PosY = $Pos_Entete;
    $pdf->Line(10, $PosY + 1, 287, $PosY + 1);
    $pdf->SetY($PosY + 1);

    /************************************/
    /* Impression des fiches Matériels  */
    /************************************/

    $pdf->Ln(2);

    $pdf->SetFont('', 'BU', 12);
    $pdf->Cell(277, 5, _("MATERIELS"), '', 1, 'C');

    $pdf->Ln(2);

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

    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT * FROM `" . TBRPDT . "` WHERE `rpdt_codecli` = '" . $rel_codecli . "'";
    if ($idsite != -1)
        $rech .= " AND (rpdt_idsite = '" . $idsite . "' OR rpdt_idsite = '-1')";
    $rech .= " ORDER BY rpdt_code";
    $rows = $db->get_results($rech, ARRAY_A);
    $db->disconnect();
    if ($rows) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            extract($row);

            include_once("../inc/rel_pdf_pdt.inc.php");
        }
    } else {
        $pdf->Ln(1);
        $pdf->SetFont('', 'I', 8);
        $pdf->MultiCell(277, 4, _("Aucune fiche Relevé matériel créée"), '', 'L');
    }
    $db->disconnect();


    $pdfcontent = $pdf->Output('rel_' . $rel_code, 'I');
}
?>