<?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_loc.inc.php");
include_once("../inc/fic_prm.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
$niv_util = @$_SESSION["niv_log"];
$cgv = TRUE;
$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)) {
            $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) {
                switch ($coloc_typcli) {
                    // Coordonnées compte client
                    case 0 :
                        $row = lit_enr_cli($coloc_codecli, TRUE);
                        $row = encode_str($row);
                        extract($row);

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

                // 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, _("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;
                            if (trim($cli_tvai) <> "")
                                $ent_cli .= "\n" . _("TVA") . " : " . $cli_tvai;
                            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;
                            if (trim($age_tvai) <> "")
                                $ent_cli .= "\n" . _("TVA") . " : " . $age_tvai;
                            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;
                            if (trim($st_tvai) <> "")
                                $ent_cli .= "\n" . _("TVA") . " : " . $st_tvai;
                            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;
                    if (trim($scli_tvai) <> "")
                        $ent_scli .= "\n" . _("TVA") . " : " . $scli_tvai;

                    $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 = _("Agence") . " :\n";
                $ent_loc .= _("Fin prévue le") . " :\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_codeage . "\n";
                $ent_loc .= $coloc_datefinf . "\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);

                // Détail de la prise en charge

                $pdf->SetFont('', 'B', 10);
                $pdf->Cell(40, 5, _("Demandé par") . ' : ', 0, 0, 'R');
                $pdf->SetFont('', '', 8);
                $txt_ccli = _("Pas de contact");
                switch (intval($coloc_ddeur)) {
                    case -1 :
                        $txt_ccli = $cli_nom . " " . $cli_prenom . " (" . _("Contact principal");
                        $txt_ccli .= " - " . $cli_telf . ")";
                        break;
                    case 0 :
                        $row = lit_enr_scli_id($coloc_idsite, TRUE);
                        if (is_array($row)) {
                            extract($row);
                            $txt_ccli = $scli_rnom . " " . $scli_rprenom . " (" . _("Contact site");
                            $txt_ccli .= " - " . $scli_telf . ")";
                        }
                        break;
                    default :
                        $row = lit_enr_ccli_id_in_cli($coloc_ddeur, $cli_code);
                        if (is_array($row)) {
                            extract($row);
                            $row2 = lit_enr_fonc($ccli_codefonc, TRUE);
                            if (is_array($row2)) {
                                $row2 = encode_str($row2);
                                extract($row2);
                                $txt_fonc = $fonc_desc;
                            } else
                                $txt_fonc = $ccli_codefonc;
                            $txt_ccli = $ccli_nom . " " . $ccli_prenom . " (" . $txt_fonc . " - " . $ccli_telf . ")";
                        }
                        break;
                }
                $pdf->MultiCell(150, 4, $txt_ccli, 1, 'L');

                $pdf->ln(2);

                if ($nbartloc <> 0) // S'il y a des articles facturés, on les affiche
                {
                    $pdf->SetFont('', 'BU', 12);
                    $pdf->Cell(180, 5, _("ARTICLES RESERVES OU LOUES"), '', 1, 'C');
                    $pdf->SetFont('', 'I', 8);
                    $pdf->Cell(180, 4, _("Tarifs comprenant les éventuelles cautions"), '', 1, 'C');
                    $pdf->SetFont('', 'I', 8);
                    $pdf->Cell(180, 4, _("(P) : Produit parti - (R) : Produit revenu"), '', 1, 'C');

                    // Type de facturation
                    $factTTC = TRUE;
                    switch ($coloc_typcli) {
                        case 0 :
                            if (($cli_modfact == "0") OR ($cli_modfact == "2"))
                                $factTTC = FALSE;
                            break;
                        case 1 :
                        case 3 :
                            $factTTC = FALSE;
                            break;
                    }

                    $pdf->SetFont('', 'B', 8);
                    $pdf->SetFillColor(0);
                    $pdf->SetTextColor(255);

                    $pdf->Cell(85, 5, _("Désignation"), 1, 0, 'C', 1);
                    $pdf->Cell(35, 5, _("Du"), 1, 0, 'C', 1);
                    $pdf->Cell(35, 5, _("Au"), 1, 0, 'C', 1);
                    if ($factTTC == TRUE)
                        $txt = _("TOTAL TTC");
                    else
                        $txt = _("TOTAL HT");
                    $pdf->Cell(35, 5, $txt, 1, 1, 'C', 1);

                    $pdf->SetFillColor(255);
                    $pdf->SetTextColor(0);

                    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
                    $rech = "SELECT * FROM `" . TBPDTL . "` WHERE pdtl_codecoloc='" . $db->escape($cl) . "' ORDER BY `pdtl_codepdtloc`";
                    $rows = $db->get_results($rech, ARRAY_A);

                    $i = 0;

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

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

                            // Couleurs alternées
                            if ($i % 2 == 0)
                                $pdf->SetFillColor(200);
                            else
                                $pdf->SetFillColor(255);

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

                            // Désignation
                            $rech2 = "SELECT pdtloc_des FROM `" . TBPDTLOC . "` WHERE pdtloc_code='" . $pdtl_codepdtloc . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2)
                            {
                                encode_str($row2);
                                extract($row2);
                            }
                            // Récupération enregistrement barème location du produit dans ce contrat
                            $rech2 = "SELECT * FROM `" . TBPDTLBL . "` WHERE pdtlbl_idpdtl='" . $pdtl_id . "'";
                            $row2 = $db->get_row($rech2, ARRAY_A);
                            if ($row2) {
                                $row2 = encode_str($row2);
                                extract($row2);
                            }
                            if ($pdtlbl_idbarloc == 0)
                                $pdtloc_des .= " (prêt)";
                            if (datetolocal($pdtl_datedep) <> "")
                                $pdtloc_des .= " (P)";
                            if (datetolocal($pdtl_dateret) <> "")
                                $pdtloc_des .= " (R)";
                            $pdf->Cell(85, 5, $pdtloc_des, 'LR', 0, 'L', 1);

                            // Du...
                            $pdf->Cell(35, 5, datetolocal($pdtl_datedeb) . " (" . substr($pdtl_heuredeb, 0, 5) . ")", 'LR', 0, 'C', 1);

                            // Au...
                            $pdf->Cell(35, 5, datetolocal($pdtl_datefin) . " (" . substr($pdtl_heurefin, 0, 5) . ")", 'LR', 0, 'C', 1);

                            // 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) {
                                $row2 = encode_str($row2);
                                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) {
                                $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) {
                                    $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) {
                                $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;

                            // Total
                            if ($factTTC == TRUE)
                                $txt = full_num_format(($totttcloc + $totttccaut + $totttcass + $totttccons1 + $totttccons2), '', '', TRUE, '', TRUE, TRUE);
                            else
                                $txt = full_num_format(($tothtloc + $tothtcaut + $tothtass + $tothtcons1 + $tothtcons2), '', '', TRUE, '', TRUE, TRUE);

                            $pdf->Cell(35, 5, $txt, 'LR', 1, 'R', 1);
                            $i++;
                        }
                    }

                    $db->disconnect();

                    $PosY = $pdf->GetY();
                    $pdf->Line(10, $PosY, 200, $PosY); // On "ferme" le tableau

                }

                if ($cgv == TRUE) {
                    $pdf->AddPage();

                    $txt_entete = "";
                    $txt_texte = "";
                    $row = lit_enr_txt("CGV_LOC");
                    if (is_array($row))
                        extract($row);

                    $pdf->SetFont('', 'BU', 10);
                    $txt_ent = parse_txtcorps("cdg", "", $txt_entete, "");
                    $pdf->MultiCell(190, 5, $txt_ent . "\n", 0, 'C');

                    $txt_texte = formatetxt($txt_texte);
                    $txt_cgl = parse_txtcorps("age", $coloc_codeage, $txt_texte, "");
                    $txt_cgl = parse_txtcorps("cli", $coloc_codecli, $txt_cgl, "");
                    $txt_cgl = parse_txtcorps("cdg", "", $txt_cgl, "");
                    $txt_cgl = parse_txtcorps("col", $coloc_code, $txt_cgl, "");

                    $pdf->SetFont('', '', 10);
                    $pdf->WriteHTML($txt_cgl, true, false, true);
                }
                $url_pdf = $url_gfc . "/pj/" . _("Location").'_'.$coloc_code;
                $pdf->Output($url_pdf, 'FI');
                @unlink($url_pdf);
            }
        } else {
            $pbm_locpdf = TRUE;
        }
    } else {
        $pbm_locpdf = TRUE;
    }
} else {
    $pbm_locpdf = TRUE;
}

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