FIX #24138 Fix box_birthdays SQL for postgres

PR #21631's implementation causes issues with PostgreSQL and potentially
other stricter RDBMSes, indeed doing an ORDER BY using a function is not
supported.

In this fix we're computing the day of each user birthday and
employment day in the SELECT columns, and use those columns as a sorting
key.
This commit is contained in:
Mickaël 'lastmikoi' FALCK 2023-03-16 15:07:48 +01:00 committed by Laurent Destailleur
parent 1a89a7ea5c
commit 59b02a9f49
2 changed files with 5 additions and 6 deletions

View File

@ -85,19 +85,18 @@ class box_birthdays extends ModeleBoxes
if ($user->rights->user->user->lire) {
$tmparray = dol_getdate(dol_now(), true);
$sql = "SELECT u.rowid, u.firstname, u.lastname, u.birth as datea, 'birth' as typea, u.email, u.statut as status";
$sql = "SELECT u.rowid, u.firstname, u.lastname, u.birth as datea, date_format(u.birth, '%d') as daya, 'birth' as typea, u.email, u.statut as status";
$sql .= " FROM ".MAIN_DB_PREFIX."user as u";
$sql .= " WHERE u.entity IN (".getEntity('user').")";
$sql .= " AND u.statut = 1";
$sql .= dolSqlDateFilter('u.birth', 0, $tmparray['mon'], 0);
$sql .= ' UNION ';
$sql .= "SELECT u.rowid, u.firstname, u.lastname, u.dateemployment as datea, 'employment' as typea, u.email, u.statut as status";
$sql .= "SELECT u.rowid, u.firstname, u.lastname, u.dateemployment as datea, date_format(u.dateemployment, '%d') as daya, 'employment' as typea, u.email, u.statut as status";
$sql .= " FROM ".MAIN_DB_PREFIX."user as u";
$sql .= " WHERE u.entity IN (".getEntity('user').")";
$sql .= " AND u.statut = 1";
$sql .= dolSqlDateFilter('u.dateemployment', 0, $tmparray['mon'], 0);
$sql .= " ORDER BY datea ASC";
$sql .= " ORDER BY daya ASC"; // We want to have date of the month sorted by the day without taking into consideration the year
dol_syslog(get_class($this)."::loadBox", LOG_DEBUG);
$result = $this->db->query($sql);

View File

@ -85,12 +85,12 @@ class box_birthdays_members extends ModeleBoxes
if ($user->rights->adherent->lire) {
$tmparray = dol_getdate(dol_now(), true);
$sql = "SELECT u.rowid, u.firstname, u.lastname, u.birth";
$sql = "SELECT u.rowid, u.firstname, u.lastname, u.birth, date_format(u.birth, '%d') as daya";
$sql .= " FROM ".MAIN_DB_PREFIX."adherent as u";
$sql .= " WHERE u.entity IN (".getEntity('adherent').")";
$sql .= " AND u.statut = ".Adherent::STATUS_VALIDATED;
$sql .= dolSqlDateFilter('u.birth', 0, $tmparray['mon'], 0);
$sql .= " ORDER BY u.birth ASC";
$sql .= " ORDER BY daya ASC"; // We want to have date of the month sorted by the day without taking into consideration the year
$sql .= $this->db->plimit($max, 0);
dol_syslog(get_class($this)."::loadBox", LOG_DEBUG);