<?php 
/* etiquettes_adherents.php
 * - Generation d'un PDF d'étiquettes
 * Copyright (c) 2003 Frédéric Jaqcuot
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
    include("includes/config.inc.php"); 
    include(WEB_ROOT."includes/database.inc.php"); 
    include(WEB_ROOT."includes/functions.inc.php"); 
    include(WEB_ROOT."includes/lang.inc.php"); 
    include(WEB_ROOT."includes/session.inc.php"); 
    include(WEB_ROOT."includes/phppdflib/phppdflib.class.php");
    
    if ($_SESSION["logged_status"]==0) 
        die();
    if ($_SESSION["admin_status"]==0) 
        die();
    $mailing_adh = array();
    if (isset($_POST["mailing_adh"]))
    {
        while (list($key,$value)=each($_POST["mailing_adh"]))
            $mailing_adh[]=$value;
    }
    else
        die();
    $requete = "SELECT id_adh, nom_adh, prenom_adh, adresse_adh,
                                titre_adh, cp_adh, ville_adh, pays_adh, adresse2_adh, panier_adh, pain_adh
                                FROM ".PREFIX_DB."adherents
                               WHERE ";
    $where_clause = "";
    if ( ! isset($_POST['mois']))
        $mois=date("m");
    else
        $mois=$_POST['mois'];
    $annee=date("Y");
       if ($mois == "13")
    {
        $mois="01";
        $annee=$annee+1;
    }
    $datenow=$annee . "-" . $mois . "-0";
    while(list($key,$value)=each($mailing_adh))
    {
        if ($where_clause!="")
            $where_clause .= " OR ";
        $where_clause .= "id_adh=".$DB->qstr($value);
    }
    $requete .= $where_clause." ORDER by nom_adh, prenom_adh;";
    $resultat = &$DB->Execute($requete);

    $pdf = new pdffile;
    $pdf->set_default('margin', 0);
    $firstpage = $pdf->new_page("a4-landscape");
    $param["height"] = PREF_ETIQ_CORPS;
    $param["fillcolor"] = $pdf->get_color('#000000');
    $param["align"] = "center";
    $param["width"] = 1;
    $param["strokecolor"] = $pdf->get_color('#000000');
    if ($resultat->EOF)
        die();
    $yorigin=545;
    $xorigin=round(PREF_ETIQ_MARGES*2.835);
    $row=1;
    $nb_etiq=0;
    $concatname = "";
    $i=0;
    $j=0;
    while (date("Ym",strtotime("$j saturday", strtotime($datenow))) <= $annee.$mois)
    {
        $j++;
        if (date("m",strtotime("$j saturday", strtotime($datenow))) == $mois) {
            $panier[$i][0]=0;
            $panier[$i][1]=0;
            $date[$i]=date("d/m/Y",strtotime($j . " saturday", strtotime($datenow)));
            $date2[$i]=date("Y-m-d",strtotime($j . " saturday", strtotime($datenow)));
            $i++;
        }
        if ($j > 10) {
            print "Problème dans le calcul des mois !";
            die();
        }
    }
    $col = $i;
    $ligne = 18;
    $x1 = 70;
    $x2 = 300;
    $ecart = 480/$col;
    $hecart = 490/$ligne;


    while (!$resultat->EOF)
    {
        $y1 = $yorigin-(($row-1)*($hecart));
        $y2 = $y1 - $hecart;
        if ($row==1)
        {
            $param["font"] = "Helvetica-Bold";
            $pdf->draw_rectangle($yorigin+20, $x1, $yorigin, $x2, $firstpage, $param);
            $pdf->draw_paragraph($yorigin+20, $x1, $yorigin, $x2, "Nom", $firstpage, $param);
                $pdf->draw_rectangle($yorigin+20, $x2, $yorigin, $x2+40, $firstpage, $param);
                        $pdf->draw_paragraph($yorigin+20, $x2, $yorigin, $x2+40, "Prix", $firstpage, $param);
            $i=0;
            while($i<$col)
            {
                $xdr1=$x2+40+$i*$ecart;
                $xdr2=$x2+40+($i+1)*$ecart;
                $pdf->draw_paragraph($yorigin+20, $xdr1, $yorigin, $xdr2, $date[$i], $firstpage, $param);
                $pdf->draw_rectangle($yorigin+20, $xdr1, $yorigin, $xdr2, $firstpage, $param);
                $i++;
            }
        }
        if ( ($_POST['panier'] == "legume" && $resultat->fields[9] != "-1") || ($_POST['panier'] == "pain" && $resultat->fields[10] != "0") ) {
        $nom_adh_ext="";
        switch($resultat->fields[4])
        {
            case "1" :
                $nom_adh_ext .= _T("M.");
                break;
            case "2" :
                $nom_adh_ext .= _T("Mme.");
                break;
            default :
                $nom_adh_ext .= _T("Mlle.");
        }

        $nom_adh_ext .= " ".strtoupper($resultat->fields[1])." ".ucfirst(strtolower($resultat->fields[2]));
        $concatname = $concatname . " - " . $nom_adh_ext;
        $param["font"] = "Helvetica";
        $pdf->draw_paragraph($y1-5, $x1, $y2, $x2, $nom_adh_ext, $firstpage, $param);
        $pdf->draw_rectangle ($y1, $x1, $y2, $x2, $firstpage, $param);
                    $pdf->draw_rectangle($y1, $x2, $y2, $x2+40, $firstpage, $param);
//            $pdf->draw_rectangle($y1, $x2+20, $y2, $x2+40, $firstpage, $param);
        if ($_POST['panier'] == "legume")
        {
            $qpanier=$resultat->fields[9];
            if ($qpanier == "0" )
                $qpanier_result="pp";    
            else
                $qpanier_result="GP";
        } else if ($_POST['panier'] == "pain") {
            $qpanier=$resultat->fields[10];
            if ($qpanier == "1" )
                $qpanier_result="3";
            if ($qpanier == "2" )
                $qpanier_result="5";
            if ($qpanier == "3" )
                $qpanier_result="7";
            if ($qpanier == "4" )
                $qpanier_result="9,50";
        }
        $pdf->draw_paragraph($y1, $x2, $y2, $x2+40, $qpanier_result, $firstpage, $param);
        $i=0;
        while($i<$col)
        {
            $absence = &$DB->Execute("SELECT *  FROM `galette_absences` WHERE `id_adh` = " . $resultat->fields[0] . " AND `date_abs` = '$date2[$i]'");
            if (!$absence->EOF)
                $pdf->draw_paragraph($y1, $x2+$i*$ecart+40, $y2, $x2+($i+1)*$ecart+40, "Absent", $firstpage, $param);
            else
                 $panier[$i+1][$qpanier]=$panier[$i+1][$qpanier]+1;
            $pdf->draw_rectangle($y1, $x2+$i*$ecart+40, $y2, $x2+($i+1)*$ecart+40, $firstpage, $param);
            $i++;
        }
        
        $row++;
        if ($row>$ligne)
        {
            $row=1;
            $firstpage = $pdf->new_page("a4-landscape");
        }
        $nb_etiq++;
        }
        $resultat->MoveNext();
    } 
    $i=0;
    while($i<$col)
    {
        $pdf->draw_rectangle($y2, $x2+$i*$ecart+40, $y2-20, $x2+($i+1)*$ecart+40, $firstpage, $param);
        if ($_POST['panier'] == "legume")
            $display="pp : " . $panier[$i+1][0] . " | GP : " . $panier[$i+1][1];
        if ($_POST['panier'] == "pain")
             $display="3E : " . $panier[$i+1][1] . " | 5E : " . $panier[$i+1][2] . " | 7E : " . $panier[$i+1][3] . " | 9E50 : " . $panier[$i+1][4];

        $pdf->draw_paragraph($y2, $x2+$i*$ecart+40, $y2-20, $x2+($i+1)*$ecart+40, $display, $firstpage, $param);
        $i++;
    }

    $resultat->Close();
    dblog(_T("Génération de ")." ".$nb_etiq." "._T("feuille emargement"),$concatname);
    header("Content-Disposition: filename=feuille_emargement.pdf");
    header("Content-Type: application/pdf");
    $temp = $pdf->generate();
    header('Content-Length: ' . strlen($temp));
    echo $temp;
?>
