<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.4
 *
 * Copyright (C) 2006-2016  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) {
    header('Location: ../index.php');
}

include_once('../inc/config.inc.php');
include_once('../inc/fic_age.inc.php');
include_once('../inc/fic_droits.inc.php');
include_once('../inc/fic_hum.inc.php');
include_once('../inc/fic_mque.inc.php');
include_once('../inc/fic_prm.inc.php');
include_once('../inc/fic_ret.inc.php');
include_once('../inc/fic_txt.inc.php');
include_once('../inc/func.inc.php');
include_once('../inc/func_date.inc.php');
include_once('../inc/func_geo.inc.php');
include_once('../inc/func_log.inc.php');
include_once('../inc/func_txt.inc.php');
include_once('../inc/func_txtcm.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(valsession('code_log'));
// Variables Formulaire
fix_magic_quotes();
$_GET = clean_str($_GET);

$cpr = $_GET['cpr']; // Récupération code Relevé

if ($cpr <> '') {
    // Récupération des informations demande de retour
    $row = lit_enr_retd($cpr, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);

        // Récupération des informations fiche produit à retourner
        $row2 = lit_enr_retp($cpr, TRUE);
        if (is_array($row2)) {
            $row2 = encode_str($row2);
            extract($row2);

            // Récupération des informations marque
            $row3 = lit_enr_mque($retd_codemque, TRUE);
            if (is_array($row3)) {
                $row3 = encode_str($row3);
                extract($row3);

                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->SetTitle(_('Demande de retour'));
                $pdf->SetSubject(_('Demande de retour'));
                $pdf->settgen();
                $pdf->settheader('retd');

                // set default font subsetting mode
                $pdf->setFontSubsetting(true);

                // 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 distributeur
                $pdf->SetFont('', 'BU', 12); // Titre
                $pdf->Cell(138, 5, _('DESTINATAIRE'), 0, 1, 'C');

                $ent_mque = ''; // Détail
                $ent_mque .= $mque_ste . "\n";
                $pdf->SetFont('', 'B', 10);
                $pdf->MultiCell(138, 4, $ent_mque, 0, 'L');

                $ent_mque = $mque_adr1 . "\n"; // Suite...
                if (trim($mque_adr2) <> '') {
                    $ent_mque .= $mque_adr2 . "\n";
                }
                $ent_mque .= Retournecp_Id($mque_ville, TRUE) . ' ' . Retourneville_Id($mque_ville, TRUE) . "\n";
                $ent_mque .= _('Tel') . ' : ' . $mque_telf . "\n";
                $ent_mque .= _('Fax') . ' : ' . $mque_fax;
                if (trim($mque_tvai) <> '') {
                    $ent_mque .= "\n" . _('TVA') . ' : ' . $mque_tvai;
                }
                $pdf->SetFont('', '', 10);
                $pdf->MultiCell(138, 4, $ent_mque, 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_ret = _('Demandé par') . " :\n";
                $ent_ret .= _('Dossier') . " :\n";
                $ent_ret .= _('Urgence') . " :\n";

                $pdf->MultiCell(30, 4, $ent_ret, 0, 'R');

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

                $pdf->SetFont('', '', 8);

                $ent_ret = nom_md($retd_codemd, TRUE) . "\n";
                if (trim($retd_dossier) <> '') {
                    $ent_ret .= $retd_dossier . "\n";
                } else {
                    $ent_ret .= "-\n";
                }

                $row = lit_enr_urg($retd_nivurg);
                if ($row) {
                    extract($row);
                }
                $ent_ret .= $urg_des . "\n";

                $pdf->MultiCell(109, 4, $ent_ret, 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);

                // Code produit chez distributeur
                $pdf->SetFont('', 'B', 8);
                $pdf->Cell(30, 4, _('Code produit') . ' : ', 0, 0, 'R');
                $pdf->SetFont('', '', 8);
                $pdf->Cell(247, 4, $retd_codepdtm, 0, 1, 'L');

                // Désignation
                $pdf->SetFont('', 'B', 8);
                $pdf->Cell(30, 4, _('Désignation') . ' : ', 0, 0, 'R');
                $pdf->SetFont('', '', 8);
                $pdf->Cell(247, 4, $retp_des, 0, 1, 'L');

                // N° série
                $pdf->SetFont('', 'B', 8);
                $pdf->Cell(30, 4, _('Numéro de série') . ' : ', 0, 0, 'R');
                $pdf->SetFont('', '', 8);
                $pdf->Cell(247, 4, $retp_ns, 0, 1, 'L');

                // BL ou facture + Numéro
                $pdf->SetFont('', 'B', 8);
                $pdf->Cell(30, 4, _('BL/Facture') . ' : ', 0, 0, 'R');
                $pdf->SetFont('', '', 8);
                $pdf->Cell(247, 4, $doc_types[$retd_blf] . ' ' . $retd_nblf . ', ' . _('du') . ' ' . datetolocal($retd_dblf), 0, 1, 'L');

                // Sous garantie ?
                $pdf->SetFont('', 'B', 8);
                $pdf->Cell(30, 4, _('Sous garantie ?') . ' : ', 0, 0, 'R');
                $pdf->SetFont('', '', 8);
                $pdf->Cell(247, 4, $non_oui[$retd_gar], 0, 1, 'L');

                // Panne
                $pdf->SetFont('', 'B', 8);
                $pdf->Cell(30, 4, _('Problème') . ' : ', 0, 0, 'R');
                $pdf->SetFont('', '', 8);
                $pdf->MultiCell(247, 4, $retp_pbm, 0, 'L');

                // Texte de pied (retour)
                $pdf->Ln();
                $row = lit_enr_txt('PIED_RETD');
                extract($row);

                $txt_ret = parse_txtcorps('age', $retp_codeage, $txt_texte, '');

                $pdf->SetFont('', '', 8);
                $pdf->writeHTML($txt_ret, true, false, true);

                $url_pdf = $url_gfc . '/pj/dde_retour.pdf';
                $pdf->Output($url_pdf, 'FI');
                killfile($url_pdf);
            }
        }
    }
}
