<?php

////////////////////////////////////////////////////////////////////////
// PANIERS - GROUPES //////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('PANIERS_GROUPES_TABLE',"paniers_groupes");
define('PANIERS_GROUPES_TABLE_STRUCT', array(
    "nom"   => array( "type" => "txt",  "length" => 64, "default" => "" ),
    "del"   => array( "type" => "bool",                 "default" => 0  )
));

function getPaniersGroupesSelectBaseSQL() {
    return  "SELECT "
            .PANIERS_GROUPES_TABLE.".`ref`,"
            .PANIERS_GROUPES_TABLE.".`nom`,"
            .PANIERS_GROUPES_TABLE.".`del`"
            ." FROM ".PANIERS_GROUPES_TABLE;
}

function buildPaniersGroupesListSearchSQL($search) { return PANIERS_GROUPES_TABLE.".`nom` LIKE '%".$search."%'"; }

////////////////////////////////////////////////////////////////////////
// PANIERS TYPES ///////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('PANIERS_TYPES_TABLE',"paniers_types");
define('PANIERS_TYPES_TABLE_STRUCT', array(
    "groupe"                => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "nom"                   => array( "type" => "txt",      "length" => 64,                     "default" => ""                                             ),
    "valeur"                => array( "type" => "float",    "min" => 0, "max" => 999.99,        "default" => 0,         "force_default_under_min" => true   ),
    "del"                   => array( "type" => "bool",                                         "default" => 0                                              )
));

function getCountPaniersTypesListSelectBaseSQL() {
    return  "SELECT count(*) as nb"
            ." FROM ".PANIERS_TYPES_TABLE
            ." LEFT JOIN ".PANIERS_GROUPES_TABLE." ON ".PANIERS_TYPES_TABLE.".`groupe`=".PANIERS_GROUPES_TABLE.".`ref`";
}

function getPaniersTypesSelectBaseSQL() {
    return  "SELECT "
            .PANIERS_TYPES_TABLE.".`ref`,"
            .PANIERS_TYPES_TABLE.".`groupe` as 'groupe_ref',"
            .PANIERS_GROUPES_TABLE.".`nom` as 'groupe_nom',"
            .PANIERS_TYPES_TABLE.".`nom`,"
            .PANIERS_TYPES_TABLE.".`valeur`"
            ." FROM ".PANIERS_TYPES_TABLE
            ." LEFT JOIN ".PANIERS_GROUPES_TABLE." ON ".PANIERS_TYPES_TABLE.".`groupe`=".PANIERS_GROUPES_TABLE.".`ref`";
}

function buildPaniersTypesListSearchSQL($search) {
    $sql = "(".PANIERS_TYPES_TABLE.".`nom` LIKE '%".$search."%'";
    $sql .= " OR ".PANIERS_GROUPES_TABLE.".`nom` LIKE '%".$search."%'";
    $sql .= " OR ".PANIERS_TYPES_TABLE.".`valeur` LIKE '%".$search."%')";
    return $sql;
}

////////////////////////////////////////////////////////////////////////
// CLIENTS /////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('CLIENTS_TABLE',"clients");
define('CLIENTS_TABLE_STRUCT', array(
    "nom"       => array( "type" => "txt",      "length" => 64,                     "default" => ""     ),
    "prenom"    => array( "type" => "txt",      "length" => 64,                     "default" => ""     ),
    "tel"       => array( "type" => "txt",      "length" => 20,                     "default" => ""     ),
    "email"     => array( "type" => "txt",      "length" => 128,                    "default" => ""     ),
    "del"       => array( "type" => "bool",                                         "default" => 0      )
));

function getCountClientsListSelectBaseSQL() {
    return  "SELECT count(*) as nb FROM ".CLIENTS_TABLE;
}

function getClientsTableSelectBaseSQL() {
    return  "SELECT "
            .CLIENTS_TABLE.".`ref`,"
            .CLIENTS_TABLE.".`nom`,"
            .CLIENTS_TABLE.".`prenom`,"
            .CLIENTS_TABLE.".`tel`,"
            .CLIENTS_TABLE.".`email`"
            ." FROM ".CLIENTS_TABLE;
}

function buildClientsListSearchSQL($search) {
    $sql = "(".CLIENTS_TABLE.".`nom` LIKE '%".$search."%'";
    $sql .= " OR ".CLIENTS_TABLE.".`prenom` LIKE '%".$search."%'";
    $sql .= " OR ".CLIENTS_TABLE.".`tel` LIKE '%".$search."%'";
    $sql .= " OR ".CLIENTS_TABLE.".`email` LIKE '%".$search."%')";
    return $sql;
}

////////////////////////////////////////////////////////////////////////
// CLIENTS - ABSENCES //////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('CLIENTS_ABSENCES_TABLE',"clients_absences");
define('CLIENTS_ABSENCES_TABLE_STRUCT', array(
    "client"    => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "debut"     => array( "type" => "date",                                         "default" => "now"                                          ),
    "fin"       => array( "type" => "date",                                         "default" => "now"                                          ),
    "remarque"  => array( "type" => "txt",      "length" => NULL,                   "default" => ""                                             ),
    "del"       => array( "type" => "bool",                                         "default" => 0                                              )
));

function getClientsAbsencesTableSelectBaseSQL() {
    return  "SELECT "
            .CLIENTS_ABSENCES_TABLE.".`ref`,"
            .CLIENTS_ABSENCES_TABLE.".`client` as 'client_ref',"
            .CLIENTS_TABLE.".`nom` as 'client_nom',"
            .CLIENTS_TABLE.".`prenom` as 'client_prenom',"
            .CLIENTS_ABSENCES_TABLE.".`debut`,"
            .CLIENTS_ABSENCES_TABLE.".`fin`,"
            .CLIENTS_ABSENCES_TABLE.".`remarque`"
            ." FROM ".CLIENTS_ABSENCES_TABLE
            ." LEFT JOIN ".CLIENTS_TABLE." ON ".CLIENTS_ABSENCES_TABLE.".`client`=".CLIENTS_TABLE.".`ref`";
}

function buildClientsAbsencesListSearchSQL($search) {
    $sql = "(".CLIENTS_TABLE.".`nom` LIKE '%".$search."%'";
    $sql .= " OR ".CLIENTS_TABLE.".`prenom` LIKE '%".$search."%')";
    return $sql;
}

////////////////////////////////////////////////////////////////////////
// CONTRATS ////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('CONTRATS_TABLE',"contrats");
define('CONTRATS_TABLE_STRUCT', array(
    "client"                        => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "type"                          => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "groupe"                        => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "frequence"                     => array( "type" => "set",      "values" => ['hebdo', 'quinz'],    "default" => "hebdo"                                        ),
    "nb_paniers"                    => array( "type" => "int",      "min" => 1,  "max" => 99,           "default" => 1,         "force_default_under_min" => true   ),
    "panier_type"                   => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "date"                          => array( "type" => "date",                                         "default" => "now"                                          ),
    "quinz_groupe"                  => array( "type" => "set",      "values" => ['A', 'B'],             "default" => NULL                                           ),
    "lieu_depot"                    => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "nb_cheque"                     => array( "type" => "int",      "min" => 1,  "max" => 99,           "default" => 0,         "force_default_under_min" => true   ),
    "np_paniers_distrib_avt_saisie" => array( "type" => "int",      "min" => 1,  "max" => 99,           "default" => 0,         "force_default_under_min" => true   ),
    "force_eligible"                => array( "type" => "bool",                                         "default" => 0                                              ),
    "archive"                       => array( "type" => "bool",                                         "default" => 0                                              ),
    "force_not_archive"             => array( "type" => "bool",                                         "default" => 0                                              ),
    "del"                           => array( "type" => "bool",                                         "default" => 0                                              )
));

define('CONTRATS_PANIERS_STATUT',"contrats_paniers_statut");

function getCountContratsListSelectBaseSQL() {
    return  "SELECT count(*) as nb"
            ." FROM ".CONTRATS_TABLE
            ." LEFT JOIN ".CLIENTS_TABLE." ON ".CONTRATS_TABLE.".`client`=".CLIENTS_TABLE.".`ref`"
            ." LEFT JOIN ".CONTRATS_TYPES_TABLE." ON ".CONTRATS_TABLE.".`type`=".CONTRATS_TYPES_TABLE.".`ref`"
            ." LEFT JOIN ".PANIERS_GROUPES_TABLE." ON ".CONTRATS_TABLE.".`groupe`=".PANIERS_GROUPES_TABLE.".`ref`"
            ." LEFT JOIN ".PANIERS_TYPES_TABLE." ON ".CONTRATS_TABLE.".`panier_type`=".PANIERS_TYPES_TABLE.".`ref`"
            ." LEFT JOIN ".LIEUX_TABLE." ON ".CONTRATS_TABLE.".`lieu_depot`=".LIEUX_TABLE.".`ref`"
            ." LEFT JOIN ".CONTRATS_PANIERS_STATUT." ON ".CONTRATS_TABLE.".`ref`=".CONTRATS_PANIERS_STATUT.".`ref`";
}

function getContratsTableSelectBaseSQL() {
    return  "SELECT "
            .CONTRATS_TABLE.".`ref`,"
            .CONTRATS_TABLE.".`client` as 'client_ref',"
            .CLIENTS_TABLE.".`nom` as 'client_nom',"
            .CLIENTS_TABLE.".`prenom` as 'client_prenom',"
            .CLIENTS_TABLE.".`tel` as 'client_tel',"
            .CLIENTS_TABLE.".`email` as 'client_email',"
            .CONTRATS_TABLE.".`type` as 'type_ref',"
            .CONTRATS_TYPES_TABLE.".`nom` as 'type_nom',"
            .CONTRATS_TABLE.".`groupe` as 'groupe_ref',"
            .PANIERS_GROUPES_TABLE.".`nom` as 'groupe_nom',"
            .CONTRATS_TABLE.".`frequence`,"
            .CONTRATS_TABLE.".`nb_paniers`,"
            .CONTRATS_PANIERS_STATUT.".`nb_paniers_livres_absolute`,"
            .CONTRATS_PANIERS_STATUT.".`nb_paniers_livres`,"
            .CONTRATS_PANIERS_STATUT.".`nb_paniers_restants`,"
            .CONTRATS_TABLE.".`panier_type` as 'panier_type_ref',"
            .PANIERS_TYPES_TABLE.".`nom` as 'panier_type_nom',"
            .CONTRATS_TABLE.".`date`,"
            .CONTRATS_TABLE.".`quinz_groupe`,"            
            .CONTRATS_TABLE.".`lieu_depot` as 'lieu_depot_ref',"
            .LIEUX_TABLE.".`nom` as 'lieu_depot_nom',"
            .CONTRATS_TABLE.".`nb_cheque`,"
            .CONTRATS_TABLE.".`np_paniers_distrib_avt_saisie`,"
            .CONTRATS_TABLE.".`force_eligible`,"
            .CONTRATS_TABLE.".`archive`,"
            .CONTRATS_TABLE.".`del`"
            ." FROM ".CONTRATS_TABLE
            ." LEFT JOIN ".CLIENTS_TABLE." ON ".CONTRATS_TABLE.".`client`=".CLIENTS_TABLE.".`ref`"
            ." LEFT JOIN ".CONTRATS_TYPES_TABLE." ON ".CONTRATS_TABLE.".`type`=".CONTRATS_TYPES_TABLE.".`ref`"
            ." LEFT JOIN ".PANIERS_GROUPES_TABLE." ON ".CONTRATS_TABLE.".`groupe`=".PANIERS_GROUPES_TABLE.".`ref`"
            ." LEFT JOIN ".PANIERS_TYPES_TABLE." ON ".CONTRATS_TABLE.".`panier_type`=".PANIERS_TYPES_TABLE.".`ref`"
            ." LEFT JOIN ".LIEUX_TABLE." ON ".CONTRATS_TABLE.".`lieu_depot`=".LIEUX_TABLE.".`ref`"
            ." LEFT JOIN ".CONTRATS_PANIERS_STATUT." ON ".CONTRATS_TABLE.".`ref`=".CONTRATS_PANIERS_STATUT.".`ref`";
}

function buildContratsListSearchSQL($search) {
    $sql = "(".CLIENTS_TABLE.".`nom` LIKE '%".$search."%'";
    $sql .= " OR ".CLIENTS_TABLE.".`prenom` LIKE '%".$search."%'";
    $sql .= " OR ".PANIERS_TYPES_TABLE.".`nom` LIKE '%".$search."%'";
    $sql .= " OR ".LIEUX_TABLE.".`nom` LIKE '%".$search."%')";
    return $sql;
}

function buildContratsListFiltresSQL($sql, $filtres) {
    if(array_key_exists('client', $filtres) && (int)$filtres['client']>0) $sql.=" AND ".CONTRATS_TABLE.".`client`=".intval($filtres['client']);
    if(array_key_exists('groupe', $filtres) && (int)$filtres['groupe']>0) $sql.=" AND ".CONTRATS_TABLE.".`groupe`=".intval($filtres['groupe']);
    if(array_key_exists('frequence', $filtres) && $filtres['frequence']!="") {
        switch($filtres['frequence']) {
            case 'hebdo' : $sql.=" AND ".CONTRATS_TABLE.".`frequence`='hebdo'"; break;
            case 'quinz' : $sql.=" AND ".CONTRATS_TABLE.".`frequence`='quinz'"; break;
            case 'quinz_A' : $sql.=" AND ".CONTRATS_TABLE.".`frequence`='quinz' AND ".CONTRATS_TABLE.".`quinz_groupe`='A'"; break;
            case 'quinz_B' : $sql.=" AND ".CONTRATS_TABLE.".`frequence`='quinz' AND ".CONTRATS_TABLE.".`quinz_groupe`='B'"; break;
        }
    }
    if(array_key_exists('panier', $filtres) && (int)$filtres['panier']>0) $sql.=" AND ".CONTRATS_TABLE.".`panier_type`=".intval($filtres['panier']);
    if(array_key_exists('lieu', $filtres) && (int)$filtres['lieu']>0) $sql.=" AND ".CONTRATS_TABLE.".`lieu_depot`=".intval($filtres['lieu']);
    if(array_key_exists('archive', $filtres) && (int)$filtres['archive']>=0) $sql.=" AND ".CONTRATS_TABLE.".`archive`=".intval($filtres['archive']);
    return $sql;
}

////////////////////////////////////////////////////////////////////////
// CONTRATS - TYPES ////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('CONTRATS_FREQUENCES',array(
    "hebdo" => "hebdomadaire",
    "quinz" => "quinzaine"
));

define('CONTRATS_TYPES_TABLE',"contrats_types");
define('CONTRATS_TYPES_TABLE_STRUCT', array(
    "groupe"                => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "nom"                   => array( "type" => "txt",      "length" => 128,                    "default" => ""                                             ),
    "frequence"             => array( "type" => "set",      "values" => ['hebdo', 'quinz'],    "default" => "hebdo"                                        ),
    "nb_paniers"            => array( "type" => "int",      "min" => 1,  "max" => 99,           "default" => 1,         "force_default_under_min" => true   ),
    "panier_type"           => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "prix_total"            => array( "type" => "float",    "min" => 0.01, "max" => NULL,       "default" => NULL,      "force_default_under_min" => true   ),
    "del"                   => array( "type" => "bool",                                         "default" => 0                                              )
));

function getContratsTypesTableSelectBaseSQL() {
    return  "SELECT "
            .CONTRATS_TYPES_TABLE.".`ref`,"
            .CONTRATS_TYPES_TABLE.".`groupe` as 'groupe_ref',"
            .PANIERS_GROUPES_TABLE.".`nom` as 'groupe_nom',"
            .CONTRATS_TYPES_TABLE.".`nom`,"
            .CONTRATS_TYPES_TABLE.".`frequence`,"
            .CONTRATS_TYPES_TABLE.".`panier_type` as 'panier_type_ref',"
            .PANIERS_TYPES_TABLE.".`nom` as 'panier_type_nom',"
            .CONTRATS_TYPES_TABLE.".`nb_paniers`,"
            .CONTRATS_TYPES_TABLE.".`prix_total`"
            ." FROM ".CONTRATS_TYPES_TABLE
            ." LEFT JOIN ".PANIERS_GROUPES_TABLE." ON ".CONTRATS_TYPES_TABLE.".`groupe`=".PANIERS_GROUPES_TABLE.".`ref`"
            ." LEFT JOIN ".PANIERS_TYPES_TABLE." ON ".CONTRATS_TYPES_TABLE.".`panier_type`=".PANIERS_TYPES_TABLE.".`ref`";
}

////////////////////////////////////////////////////////////////////////
// LEGUMES /////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('LEGUMES_TABLE',"legumes");
define('LEGUMES_TABLE_STRUCT', array(
    "nom"                   => array( "type" => "txt",      "length" => 64,                     "default" => ""                                             ),
    "tarif"                 => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "del"                   => array( "type" => "bool",                                         "default" => 0                                              )
));

function getCountLegumesListSelectBaseSQL() {
    return  "SELECT count(*) as nb FROM ".LEGUMES_TABLE;
}

function getLegumesTableSelectBaseSQL() {
    return  "SELECT "
            .LEGUMES_TABLE.".`ref`,"
            .LEGUMES_TABLE.".`nom`,"
            .LEGUMES_TABLE.".`tarif` as 'tarif_ref',"
            .LEGUMES_TARIFS_TABLE.".`prix` as 'tarif_prix',"
            .LEGUMES_TARIFS_TABLE.".`unite` as 'tarif_unite',"
            .LEGUMES_TARIFS_TABLE.".`date` as 'tarif_date'"
            ." FROM ".LEGUMES_TABLE
            ." LEFT JOIN ".LEGUMES_TARIFS_TABLE." ON ".LEGUMES_TABLE.".`tarif`=".LEGUMES_TARIFS_TABLE.".`ref`";
}

function buildLegumesListSearchSQL($search) { return LEGUMES_TABLE.".`nom` LIKE '%".$search."%'"; }

////////////////////////////////////////////////////////////////////////
// LEGUMES - TARIFS ////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('LEGUMES_TARIFS_UNITES',array( 'kg', 'pièce', 'botte' ));
define('LEGUMES_TARIFS_UNITES_ACRONYMES',array( 'kg' => 'kg', 'pièce' => 'pc', 'botte' => 'bt' ));

define('LEGUMES_TARIFS_TABLE',"legumes_tarifs");
define('LEGUMES_TARIFS_TABLE_STRUCT', array(
    "legume"                => array( "type" => "int",      "min" => 1,  "max" => NULL,             "default" => NULL,      "force_default_under_min" => true   ),
    "groupe"                => array( "type" => "int",      "min" => 1,  "max" => NULL,             "default" => NULL,      "force_default_under_min" => true   ),
    "prix"                  => array( "type" => "float",    "min" => 0, "max" => 999.99,            "default" => 0,         "force_default_under_min" => true   ),
    "unite"                 => array( "type" => "set",      "values" => LEGUMES_TARIFS_UNITES,      "default" => "poids"                                        ),
    "date"                  => array( "type" => "date",                                             "default" => "today"                                        ),
    "archive"               => array( "type" => "bool",                                             "default" => 0                                              ),
    "del"                   => array( "type" => "bool",                                             "default" => 0                                              )
));

function getLegumesTarifsTableSelectBaseSQL() {
    return  "SELECT "
            .LEGUMES_TARIFS_TABLE.".`ref`,"
            .LEGUMES_TARIFS_TABLE.".`legume` as 'legume_ref',"
            .LEGUMES_TABLE.".`nom` as 'legume_nom',"
            .LEGUMES_TARIFS_TABLE.".`groupe` as 'groupe_ref',"
            .PANIERS_GROUPES_TABLE.".`nom` as 'groupe_nom',"
            .LEGUMES_TARIFS_TABLE.".`prix` as 'tarif_prix',"
            .LEGUMES_TARIFS_TABLE.".`unite` as 'tarif_unite',"
            .LEGUMES_TARIFS_TABLE.".`date` as 'tarif_date',"
            .LEGUMES_TARIFS_TABLE.".`archive`"
            ." FROM ".LEGUMES_TARIFS_TABLE
            ." LEFT JOIN ".LEGUMES_TABLE." ON ".LEGUMES_TARIFS_TABLE.".`legume`=".LEGUMES_TABLE.".`ref`"
            ." LEFT JOIN ".PANIERS_GROUPES_TABLE." ON ".LEGUMES_TARIFS_TABLE.".`groupe`=".PANIERS_GROUPES_TABLE.".`ref`";
}

////////////////////////////////////////////////////////////////////////
// LIEUX ///////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('LIEUX_TABLE',"lieux");
define('LIEUX_TABLE_STRUCT', array(
    "nom"   => array( "type" => "txt",  "length" => 64, "default" => "" ),
    "del"   => array( "type" => "bool",                 "default" => 0  )
));

function getLieuxTableSelectBaseSQL() {
    return  "SELECT "
            .LIEUX_TABLE.".`ref`,"
            .LIEUX_TABLE.".`nom`,"
            .LIEUX_TABLE.".`del`"
            ." FROM ".LIEUX_TABLE;
}

function buildLieuxSearchSQL($search) { return LIEUX_TABLE.".`nom` LIKE '%".$search."%'"; }

////////////////////////////////////////////////////////////////////////
// LIVRAISONS //////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('LIVRAISONS_NB_PANIERS_VIEW',"livraisons_nb_paniers");

define('LIVRAISONS_TABLE',"livraisons");
define('LIVRAISONS_TABLE_STRUCT', array(
    "paniers_groupe"    => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "date"              => array( "type" => "date",                                         "default" => "today"                                        ),
    "quinz_groupe"      => array( "type" => "set",      "values" => ['A', 'B'],             "default" => NULL                                           ),
    "archive"           => array( "type" => "bool",                                         "default" => 0                                              ),
    "force_not_archive" => array( "type" => "bool",                                         "default" => 0                                              ),
    "del"               => array( "type" => "bool",                                         "default" => 0                                              )
));

function getCountLivraisonsListSelectBaseSQL() {
    return  "SELECT count(*) as nb"
            ." FROM ".LIVRAISONS_TABLE
            ." LEFT JOIN ".PANIERS_GROUPES_TABLE." ON ".LIVRAISONS_TABLE.".`paniers_groupe`=".PANIERS_GROUPES_TABLE.".`ref`"
            ." LEFT JOIN ".LIVRAISONS_NB_PANIERS_VIEW." ON ".LIVRAISONS_TABLE.".`ref`=".LIVRAISONS_NB_PANIERS_VIEW.".`ref`";
}

function getLivraisonsTableSelectBaseSQL() {
    return  "SELECT "
            .LIVRAISONS_TABLE.".`ref`,"
            .LIVRAISONS_TABLE.".`paniers_groupe` as 'paniers_groupe_ref',"
            .PANIERS_GROUPES_TABLE.".`nom` as 'paniers_groupe_nom',"
            .LIVRAISONS_TABLE.".`date`,"
            .LIVRAISONS_TABLE.".`quinz_groupe`,"
            .LIVRAISONS_NB_PANIERS_VIEW.".`nb_paniers`,"
            .LIVRAISONS_TABLE.".`archive`"
            ." FROM ".LIVRAISONS_TABLE
            ." LEFT JOIN ".PANIERS_GROUPES_TABLE." ON ".LIVRAISONS_TABLE.".`paniers_groupe`=".PANIERS_GROUPES_TABLE.".`ref`"
            ." LEFT JOIN ".LIVRAISONS_NB_PANIERS_VIEW." ON ".LIVRAISONS_TABLE.".`ref`=".LIVRAISONS_NB_PANIERS_VIEW.".`ref`";
}

////////////////////////////////////////////////////////////////////////
// LIVRAISONS - LEGUMES ////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('LIVRAISONS_LEGUMES_TABLE',"livraisons_legumes");
define('LIVRAISONS_LEGUMES_TABLE_STRUCT', array(
    "livraison"             => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "panier_type"           => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "legume"                => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "tarif"                 => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "quantite"              => array( "type" => "float",    "min" => 0, "max" => 999.99,        "default" => 0,         "force_default_under_min" => true   )
));

function getLivraisonsLegumesTableSelectBaseSQL() {
    return  "SELECT "
            .LIVRAISONS_LEGUMES_TABLE.".`legume` as 'ref',"
            .LEGUMES_TABLE.".`nom` as 'nom',"
            .LIVRAISONS_LEGUMES_TABLE.".`livraison` as 'livraison_ref',"
            .LIVRAISONS_TABLE.".`date` as 'livraison_date',"
            .LIVRAISONS_LEGUMES_TABLE.".`panier_type` as 'panier_type_ref',"
            .PANIERS_TYPES_TABLE.".`nom` as 'panier_type_nom',"
            .LIVRAISONS_LEGUMES_TABLE.".`tarif` as 'tarif_ref',"
            .LEGUMES_TARIFS_TABLE.".`prix` as 'tarif_prix',"
            .LEGUMES_TARIFS_TABLE.".`unite` as 'tarif_unite',"
            .LIVRAISONS_LEGUMES_TABLE.".`quantite`"
            ." FROM ".LIVRAISONS_LEGUMES_TABLE
            ." LEFT JOIN ".LIVRAISONS_TABLE." ON ".LIVRAISONS_LEGUMES_TABLE.".`livraison`=".LIVRAISONS_TABLE.".`ref`"
            ." LEFT JOIN ".PANIERS_TYPES_TABLE." ON ".LIVRAISONS_LEGUMES_TABLE.".`panier_type`=".PANIERS_TYPES_TABLE.".`ref`"
            ." LEFT JOIN ".LEGUMES_TABLE." ON ".LIVRAISONS_LEGUMES_TABLE.".`legume`=".LEGUMES_TABLE.".`ref`"
            ." LEFT JOIN ".LEGUMES_TARIFS_TABLE." ON ".LIVRAISONS_LEGUMES_TABLE.".`tarif`=".LEGUMES_TARIFS_TABLE.".`ref`";
}

////////////////////////////////////////////////////////////////////////
// LIVRAISONS - PANIERS ////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

define('LIVRAISONS_PANIERS_TABLE',"livraisons_paniers");
define('LIVRAISONS_PANIERS_TABLE_STRUCT', array(
    "livraison"             => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "contrat"               => array( "type" => "int",      "min" => 1,  "max" => NULL,         "default" => NULL,      "force_default_under_min" => true   ),
    "del"                   => array( "type" => "bool",                                         "default" => 0                                              )
));

function getLivraisonsPaniersSelectBaseSQL() {
    return  "SELECT "
            .LIVRAISONS_PANIERS_TABLE.".`livraison` as 'livraison_ref',"
            .LIVRAISONS_TABLE.".`date` as 'livraison_date',"
            .LIVRAISONS_PANIERS_TABLE.".`contrat` as 'contrat_ref',"
            .CONTRATS_TABLE.".`client` as 'client_ref',"
            .CLIENTS_TABLE.".`nom` as 'client_nom',"
            .CLIENTS_TABLE.".`prenom` as 'client_prenom',"
            .CONTRATS_TABLE.".`type`,"
            .CONTRATS_TABLE.".`frequence`,"
            .CONTRATS_TABLE.".`nb_paniers`,"
            .CONTRATS_TABLE.".`quinz_groupe`,"
            .CONTRATS_PANIERS_STATUT.".`nb_paniers_livres_absolute`,"
            .CONTRATS_PANIERS_STATUT.".`nb_paniers_livres`,"
            .CONTRATS_PANIERS_STATUT.".`nb_paniers_restants`,"
            .CONTRATS_TABLE.".`panier_type` as 'panier_type_ref',"
            .PANIERS_TYPES_TABLE.".`nom` as 'panier_type_nom',"
            .CONTRATS_TABLE.".`lieu_depot` as 'lieu_depot_ref',"
            .LIEUX_TABLE.".`nom` as 'lieu_depot_nom',"
            .LIVRAISONS_PANIERS_TABLE.".`del`"
            ." FROM ".LIVRAISONS_PANIERS_TABLE
            ." LEFT JOIN ".LIVRAISONS_TABLE." ON ".LIVRAISONS_PANIERS_TABLE.".`livraison`=".LIVRAISONS_TABLE.".`ref`"
            ." LEFT JOIN ".CONTRATS_TABLE." ON ".LIVRAISONS_PANIERS_TABLE.".`contrat`=".CONTRATS_TABLE.".`ref`"
            ." LEFT JOIN ".CLIENTS_TABLE." ON ".CONTRATS_TABLE.".`client`=".CLIENTS_TABLE.".`ref`"
            ." LEFT JOIN ".PANIERS_TYPES_TABLE." ON ".CONTRATS_TABLE.".`panier_type`=".PANIERS_TYPES_TABLE.".`ref`"
            ." LEFT JOIN ".LIEUX_TABLE." ON ".CONTRATS_TABLE.".`lieu_depot`=".LIEUX_TABLE.".`ref`"
            ." LEFT JOIN ".CONTRATS_PANIERS_STATUT." ON ".CONTRATS_TABLE.".`ref`=".CONTRATS_PANIERS_STATUT.".`ref`";
}
