<?php
/* **
 * @application PhpMyShopping
 * @part        Admin Boutique (AB)
 * @file        class.frais_envoi.php
 * @path        ./admin/include/
 * @author      Stephane DINE <stephane@phpmyshopping.org>
 * @site        http://www.phpmyshopping.org
 * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
 *
 * PHP version 5
 *
 * Classe Frais_envoi
 *
 * Gestion des frais d'envoi
 */

require_once 'class.lib.php';

class Frais_envoi extends Lib
{




    /** *****************************************************************************************************************************
     * Constructeur
     */
    public function __construct()
    {
    }




    /** *****************************************************************************************************************************
     * Methode listCarrier
     *
     * @desc   Liste des transporteurs
     * @return string
     * @access public
     */
    public function listCarrier()
    {
        $list = '
        <div class="heading">Liste des transporteurs</div>
        <div id="list">

        <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="600">
          <tr bgcolor="#F1F3F5">
            <th align="left" width="74%">Transporteurs</th>
            <th align="center" width="24%">Disponibilit&eacute;</th>
            <th align="center" width="1%">Modifier</th>
            <th align="center" width="1%">Effacer</th>
          </tr>';

        /**
         * Recuperation des donnees pour la creation de la liste des transporteurs
         */
        $sqlCarrier = 'SELECT ID_Carrier, CarrierName,
                              (CASE Status
                               WHEN "1"
                               THEN CONCAT("<b class=\"green\">", "Actif", "</b>")
                               ELSE CONCAT("<b class=\"red\">", "Non actif", "</b>")
                               END) AS Status
                       FROM Carrier
                       ORDER BY CarrierName ASC';

        $queryCarrier = $this->query($sqlCarrier);

        while ($resCarrier = @mysql_fetch_array($queryCarrier)) {
            $list .= '
            <tr class="row2" onmouseover="this.className=\'highlight\'" onmouseout="this.className=\'row2\'">
              <td align="left">'.$resCarrier['CarrierName'].'</a></td>
              <td align="center">'.$resCarrier['Status'].'</a></td>
              <td align="center"><a href="?C1=FE&C2=TR&C3=edit&ID_Carrier='.$resCarrier['ID_Carrier'].'"><img src="./images/update.png" alt="Edit" title="Editer ce transporteur" /></a></td>
              <td align="center"><a href="?C1=FE&C2=TR&C3=delete&ID_Carrier='.$resCarrier['ID_Carrier'].'"><img src="./images/delete.png" alt="Del" title="Effacer ce transporteur" /></a></td>
            </tr>';
        }

        $list .= '
          </table>
        </div>';

        return $list;
    }




    /** *****************************************************************************************************************************
     * Methode formCarrier
     *
     * @desc   Formulaire d'ajout/edition de transporteur
     * @param  int $ID_Carrier Identifiant du transporteur
     * @return string
     * @access public
     */
    public function formCarrier($ID_Carrier = 0)
    {
        if ($ID_Carrier > 0) {
            /**
             * Modification donnees transporteur
             */
            $sql   = 'SELECT * FROM Carrier WHERE ID_Carrier = '.$ID_Carrier;
            $field = mysql_fetch_array($this->query($sql));
            
            $case            = 'update';
            $hiddenField     = '<input type="hidden" name="data[ID_Carrier]" value="'.$ID_Carrier.'" />';
            $title           = 'Modification donn&eacute;es transporteur';
            $textValidButton = 'Enregistrer les modifications';
            //$buttonAddZone   = '';
            $Status          = 'Status_'.$field['Status'];
            $$Status         = ' checked="checked"';
        } else {
            /**
             * Ajout d'un transporteur
             */
            $case            = 'insert_ok';
            $hiddenField     = '';
            $title           = 'Cr&eacute;ation d\'un transporteur';
            $textValidButton = 'Enregistrer le transporteur';
            //$buttonAddZone   = '<br /><input type="submit" name="AddZone" value="[ Enregistrer le transporteur et d&eacute;finir les zones de livraison ]" style="color: blue; font-weight: normal;" />';
            $Status_1        = ' checked="checked"';
        }                

        $form = '
        <div class="heading">'.$title.'</div>
        <div id="list">

        <form action="" method="POST" style="margin: 0px" name="formCarrier" id="formCarrier">
          <input type="hidden" name="C1" value="FE" />
          <input type="hidden" name="C2" value="TR" />
          <input type="hidden" name="C3" value="'.$case.'" />
          '.$hiddenField.'

          <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="600">
            <tr bgcolor="#F1F3F5">
              <td colspan="2">&nbsp;</td>
            </tr>';
            $form .= $this->create_field('Nom du transporteur :', array('R', 'T', 'L', 'M', 'CarrierName'), 'input', 'text', 'CarrierName', 50, 60, $field['CarrierName']);           
            $form .= '
            <tr bgcolor="#FFFFFF">
              <td align="right">Transporteur activ&eacute; ? :</td>
              <td align="left">
                <input type="radio" name="Status" value="1"'.$Status_1.' /> Oui
                <input type="radio" name="Status" value="0"'.$Status_0.' /> Non
              </td>
            </tr>
            <!--<tr>
              <td align="center" colspan="2" bgcolor="#F1F3F5">
                <input type="submit" name="ValidCarrier" value="'.$textValidButton.'" style="color: green; font-weight: normal;" />
                '.$buttonAddZone.'
              </td>
            </tr>-->
            <tr bgcolor="#F1F3F5">
              <td colspan="2" align="center">
                <a href="#" onClick="checkForm(\'formCarrier\')" class="formButton">'.$textValidButton.'</a>
                <b>::</b>
                <a href="?C1=FE&C2=TR&C3=list" class="formButton">Annuler</a>
              </td>
            </tr>
          </table>

        </form>
        </div>';

        return $form;
    }




    /** ****************************************************************************************************
     * Methode listZone
     *
     * @desc   Affichage des zones sous forme de liste avec liens de modificatins et de suppression
     * @param  int $ID_Carrier Identifiant du transporteur
     * @return string
     * @access public
     */
    public function listZone($ID_Carrier = 0)
    {
        $list = '
        <div class="heading">Zones de livraison disponibles par transporteurs</div>
        <div id="list">

        <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="600">
          <tr bgcolor="#F1F3F5">
            <td colspan="4">&nbsp;</td>
          </tr>

          <tr bgcolor="#FFFFFF">
            <td align="left" colspan="4">

              <form action="" method="POST">
                <input type="hidden" name="C1" value="FE" />
                <input type="hidden" name="C2" value="ZG" />
                <input type="hidden" name="C3" value="list" />
                Liste des zones pour le transporteur >> ';

        /**
         * Recuperation des donnees pour la creation de la liste des zones de livraison
         */
        if ($ID_Carrier < 1) {
            $sql = 'SELECT ID_Zone, ID_Carrier, ZoneName,
                           (CASE Status
                            WHEN "1"
                            THEN CONCAT("<b class=\"green\">", "Actif", "</b>")
                            ELSE CONCAT("<b class=\"red\">", "Non actif", "</b>")
                            END) AS Status
                    FROM Zone
                    WHERE ID_Carrier IN (
                        SELECT MIN(zon.ID_Carrier)
                        FROM Zone AS zon
                        LEFT JOIN Carrier AS car ON car.ID_Carrier = zon.ID_Carrier
                    )
                    ORDER BY ZoneName ASC';

            $sqlSel = 'SELECT DISTINCT(ID_Carrier) AS ID_Carrier
                       FROM Zone
                       WHERE ID_Carrier IN (
                           SELECT MIN(zon.ID_Carrier)
                           FROM Zone AS zon
                           LEFT JOIN Carrier AS car ON car.ID_Carrier = zon.ID_Carrier
                       )';

            $resSel = mysql_fetch_array($this->query($sqlSel));
            $ID_Carrier = $resSel['ID_Carrier'];
        } else {
            $sql = 'SELECT zon.ID_Zone, zon.ID_Carrier, ZoneName,
                           (CASE zon.Status
                            WHEN "1"
                            THEN CONCAT("<b class=\"green\">", "Actif", "</b>")
                            ELSE CONCAT("<b class=\"red\">", "Non actif", "</b>")
                            END) AS Status
                    FROM Carrier AS car,
                         Zone AS zon
                    WHERE car.ID_Carrier = zon.ID_Carrier
                    AND zon.ID_Carrier = '.$ID_Carrier.'
                    ORDER BY ZoneName ASC';
        }

        $new_sql = 'SELECT ID_Carrier, CarrierName FROM Carrier';
        $list .= '<select name="ID_Carrier" onChange="submit()">';
        $list .= $this->liste_select('ID_Carrier', array('ID_Carrier', 'CarrierName'), 'Carrier', 'CarrierName', $ID_Carrier, false, '', $new_sql, false);
        $list .= '&nbsp;
              <input type="submit" value="[ OK ]" name="ViewZone">
            </form>

            </td>
          </tr>
          <tr bgcolor="#F1F3F5">
            <th align="left" width="79%">Zones</th>
            <th align="center" width="19%">Disponibilit&eacute;</th>
            <th align="center" width="1%">Modifier</th>
            <th align="center" width="1%">Effacer</th>
          </tr>';

        $query = $this->query($sql);

        while ($res = @mysql_fetch_array($query)) {
            $list .= '
            <tr class="row2" onmouseover="this.className=\'highlight\'" onmouseout="this.className=\'row2\'">
              <td align="left">'.$res['ZoneName'].'</a></td>
              <td align="center">'.$res['Status'].'</a></td>
              <td align="center"><a href="?C1=FE&C2=ZG&C3=edit&ID_Zone='.$res['ID_Zone'].'"><img src="./images/update.png" alt="Edit" title="Editer cette zone de livraison" /></a></td>
              <td align="center"><a href="?C1=FE&C2=ZG&C3=delete&ID_Carrier='.$res['ID_Carrier'].'&ID_Zone='.$res['ID_Zone'].'"><img src="./images/delete.png" alt="Del" title="Effacer cette zone de livraison" /></a></td>
            </tr>';
        }

        $list .= '
          </table>
        </div>';

        return $list;
    }




    /** ****************************************************************************************************
     * Methode formZone
     *
     * @desc   Affichage du formulaire d'ajout/edition de zone de livraison
     * @param  int    $ID_Zone         Identifiant de la zone de livraison
     * @param  int    $ID_Carrier      Identifiant du transporteur
     * @param  int    $viewListCarrier Definit si oui ou non on affiche la liste des transporteurs
     * @param  string $typeOperation    
     * @return string
     * @access public
     */
    public function formZone($ID_Zone = 0, $ID_Carrier = 0, $viewListCarrier = 0, $typeOperation = 'A')
    {
        if ($ID_Zone > 0) {
            /**
             * Modification donnees zone de livraison
             *
             * Recuperation donnees
             */
            $sql   = 'SELECT * FROM Zone WHERE ID_Zone = '.$ID_Zone;
            $field = @mysql_fetch_array($this->query($sql));

            $case             = 'update';
            $hiddenField      = '<input type="hidden" name="data[ID_Zone]" value="'.$ID_Zone.'" />
                                 <input type="hidden" name="data[ID_Carrier]" value="'.$field['ID_Carrier'].'" />
                                 <input type="hidden" name="typeOperation" value="'.$typeOperation.'" />';
            $title            = 'Modification donn&eacute;es de zone';
            $textValidButton  = 'Enregistrer les modifications';
            $buttonAddZone    = '';
            $buttonAddPackage = '';
            $Status           = 'Status_'.$field['Status'];
            $$Status          = ' checked="checked"';

            //$typeOperation    = '<input type="hidden" name="TypeOperation" value="M" />';

            if ($typeOperation == 'A') {
                $linkTypeOperation = '[&nbsp;<a href="?C1=FE&C2=ZG&C3=edit&ID_Zone='.$ID_Zone.'&typeOperation=D">Cliquez ici pour enlever un ou plusieurs pays &agrave; cette zone</a>&nbsp;]<br />
                                      <br/>Liste des pays disponibles et ne faisant pas partis de cette zone';
                $sens1             = 'Ajouter &agrave; la zone >>>';
                $sens2             = '<<< Retirer de la zone';
                $sqlC = 'SELECT ID_Country, CountryName
                         FROM Country
                         WHERE ID_Country <> ALL (
                             SELECT ID_Country
                             FROM CountryToZone
                             WHERE ID_Zone = '.$ID_Zone.'
                         )
                         ORDER BY CountryName';
            } else {
                $linkTypeOperation = '[&nbsp;<a href="?C1=FE&C2=ZG&C3=edit&ID_Zone='.$ID_Zone.'&typeOperation=A">Cliquez ici pour ajouter un ou plusieurs pays &agrave; cette zone</a>&nbsp;]<br />
                                      <br/>Liste des pays actuellement d&eacute;finis pour cette zone';
                $sens1             = 'Retirer de la zone >>>';
                $sens2             = '<<< Ajouter &agrave; la zone';
                $sqlC = 'SELECT ID_Country, CountryName
                         FROM Country
                         WHERE ID_Country IN (
                             SELECT ID_Country
                             FROM CountryToZone
                             WHERE ID_Zone = '.$ID_Zone.'
                         )
                         ORDER BY CountryName';
            }
        } else {
            /**
             * Ajout d'une zone de livraison
             */
             
            /*
             * Affichage de la liste des tansporteurs si $viewListCarrier = 1
             */
            if ($viewListCarrier == 1) {
                $sqlCarrierList = 'SELECT ID_Carrier, CarrierName FROM Carrier';
                $list        = '<tr bgcolor="#FFFFFF">
                                <td align="right">Transporteur : </td>
                                <td align="left">'.$this->liste_select('data[ID_Carrier]', array('ID_Carrier', 'CarrierName'), 'Carrier', 'CarrierName', $ID_Carrier, false, false, $sqlCarrierList).'</td>
                                </tr>';
                $hiddenField = '<input type="hidden" name="cat3" value="new" />';
                $title       = 'D&eacute;finition d\'une zone de livraison';
            } else {
                $list        = '';
                $hiddenField = '<input type="hidden" name="data[ID_Carrier]" value="'.$ID_Carrier.'" />';
                $title       = 'D&eacute;finition d\'une zone de livraison pour le transporteur <b class="blue">'.$this->getField('Carrier', 'CarrierName', 'ID_Carrier', $ID_Carrier).'</b>';
            }
            
            $case             = 'insert_ok';
            $textValidButton  = 'Enregistrer les donn&eacute;es et retour &agrave; la liste des zones';
            $buttonAddZone    = '<br /><br /><input type="submit" name="NewZone" OnClick="return SelectionnerItems()" value="Ajouter une nouvelle zone" style="color: black; font-weight: normal;" />';
            $buttonAddPackage = '<br /><br /><input type="submit" name="AddPackage" OnClick="return SelectionnerItems()" value="Enregistrer les donn&eacute;es et d&eacute;finir les formules" style="color: blue; font-weight: normal;" />';
            //$typeOperation    = '';
            $Status_1         = ' checked="checked"';
            $addSql           = '';
            $sens1            = 'Ajouter &agrave; la zone >>>';
            $sens2            = '<<< Retirer de la zone';
            $sqlC             = 'SELECT ID_Country, CountryName FROM Country ORDER BY CountryName';
                     /*WHERE ID_Country <> ALL (
                         SELECT ID_Country
                         FROM CountryToZone, Zone
                         WHERE CountryToZone.ID_Zone = Zone.ID_Zone
                     )
                     ORDER BY CountryName;';*/
        }                

        $resultat = $this->query($sqlC);

        while ($enr = @mysql_fetch_array($resultat)) {
            $liste = 'listeIn';
            $$liste .= '<option value="'.$enr[0].'">'.$enr[1].'</option>';
        }
            
        $form = '
        <div class="heading">'.$title.'</div>
        <div id="list">

        <form action="" method="POST" style="margin: 0px" name="newZone">
          <input type="hidden" name="C1" value="FE" />
          <input type="hidden" name="C2" value="ZG" />
          <input type="hidden" name="C3" value="'.$case.'" />
          '.$hiddenField.'

          <table cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="700">
            <tr bgcolor="#F1F3F5">
              <td colspan="2" class="h1" align="left">&nbsp;</td>
            </tr>
            '.$list;
        
            $form .= $this->create_field('Nom de la zone de livraison :', array('R','T','L','M'), 'input', 'text', 'ZoneName', 50, 60, $field['ZoneName']);
            $form .= '
            <tr bgcolor="#FFFFFF">
              <td align="right">Zone activ&eacute; ? :</td>
              <td align="left">
                <input type="radio" name="Status" value="1"'.$Status_1.' /> Oui
                <input type="radio" name="Status" value="0"'.$Status_0.' /> Non
              </td>
            </tr>

            <tr bgcolor="#FFFFFF">
              <td colspan="2">

                <table width="100%">
                  <tr>
                    <td colspan="3" align="left">'.$linkTypeOperation.'<td>
                  </tr>
                  <tr>
                    <td align="center" width="33%"><select name="IN[]" size="20" multiple="multiple">'.$listeIn.'</select></td>
                    <td align="center" width="34%">
                      <input type="submit" value="'.$sens1.'" onClick="return DeplacerItemDeVers(\'IN\', \'OUT\')" /><br /><br />
                      <input type="submit" value="'.$sens2.'" onClick="return DeplacerItemDeVers(\'OUT\', \'IN\')" />
                    </td>
                    <td align="center" width="33%"><select name="OUT[]" size="20" multiple="multiple">'.$listeOut.'</select></td>
                  </tr>
                </table>

            </td>
          </tr>

          <tr>
            <td align="center" colspan="2" bgcolor="#F1F3F5">
              <input type="submit" name="ValidZone" value="'.$textValidButton.'" OnClick="return SelectionnerItems()" style="color: green;" />
              '.$buttonAddZone.'
              '.$buttonAddPackage.'
            </td>
          </tr>
        </table>

        </form>
        </div>';

        return $form;
    }




    /** ****************************************************************************************************
     * Methode listPackage
     *
     * @desc   Affiche la liste des formules pour une zone donnee
     * @param  int $ID_Zone Identifiant de la zone
     * @return string
     * @access public
     */
    public function listPackage($ID_Zone)
    {
        /**
         * Recuperation du plus petit identifiant de zone si $ID_Zone = 0
         */
        if ($ID_Zone < 1) {
            $sqlCar = 'SELECT MIN(pa.ID_Zone) AS ID_Zone
                       FROM Package AS pa
                       LEFT JOIN Zone AS zo ON zo.ID_Zone = pa.ID_Zone
                       LEFT JOIN Carrier AS ca ON zo.ID_Carrier = ca.ID_Carrier';

            $queryCar = $this->query($sqlCar);
            $resCar = @mysql_fetch_array($queryCar);
            $ID_Zone = $resCar['ID_Zone'];
        }        

        $list = '
        <div class="heading">Formules disponibles</div>
        <div id="list">

          <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="50%">
            <tr bgcolor="#F1F3F5">
              <td colspan="4">&nbsp;</td>
            </tr>

            <tr bgcolor="#FFFFFF">
              <td align="left" colspan="4" align="left">

                <form action="" method="POST">
                  <input type="hidden" name="C1" value="FE" />
                  <input type="hidden" name="C2" value="FO" />
                  <input type="hidden" name="C3" value="list" />
                  Afficher les formules pour la zone suivante : ';

        /**
         * Liste deroulante avec le transporteur et la zone
         */
        $newSql = 'SELECT Zone.ID_Zone AS ID_Zone, CarrierName, ZoneName
                   FROM Zone
                   LEFT JOIN Carrier ON Carrier.ID_Carrier = Zone.ID_Carrier
                   WHERE Zone.ID_Carrier IN (
                       SELECT ID_Carrier FROM Carrier
                   )
                   ORDER BY CarrierName, ZoneName ASC';

        $list .= '<select name="ID_Zone" onChange="submit()">';
        $list .= $this->liste_select('ID_Zone', array('ID_Zone', 'CarrierName', 'ZoneName'), 'Zone', 'ZoneName', $ID_Zone, false, '', $newSql, false, '  #  ');
        $list .= '&nbsp;
                  <input type="submit" value="[ OK ]" name="ViewPackage" />
                </form>

              </td>
            </tr>
            <tr bgcolor="#F3F1ED">
              <th align="left" width="79%">Formules</th>
              <th align="center" width="19%">Disponibilit&eacute;</th>
              <th align="center" width="1%">Modifier</th>
              <th align="center" width="1%">Effacer</th>
            </tr>';

        /**
         * Recuperation des donnees pour la creation de la liste des formules
         */
        $sql = 'SELECT ID_Package, ID_Zone, PackageName,
                       (CASE Status
                        WHEN "1"
                        THEN CONCAT("<b class=\"green\">", "Actif", "</b>")
                        ELSE CONCAT("<b class=\"red\">", "Non actif", "</b>")
                        END) AS Status
                FROM Package
                WHERE ID_Zone IN (
                    SELECT ID_Zone FROM Zone WHERE ID_Carrier IN (
                        SELECT ID_Carrier FROM Carrier
                    )
                )
                AND ID_Zone = '.$ID_Zone.'
                ORDER BY PackageName ASC';

        $query = $this->query($sql);

        while ($res = @mysql_fetch_array($query)) {
            $list .= '
            <tr class="row2" onmouseover="this.className=\'highlight\'" onmouseout="this.className=\'row2\'">
              <td align="left">'.$res['PackageName'].'</a></td>
              <td align="center">'.$res['Status'].'</a></td>
              <td align="center" width="1%"><a href="?C1=FE&C2=FO&C3=edit&ID_Package='.$res['ID_Package'].'"><img src="./images/update.png" alt="Edit" title="Editer cette formule" /></a></td>
              <td align="center" width="1%"><a href="?C1=FE&C2=FO&C3=delete&ID_Package='.$res['ID_Package'].'&ID_Zone='.$res['ID_Zone'].'"><img src="./images/delete.png" alt="Del" title="Effacer cette formule" /></a></td>
            </tr>';
        }

        $list .= '
          </table>
        </div>';

        return $list;
    }





    /** ****************************************************************************************************
     * Methode formPackage
     *
     * @desc   Affichage du formulaire d'ajout/edition de formules
     * @param  int $ID_Package      Identifiant de la zone de la formule
     * @param  int $ID_Zone         Identifiant de la zone de livraison
     * @param  int $viewListCarrier Definit si oui ou non on affiche la liste des zones de livraison
     * @return string
     * @access public
     */
    public function formPackage($ID_Package = 0, $ID_Zone = 1, $viewListZone = 0)
    {
        if ($ID_Package > 0) {
            /**
             * Modification donnees formules
             *
             * Recuperation donnees
             */
            $sql   = 'SELECT * FROM Package WHERE ID_Package = '.$ID_Package;
            $field = @mysql_fetch_array($this->query($sql));
            
            $case             = 'update';
            $hiddenField      = '<input type="hidden" name="data[ID_Package]" value="'.$ID_Package.'" />
                                 <input type="hidden" name="data[ID_Zone]" value="'.$field['ID_Zone'].'" />';
            $title            = 'Modification donn&eacute;es de formule';
            $textValidButton  = 'Enregistrer les modifications';
            $buttonAddPackage = '';
            $buttonAddCost    = '';
            $Status           = 'Status_'.$field['Status'];
            $$Status          = ' checked="checked"';
        } else {
            /**
             * Ajout d'une formule
             *
             * Affichage de la liste des zones si $viewListZone = 1
             */
            if ($viewListZone == 1) {
                $sqlListCarrier = 'SELECT ID_Zone, CarrierName, ZoneName
                                   FROM Zone AS zon, Carrier AS car
                                   WHERE zon.ID_Carrier = car.ID_Carrier
                                   ORDER BY CarrierName, ZoneName ASC;';

                $list        = '<tr bgcolor="#FFFFFF">
                                  <td align="right">Transporteur -- Zones : </td>
                                  <td align="left">'.$this->liste_select('data[ID_Zone]', array('ID_Zone', 'CarrierName', 'ZoneName'), 'Zone', 'ZoneName', $ID_Zone, false, false, $sqlListCarrier, true, '  #  ').'</td>
                                </tr>';
                $hiddenField = '<input type="hidden" name="cat3" value="new" />';
                $title       = 'D&eacute;finition d\'une formule';
            } else {
                $list        = '';
                $hiddenField = '<input type="hidden" name="data[ID_Zone]" value="'.$ID_Zone.'" />';
                $title       = 'D&eacute;finition d\'une formule pour la zone de livraison <b class="blue">'.$this->getField('Zone', 'ZoneName', 'ID_Zone', $ID_Zone).'</b>';
            }
            
            $case             = 'insert_ok';
            $textValidButton  = 'Enregistrer les donn&eacute;es et retour &agrave; la liste des formules';
            $buttonAddPackage = '<br /><br /><input type="submit" name="NewPackage" value="Ajouter une nouvelle formule" style="color: black;" />';
            $buttonAddCost    = '<br /><br /><input type="submit" name="AddCost" value="Enregistrer les donn&eacute;es et d&eacute;finir les montants" style="color: blue;" />';
            $Status_1         = ' checked="checked"';
        }

        $form = '
        <div class="heading">'.$title.'</div>
        <div id="list">

          <form action="" method="POST" style="margin: 0px" name="Package">
            <input type="hidden" name="C1" value="FE" />
            <input type="hidden" name="C2" value="FO" />
            <input type="hidden" name="C3" value="'.$case.'" />
            '.$hiddenField.'

            <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="50%">
              <tr bgcolor="#F1F3F5">
                <td colspan="2">&nbsp;</td>
              </tr>
            '.$list;

            $form .= $this->create_field('Nom de la formule :', array('R','T','L','M'), 'input', 'text', 'PackageName', 50, 60, $field['PackageName']);           
            $form .= '
            <tr bgcolor="#FFFFFF">
              <td align="right">Formule activ&eacute;e ? :</td>
              <td align="left">
                <input type="radio" name="Status" value="1"'.$Status_1.' /> Oui
                <input type="radio" name="Status" value="0"'.$Status_0.' /> Non
              </td>
            </tr>
            <tr>
              <td align="center" colspan="2" bgcolor="#F1F3F5">
                <input type="submit" name="ValidPackage" value="'.$textValidButton.'" style="color: green;" />
                '.$buttonAddPackage.'
                '.$buttonAddCost.'
              </td>
            </tr>
          </table>

        </form>
        </div>';

        return $form;
    }




    /** ****************************************************************************************************
     * Methode listCost
     *
     * @desc   Affiche la liste des prix et poids pour une formule donnee
     * @param  int $ID_Package Identifiant de la zone
     * @return string
     * @access public
     */
    public function listCost($ID_Package)
    {
        $list = '
        <div class="heading">Frais d\'envoi par formule</div>
        <div id="list">

          <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="40%">
            <tr bgcolor="#F1F3F5">
             <td colspan="5">&nbsp;</td>
            </tr>

            <tr bgcolor="#FFFFFF">
              <td align="left" colspan="5" align="left">

                <form action="" method="POST">
                  <input type="hidden" name="C1" value="FE" />
                  <input type="hidden" name="C2" value="PT" />
                  <input type="hidden" name="C3" value="list" />';

        /**
         * Liste deroulante avec le transporteur, la zone et la formule
         */
        $newSql = 'SELECT pac.ID_Package, zon.ID_Zone, CarrierName, ZoneName, PackageName
                   FROM Package AS pac
                   LEFT JOIN Zone AS zon ON pac.ID_Zone = zon.ID_Zone
                   LEFT JOIN Carrier AS car ON zon.ID_Carrier = car.ID_Carrier
                   ORDER BY CarrierName, ZoneName, PackageName ASC;';

        $newLine = array('-[ Choisissez la formule dont vous souhaitez voir les tarifs ]-' => 'X');
        $list .= '<select name="ID_Package" onChange="submit()">';
        $list .= $this->liste_select('ID_Package', array('ID_Package', 'CarrierName', 'ZoneName', 'PackageName'), 'Package', 'PackageName', $ID_Package, true, $newLine, $newSql, false, ' #-> ', ' #-> ');
        $list .= '&nbsp;
                <input type="submit" value="[ OK ]" name="ViewCost">
              </form>

            </td>
          </tr>
          <tr bgcolor="#F1F3F5">
            <th align="left" colspan="2">Tarif par poids</th>
            <th align="left" >Disponibilit&eacute;</th>
            <th align="center" width="1%">Modifier</th>
            <th align="center" width="1%">Effacer</th>
          </tr>';

        /**
         * Recuperation des donnees pour la creation de la liste des prix et poids
         */
        $sql = 'SELECT ID_Cost,
                       ID_Package,
                       Cost,
                       (CASE Weight 
                        WHEN 4000000000 THEN CONCAT(\' \')
                        ELSE CONCAT(Weight, \' \', \'Grs \')
                        END) AS Weight,
                       (CASE Status
                        WHEN "1"
                        THEN CONCAT("<b class=\"green\">", "Actif", "</b>")
                        ELSE CONCAT("<b class=\"red\">", "Non actif", "</b>")
                        END) AS Status
                FROM Cost
                WHERE ID_Package = '.$ID_Package.'
                ORDER BY Cost ASC';
        $query = $this->query($sql);

        while ($res = @mysql_fetch_array($query)) {
            $list .= '
            <tr class="row2" onmouseover="this.className=\'highlight\'" onmouseout="this.className=\'row2\'">
              <td align="right" width="49%">'.$res['Cost'].'&nbsp;&euro;</a></td>
              <td align="right" width="49%">'.$res['Weight'].'</a></td>
              <td align="center">'.$res['Status'].'</a></td>
              <td align="center" width="1%"><a href="?C1=FE&C2=PT&C3=edit&ID_Cost='.$res['ID_Cost'].'"><img src="./images/update.png" alt="Edit" title="Editer ce tarif" /></a></td>
              <td align="center" width="1%"><a href="?C1=FE&C2=PT&C3=delete&ID_Cost='.$res['ID_Cost'].'&ID_Package='.$res['ID_Package'].'"><img src="./images/delete.png" alt="Del" title="Effacer ce tarif" /></a></td>
            </tr>';
        }

        $list .= '
          </table>
        </div>';

        return $list;
    }




    /** ****************************************************************************************************
     * Methode formCost
     *
     * @desc   Affichage du formulaire d'ajout/edition de prix et poids
     * @param  int $ID_Package      Identifiant de la zone de la formule
     * @param  int $ID_Zone         Identifiant de la zone de livraison
     * @param  int $viewListCarrier Definit si oui ou non on affiche la liste des zones de livraison
     * @return string
     * @access public
     */
    public function formCost($ID_Cost = 0, $ID_Package = 1, $viewListPackage = 0)
    {
        if ($ID_Cost > 0) {
            /**
             * Modification donnees prix
             *
             * Recuperation donnees
             */
            $sql   = 'SELECT * FROM Cost WHERE ID_Cost = '.$ID_Cost;
            $field = @mysql_fetch_array($this->query($sql));

            if ($field['Weight'] == 4000000000) {
                $Weight = '';
            } else {
                $Weight = $field['Weight'];
            }

            $C3               = 'update';
            $hiddenField      = '<input type="hidden" name="data[ID_Cost]" value="'.$ID_Cost.'" />
                                 <input type="hidden" name="data[ID_Package]" value="'.$field['ID_Package'].'" />';
            $title            = 'Modification donn&eacute;es prix/poids';
            $textValidButton  = '[ Enregistrer les modifications ]';
            $buttonAddCost    = '';
            $Status           = 'Status_'.$field['Status'];
            $$Status          = ' checked="checked"';
        } else {
            /**
             * Ajout d'un prix/poids
             *
             * Affichage de la liste des zones si $viewListPackage = 1
             */
            if ($viewListPackage == 1) {
                $newSql = 'SELECT pac.ID_Package, CarrierName, ZoneName, PackageName
                           FROM Package AS pac
                           LEFT JOIN Zone AS zon ON pac.ID_Zone = zon.ID_Zone
                           LEFT JOIN Carrier AS car ON zon.ID_Carrier = car.ID_Carrier
                           ORDER BY CarrierName, ZoneName, PackageName ASC;';

                $list        = '<tr bgcolor="#FFFFFF">
                                  <td align="right">Transporteur -- Zone -- Formule : </td>
                                  <td align="left">'.$this->liste_select('data[ID_Package]', array('ID_Package', 'CarrierName', 'ZoneName', 'PackageName'), 'Package', 'PackageName', $ID_Package, false, '', $newSql, true, ' --- ', ' --- ').'</td>
                                </tr>';
                $hiddenField = '<input type="hidden" name="cat3" value="new" />';
                $title       = 'D&eacute;finition d\'un tarif / poids';
            } else {
                $list        = '';
                $hiddenField = '<input type="hidden" name="data[ID_Package]" value="'.$ID_Package.'" />';
                $title       = 'D&eacute;finition de frais d\'envois pour la formule <b class="blue">'.$this->getField('Package', 'PackageName', 'ID_Package', $ID_Package).'</b>';
            }

            $C3               = 'insert_ok';
            $textValidButton  = '[ Enregistrer les donn&eacute;es et redirection sur la liste des montants/poids ]';
            $buttonAddCost    = '<br /><br /><input type="submit" name="NewCost" value="[ D&eacute;finir un nouveau prix ]" style="color: black;" />';
            $Status_1         = ' checked="checked"';
        }                

        $form = '
        <div class="heading">'.$title.'</div>
        <div id="list">

          <form action="" method="POST" style="margin: 0px" name="newCost">
            <input type="hidden" name="C1" value="FE" />
            <input type="hidden" name="C2" value="PT" />
            <input type="hidden" name="C3" value="'.$C3.'" />
            '.$hiddenField.'

            <table align="left" cellspacing="1" cellpadding="5" border="0" bgcolor="#D5D5D5" width="50%">
              <tr bgcolor="#F1F3F5">
                <td colspan="2">&nbsp;</td>
              </tr>'.$list;
              $form .= $this->create_field('Prix :', array('R','T','L','M'), 'input', 'text', 'Cost', 10, 12, $field['Cost'], 0, 0, '&nbsp;&euro;');
              $form .= $this->create_field('Poids en grammes :', array('R','T','L','M'), 'input', 'number', 'Weight', 10, 10, $Weight, 0, 0, '');                      
              $form .= '
              <tr bgcolor="#FFFFFF">
                <td align="right">Frais d\'envoi activf ? :</td>
                <td align="left">
                  <input type="radio" name="Status" value="1"'.$Status_1.' /> Oui
                  <input type="radio" name="Status" value="0"'.$Status_0.' /> Non
                </td>
              </tr>
              <tr>
                <td align="center" colspan="2" bgcolor="#F1F3F5">
                  <input type="submit" name="ValidCost" value="'.$textValidButton.'" style="color: green;" />'.$buttonAddCost.'
                </td>
              </tr>
            </table>

          </form>
        </div>';

        return $form;
    }




    /** *****************************************************************************************************************************
     * Methode 
     *
     * @desc   
     * @param  
     * @return 
     * @access public
     */
    //public function ()
    //{
    //}


}
?>
