<?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_loc.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_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);

$cl = $_GET['cl']; // Récupération code Location
$ca = $_GET['ca']; // Récupération code article
$niv_util = @$_SESSION["niv_log"];
$pbm_locpdf = FALSE;

if ($cl <> "") {
    $nbartloc = nb_pdtloc_in_coloc($cl, TRUE);

    if ($nbartloc > 0) // On imprime que s'il y a des articles dans la location
    {
        $row = lit_enr_coloc($cl);
        if (is_array($row)) {
            encode_str($row);
            extract($row);

            $row = lit_enr_pdtloc($ca);
            if (is_array($row)) {
                encode_str($row);
                extract($row);

                $row = lit_enr_pdt_in_coloc($ca, $cl);
                if (is_array($row)) {
                    encode_str($row);
                    extract($row);

                    // Contrôle de hack pour un client
                    if (($niv_util == 1)  AND ($coloc_codecli <> @$_SESSION["code_log"]))
                        $pbm_locpdf = TRUE;

                    // Contrôle de hack pour une agence
                    if ((($niv_util == 4) OR ($niv_util == 3)) AND ($coloc_codeage <> @$_SESSION["code_age"]))
                        $pbm_locpdf = TRUE;

                    // Contrôle de hack pour un donneur d'ordres
                    if ((($niv_util == 7) OR ($niv_util == 6)) AND ($coloc_codedo <> @$_SESSION["code_do"]))
                        $pbm_locpdf = TRUE;

                    // Contrôle de hack pour un sous-traitant
                    if ((($niv_util == 11) OR ($niv_util == 10)) AND ($coloc_codest <> @$_SESSION["code_st"]))
                        $pbm_locpdf = TRUE;

                    if ($pbm_locpdf == FALSE) {
                        $codeadev = give_dev_codea_defaut_pdtloc($ca, $_SESSION["lng"], TRUE);
                        $symbole_monnaie_txt = give_dev_TXT($codeadev, TRUE);

                        // Récupération des indexes
                        $udesc1 = "";
                        $udesc2 = "";
                        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                        $rech = "SELECT pdtu_desc FROM `" . TBPDTU . "` WHERE `pdtu_code`= '" . $pdtloc_pdtucons1 . "'";
                        $row = $db->get_row($rech, ARRAY_A);
                        if ($row) {
                            encode_str($row);
                            extract($row);
                            $udesc1 = $pdtu_desc;
                        }
                        $rech = "SELECT pdtu_desc FROM `" . TBPDTU . "` WHERE `pdtu_code`= '" . $pdtloc_pdtucons2 . "'";
                        $row = $db->get_row($rech, ARRAY_A);
                        if ($row) {
                            encode_str($row);
                            extract($row);
                            $udesc2 = $pdtu_desc;
                        }

                        // Récupération du barème
                        $rech = "SELECT * FROM `" . TBPDTLBL . "` WHERE pdtlbl_idpdtl='" . $pdtl_id . "'";
                        $row = $db->get_row($rech, ARRAY_A);
                        if ($row)
                        {
                            encode_str($row);
                            extract($row);
                        }

                        // Calculs divers
                        //... Montant location
                        $pvht = $pdtlbl_pvht;
                        $tothtloc = round(($pdtlbl_qte * $pvht) * 100) / 100;
                        $rech2 = "SELECT * FROM `" . TBTVA . "` WHERE tva_code='" . $pdtlbl_codetva . "'";
                        $row2 = $db->get_row($rech2, ARRAY_A);
                        if ($row2)
                        {
                            encode_str($row);
                            extract($row2);
                        }
                        $txtva = $tva_tx / 100;
                        $totttcloc = round(($tothtloc * (1 + $txtva)) * 100) / 100;
                        $tottvaloc = $totttcloc - $tothtloc;
                        //... Caution
                        $pvht = $pdtlbl_caution;
                        $tothtcaut = round($pvht * 100) / 100;
                        $rech2 = "SELECT * FROM `" . TBTVA . "` WHERE tva_code='" . $pdtlbl_tvacaut . "'";
                        $row2 = $db->get_row($rech2, ARRAY_A);
                        if ($row2)
                        {
                            encode_str($row2);
                            extract($row2);
                        }
                        $txtva = $tva_tx / 100;
                        $totttccaut = round(($tothtcaut * (1 + $txtva)) * 100) / 100;
                        $tottvacaut = $totttccaut - $tothtcaut;
                        //... Assurance
                        $tothtass = 0;
                        $totttcass = 0;
                        $tottvaass = 0;
                        if ($pdtlbl_avecass == 1) {
                            $pvht = $pdtlbl_mtass;
                            $tothtass = round(($pdtlbl_qte * $pvht) * 100) / 100;
                            $rech2 = "SELECT * FROM `" . TBTVA . "` WHERE tva_code='" . $pdtlbl_codetvaass . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                            {
                                encode_str($row2);
                                extract($row2);
                            }
                            $txtva = $tva_tx / 100;
                            $totttcass = round(($tothtass * (1 + $txtva)) * 100) / 100;
                            $tottvaass = $totttcass - $tothtass;
                        }
                        //...Consommables
                        $rech2 = "SELECT * FROM `" . TBTVA . "` WHERE tva_code='" . $pdtlbl_codetva . "'";
                        $row2 = $db->get_row($rech2, ARRAY_A);
                        if ($row2)
                        {
                            encode_str($row2);
                            extract($row2);
                        }
                        $txtva = $tva_tx / 100;
                        //... Consommable 1
                        $pvht = $pdtlbl_puconsi1;
                        $tothtcons1 = round(($pdtlbl_qte * $pvht) * 100) / 100;
                        $totttccons1 = round(($tothtcons1 * (1 + $txtva)) * 100) / 100;
                        $tottvacons1 = $totttccons1 - $tothtcons1;
                        //... Consommable 2
                        $pvht = $pdtlbl_puconsi2;
                        $tothtcons2 = round(($pdtlbl_qte * $pvht) * 100) / 100;
                        $totttccons2 = round(($tothtcons2 * (1 + $txtva)) * 100) / 100;
                        $tottvacons2 = $totttccons2 - $tothtcons2;

                        $db->disconnect();

                        switch ($coloc_typcli) {
                            // Coordonnées compte client
                            case 0 :
                                $row = lit_enr_cli($coloc_codecli, TRUE);
                                encode_str($row);
                                extract($row);

                                // Coordonnées site intervention
                                if ($coloc_idsite > 0) {
                                    $row = lit_enr_scli_id($coloc_idsite, TRUE);
                                    encode_str($row);
                                    extract($row);
                                }
                                break;
                            // Coordonnées compte agence
                            case 1 :
                                $row = lit_enr_age($coloc_codecli, TRUE);
                                encode_str($row);
                                extract($row);
                                break;
                            // Coordonnées compte technicien
                            case 2 :
                            case 4 :
                                $row = lit_enr_tec($coloc_codecli, TRUE);
                                encode_str($row);
                                extract($row);
                                break;
                            // Coordonnées compte sous-traitant
                            case 3 :
                                $row = lit_enr_st($coloc_codecli, TRUE);
                                encode_str($row);
                                extract($row);
                                break;
                        }

                        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(PDF_PAGE_ORIENTATION, 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(_("Location N°"). $coloc_code);
                        $pdf->SetSubject(_("Location N°"). $coloc_code);
                        $pdf->settheader("dr");

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

                        $pdf->SetFont('', 'BU', 12); // Titre
                        $pdf->Cell(95, 5, _("LOUE PAR"), 0, 1, 'C');

                        if ($coloc_idsite == 0) // S'il s'agit du site principal
                        {
                            $ent_cli = ""; // Détail
                            switch ($coloc_typcli) {
                                // Client
                                case 0 :
                                    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;
                                    break;
                                // Agence
                                case 1 :
                                    $ent_cli .= $age_nom . "\n";
                                    $pdf->SetFont('', 'B', 10);
                                    $pdf->MultiCell(95, 4, $ent_cli, 0, 'L');

                                    $ent_cli = $age_adr1 . "\n"; // Suite...
                                    if (trim($age_adr2) <> "")
                                        $ent_cli .= $age_adr2 . "\n";
                                    $ent_cli .= Retournecp_Id($age_ville, TRUE) . " " . Retourneville_Id($age_ville, TRUE) . "\n";
                                    $ent_cli .= _("Tel")." : " . $age_telf . "\n";
                                    $ent_cli .= _("Fax")." : " . $age_fax;
                                    break;
                                // Technicien
                                case 2 :
                                case 4 :
                                    $ent_cli .= $tec_nom . "\n";
                                    $pdf->SetFont('', 'B', 10);
                                    $pdf->MultiCell(95, 4, $ent_cli, 0, 'L');

                                    $ent_cli = $tec_adr1 . "\n"; // Suite...
                                    if (trim($tec_adr2) <> "")
                                        $ent_cli .= $tec_adr2 . "\n";
                                    $ent_cli .= Retournecp_Id($tec_ville, TRUE) . " " . Retourneville_Id($tec_ville, TRUE) . "\n";
                                    $ent_cli .= _("Tel")." : " . $tec_telf . "\n";
                                    break;
                                // Sous-traitant
                                case 3 :
                                    $ent_cli .= $st_nom . "\n";
                                    $pdf->SetFont('', 'B', 10);
                                    $pdf->MultiCell(95, 4, $ent_cli, 0, 'L');

                                    $ent_cli = $st_adr1 . "\n"; // Suite...
                                    if (trim($st_adr2) <> "")
                                        $ent_cli .= $st_adr2 . "\n";
                                    $ent_cli .= Retournecp_Id($st_ville, TRUE) . " " . Retourneville_Id($st_ville, TRUE) . "\n";
                                    $ent_cli .= _("Tel")." : " . $st_telf . "\n";
                                    $ent_cli .= _("Fax")." : " . $st_fax;
                                    break;
                            }

                            $pdf->SetFont('', '', 10);
                            $pdf->MultiCell(95, 4, $ent_cli, 0, 'L');
                        } else // S'il s'agit d'un site secondaire
                        {
                            $ent_scli = ""; // Détail
                            $ent_scli .= $scli_ste . "\n";
                            $pdf->SetFont('', 'B', 10);
                            $pdf->MultiCell(95, 4, $ent_scli, 0, 'L');

                            $ent_scli = $scli_adr1 . "\n"; // Suite...
                            if (trim($scli_adr2) <> "")
                                $ent_scli .= $scli_adr2 . "\n";
                            $ent_scli .= Retournecp_Id($scli_ville, TRUE) . " " . Retourneville_Id($scli_ville, TRUE) . "\n";
                            $ent_scli .= _("Tel")." : " . $scli_telf . "\n";
                            $ent_scli .= _("Fax")." : " . $scli_fax;

                            $pdf->SetFont('', '', 10);
                            $pdf->MultiCell(95, 4, $ent_scli, 0, 'L');
                        }

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

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

                        $pdf->Cell(95);

                        $pdf->SetFont('', '', 8); // Niveau d'urgence
                        $pdf->Cell(95, 5, $coloc_urg, 0, 1, 'R');

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

                        $pdf->Cell(95);

                        $ent_loc = _("Contrat")." :\n";
                        $ent_loc .= _("Agence")." :\n";
                        $ent_loc .= _("Demandé par")." :\n";
                        $ent_loc .= _("Départ prévu")." :\n";
                        $ent_loc .= _("Retour prévu")." :\n";
                        $ent_loc .= _("Caution")." :\n";

                        $pdf->MultiCell(22, 4, $ent_loc, 0, 'R');

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

                        $ent_loc = $coloc_code . "\n";
                        $ent_loc .= $coloc_codeage . "\n";
                        $txt_ccli = _("Pas de contact");
                        switch (intval($coloc_ddeur)) {
                            case -1 :
                                switch ($tc) {
                                    case 0 :
                                        $txt_ccli = $cli_nom . " " . $cli_prenom . " (Contact principal)";
                                        break;
                                    case 1 :
                                        $txt_ccli = $age_resp_nom . " " . $age_resp_prenom . " (Contact principal)";
                                        break;
                                    case 3 :
                                        $txt_ccli = $st_resp_nom . " " . $st_resp_prenom . " (Contact principal)";
                                        break;
                                }
                                break;
                            case 0 :
                                $row = lit_enr_scli_id($coloc_idsite, TRUE);
                                if (is_array($row)) {
                                    encode_str($row);
                                    extract($row);
                                    $txt_ccli = $scli_rnom . " " . $scli_rprenom . " (Contact site)";
                                }
                                break;
                            default :
                                switch ($tc) {
                                    case 0 :
                                        $row = lit_enr_ccli_id_in_cli($coloc_ddeur, $cli_code);
                                        if (is_array($row)) {
                                            encode_str($row);
                                            extract($row);
                                            $txt_ccli = $ccli_nom . " " . $ccli_prenom . " (" . $cli_codefonc . ")";
                                        }
                                        break;
                                    case 1 :
                                        $row = lit_enr_cage_id_in_age($coloc_ddeur, $age_code);
                                        if (is_array($row)) {
                                            encode_str($row);
                                            extract($row);
                                            $txt_ccli = $cage_nom . " " . $cage_prenom . " (" . $age_codefonc . ")";
                                        }
                                        break;
                                    case 3 :
                                        $row = lit_enr_cst_id_in_st($coloc_ddeur, $st_code);
                                        if (is_array($row)) {
                                            encode_str($row);
                                            extract($row);
                                            $txt_ccli = $cst_nom . " " . $cst_prenom . " (" . $st_codefonc . ")";
                                        }
                                        break;
                                }
                                break;
                        }
                        $ent_loc .= $txt_ccli . "\n";
                        $ent_loc .= datetolocal($pdtl_datedeb) . " " . substr($pdtl_heuredeb, 0, 5) . "\n";
                        $ent_loc .= datetolocal($pdtl_datefin) . " " . substr($pdtl_heurefin, 0, 5) . "\n";

                        $ent_loc .= full_num_format($totttccaut, '', '', TRUE, '', TRUE, TRUE)."\n";
                        // $ent_loc .= $totttccaut . " " . $symbole_monnaie_txt . " "._("TTC")."\n";

                        $pdf->MultiCell(73, 4, $ent_loc, 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, $PosY);
                        $pdf->SetDrawColor(0);
                        $pdf->SetLineWidth(0.2);
                        $pdf->SetDrawColor(220);
                        $pdf->SetLineStyle(['dash' => 0]);
                        $pdf->ln(1);

                        /* Barème applicable */
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Barème applicable").' : ', 0, 0, 'R');
                        $txtbarloc = "";
                        if ($pdtlbl_idbarloc <> 0) {
                            $row = lit_enr_barloc_id($pdtlbl_idbarloc, TRUE);
                            if (is_array($row)) {
                                encode_str($row);
                                extract($row);
                                $txtbarloc = $barloc_des;
                            }
                        } else
                            $txtbarloc = _("Prêt de matériel (Gratuit)");
                        $txtbarloc = $txtbarloc . " ("._("inclus caution");
                        if ($pdtlbl_avecass == 1)
                            $txtbarloc .= " "._("et assurance");
                        else
                            $txtbarloc .= ", "._("sans assurance");
                        $txtbarloc .= ")";
                        $pdf->SetFont('', '', 8);
                        $pdf->MultiCell(150, 5, $txtbarloc, '', 'L');

                        // Montant barème
                        //... Titres tableau
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, '', 0, 0, 'R');
                        $pdf->SetFont('', 'B', 8);
                        $pdf->SetFillColor(0);
                        $pdf->SetTextColor(255);
                        $pdf->Cell(20, 5, _("Qté"), 1, 0, 'C', 1);
                        $pdf->Cell(50, 5, _("Total HT"), 1, 0, 'C', 1);
                        $pdf->Cell(20, 5, _("TVA"), 1, 0, 'C', 1);
                        $pdf->Cell(50, 5, _("Total TTC"), 1, 1, 'C', 1);
                        //... Détail
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, '', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        $pdf->SetFillColor(200);
                        $pdf->SetTextColor(0);
                        $pdf->Cell(20, 5, $pdtlbl_qte, 1, 0, 'C', 1);

                        // Affichage barème
                        $totbarht = $tothtloc + $tothtcaut + $tothtass + $tothtcons1 + $tothtcons2;
                        $txt = full_num_format($totbarht, '', '', TRUE, '', TRUE, TRUE);
                        $pdf->Cell(50, 5, $txt, 1, 0, 'R', 1);
                        $totbartva = $tottvaloc + $tottvacaut + $tottvaass + $tottvacons1 + $tottvacons2;
                        $txt = full_num_format($totbartva, '', '', TRUE, '', TRUE, TRUE);
                        $pdf->Cell(20, 5, $txt, 1, 0, 'R', 1);
                        $totbarttc = $totttcloc + $totttccaut + $totttcass + $totttccons1 + $totttccons2;
                        $txt = full_num_format($totbarttc, '', '', TRUE, '', TRUE, TRUE);
                        $pdf->Cell(50, 5, $txt, 1, 1, 'R', 1);

                        $txt = "";
                        if ($pdtlbl_qconsi1 > 0) {
                            $txt .= _("Inclus")." " . $pdtlbl_qte . " "._("forfait(s)")." " . $pdtlbl_qconsi1 . " " . strtolower($udesc1);
                            if ($pdtlbl_qconsi2 > 0)
                                $txt .= " / ";
                        }

                        if ($pdtlbl_qconsi2 > 0)
                            $txt .= _("Inclus")." " . $pdtlbl_qte . " "._("forfait(s)")." " . $pdtlbl_qconsi2 . " " . strtolower($udesc2);

                        if (trim($txt) <> "") {
                            $pdf->SetFont('', 'B', 10);
                            $pdf->Cell(40, 4, '', 0, 0, 'R');
                            $pdf->SetFont('', 'I', 8);
                            $pdf->Cell(50, 4, $txt, 0, 1, 'L');
                        }


                        $pdf->ln(2);

                        /* DEPART PRODUIT */
                        $PosY = $pdf->GetY();
                        $pdf->SetDrawColor(125);
                        $pdf->SetLineWidth(0.4);
                        $pdf->Line(10, $PosY, 200, $PosY);
                        $pdf->SetDrawColor(0);
                        $pdf->SetLineWidth(0.2);
                        $pdf->SetDrawColor(220);

                        $pdf->ln(2);
                        $pdf->SetFont('', 'BU', 12);
                        $pdf->Cell(180, 5, _("DEPART PRODUIT"), '', 1, 'C');

                        // Détail de celui qui prend le produit
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Pris par").' : ', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        if (datetolocal($pdtl_datedep) == "")
                            $pdf->MultiCell(150, 5, $pdtl_prispar, 1, 'L');
                        else
                            $pdf->MultiCell(150, 5, $pdtl_prispar, 0, 'L');

                        // Date/heure du départ réels
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Remis le").' : ', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        if (datetolocal($pdtl_datedep) == "")
                            $pdf->Cell(30, 5, '', 1, 0, 'L');
                        else
                            $pdf->Cell(30, 5, datetolocal($pdtl_datedep), '', 0, 'L');

                        $pdf->Cell(5, 5, ' à ', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        if (datetolocal($pdtl_datedep) == "")
                            $pdf->Cell(30, 5, '', 1, 1, 'L');
                        else
                            $pdf->Cell(30, 5, substr($pdtl_heuredep, 0, 5), '', 1, 'L');

                        // Indexes
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Indexes").' : ', 0, 0, 'R');
                        if (trim($udesc1) <> "") {
                            $pdf->SetFont('', 'BI', 8);
                            $pdf->Cell(40, 5, $udesc1 . ' :', 0, 0, 'R');
                            $pdf->SetFont('', '', 8);
                            if (datetolocal($pdtl_datedep) == "")
                                $pdf->Cell(40, 5, '', 1, 0, 'L');
                            else
                                $pdf->Cell(40, 5, $pdtl_didx1, '', 0, 'L');
                            $pdf->SetFont('', 'I', 8);
                            $pdf->Cell(20, 5, '('._("précédent").' : ' . $pdtloc_idxcons1 . ')', '', 1, 'L');

                        }
                        if (trim($udesc2) <> "") {
                            if (trim($udesc1) <> "") {
                                $pdf->SetFont('', 'B', 10);
                                $pdf->Cell(40, 5, '', 0, 0, 'R');
                            }
                            $pdf->SetFont('', 'BI', 8);
                            $pdf->Cell(40, 5, $udesc2 . ' :', 0, 0, 'R');
                            $pdf->SetFont('', '', 8);
                            if (datetolocal($pdtl_datedep) == "")
                                $pdf->Cell(40, 5, '', 1, 0, 'L');
                            else
                                $pdf->Cell(40, 5, $pdtl_didx2, '', 0, 'L');
                            $pdf->SetFont('', 'I', 8);
                            $pdf->Cell(20, 5, '('._("précédent").' : ' . $pdtloc_idxcons2 . ')', '', 1, 'L');
                        }
                        if ((trim($udesc1) == "") AND (trim($udesc2) == "")) {
                            $pdf->SetFont('', 'BI', 8);
                            $pdf->Cell(40, 5, _("Pas de consommables"), 0, 1, 'L');
                        }

                        // Caution
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Mode caution").' : ', 0, 0, 'R');
                        if (datetolocal($pdtl_datedep) == "") {
                            $pdf->SetFont('', '', 8);
                            $pdf->Cell(80, 5, '', 1, 1, 'L');
                        } else {
                            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                            $rech = "SELECT * FROM `" . TBREG . "` WHERE `reg_code` = '" . $pdtl_coderegc . "'";
                            $row = $db->get_row($rech, ARRAY_A);
                            if ($row)
                            {
                                encode_str($row);
                                extract($row);
                            }
                            $db->disconnect();
                            $pdf->SetFont('', '', 8);
                            $pdf->Cell(80, 5, $reg_desc, 0, 1, 'L');
                        }

                        // Notes départ
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Notes").' : ', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        if (datetolocal($pdtl_datedep) == "") {
                            $txt = "";
                            for ($i = 0; $i < 5; $i++) {
                                $txt .= "\n";
                            }
                            $pdf->MultiCell(150, 5, $txt, 1, 'L');
                        } else
                            $pdf->MultiCell(150, 5, $pdtl_noted, 0, 'L');

                        // Date / Signature client
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Signature client").' : ', 0, 0, 'R');
                        $PosX = $pdf->GetX() + 15;
                        $PosY = $pdf->GetY() + 10;
                        $pdf->SetFont('', '', 8);
                        $txt = "";
                        for ($i = 0; $i < 5; $i++) {
                            $txt .= "\n";
                        }
                        $pdf->MultiCell(75, 5, $txt, 1, 'L');
                        $Pos_Suivant = $pdf->GetY();

                        // On signale si c'est déjà signé au départ
                        if ((datetolocal($pdtl_datedep) <> "") AND ($pdtl_datedep <> $datea)) {
                            // On passe en mode transparent
                            $pdf->SetAlpha(0.2);

                            // Rotation de 45° et écriture
                            $pdf->StartTransform();
                            $pdf->Rotate(20, $PosX, $PosY);
                            $pdf->SetFont('', 'B', 20);
                            $pdf->Text($PosX, $PosY, _("Déjà signé"));
                            $pdf->StopTransform();

                            $pdf->SetAlpha(1);
                            $pdf->setPageMark();
                        }

                        $pdf->SetY($Pos_Suivant);
                        $pdf->ln(2);

                        /* RETOUR PRODUIT */
                        $PosY = $pdf->GetY();
                        $pdf->SetDrawColor(125);
                        $pdf->SetLineWidth(0.4);
                        $pdf->Line(10, $PosY, 200, $PosY);
                        $pdf->SetDrawColor(0);
                        $pdf->SetLineWidth(0.2);
                        $pdf->SetDrawColor(220);

                        $pdf->ln(2);
                        $pdf->SetFont('', 'BU', 12);
                        $pdf->Cell(180, 5, _("RETOUR PRODUIT"), '', 1, 'C');

                        // Détail de celui qui prend le produit
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Rendu par").' : ', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        if (datetolocal($pdtl_dateret) == "")
                            $pdf->MultiCell(150, 5, $pdtl_retpar, 1, 'L');
                        else
                            $pdf->MultiCell(150, 5, $pdtl_retpar, 0, 'L');

                        // Date/heure du retour réels
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Rendu le").' : ', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        if (datetolocal($pdtl_dateret) == "")
                            $pdf->Cell(30, 5, '', 1, 0, 'L');
                        else
                            $pdf->Cell(30, 5, datetolocal($pdtl_dateret), '', 0, 'L');

                        $pdf->Cell(5, 5, ' '._("à").' ', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        if (datetolocal($pdtl_dateret) == "")
                            $pdf->Cell(30, 5, '', 1, 1, 'L');
                        else
                            $pdf->Cell(30, 5, substr($pdtl_heureret, 0, 5), '', 1, 'L');

                        // Indexes
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Indexes").' : ', 0, 0, 'R');
                        if (trim($udesc1) <> "") {
                            $pdf->SetFont('', 'BI', 8);
                            $pdf->Cell(40, 5, $udesc1 . ' :', 0, 0, 'R');
                            $pdf->SetFont('', '', 8);
                            if (datetolocal($pdtl_dateret) == "")
                                $pdf->Cell(40, 5, '', 1, 1, 'L');
                            else
                                $pdf->Cell(40, 5, $pdtl_ridx1, '', 1, 'L');

                        }
                        if (trim($udesc2) <> "") {
                            if (trim($udesc1) <> "") {
                                $pdf->SetFont('', 'B', 10);
                                $pdf->Cell(40, 5, '', 0, 0, 'R');
                            }
                            $pdf->SetFont('', 'BI', 8);
                            $pdf->Cell(40, 5, $udesc2 . ' :', 0, 0, 'R');
                            $pdf->SetFont('', '', 8);
                            if (datetolocal($pdtl_dateret) == "")
                                $pdf->Cell(40, 5, '', 1, 1, 'L');
                            else
                                $pdf->Cell(40, 5, $pdtl_ridx2, '', 1, 'L');
                        }
                        if ((trim($udesc1) == "") AND (trim($udesc2) == "")) {
                            $pdf->SetFont('', 'BI', 8);
                            $pdf->Cell(40, 5, _("Pas de consommables"), 0, 1, 'L');
                        }

                        // Caution rendue ?
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Caution rendue").' : ', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        if (datetolocal($pdtl_dateret) == "")
                            $pdf->Cell(10, 5, _("Oui / Non"), 0, 1, 'L');
                        else
                            $pdf->Cell(10, 5, $non_oui[$pdtl_rcaut], 0, 1, 'L');

                        // Remise en location
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Remise en location").' : ', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        if (datetolocal($pdtl_dateret) == "")
                            $pdf->Cell(10, 5, _("Oui / Non"), 0, 1, 'L');
                        else
                            $pdf->Cell(10, 5, $non_oui[$pdtl_reloc], 0, 1, 'L');

                        // Notes retour
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Notes").' : ', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        if (datetolocal($pdtl_dateret) == "") {
                            $txt = "";
                            for ($i = 0; $i < 5; $i++) {
                                $txt .= "\n";
                            }
                            $pdf->MultiCell(150, 5, $txt, 1, 'L');
                        } else
                            $pdf->MultiCell(150, 5, $pdtl_noter, 0, 'L');

                        // Date / Signature client
                        $pdf->SetFont('', 'B', 10);
                        $pdf->Cell(40, 5, _("Signature client").' : ', 0, 0, 'R');
                        $pdf->SetFont('', '', 8);
                        $txt = "";
                        for ($i = 0; $i < 5; $i++) {
                            $txt .= "\n";
                        }
                        $pdf->MultiCell(75, 5, $txt, 1, 'L');

                        /* FACTURATION FINALE */
                        if (datetolocal($pdtl_dateret) <> "") {
                            $pdf->ln(2);

                            $PosY = $pdf->GetY();
                            $pdf->SetDrawColor(125);
                            $pdf->SetLineWidth(0.4);
                            $pdf->Line(10, $PosY, 200, $PosY);
                            $pdf->SetDrawColor(0);
                            $pdf->SetLineWidth(0.2);
                            $pdf->SetDrawColor(220);

                            $pdf->ln(2);
                            $pdf->SetFont('', 'BU', 12);
                            $pdf->Cell(180, 5, _("A FACTURER"), '', 1, 'C');

                            // Montant barème
                            //... Titres tableau
                            $pdf->SetFont('', 'B', 8);
                            $pdf->SetFillColor(0);
                            $pdf->SetTextColor(255);
                            $pdf->Cell(92, 5, _("Désignation"), 1, 0, 'C', 1);
                            $pdf->Cell(20, 5, _("Qté"), 1, 0, 'C', 1);
                            $pdf->Cell(30, 5, _("Total HT"), 1, 0, 'C', 1);
                            $pdf->Cell(20, 5, _("TVA"), 1, 0, 'C', 1);
                            $pdf->Cell(30, 5, _("Total TTC"), 1, 1, 'C', 1);
                            //... Détail
                            $pdf->SetFont('', '', 7);
                            $pdf->SetFillColor(200);
                            $pdf->SetTextColor(0);
                            $pdf->Cell(92, 5, $txtbarloc, 1, 0, 'L', 1);
                            $pdf->SetFont('', '', 8);
                            $pdf->Cell(20, 5, $pdtlbl_qte, 1, 0, 'C', 1);

                            // BAREME APPLICABLE
                            $totbarht = $tothtloc + $tothtcaut + $tothtass + $tothtcons1 + $tothtcons2;
                            $txt = full_num_format($totbarht, '', '', TRUE, '', TRUE, TRUE);
                            $pdf->Cell(30, 5, $txt, 1, 0, 'R', 1);
                            $totbartva = $tottvaloc + $tottvacaut + $tottvaass + $tottvacons1 + $tottvacons2;
                            $txt = full_num_format($totbartva, '', '', TRUE, '', TRUE, TRUE);
                            $pdf->Cell(20, 5, $txt, 1, 0, 'R', 1);
                            $totbarttc = $totttcloc + $totttccaut + $totttcass + $totttccons1 + $totttccons2;
                            $txt = full_num_format($totbarttc, '', '', TRUE, '', TRUE, TRUE);
                            $pdf->Cell(30, 5, $txt, 1, 1, 'R', 1);

                            // PENALITE
                            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                            $rech2 = "SELECT * FROM `" . TBPDTLPL . "` WHERE `pdtlpl_idpdtl` = '$pdtl_id'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                            {
                                encode_str($row2);
                                extract($row2);
                            }

                            // Calcul montant pénalité
                            $penht = $pdtlpl_valeur;
                            $rech2 = "SELECT * FROM `" . TBTVA . "` WHERE tva_code='" . $pdtlpl_codetva . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                            {
                                encode_str($row2);
                                extract($row2);
                            }
                            $txtva = $tva_tx / 100;
                            $penttc = round(($penht * (1 + $txtva)) * 100) / 100;
                            $pentva = $penttc - $penht;

                            if ($pdtlpl_valeur <> 0) // On affiche que si pénalité non nulle
                            {
                                // Affichage pénalité
                                $pdf->SetFont('', '', 7);
                                $pdf->SetFillColor(220);
                                $pdf->SetTextColor(0);
                                $pdf->Cell(92, 5, _("Pénalité sur retard"), 1, 0, 'L', 1);
                                $pdf->SetFont('', '', 8);
                                $pdf->Cell(20, 5, '1', 1, 0, 'C', 1);
                                $txt = full_num_format($penht, '', '', TRUE, '', TRUE, TRUE);
                                $pdf->Cell(30, 5, $txt, 1, 0, 'R', 1);
                                $txt = full_num_format($pentva, '', '', TRUE, '', TRUE, TRUE);
                                $pdf->Cell(20, 5, $txt, 1, 0, 'R', 1);
                                $txt = full_num_format($penttc, '', '', TRUE, '', TRUE, TRUE);
                                $pdf->Cell(30, 5, $txt, 1, 1, 'R', 1);
                            }


                            // CONSOMMABLES
                            // ...On vérifie s'il n'y a pas déjà un montant consommables enregistré
                            $rech2 = "SELECT * FROM `" . TBPDTLCL . "` WHERE `pdtlcl_idpdtl` = '$pdtl_id'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                            {
                                encode_str($row2);
                                extract($row2);
                            }

                            // Calcul montant pénalité
                            $consht = $pdtlcl_valeur;
                            $rech2 = "SELECT * FROM `" . TBTVA . "` WHERE tva_code='" . $pdtlpl_codetva . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                            {
                                encode_str($row2);
                                extract($row2);
                            }
                            $txtva = $tva_tx / 100;
                            $consttc = round(($consht * (1 + $txtva)) * 100) / 100;
                            $constva = $consttc - $consht;

                            if ($pdtlcl_valeur <> 0) // On affiche que si montant non nul
                            {
                                // Affichage consommables
                                $pdf->SetFont('', '', 7);
                                $pdf->SetFillColor(200);
                                $pdf->SetTextColor(0);
                                $pdf->Cell(92, 5, _("Consommables en sus"), 1, 0, 'L', 1);
                                $pdf->SetFont('', '', 8);
                                $pdf->Cell(20, 5, '1', 1, 0, 'C', 1);
                                $txt = full_num_format($consht, '', '', TRUE, '', TRUE, TRUE);
                                $pdf->Cell(30, 5, $txt, 1, 0, 'R', 1);
                                $txt = full_num_format($constva, '', '', TRUE, '', TRUE, TRUE);
                                $pdf->Cell(20, 5, $txt, 1, 0, 'R', 1);
                                $txt = full_num_format($consttc, '', '', TRUE, '', TRUE, TRUE);
                                $pdf->Cell(30, 5, $txt, 1, 1, 'R', 1);
                            }

                            $db->disconnect();

                            // Totaux généraux
                            $pdf->SetFont('', 'BI', 8);
                            $pdf->SetFillColor(255);
                            $pdf->SetTextColor(0);
                            $pdf->Cell(92, 5, _("Total général"), 1, 0, 'R', 1);
                            $pdf->Cell(20, 5, '', 1, 0, 'C', 1);
                            $txt = full_num_format(($totbarht + $penht + $consht), '', '', TRUE, '', TRUE, TRUE);
                            $pdf->Cell(30, 5, $txt, 1, 0, 'R', 1);
                            $txt = full_num_format(($totbartva + $pentva + $constva), '', '', TRUE, '', TRUE, TRUE);
                            $pdf->Cell(20, 5, $txt, 1, 0, 'R', 1);
                            $txt = full_num_format(($totbarttc + $penttc + $consttc), '', '', TRUE, '', TRUE, TRUE);
                            $pdf->Cell(30, 5, $txt, 1, 1, 'R', 1);

                        }
                        /*
                                //Détermination d'un nom de fichier temporaire dans le répertoire courant
                                $file=basename(tempnam(getcwd(),'tmp'));
                                rename($file,$file.'.pdf');
                                $file.='.pdf';
                        */
                        // $pdf->Output($file,'D');
                        $pdf->Output('loc_' . $coloc_code, 'I');
                    }
                } else
                    $pbm_locpdf = TRUE;
            } else
                $pbm_locpdf = TRUE;
        } else
            $pbm_locpdf = TRUE;
    } else
        $pbm_locpdf = TRUE;
} else
    $pbm_locpdf = TRUE;

if ($pbm_locpdf == TRUE)
    close_fen_auto('', '');
?>
