<?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");

$aff_sfact = FALSE;
switch ($_SESSION["niv_log"]) {
    // Agences
    case 4 :
        $row = lit_enr_age($_SESSION["code_age"], FALSE);
        if (is_array($row)) {
            extract($row);
            if (intval($age_fact) == 1)
                $aff_sfact = TRUE;
        }
        break;

    case 5 :
    case 9 :
        $aff_sfact = TRUE;
        break;
    default :
        break;
}

// Tableau des entêtes (largeur => libellé)
$tab_titre_col_xls = [
    _("Urgence"),
    _("Intervention"),
    _("Suivi interne"),
    _("Ordre client"),
    _("Catégorie"),
    _("Date Saisie"),
    _("Travail à effectuer"),
    _("Intervention(s) le"),
    _("Prochain RDV"),
    _("Dernier RDV pris"),
    _("Prévenu le"),
    _("Terminé le"),
    _("Travail effectué"),
    _("Code client"),
    _("Détail client"),
    _("Code site"),
    _("Détail site"),
    _("Technicien"),
    _("Statut"),
    _("Facturé"),
    _("Montant")
];

$no_col = 1;
$no_ligne = 3;
foreach ($tab_titre_col_xls as $value) {
    $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, $value);
    $style_cellule = $feuille->getStyle(numtochar($no_col) . '' . $no_ligne);
    $styleFont = $style_cellule->getFont();
    $styleFont->setBold(true);
    $styleFont->setUnderline(true);
    $feuille->getColumnDimension(numtochar($no_col))->setWidth(20);
    $no_col++;
}

if ($numrows > 0) {
    $rows = $db->get_results($rech, ARRAY_A); // On met en cache le résultat
    $no_col = 1;
    $no_ligne++;

    foreach ($rows as $row) {
        set_time_limit(0);

        // $row = $db->get_row(null,ARRAY_A,$nb_lu);

        $aff_ligne = TRUE;

        // Au cas où l'intervention n'est pas encore affectée et que l'on est en mode technicien agence ou agence,
        // il faut vérifier si le client fait partie de la zone de chalandise de l'agence
        if ((($_SESSION["niv_log"] == 3) OR ($_SESSION["niv_log"] == 4)) AND (trim($row["int_codeage"]) == "")) {
            if (trim($row["cli_prop"]) == "") {
                $rech2 = "SELECT COUNT(*) FROM `" . TBZCC . "` WHERE `zcc_code` = '" . $_SESSION["code_age"] . "' AND `zcc_codepays` = '" . $row["cli_codepays"] . "' AND `zcc_codedep` = '" . $row["cli_dep"] . "'";
                $nbres2 = $db->get_var($rech2);
                if ($nbres2 == 0)
                    $aff_ligne = FALSE;
            } else {
                if (trim($row["cli_prop"]) <> $_SESSION["code_age"])
                    $aff_ligne = FALSE;
            }
        }

        if ($aff_ligne == TRUE) {
            // Urgence
            $couleur = give_coul_urg($row["int_urg"], FALSE);
            $detail = give_nom_urg($row["int_urg"], FALSE);
            $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, utf8_encode($detail), PHPExcel_Cell_DataType::TYPE_STRING);
            $no_col++;

            // Code intervention
            $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, utf8_encode($row["int_code"]), PHPExcel_Cell_DataType::TYPE_STRING);
            $no_col++;

            // S.I.
            $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, utf8_encode($row["int_codeman"]), PHPExcel_Cell_DataType::TYPE_STRING);
            $no_col++;

            // O.C.
            $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, utf8_encode($row["int_codeocli"]), PHPExcel_Cell_DataType::TYPE_STRING);
            $no_col++;

            // Catégorie
            $rechcat = "SELECT intc_desc FROM `" . TBINTC . "` WHERE intc_code = '" . $row["int_codecat"] . "'";
            $rowcat = $db->get_row($rechcat, ARRAY_A);
            if ($rowcat) {
                extract($rowcat);
                $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, utf8_encode($intc_desc), PHPExcel_Cell_DataType::TYPE_STRING);
            } else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;

            // Date
            /*
            $date = explode("/",$date);
            $time = PHPExcel_Shared_Date::FormattedPHPToExcel($date[0], $date[1], $date[2]);
            $sheet->setCellValue('E1', $time);
            $sheet->getStyle('E1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY);
             */
            $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(datetolocal($row["int_datedde"])));
            $no_col++;

            // Problème
            // Construction de la chaîne des problèmes sélectionnés pour cette intervention
            $lstpbm = cree_str_lstpbm_int($row["int_code"],FALSE);
            $str_pbm = cre_str_pbm_int($lstpbm,FALSE);

            // Texte complet du travail effectué
            $txt_pbm_full = '';
            if ($str_pbm <> '')
                $txt_pbm_full .= "*** "._("RELEVE DE PROBLEMES")."\n\n" . str_replace("%%", "\n", $str_pbm) . "\n\n";
            $txt_pbm_full .= utf8_encode($row["int_pbm"]);

            if ($txt_pbm_full <> '')
                $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, $txt_pbm_full, PHPExcel_Cell_DataType::TYPE_STRING);
            else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;

            // Interventions le
            $txt_inth = "";
            $rech2 = "SELECT * FROM `" . TBINTH . "`";
            $rech2 .= " WHERE inth_codeint='" . $row["int_code"] . "'";
            $rech2 .= " ORDER BY inth_date, inth_heurea";
            $rows2 = $db->get_results($rech2, ARRAY_A);
            if ($rows2) {
                $ninth = 1;
                foreach ($rows2 as $row2) {
                    extract($row2);
                    if ($ninth > 1)
                        $txt_inth .= "\n";
                    $txt_inth .= datetolocal($inth_date) . " "._("à")." " . substr($inth_heurea, 0, 5);
                    if (intval($inth_idrdv) > 0) {
                        $row3 = lit_enr_rdv_id(intval($inth_idrdv), FALSE, '', FALSE);
                        if (is_array($row3)) {
                            $row3 = encode_str($row3);
                            extract($row3);
                            $txt_inth .= " ("._("RDV concerné")." : " . datetolocal($rdv_date) . " "._("à")." " . substr($rdv_heure, 0, 5) . ")";
                        } else
                            $txt_inth .= " ("._("RDV concerné")." : " . $inth_idrdv . ")";

                    }
                    $ninth++;
                }
                $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, $txt_inth, PHPExcel_Cell_DataType::TYPE_STRING);
                $style_curr = $feuille->getStyle(numtochar($no_col) . '' . $no_ligne);
                $align_curr = $style_curr->getAlignment();
                $align_curr->setWrapText(true);
            } else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;

            // Prochain RDV
            $row2 = next_rdv('1', $row["int_code"], FALSE);
            if (is_array($row2))
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(datetolocal($row2["rdv_date"])));
            else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;

            // Dernier RDV pris
            $row2 = last_rdv('1', $row["int_code"], FALSE);
            if (is_array($row2))
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(datetolocal($row2["rdv_date"])));
            else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;


            // Prévenu le
            if (datetolocal($row["int_dateprev"]) <> "")
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(datetolocal($row["int_dateprev"])));
            else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;

            // Terminé le
            if (datetolocal($row["int_datefin"]) <> "")
            {
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(datetolocal($row["int_datefin"])));
            }
            else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;

            // Travail effectué
            // Construction de la chaîne des problèmes sélectionnés pour cette intervention
            $tab_pbmfait = cre_tab_pbmfait_int($lstpbm,FALSE);
            $str_pbmfait = cre_str_pbmfait_int($tab_pbmfait,FALSE);

            // Texte des travaux effectués (saisie manuelle)
            $txt_fait = cree_txt_fait_int($row["int_code"], FALSE, FALSE, FALSE, TRUE);

            // Texte complet du travail effectué
            $txt_fait_full = '';
            if ($str_pbmfait <> '')
                $txt_fait_full .= "*** "._("TRAITEMENT RELEVE DE PROBLEMES")."\n\n" . str_replace("%%", "\n", $str_pbmfait) . "\n\n";
            $txt_fait_full .= $txt_fait;

            if ($row["int_fait"] <> "") {
                if ($txt_fait_full <> "")
                    $txt_fait_full .= "\n\n";
                $txt_fait_full .= "*** "._("OBSERVATIONS")."\n\n";
                $txt_fait_full .= utf8_encode($row["int_fait"])."\n";
            }

            if ($txt_fait_full <> '')
                $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, $txt_fait_full, PHPExcel_Cell_DataType::TYPE_STRING);
            else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;

            // Code client
            $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, utf8_encode($row["int_codecli"]), PHPExcel_Cell_DataType::TYPE_STRING);
            $no_col++;

            // Détail client
            $detailcli = "";
            if ($row["cli_ste"] <> "")
                $detailcli .= $row["cli_ste"] . " - ";
            $detailcli .= $row["cli_civilite"] . " " . $row["cli_nom"] . " " . $row["cli_prenom"];
            $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode($detailcli));
            $no_col++;


            // Code et Site client
            $sitecli = "";
            switch (trim($row["int_idsite"])) {
                case "0" :
                    $sitecli = _("Site principal");
                    break;
                default :
                    $rech2 = "SELECT scli_code,scli_ste FROM `" . TBSCLI . "` WHERE `scli_id` = '" . trim($row["int_idsite"]) . "'";
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2) {
                        extract($row2);
                        $sitecli .= $scli_ste;
                    }
            }

            if (trim($scli_code) <> "")
                $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, utf8_encode($scli_code), PHPExcel_Cell_DataType::TYPE_STRING);
            else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;
            $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode($sitecli));
            $no_col++;

            // Technicien / Agence traitants
            if (($row["int_codetec"] <> "") AND ($row["int_codetec"] <> "-1"))
                $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, utf8_encode($row["int_codetec"]), PHPExcel_Cell_DataType::TYPE_STRING);
            else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;

            // Affichage Icône Statut
            $rech2 = "SELECT statut_desc FROM `" . TBSTA . "` WHERE statut_code = '" . $row["int_codestatut"] . "'";
            $row2 = $db->get_row($rech2, ARRAY_A);
            if ($row2) {
                extract($row2);
                if ($statut_desc <> "")
                    $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, utf8_encode($statut_desc), PHPExcel_Cell_DataType::TYPE_STRING);
                else
                    $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, utf8_encode($row["int_codestatut"]), PHPExcel_Cell_DataType::TYPE_STRING);
            } else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;

            // Facturé
            $nb_intf = nb_intf_int($row["int_code"], FALSE);
            if ($nb_intf > 0) {
                $rech2 = "SELECT * FROM `" . TBINTF . "`";
                $rech2 .= " WHERE intf_codeint = '" . $row["int_code"] . "'";
                $rech2 .= "ORDER BY intf_datefac";
                $rows2 = $db->get_results($rech2, ARRAY_A);
                if ($rows2) {
                    $nfact = 1;
                    foreach ($rows2 as $row2) {
                        $txt_fact = "";
                        $txt_montant = "";
                        extract($row2);
                        if (trim($intf_numf) <> "")
                            $txt_fact = _("N°") . ' ' . $intf_numf . " (" . datetolocal($intf_datefac) . ")";
                        else {
                            if ($intf_fnulle == 1)
                                $txt_fact = _("Non facturé");
                            if (trim($intf_nota) <> "")
                                $txt_fact .= " (" . utf8_encode(formatetxt($intf_nota)) . ")";
                        }
                        $txt_montant = floatval($intf_mt);
                        // 1er résultat : on reste dans la même ligne Excel
                        // Sinon, on rajoute une ligne vierge
                        if ($nfact > 1)
                        {
                            $no_ligne++;
                            // Colonne Urgence
                            $feuille->setCellValueExplicit(numtochar(1) . '' . $no_ligne, utf8_encode(' '), PHPExcel_Cell_DataType::TYPE_STRING);
                            // Colonne Code intervention
                            $feuille->setCellValueExplicit(numtochar(2) . '' . $no_ligne, utf8_encode(' '), PHPExcel_Cell_DataType::TYPE_STRING);

                            // Colonne S.I.
                            $feuille->setCellValueExplicit(numtochar(3) . '' . $no_ligne, utf8_encode(' '), PHPExcel_Cell_DataType::TYPE_STRING);

                            // Colonne O.C.
                            $feuille->setCellValueExplicit(numtochar(4) . '' . $no_ligne, utf8_encode(' '), PHPExcel_Cell_DataType::TYPE_STRING);

                            // Colonne catégorie
                            $feuille->setCellValue(numtochar(5) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne Date
                            $feuille->setCellValue(numtochar(6) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne Problème
                            $feuille->setCellValue(numtochar(7) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne Interventions
                            $feuille->setCellValue(numtochar(8) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne prochain RDV
                            $feuille->setCellValue(numtochar(9) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne dernier RDV
                            $feuille->setCellValue(numtochar(10) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne prévenu le
                            $feuille->setCellValue(numtochar(11) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne terminé le
                            $feuille->setCellValue(numtochar(12) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne travail effectué
                            $feuille->setCellValue(numtochar(13) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne code client
                            $feuille->setCellValue(numtochar(14) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne détail client
                            $feuille->setCellValue(numtochar(15) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne code site
                            $feuille->setCellValue(numtochar(16) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne détail site
                            $feuille->setCellValue(numtochar(17) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne technicien
                            $feuille->setCellValue(numtochar(18) . '' . $no_ligne, utf8_encode(' '));

                            // Colonne statut
                            $feuille->setCellValue(numtochar(19) . '' . $no_ligne, utf8_encode(' '));

                            $no_col = 20;
                        }
                        $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, $txt_fact, PHPExcel_Cell_DataType::TYPE_STRING);
                        $style_curr = $feuille->getStyle(numtochar($no_col) . '' . $no_ligne);
                        $align_curr = $style_curr->getAlignment();
                        $align_curr->setWrapText(true);
                        $no_col++;
                        $feuille->setCellValueExplicit(numtochar($no_col) . '' . $no_ligne, $txt_montant, PHPExcel_Cell_DataType::TYPE_NUMERIC);
                        $style_curr = $feuille->getStyle(numtochar($no_col) . '' . $no_ligne);
                        $align_curr = $style_curr->getAlignment();
                        $align_curr->setWrapText(true);

                        $nfact++;
                    }
                }
            } else
                $feuille->setCellValue(numtochar($no_col) . '' . $no_ligne, utf8_encode(' '));
            $no_col++;
        }

        // Fin affichage résumé intervention

        @session_start();
        $_SESSION['generationStatut'] = $no_ligne . ' '._("lignes generees");

        session_write_close();

        $no_ligne++;
        $no_col = 1;
    }
}
?>
