<?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_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_co.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_droits.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_ope.inc.php");
include_once("../inc/fic_prm.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_taxes.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("../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);

$cc = $_GET['cc']; // Récupération code client
$tco = $_GET['tco']; // Récupération type contrat ("a" : actifs, "c" : clots, rien : tous)

$pbm_cocl = FALSE;
$tab_cli = [];

// Niveau 1
if (@$_SESSION["niv_log"] == 1) {
    $tab_cli = give_tab_rcli($_SESSION["code_log"], TRUE);
    if (exist_in_array($cc, $tab_cli) == FALSE)
        $pbm_cocl = TRUE;
} else
    $tab_cli[] = $cc;

if ($cc == "")
    $pbm_cocl = TRUE;
else {
    $row = lit_enr_cli($cc, TRUE);
    if (is_array($row)) {
        $row = encode_str($row);
        extract($row);
        // Contrôle qu'une agence n'essaye pas de "hacker" un client
        if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3)) {
            if (client_age_ok($cli_code, @$_SESSION["code_age"], TRUE) == FALSE)
                $pbm_cocl = TRUE;
        }
    } else
        $pbm_cocl = TRUE;
}

// Niveau 2 = interdit
if (@$_SESSION["niv_log"] == 2)
    $pbm_cocl = TRUE;

if (($tco <> "") AND ($tco <> "o") AND ($tco <> "c"))
    $pbm_cocl = TRUE;

if ($pbm_cocl == FALSE) {
    if (!ini_get('safe_mode'))
        set_time_limit(0); // On demande à PHP de ne pas interrrompre le script pendant le traitement

    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);

    $rech = " SELECT * FROM `" . TBCOCL . "`";
    if (!stristr($rech, "WHERE "))
        $rech .= " WHERE ";
    else
        $rech .= " AND ";
    if (count($tab_cli) > 1)
        $rech .= "(";
    foreach ($tab_cli as $key => $value) {
        $rech .= " " . TBCOCL . ".cocl_codecli='" . $value . "' OR";
    }
    // Suppression du dernier 'OR'
    $rech = substr($rech, 0, strlen($rech) - 3);

    if (count($tab_cli) > 1)
        $rech .= ")";
    if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
        $rech .= " AND cocl_codeage='" . @$_SESSION["code_age"] . "'";
    if ($tco == "o")
        $rech .= " AND cocl_datefin='0000-00-00'";
    if ($tco == "c")
        $rech .= " AND cocl_datefin<>'0000-00-00'";
    $rech .= " ORDER BY cocl_code";

    $rows = $db->get_results($rech, ARRAY_A);

    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('Synthèse de contrat');
    $pdf->SetSubject('Synthèse de contrat');
    $pdf->settheader("coclsm");

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

    if (($rows) AND (count($rows) > 0)) {
        foreach ($rows as $row) {
            $row = encode_str($row);
            $pdf->AddPage();

            extract($row);

            $codeadev = give_dev_codea_defaut_cocl($cocl_code, $_SESSION["lng"], FALSE);

            $cocl_datecreaf = datetolocal($cocl_datecrea);
            $cocl_datesigf = datetolocal($cocl_datesig);
            $cocl_datefinf = datetolocal($cocl_datefin);

            $rech2 = "SELECT * FROM `" . TBCO . "` WHERE `co_code` = '$cocl_codeco'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2) {
                $row2 = encode_str($row2);
                extract($row2);
            }

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

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

            $ent_cli = ""; // Détail
            $row2 = lit_enr_cli($cocl_codecli, FALSE);
            if (is_array($row2)) {
                $row2 = encode_str($row2);
                extract($row2);
                if ($cli_ste <> "") {
                    $ent_cli .= $cli_ste . "\n";
                    $ent_cli .= $cli_civilite . " " . $cli_prenom . " " . $cli_nom . "\n";
                } else {
                    $ent_cli .= $cli_civilite . " " . $cli_prenom . " " . $cli_nom . "\n\n"; // Double saut pour compenser le manque de la société
                }
                $pdf->SetFont('', 'B', 10);
                $pdf->MultiCell(190, 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, FALSE) . " " . Retourneville_Id($cli_ville, FALSE) . "\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(190, 4, $ent_cli, 0, 'L');
            }

            /**********************************/
            /* Impression de la fiche contrat */
            /**********************************/

            $pdf->Ln(1);
            $pdf->SetFont('', '', 10);
            $pdf->SetFillColor(0);
            $pdf->SetTextColor(255);
            $pdf->Cell(80, 5, _("Contrat") . ' : ' . $cocl_code, 1, 0, 'C', 1);
            $pdf->SetFillColor(255);
            $pdf->SetTextColor(0);

            $echeance_ok = TRUE;
            $txterr = "";
            if (datetolocal($cocl_datef) <> "") {
                if (echeance_depassee_date($datea, $cocl_datef) == TRUE)
                    $echeance_ok = FALSE;
            }
            if ((datetolocal($cocl_datesig) == "") OR (datetolocal($cocl_datefin) <> "") OR ($echeance_ok == FALSE)) {
                if (datetolocal($cocl_datesig) == "") {
                    $txterr .= _("Non Signé");
                    if ((datetolocal($cocl_datefin) <> "") OR ($echeance_ok == FALSE))
                        $txterr .= " / ";
                }
                if ((datetolocal($cocl_datefin) <> "") OR ($echeance_ok == FALSE))
                    $txterr .= _("Terminé");
            }
            if ($txterr <> "") {
                $pdf->Cell(40, 5, _("Créé le") . ' ' . datetolocal($cocl_datecrea), 0, 0, 'C', 0);
                $pdf->SetFont('', 'BI', 10);
                $pdf->Cell(70, 5, $txterr, 0, 1, 'C', 0);
            } else
                $pdf->Cell(40, 5, _("Créé le") . ' ' . datetolocal($cocl_datecrea), 0, 1, 'C', 0);

            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Site Principal") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            if ($cocl_sitep == 1)
                $pdf->Cell(30, 4, _("Oui"), 0, 1, 'L', 0);
            else
                $pdf->Cell(30, 4, _("Non"), 0, 1, 'L', 0);

            // Liste des sites, inclues les annexes
            $lstsites = cree_str_lstsites_cocl($cocl_code, TRUE, 0, FALSE);

            if ($lstsites <> "") {
                $pdf->SetFont('', 'B', 8);
                $pdf->Cell(30, 4, _("Sites Secondaires") . ' : ', 0, 0, 'R', 0);
                $pdf->SetFont('', '', 8);
                $tab_scli = explode("-", $lstsites);
                foreach ($tab_scli as $key => $value) {
                    if ($key <> 0)
                        $pdf->Cell(30);
                    $nom_site = "";
                    $rech2 = "SELECT scli_ste,scli_adr1,scli_adr2,scli_ville FROM `" . TBSCLI . "` WHERE `scli_id` = '$value'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2) {
                        $row2 = encode_str($row2);
                        extract($row2);
                        $nom_site .= $scli_ste;
                        $nom_site .= ", ";
                        $nom_site .= $scli_adr1;
                        $nom_site .= ", ";
                        if ($scli_adr2 <> "") {
                            $nom_site .= $scli_adr2;
                            $nom_site .= ", ";
                        }
                        $nom_site .= Retournecp_Id($scli_ville, FALSE) . " " . Retourneville_Id($scli_ville, FALSE);
                    }
                    $pdf->Cell(30, 4, $nom_site, 0, 1, 'L', 0);
                }
            }

            // Agence
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Agence") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $rech2 = "SELECT * FROM `" . TBAGE . "` WHERE age_code = '$cocl_codeage'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2) {
                $row2 = encode_str($row2);
                extract($row2);
            }
            $age_nomcode = $age_nom . " (" . $age_code . ")";
            $pdf->Cell(95, 4, $age_nomcode, 0, 1, 'L', 0);
            // Contrat type
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Contrat type") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $pdf->Cell(160, 4, $co_nom, 0, 1, 'L', 0);
            // Fréquence
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Fréquence") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $rech2 = "SELECT * FROM `" . TBPER . "` WHERE `per_code` = '" . $cocl_codeper . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2) {
                $row2 = encode_str($row2);
                extract($row2);
            }
            $pdf->Cell(160, 4, $per_desc, 0, 1, 'L', 0);
            // Délai
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Délai") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $pdf->Cell(160, 4, $cocl_delai . ' ' . _("heures ouvrées"), 0, 1, 'L', 0);
            // Temps / Type
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Temps / Type") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $pdf->Cell(160, 4, heure_to_hhmm(sec_heure($cocl_nbs, 3, TRUE)) . " / " . $typ_horaire[$cocl_typh], 0, 1, 'L', 0);
            if ($cocl_typh == 1) {
                $pdf->Cell(30);
                $pdf->SetFont('', 'I', 7);
                $pdf->Cell(160, 4, _("Solde") . ' : ' . heure_to_hhmm(sec_heure(solde_reel_contrat_sec($cocl_code, FALSE), 3, TRUE)) . ' (' . _("Heures à sortir") . ' : ' . heure_to_hhmm(sec_heure(tps_trav_non_clot_sec($cocl_code, FALSE), 3, TRUE)) . ")", 0, 1, 'L', 0);
            }

            // Création de la liste des reports affectés à ce contrat (s'il y en a)
            $rech2 = "SELECT * FROM `" . TBCOCLR . "` WHERE coclr_cocldest = '" . $cocl_code . "' ORDER BY coclr_codeint";
            $rows2 = $db->get_results($rech2, ARRAY_A);

            if (($rows2) AND (count($rows2) > 0)) {
                $totalreport = 0;

                // Titres
                $pdf->SetLineWidth(0.1);
                $pdf->SetFont('', 'IU', 7);
                $pdf->Cell(30, 4, '', 0, 0, 'R', 0);
                $pdf->Cell(120, 4, _("Détails des reports affectés à ce contrat"), 1, 0, 'C');
                $pdf->Cell(30, 4, _("Durée"), 1, 1, 'C');
                $pdf->SetFont('', 'I', 8);

                foreach ($rows2 as $row2) {
                    $row2 = encode_str($row2);
                    extract($row2);
                    $pdf->Cell(30, 4, '', 0, 0, 'R', 0);
                    if ($coclr_nbsrep > 0)
                        $txt = _("Report non consommé du contrat") . ' ';
                    else
                        $txt = _("Déduction sur-consommé du contrat") . ' ';
                    $txt .= $coclr_coclori;
                    if ($coclr_codeint <> '')
                        $txt .= ' (' . _("Intervention") . ' ' . $coclr_codeint . ')';
                    else
                        $txt .= ' (' . _("Clôture manuelle") . ')';
                    $pdf->Cell(120, 4, $txt, 1, 0, 'L');

                    $pdf->Cell(30, 4, heure_to_hhmm(sec_heure($coclr_nbsrep, 3, TRUE)), 1, 1, 'C');
                    $totalreport = $totalreport + $coclr_nbsrep;
                }
                $PosY = $pdf->GetY();
                $pdf->Line(40, $PosY, 150, $PosY); // On "ferme" le tableau
                $pdf->SetLineWidth(0.2);
                $pdf->Cell(150, 4, _("Total général") . ' : ', 0, 0, 'R', 0);
                $pdf->Cell(30, 4, heure_to_hhmm(sec_heure($totalreport, 3, TRUE)), 0, 1, 'C');
            }

            // Report heures non consommées
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Report heures non cons.") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $pdf->Cell(160, 4, $non_oui[$cocl_nbsnc], 0, 1, 'L', 0);
            // Report heures dépassées
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Report heures dépassées") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $pdf->Cell(160, 4, $non_oui[$cocl_nbsdep], 0, 1, 'L', 0);
            // Validité
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Validité") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $txtval = datetolocal($cocl_dated) . _(" "._("au")." ");
            if (datetolocal($cocl_datef) <> "")
                $txtval .= datetolocal($cocl_datef);
            else
                $txtval .= " --- ";
            $pdf->Cell(160, 4, $txtval . ', ' . _("avec préavis de") . ' ' . $cocl_preavis . ' ' . _("jours") . '.', 0, 1, 'L', 0);
            // Renouvellement automatique
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Renouvellement auto.") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $pdf->Cell(160, 4, $non_oui[$cocl_renauto], 0, 1, 'L', 0);
            // Matériel
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Matériel") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $pdf->MultiCell(160, 4, $cocl_matos, 0, 'L');
            // Date Signature
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Signé le") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $pdf->Cell(160, 4, datetolocal($cocl_datesig), 0, 1, 'L', 0);
            // Signé par
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("Signé par") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $pdf->Cell(160, 4, $cocl_signe, 0, 1, 'L', 0);
            // Relancé
            if (datetolocal($cocl_daterel) <> "") {
                $pdf->SetFont('', 'B', 8);
                $pdf->Cell(30, 4, _("Relancé") . ' : ', 0, 0, 'R', 0);
                $pdf->SetFont('', '', 8);
                $pdf->Cell(160, 4, datetolocal($cocl_daterel) . '(' . $cocl_nivrel . ' ' . _("fois") . ')', 0, 1, 'L', 0);
            }

            if (datetolocal($cocl_datesig) <> "") {
                // renouvelé
                $pdf->SetFont('', 'B', 8);
                $pdf->Cell(30, 4, _("Renouvelé") . ' : ', 0, 0, 'R', 0);
                $pdf->SetFont('', '', 8);
                $txtrnum = $non_oui[$cocl_renouv];
                if ($cocl_rnum <> "")
                    $txtrnum .= ' (' . _("Nouveau contrat") . ' : ' . $cocl_rnum . ')';
                $pdf->Cell(160, 4, $txtrnum, 0, 1, 'L', 0);

                // Clôt
                $pdf->SetFont('', 'B', 8);
                $pdf->Cell(30, 4, _("Clôt le") . ' : ', 0, 0, 'R', 0);
                $pdf->SetFont('', '', 8);
                $pdf->Cell(160, 4, datetolocal($cocl_datefin), 0, 1, 'L', 0);
            }
            // Prix HT
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("PV HT") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $pdf->Cell(160, 4, full_num_format($cocl_pvht, '', '', TRUE, '', TRUE, TRUE), 0, 1, 'L', 0);

            // TVA
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("TVA") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $row = lit_enr_tva($cocl_codetva, TRUE);
            if (is_array($row)) {
                $row = encode_str($row);
                extract($row);
            }

            $pdf->Cell(160, 4, $tva_desc . " (" . full_num_format($tva_tx, '', '', FALSE, '', TRUE, TRUE) . "%)", 0, 1, 'L', 0);

            // Prix TTC
            $pdf->SetFont('', 'B', 8);
            $pdf->Cell(30, 4, _("PV TTC") . ' : ', 0, 0, 'R', 0);
            $pdf->SetFont('', '', 8);
            $txtva = $tva_tx / 100;
            $pvttc = round(($cocl_pvht * (1 + $txtva)) * 100) / 100;
            $pdf->Cell(160, 4, full_num_format($pvttc, '', '', TRUE, '', TRUE, TRUE), 0, 1, 'L', 0);

            /**************/
            /* Historique */
            /**************/
            $pdf->Ln(2);
            $pdf->SetFont('', 'BU', 10);
            $pdf->Cell(190, 5, _("HISTORIQUE"), '', 1, 'C');
            $pdf->Ln(2);

            $rech2 = "SELECT " . TBINT . ".int_code, " . TBINT . ".int_datedde, " . TBINT . ".int_codecli, " . TBINT . ".int_codecocl, ";
            $rech2 .= TBINT . ".int_codeage, " . TBINT . ".int_daterdv, " . TBINT . ".int_durees, " . TBINT . ".int_codestatut, ";
            $rech2 .= TBINT . ".int_datefin, " . TBCOCLH . ".coclh_codecli, " . TBCOCLH . ".coclh_codecocl, " . TBCOCLH . ".coclh_codeint ";
            $rech2 .= "FROM `" . TBINT . "`, `" . TBCOCLH . "`";
            $rech2 .= " WHERE " . TBINT . ".int_codecocl = '" . $cocl_code . "'";
            $rech2 .= " AND " . TBINT . ".int_code = " . TBCOCLH . ".coclh_codeint";
            $rows2 = $db->get_results($rech2, ARRAY_A);

            if (($rows2) AND (count($rows2) > 0)) {
                // Titres
                $pdf->SetFont('', 'IU', 7);
                $pdf->Cell(30, 4, _("Intervention"), 1, 0, 'C');
                $pdf->Cell(14, 4, _("Début"), 1, 0, 'C');
                $pdf->Cell(14, 4, _("RDV"), 1, 0, 'C');
                $pdf->Cell(14, 4, _("Fin"), 1, 0, 'C');
                $pdf->Cell(30, 4, _("Agence"), 1, 0, 'C');
                $pdf->Cell(40, 4, _("Durée"), 1, 0, 'C');
                $pdf->Cell(45, 4, _("Statut"), 1, 1, 'C');
                $pdf->SetFont('', 'I', 8);

                $totalgen = 0;
                $totalgenoff = 0;

                foreach ($rows2 as $row2) {
                    $row2 = encode_str($row2);
                    extract($row2);
                    $pdf->SetFont('', '', 7);
                    $pdf->Cell(30, 4, $int_code, 'LR', 0, 'L');
                    $pdf->Cell(14, 4, datetolocal($int_datedde), 'LR', 0, 'C');
                    if (datetolocal($int_daterdv) <> "")
                        $pdf->Cell(14, 4, datetolocal($int_daterdv), 'LR', 0, 'C');
                    else
                        $pdf->Cell(14, 4, "-", 'LR', 0, 'C');
                    if (datetolocal($int_datefin) <> "")
                        $pdf->Cell(14, 4, datetolocal($int_datefin), 'LR', 0, 'C');
                    else
                        $pdf->Cell(14, 4, "-", 'LR', 0, 'C');
                    $pdf->Cell(30, 4, $int_codeage, 'LR', 0, 'C');

                    // Récupération du total travaillé
                    $nbtotalsec = 0;
                    $nbtotalsecoff = 0;

                    // Récupération total travaillé
                    $rech3 = "SELECT * FROM `" . TBINTH . "` WHERE `inth_codeint`= '" . $int_code . "'";
                    $rows3 = $db->get_results($rech3, ARRAY_A);
                    if (($rows3) AND (count($rows3) > 0)) {
                        foreach ($rows3 as $row3) {
                            $row3 = encode_str($row3);
                            $nbtotalsec = $nbtotalsec + diff_time_sec($row3["inth_heurea"], $row3["inth_heured"]);
                            if ($row3["inth_offert"] == 1)
                                $nbtotalsecoff = $nbtotalsecoff + diff_time_sec($row3["inth_heurea"], $row3["inth_heured"]);
                        }
                    }

                    $totalgen += $nbtotalsec;
                    $totalgenoff += $nbtotalsecoff;

                    // Si un utilisateur a quitté avant de sauver (bien qu'ayant modifié le détail horaire,
                    // on remet en état car seul le détail horaire et technicien enregistré est valable
                    if ($int_durees <> $nbtotalsec) {
                        $rech3 = "UPDATE `" . TBINT . "` SET `int_datemod`='" . $datea . "', `int_codemod`='" . $codeuser . "',`int_durees`='" . $nbtotalsec . "'";
                        $rech3 .= " WHERE `int_code` = '" . $int_code . "'";
                        $db->query($rech3);
                        $int_durees = $nbtotalsec;
                    }

                    $txttps = "";
                    if ($int_durees <> 0)
                        $txttps .= heure_to_hhmm(sec_heure($int_durees, 3, TRUE));
                    else
                        $txttps .= "-";
                    if ($nbtotalsecoff <> 0)
                        $txttps .= ' (' . _("Offert") . ' : ' . heure_to_hhmm(sec_heure($nbtotalsecoff, 3, TRUE)) . ')';
                    if ($txttps <> "")
                        $pdf->Cell(40, 4, $txttps, 'LR', 0, 'C');
                    else
                        $pdf->Cell(40, 4, "-", 'LR', 0, 'C');

                    // Statut
                    $rech4 = "SELECT statut_desc FROM `" . TBSTA . "` WHERE statut_code = '$int_codestatut'";
                    $row4 = $db->get_row($rech4, ARRAY_A);
                    $txtstatut = "";

                    if ($row4) {
                        $row4 = encode_str($row4);
                        extract($row4);
                        $txtstatut = $statut_desc;
                    }
                    if ($txtstatut <> "")
                        $pdf->Cell(45, 4, $txtstatut, 'LR', 1, 'C');
                    else
                        $pdf->Cell(45, 4, $int_codestatut, 'LR', 1, 'C');

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

                // Totaux
                $pdf->SetFont('', 'I', 7);
                $pdf->Cell(72, 4, '', 0, 0, 'C');
                $pdf->Cell(30, 4, _("Total général") . ' : ', 0, 0, 'R');
                if ($totalgen <> 0)
                    $pdf->Cell(40, 4, heure_to_hhmm(sec_heure($totalgen, 3, TRUE)), 0, 1, 'C');
                else
                    $pdf->Cell(40, 4, "-", 0, 1, 'C');

                if ($totalgenoff <> 0) {
                    $pdf->SetFont('', 'I', 7);
                    $pdf->Cell(72, 4, '', 0, 0, 'C');
                    $pdf->Cell(30, 4, _("Dont offert") . ' : ', 0, 0, 'R');
                    $pdf->Cell(40, 4, heure_to_hhmm(sec_heure($totalgenoff, 3, TRUE)), 0, 1, 'C');
                }
            } else {
                $pdf->SetFont('', 'BI', 8);
                $pdf->Cell(190, 4, _("Aucun historique pour ce contrat"), 0, 1, 'C', 0);
                $pdf->SetFont('', '', 8);
            }
        }
    } else {
        $pdf->AddPage();
        $pv_det_cli = $pv_ent_age; // Récupération de la position sous l'entête agence pour le pointeur détail client
        $pdf->SetLineWidth(0.4);
        $pdf->SetY($pv_det_cli); // Renvoie le pointeur SOUS l'entête agence
        $pdf->SetFont('', 'BI', 8);
        switch ($tco) {
            case "o" :
                $pdf->Cell(190, 4, _("Aucun contrat ouvert pour ce client"), 0, 1, 'C', 0);
                break;
            case "c" :
                $pdf->Cell(190, 4, _("Aucun contrat clot pour ce client"), 0, 1, 'C', 0);
                break;
            default :
                $pdf->Cell(190, 4, _("Aucun contrat pour ce client"), 0, 1, 'C', 0);
                break;
        }
        $pdf->SetFont('', '', 8);
    }
    $pdf->Output(_("Contrat") . '_' . $cocl_code, 'I');

    $db->disconnect();
}
?>