357 lines
13 KiB
PHP
357 lines
13 KiB
PHP
<?php
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
// CONTRATS ////////////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// LIST
|
|
|
|
$allowContratsListOrder = array(
|
|
'ref' => "réf.",
|
|
'groupe' => "groupe",
|
|
'type' => 'type de contrat',
|
|
'nom' => "nom",
|
|
'prenom' => "prénom",
|
|
'date' => "début de contrat",
|
|
'frequence' => "fréquence",
|
|
'panier_type' => "type de panier",
|
|
'nb_paniers_livre' => "nb. de paniers livrés",
|
|
'nb_paniers_restant' => "nb. de paniers restant",
|
|
'lieu_depot' => "lieu de dépôt"
|
|
);
|
|
|
|
function getNbContratsInList($filtres=array(), $search=false) {
|
|
$sql = getCountContratsListSelectBaseSQL()." WHERE ".CONTRATS_TABLE.".`del`=0";
|
|
$sql = buildContratsListFiltresSQL($sql, $filtres);
|
|
$sql = buildSqlSearch($sql, $search, buildContratsListSearchSQL);
|
|
$r = $GLOBALS['db_admin']['man']->select($sql, 1);
|
|
if(!$r['erreur']) return intval($r['datas']['nb']);
|
|
else {
|
|
$er = "</br>sql: ".$sql."</br>error: ".getReadableVar($r['erreur']);
|
|
return "Une erreur est survenue durant du nombre d'item dans la liste !".$er;
|
|
}
|
|
}
|
|
|
|
function getContratsList($order='id', $sens='ASC', $filtres=array(), $search=false, $limits=false) {
|
|
// ARCHIVES OLD CONTRATS
|
|
archiveOldContrat();
|
|
|
|
// LIST
|
|
$list = array();
|
|
|
|
$sql = getContratsTableSelectBaseSQL()." WHERE ".CONTRATS_TABLE.".`del`=0";
|
|
|
|
// FILTRES
|
|
$sql = buildContratsListFiltresSQL($sql, $filtres);
|
|
|
|
// SEARCH
|
|
$sql = buildSqlSearch($sql, $search, buildContratsListSearchSQL);
|
|
|
|
// ORDRE
|
|
$sql.=" ORDER BY ";
|
|
|
|
if($order=='ref') $sql .= CONTRATS_TABLE.".`ref`";
|
|
else if($order=='groupe') $sql .= CONTRATS_TABLE.".`groupe`";
|
|
else if($order=='type') $sql .= CONTRATS_TYPES_TABLE.".`nom`";
|
|
else if($order=='nom') $sql .= CLIENTS_TABLE.".`nom`";
|
|
else if($order=='prenom') $sql .= CLIENTS_TABLE.".`prenom`";
|
|
else if($order=='date') $sql .= CONTRATS_TABLE.".`date`";
|
|
else if($order=='frequence') $sql .= CONTRATS_TABLE.".`frequence`";
|
|
else if($order=='panier_type') $sql .= PANIERS_TYPES_TABLE.".`nom`";
|
|
else if($order=='nb_paniers_livre') $sql .= CONTRATS_PANIERS_STATUT.".`nb_paniers_livres`";
|
|
else if($order=='nb_paniers_restant') $sql .= CONTRATS_PANIERS_STATUT.".`nb_paniers_restants`";
|
|
else if($order=='lieu_depot') $sql .= LIEUX_TABLE.".`nom`";
|
|
else $sql .= CLIENTS_TABLE.".`nom`";
|
|
|
|
// SENS
|
|
if($sens=='DESC') $sql .= ' DESC';
|
|
else $sql .= ' ASC';
|
|
|
|
// LIMITS
|
|
if(is_array($limits)) $sql .= " LIMIT ".$limits['start'].", ".$limits['nbItemsByPage'];
|
|
|
|
$r = $GLOBALS['db_admin']['man']->select($sql);
|
|
|
|
if(!$r['erreur']) {
|
|
foreach($r['datas'] as $i) {
|
|
if(!$i["type_ref"]>0) $i["type_nom"] = "personnalisé";
|
|
|
|
$i["date_print"] = formatDate($i["date"], "mysql_date", "date");
|
|
|
|
$i["frequence_print"] = CONTRATS_FREQUENCES[$i["frequence"]];
|
|
if($i["frequence"] == "quinz") $i["frequence_print"] .= " (groupe ".$i["quinz_groupe"].')';
|
|
|
|
$i["warning_nb_paniers"] = ($i["frequence"]=="hebdo" && $i["nb_paniers_restants"]<=WARNING_NB_PANIERS_HEBDO)
|
|
|| ($i["frequence"]=="quinz" && $i["nb_paniers_restants"]<=WARNING_NB_PANIERS_QUINZE);
|
|
|
|
$list[$i['ref']] = $i;
|
|
}
|
|
}
|
|
else {
|
|
$er = "</br>sql: ".$sql."</br>error: ".getReadableVar($r['erreur']);
|
|
$GLOBALS['errors'][] = "Une erreur est survenue durant la récupération de la liste des contrats dans le base de données !".$er;
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
// GET DATAS
|
|
|
|
function getContratDatas($id) {
|
|
$i = array();
|
|
|
|
$sql = getContratsTableSelectBaseSQL()." WHERE ".CONTRATS_TABLE.".`ref`=".intval($id);
|
|
|
|
$r = $GLOBALS['db_admin']['man']->select($sql,1);
|
|
|
|
if(!$r['erreur']) {
|
|
$i = $r['datas'];
|
|
|
|
if(!$i["type_ref"]>0) $i["type_nom"] = "personnalisé <small>(".$i["groupe_nom"]." - ".$i["nb_paniers"]." x ".$i["panier_type_nom"]." - ".CONTRATS_FREQUENCES[$i["frequence"]].")</small>";
|
|
|
|
$i["date_print"] = formatDate($i["date"], "mysql_date", "date");
|
|
|
|
$i["frequence_print"] = CONTRATS_FREQUENCES[$i["frequence"]];
|
|
if($i["frequence"] == "quinz") $i["frequence_print"] .= " (groupe ".$i["quinz_groupe"].')';
|
|
|
|
$i["paniers_livres"] = getContratPaniersLivres($id);
|
|
}
|
|
else {
|
|
$er = "</br>sql: ".$sql."</br>error: ".getReadableVar($r['erreur']);
|
|
$GLOBALS['errors'][] = "Une erreur est survenue durant la récupération des informations du contrat dans le base de données !".$er;
|
|
}
|
|
|
|
return $i;
|
|
}
|
|
|
|
function getContratPaniersLivres($id) {
|
|
$list = array();
|
|
|
|
$sql = getLivraisonsPaniersSelectBaseSQL()
|
|
." WHERE ".LIVRAISONS_PANIERS_TABLE.".`contrat`=".intval($id)
|
|
." ORDER BY ".LIVRAISONS_TABLE.".`date` ASC";
|
|
|
|
$r = $GLOBALS['db_admin']['man']->select($sql);
|
|
|
|
if(!$r['erreur']) {
|
|
foreach($r['datas'] as $i) {
|
|
$i["livraison_date_print"] = formatDate($i["livraison_date"], "mysql_date", "print_date");
|
|
$list[$i['livraison_ref']] = $i;
|
|
}
|
|
}
|
|
else {
|
|
$er = "</br>sql: ".$sql."</br>error: ".getReadableVar($r['erreur']);
|
|
$GLOBALS['errors'][] = "Une erreur est survenue durant la récupération de la liste des paniers livrés du contrat dans le base de données !".$er;
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
function getContratLastPanier($id) {
|
|
$i = false;
|
|
|
|
$sql = getLivraisonsPaniersSelectBaseSQL()." WHERE ".CONTRATS_TABLE.".`ref`=$id ORDER BY ".LIVRAISONS_TABLE.".`date` DESC LIMIT 0,1";
|
|
$r = $GLOBALS['db_admin']['man']->select($sql);
|
|
|
|
if(!$r['erreur']) {
|
|
if(count($r['datas'])>0) {
|
|
$i = $r['datas'][0];
|
|
|
|
$i["livraison_date_print"] = formatDate($i["livraison_date"], "mysql_date", "date");
|
|
|
|
$i["frequence_print"] = CONTRATS_FREQUENCES[$i["frequence"]];
|
|
if($i["frequence"] == "quinz") $i["frequence_print"] .= " (groupe ".$i["quinz_groupe"].')';
|
|
}
|
|
}
|
|
else {
|
|
$er = "</br>sql: ".$sql."</br>error: ".getReadableVar($r['erreur']);
|
|
$GLOBALS['errors'][] = "Une erreur est survenue durant la récupération des informations du contrat dans le base de données !".$er;
|
|
}
|
|
|
|
return $i;
|
|
}
|
|
|
|
// ADD / UPDATE DATAS
|
|
|
|
function getContratDatasFromRequest() {
|
|
$datas = getRequestTableDatas(CONTRATS_TABLE_STRUCT);
|
|
if(isset($datas['frequence']) && $datas['frequence']=="hebdo") $datas['quinz_groupe'] = NULL;
|
|
return $datas;
|
|
}
|
|
|
|
function addContrat($datas) {
|
|
return addDatasInTable(
|
|
$GLOBALS['db_admin']['man'], // DB MANAGER
|
|
CONTRATS_TABLE, // TABLE NAME
|
|
CONTRATS_TABLE_STRUCT, // TABLE STRUCTURE
|
|
$datas, // DATAS
|
|
"du contrat" // ERROR STRING
|
|
);
|
|
}
|
|
|
|
function updateContrat($id, $datas) {
|
|
return updateDatasInTable(
|
|
$GLOBALS['db_admin']['man'], // DB MANAGER
|
|
CONTRATS_TABLE, // TABLE NAME
|
|
CONTRATS_TABLE_STRUCT, // TABLE STRUCTURE
|
|
$id, // ID
|
|
$datas, // DATAS
|
|
"au contrat", // NULL STRING
|
|
"du contrat" // ERROR STRING
|
|
);
|
|
}
|
|
|
|
function archiveContrat($id, $state) {
|
|
$datas = array(
|
|
"archive" => ($state == true) ? 1 : 0,
|
|
"force_not_archive" => ($state == true) ? 0 : 1,
|
|
);
|
|
$r = updateContrat($id, $datas);
|
|
if(!(int)$r>0) {
|
|
$er = "ERREUR : une erreur est survenue ";
|
|
$er.= ($state == true) ? "à l'archivage" : "au désarchivage";
|
|
$er = " du contrat ! ($r)";
|
|
return $er;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
function archiveOldContrat() {
|
|
$refs = "0";
|
|
|
|
$sql = "SELECT ".CONTRATS_TABLE.".`ref` FROM ".CONTRATS_TABLE
|
|
." LEFT JOIN ".CONTRATS_PANIERS_STATUT." ON ".CONTRATS_TABLE.".`ref`=".CONTRATS_PANIERS_STATUT.".`ref`"
|
|
." WHERE ".CONTRATS_PANIERS_STATUT.".`nb_paniers_restants`<=0"
|
|
." AND ".CONTRATS_TABLE.".`force_eligible`=0"
|
|
." AND ".CONTRATS_TABLE.".`archive`=0"
|
|
." AND ".CONTRATS_TABLE.".`del`=0";
|
|
|
|
$r = $GLOBALS['db_admin']['man']->select($sql);
|
|
|
|
if(!$r['erreur']) {
|
|
if(count($r['datas']>0)) {
|
|
$date = new DateTime(DATE_TIME_ZONE);
|
|
$dateInt = new DateInterval( "P".AUTO_ARCHIVE_OLD_CONTRATS_DELAY."D" );
|
|
$date->sub( $dateInt );
|
|
|
|
foreach($r['datas'] as $i) {
|
|
$lastPanier = getContratLastPanier($i['ref']);
|
|
if($lastPanier) {
|
|
$lastDate = DateTime::createFromFormat("Y-m-d", $lastPanier["livraison_date"], new DateTimeZone(DATE_TIME_ZONE));
|
|
if($lastDate < $date) $refs .= ",".$i['ref'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
$er = "</br>sql: ".$sql."</br>error: ".getReadableVar($r['erreur']);
|
|
$GLOBALS['errors'][] = "Une erreur est survenue durant la récupération de la liste des contrats expirés dans le base de données !".$er;
|
|
return;
|
|
}
|
|
|
|
if($refs != "0") {
|
|
$r = updateDatasInTableByCondition(
|
|
$GLOBALS['db_admin']['man'], // DB MANAGER
|
|
CONTRATS_TABLE, // TABLE NAME
|
|
CONTRATS_TABLE_STRUCT, // TABLE STRUCTURE
|
|
"WHERE ref IN ($refs)", // CONDITION
|
|
array("archive" => 1), // DATAS
|
|
"au contrat", // NULL STRING
|
|
"du contrat" // ERROR STRING
|
|
);
|
|
if(!$r>0) $GLOBALS['errors'][] = "ERREUR : une erreur est survenue durant l'archivage automatique des anciens contrats...<br/>($r)";
|
|
// else addDebug($refs, "ARCHIVE OLD CONTRATS DONE !");
|
|
}
|
|
}
|
|
|
|
function deleteContrat($id) {
|
|
return deleteItemInTable(
|
|
$GLOBALS['db_admin']['man'], // DB MANAGER
|
|
CONTRATS_TABLE, // TABLE NAME
|
|
$id, // ID
|
|
"du contrat" // ERROR STRING
|
|
);
|
|
}
|
|
|
|
function eraseContrat($id) {
|
|
return eraseItemInTable(
|
|
$GLOBALS['db_admin']['man'], // DB MANAGER
|
|
CONTRATS_TABLE, // TABLE NAME
|
|
$id, // ID
|
|
"du contrat" // ERROR STRING
|
|
);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
// PRINT LIST //////////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
function printContratsList($list) {
|
|
$GLOBALS['smarty'] -> assign('logo', "file://".PUBLIC_HTML_ADMIN_PATH."/img/logo.png");
|
|
$GLOBALS['smarty'] -> assign('date', formatDate('', '', 'print_date'));
|
|
$GLOBALS['smarty'] -> assign('time', formatDate('', '', 'print_time'));
|
|
$GLOBALS['smarty'] -> assign('cssFiles',array(
|
|
"file://".PUBLIC_HTML_ADMIN_PATH."css/bootstrap_print_light.css",
|
|
"file://".PUBLIC_HTML_ADMIN_PATH."css/print.css"
|
|
));
|
|
|
|
// LIST
|
|
$pList = array();
|
|
foreach($list as $i) {
|
|
// GROUP BY - GROUPE / LIEU / TYPE DE PANNIER
|
|
$groupe = intval($i['groupe_ref']);
|
|
$lieu = intval($i['lieu_depot_ref']);
|
|
$pType = intval($i['panier_type_ref']);
|
|
|
|
// CREATION DU GROUPE
|
|
if(!array_key_exists($groupe, $pList)) {
|
|
$pList[$groupe] = array(
|
|
"ref" => $groupe,
|
|
"nom" => $i['groupe_nom'],
|
|
"nb_contrats" => 0,
|
|
"lieux" => array()
|
|
);
|
|
}
|
|
|
|
// CREATION DU LIEU
|
|
if(!array_key_exists($lieu, $pList[$groupe]["lieux"])) {
|
|
$pList[$groupe]["lieux"][$lieu] = array(
|
|
"ref" => $lieu,
|
|
"nom" => $i['lieu_depot_nom'],
|
|
"nb_contrats" => 0,
|
|
"paniers" => array()
|
|
);
|
|
}
|
|
|
|
// CREATION DU TYPE DE PANIER
|
|
if(!array_key_exists($pType, $pList[$groupe]["lieux"][$lieu]["paniers"])) {
|
|
$pList[$groupe]["lieux"][$lieu]["paniers"][$pType] = array(
|
|
"type_ref" => $pType,
|
|
"type_nom" => $i['panier_type_nom'],
|
|
"nb_contrats" => 0,
|
|
"contrats" => array()
|
|
);
|
|
}
|
|
|
|
// AJOUT
|
|
$pList[$groupe]["lieux"][$lieu]["paniers"][$pType]["contrats"][] = $i;
|
|
$pList[$groupe]["nb_contrats"]++;
|
|
$pList[$groupe]["lieux"][$lieu]["nb_contrats"]++;
|
|
$pList[$groupe]["lieux"][$lieu]["paniers"][$pType]["nb_contrats"]++;
|
|
}
|
|
|
|
$GLOBALS['smarty'] -> assign('list',$pList);
|
|
$html = $GLOBALS['smarty']->fetch("contrats/contrats_list_print.tpl");
|
|
|
|
$dompdf = new Dompdf\Dompdf(["chroot" => ROOT_DIR_PATH]);
|
|
$dompdf->loadHtml($html);
|
|
$dompdf->setPaper('A4', "portrait");
|
|
$options = $dompdf->getOptions();
|
|
$options->setIsRemoteEnabled(true);
|
|
$dompdf->setOptions($options);
|
|
|
|
$dompdf->render();
|
|
$dompdf->stream("Liste des contrats (".formatDate('', '', 'strdate').").pdf");
|
|
|
|
die();
|
|
} |