From 6966651d5164603d330158643f1a68156acb65a2 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 28 Sep 2019 13:22:05 +0200 Subject: [PATCH] Standardize code to use dolSqlDateFilter --- htdocs/accountancy/customer/lines.php | 14 +------- htdocs/accountancy/customer/list.php | 14 +------- htdocs/accountancy/expensereport/lines.php | 14 +------- htdocs/accountancy/expensereport/list.php | 16 ++-------- htdocs/accountancy/supplier/lines.php | 14 +------- htdocs/accountancy/supplier/list.php | 14 +------- .../compta/facture/invoicetemplate_list.php | 29 ++--------------- htdocs/compta/paiement/cheque/list.php | 15 ++------- htdocs/compta/tva/list.php | 12 +------ htdocs/contact/consumption.php | 14 +------- htdocs/contrat/list.php | 14 +------- htdocs/core/lib/date.lib.php | 21 ++++++------ htdocs/fourn/facture/paiement.php | 14 +------- htdocs/holiday/month_report.php | 2 +- htdocs/product/stock/movement_card.php | 12 +------ htdocs/product/stock/movement_list.php | 12 +------ htdocs/product/stock/replenishorders.php | 15 +-------- htdocs/projet/list.php | 28 ++-------------- htdocs/projet/tasks.php | 32 +++---------------- htdocs/projet/tasks/list.php | 28 ++-------------- htdocs/projet/tasks/time.php | 15 +-------- htdocs/societe/consumption.php | 14 +------- test/phpunit/CodingSqlTest.php | 4 +++ 23 files changed, 45 insertions(+), 322 deletions(-) diff --git a/htdocs/accountancy/customer/lines.php b/htdocs/accountancy/customer/lines.php index 0e7047b84de..a209d6c41bb 100644 --- a/htdocs/accountancy/customer/lines.php +++ b/htdocs/accountancy/customer/lines.php @@ -215,19 +215,7 @@ if (strlen(trim($search_account))) { if (strlen(trim($search_vat))) { $sql .= natural_search("fd.tva_tx", price2num($search_vat), 1); } -if ($search_month > 0) -{ - if ($search_year > 0 && empty($search_day)) - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($search_year, $search_month, false))."' AND '".$db->idate(dol_get_last_day($search_year, $search_month, false))."'"; - elseif ($search_year > 0 && ! empty($search_day)) - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_month, $search_day, $search_year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_month, $search_day, $search_year))."'"; - else - $sql.= " AND date_format(f.datef, '%m') = '".$db->escape($search_month)."'"; -} -elseif ($search_year > 0) -{ - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($search_year, 1, false))."' AND '".$db->idate(dol_get_last_day($search_year, 12, false))."'"; -} +$sql.=dolSqlDateFilter('f.datef', $search_day, $search_month, $search_year); if (strlen(trim($search_country))) { $arrayofcode = getCountriesInEEC(); $country_code_in_EEC = $country_code_in_EEC_without_me = ''; diff --git a/htdocs/accountancy/customer/list.php b/htdocs/accountancy/customer/list.php index b738933c89c..65fd0886c79 100644 --- a/htdocs/accountancy/customer/list.php +++ b/htdocs/accountancy/customer/list.php @@ -251,19 +251,7 @@ if (strlen(trim($search_account))) { if (strlen(trim($search_vat))) { $sql .= natural_search("l.tva_tx", $search_vat, 1); } -if ($search_month > 0) -{ - if ($search_year > 0 && empty($search_day)) - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($search_year, $search_month, false))."' AND '".$db->idate(dol_get_last_day($search_year, $search_month, false))."'"; - elseif ($search_year > 0 && ! empty($search_day)) - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_month, $search_day, $search_year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_month, $search_day, $search_year))."'"; - else - $sql.= " AND date_format(f.datef, '%m') = '".$db->escape($search_month)."'"; -} -elseif ($search_year > 0) -{ - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($search_year, 1, false))."' AND '".$db->idate(dol_get_last_day($search_year, 12, false))."'"; -} +$sql.=dolSqlDateFilter('f.datef', $search_day, $search_month, $search_year); if (strlen(trim($search_country))) { $arrayofcode = getCountriesInEEC(); $country_code_in_EEC = $country_code_in_EEC_without_me = ''; diff --git a/htdocs/accountancy/expensereport/lines.php b/htdocs/accountancy/expensereport/lines.php index d15ef52ad39..9002c95e672 100644 --- a/htdocs/accountancy/expensereport/lines.php +++ b/htdocs/accountancy/expensereport/lines.php @@ -186,19 +186,7 @@ if (strlen(trim($search_account))) { if (strlen(trim($search_vat))) { $sql .= natural_search("erd.tva_tx", price2num($search_vat), 1); } -if ($search_month > 0) -{ - if ($search_year > 0 && empty($search_day)) - $sql.= " AND erd.date BETWEEN '".$db->idate(dol_get_first_day($search_year, $search_month, false))."' AND '".$db->idate(dol_get_last_day($search_year, $search_month, false))."'"; - elseif ($search_year > 0 && ! empty($search_day)) - $sql.= " AND erd.date BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_month, $search_day, $search_year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_month, $search_day, $search_year))."'"; - else - $sql.= " AND date_format(erd.date, '%m') = '".$db->escape($search_month)."'"; -} -elseif ($search_year > 0) -{ - $sql.= " AND erd.date BETWEEN '".$db->idate(dol_get_first_day($search_year, 1, false))."' AND '".$db->idate(dol_get_last_day($search_year, 12, false))."'"; -} +$sql.=dolSqlDateFilter('erd.date', $search_day, $search_month, $search_year); $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy $sql .= $db->order($sortfield, $sortorder); diff --git a/htdocs/accountancy/expensereport/list.php b/htdocs/accountancy/expensereport/list.php index 60b24638775..f29f93d6322 100644 --- a/htdocs/accountancy/expensereport/list.php +++ b/htdocs/accountancy/expensereport/list.php @@ -215,20 +215,8 @@ if (strlen(trim($search_account))) { if (strlen(trim($search_vat))) { $sql .= natural_search("erd.tva_tx", $search_vat, 1); } -if ($search_month > 0) -{ - if ($search_year > 0 && empty($search_day)) - $sql.= " AND erd.date BETWEEN '".$db->idate(dol_get_first_day($search_year, $search_month, false))."' AND '".$db->idate(dol_get_last_day($search_year, $search_month, false))."'"; - elseif ($search_year > 0 && ! empty($search_day)) - $sql.= " AND erd.date BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_month, $search_day, $search_year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_month, $search_day, $search_year))."'"; - else - $sql.= " AND date_format(erd.date, '%m') = '".$db->escape($search_month)."'"; -} -elseif ($search_year > 0) -{ - $sql.= " AND erd.date BETWEEN '".$db->idate(dol_get_first_day($search_year, 1, false))."' AND '".$db->idate(dol_get_last_day($search_year, 12, false))."'"; -} -$sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy +$sql.=dolSqlDateFilter('erd.date', $search_day, $search_month, $search_year); +$sql.= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy $sql .= $db->order($sortfield, $sortorder); diff --git a/htdocs/accountancy/supplier/lines.php b/htdocs/accountancy/supplier/lines.php index c4aa1316876..c60f9bf0ff2 100644 --- a/htdocs/accountancy/supplier/lines.php +++ b/htdocs/accountancy/supplier/lines.php @@ -210,19 +210,7 @@ if (strlen(trim($search_account))) { if (strlen(trim($search_vat))) { $sql .= natural_search("l.tva_tx", price2num($search_vat), 1); } -if ($search_month > 0) -{ - if ($search_year > 0 && empty($search_day)) - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($search_year, $search_month, false))."' AND '".$db->idate(dol_get_last_day($search_year, $search_month, false))."'"; - elseif ($search_year > 0 && ! empty($search_day)) - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_month, $search_day, $search_year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_month, $search_day, $search_year))."'"; - else - $sql.= " AND date_format(f.datef, '%m') = '".$db->escape($search_month)."'"; -} -elseif ($search_year > 0) -{ - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($search_year, 1, false))."' AND '".$db->idate(dol_get_last_day($search_year, 12, false))."'"; -} +$sql.=dolSqlDateFilter('f.datef', $search_day, $search_month, $search_year); if (strlen(trim($search_country))) { $arrayofcode = getCountriesInEEC(); $country_code_in_EEC = $country_code_in_EEC_without_me = ''; diff --git a/htdocs/accountancy/supplier/list.php b/htdocs/accountancy/supplier/list.php index 9ce49b23e6a..6a2327578b0 100644 --- a/htdocs/accountancy/supplier/list.php +++ b/htdocs/accountancy/supplier/list.php @@ -252,19 +252,7 @@ if (strlen(trim($search_account))) { if (strlen(trim($search_vat))) { $sql .= natural_search("l.tva_tx", price2num($search_vat), 1); } -if ($search_month > 0) -{ - if ($search_year > 0 && empty($search_day)) - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($search_year, $search_month, false))."' AND '".$db->idate(dol_get_last_day($search_year, $search_month, false))."'"; - elseif ($search_year > 0 && ! empty($search_day)) - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_month, $search_day, $search_year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_month, $search_day, $search_year))."'"; - else - $sql.= " AND date_format(f.datef, '%m') = '".$db->escape($search_month)."'"; -} -elseif ($search_year > 0) -{ - $sql.= " AND f.datef BETWEEN '".$db->idate(dol_get_first_day($search_year, 1, false))."' AND '".$db->idate(dol_get_last_day($search_year, 12, false))."'"; -} +$sql.=dolSqlDateFilter('f.datef', $search_day, $search_month, $search_year); if (strlen(trim($search_country))) { $arrayofcode = getCountriesInEEC(); $country_code_in_EEC = $country_code_in_EEC_without_me = ''; diff --git a/htdocs/compta/facture/invoicetemplate_list.php b/htdocs/compta/facture/invoicetemplate_list.php index bb072291cb8..b88cf6bfa0a 100644 --- a/htdocs/compta/facture/invoicetemplate_list.php +++ b/htdocs/compta/facture/invoicetemplate_list.php @@ -40,6 +40,7 @@ if (! empty($conf->projet->enabled)) { require_once DOL_DOCUMENT_ROOT . '/core/class/html.formprojet.class.php'; require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php'; require_once DOL_DOCUMENT_ROOT . '/core/lib/invoice.lib.php'; +require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php'; require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php'; // Load translation files required by the page @@ -246,32 +247,8 @@ if ($search_status != '' && $search_status >= -1) if ($search_status == 1) $sql.= ' AND frequency != 0 AND suspended = 0'; if ($search_status == -1) $sql.= ' AND suspended = 1'; } -if ($search_month > 0) -{ - if ($search_year > 0 && empty($search_day)) - $sql.= " AND f.date_last_gen BETWEEN '".$db->idate(dol_get_first_day($search_year, $search_month, false))."' AND '".$db->idate(dol_get_last_day($search_year, $search_month, false))."'"; - elseif ($search_year > 0 && ! empty($search_day)) - $sql.= " AND f.date_last_gen BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_month, $search_day, $search_year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_month, $search_day, $search_year))."'"; - else - $sql.= " AND date_format(f.date_last_gen, '%m') = '".$db->escape($search_month)."'"; -} -elseif ($search_year > 0) -{ - $sql.= " AND f.date_last_gen BETWEEN '".$db->idate(dol_get_first_day($search_year, 1, false))."' AND '".$db->idate(dol_get_last_day($search_year, 12, false))."'"; -} -if ($search_month_date_when > 0) -{ - if ($search_year_date_when > 0 && empty($search_day_date_when)) - $sql.= " AND f.date_when BETWEEN '".$db->idate(dol_get_first_day($search_year_date_when, $search_month_date_when, false))."' AND '".$db->idate(dol_get_last_day($search_year_date_when, $search_month_date_when, false))."'"; - elseif ($search_year_date_when > 0 && ! empty($search_day_date_when)) - $sql.= " AND f.date_when BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_month_date_when, $search_day_date_when, $search_year_date_when))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_month_date_when, $search_day_date_when, $search_year_date_when))."'"; - else - $sql.= " AND date_format(f.date_when, '%m') = '".$db->escape($search_month_date_when)."'"; -} -elseif ($search_year_date_when > 0) -{ - $sql.= " AND f.date_when BETWEEN '".$db->idate(dol_get_first_day($search_year_date_when, 1, false))."' AND '".$db->idate(dol_get_last_day($search_year_date_when, 12, false))."'"; -} +$sql.=dolSqlDateFilter('f.date_last_gen', $search_day, $search_month, $search_year); +$sql.=dolSqlDateFilter('f.date_last_gen', $search_day_date_when, $search_month_date_when, $search_year_date_when); $sql.= $db->order($sortfield, $sortorder); diff --git a/htdocs/compta/paiement/cheque/list.php b/htdocs/compta/paiement/cheque/list.php index 64ae1dea1f2..b43ed9dfe1c 100644 --- a/htdocs/compta/paiement/cheque/list.php +++ b/htdocs/compta/paiement/cheque/list.php @@ -96,19 +96,8 @@ $sql.= " AND bc.entity = ".$conf->entity; if ($search_ref) $sql.=natural_search("bc.ref", $search_ref); if ($search_account > 0) $sql.=" AND bc.fk_bank_account=".$search_account; if ($search_amount) $sql.=natural_search("bc.amount", price2num($search_amount)); -if ($month > 0) -{ - if ($year > 0 && empty($day)) - $sql.= " AND bc.date_bordereau BETWEEN '".$db->idate(dol_get_first_day($year, $month, false))."' AND '".$db->idate(dol_get_last_day($year, $month, false))."'"; - elseif ($year > 0 && ! empty($day)) - $sql.= " AND bc.date_bordereau BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month, $day, $year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $month, $day, $year))."'"; - else - $sql.= " AND date_format(bc.date_bordereau, '%m') = '".$month."'"; -} -elseif ($year > 0) -{ - $sql.= " AND bc.date_bordereau BETWEEN '".$db->idate(dol_get_first_day($year, 1, false))."' AND '".$db->idate(dol_get_last_day($year, 12, false))."'"; -} +$sql.= dolSqlDateFilter('bc.date_bordereau', 0, $month, $year); + $sql.= $db->order($sortfield, $sortorder); $nbtotalofrecords = ''; diff --git a/htdocs/compta/tva/list.php b/htdocs/compta/tva/list.php index d735edd5840..1214b0b0871 100644 --- a/htdocs/compta/tva/list.php +++ b/htdocs/compta/tva/list.php @@ -108,17 +108,7 @@ if ($search_ref) $sql.= natural_search("t.rowid", $search_ref); if ($search_label) $sql.= natural_search("t.label", $search_label); if ($search_amount) $sql.= natural_search("t.amount", price2num(trim($search_amount)), 1); if ($search_account > 0) $sql .=" AND b.fk_account=".$search_account; -if ($month > 0) -{ - if ($year > 0) - $sql.= " AND t.datev BETWEEN '".$db->idate(dol_get_first_day($year, $month, false))."' AND '".$db->idate(dol_get_last_day($year, $month, false))."'"; - else - $sql.= " AND date_format(t.datev, '%m') = '$month'"; -} -elseif ($year > 0) -{ - $sql.= " AND t.datev BETWEEN '".$db->idate(dol_get_first_day($year, 1, false))."' AND '".$db->idate(dol_get_last_day($year, 12, false))."'"; -} +$sql.= dolSqlDateFilter('t.datev', 0, $month, $year); if ($filtre) { $filtre=str_replace(":", "=", $filtre); $sql .= " AND ".$filtre; diff --git a/htdocs/contact/consumption.php b/htdocs/contact/consumption.php index 9cde6e2df4e..67972aabfd4 100644 --- a/htdocs/contact/consumption.php +++ b/htdocs/contact/consumption.php @@ -300,19 +300,7 @@ if (!empty($sql_select)) $sql.= " FROM "/*.MAIN_DB_PREFIX."societe as s, "*/.$tables_from; // if ($type_element != 'fichinter') $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON d.fk_product = p.rowid '; $sql.= $where; - if ($month > 0) { - if ($year > 0) { - $start = dol_mktime(0, 0, 0, $month, 1, $year); - $end = dol_time_plus_duree($start, 1, 'm') - 1; - $sql.= " AND ".$dateprint." BETWEEN '".$db->idate($start)."' AND '".$db->idate($end)."'"; - } else { - $sql.= " AND date_format(".$dateprint.", '%m') = '".sprintf('%02d', $month)."'"; - } - } elseif ($year > 0) { - $start = dol_mktime(0, 0, 0, 1, 1, $year); - $end = dol_time_plus_duree($start, 1, 'y') - 1; - $sql.= " AND ".$dateprint." BETWEEN '".$db->idate($start)."' AND '".$db->idate($end)."'"; - } + $sql.= dolSqlDateFilter($dateprint, 0, $month, $year); if ($sref) $sql.= " AND ".$doc_number." LIKE '%".$db->escape($sref)."%'"; if ($sprod_fulldescr) { diff --git a/htdocs/contrat/list.php b/htdocs/contrat/list.php index 9709eed637d..5fea2cee66f 100644 --- a/htdocs/contrat/list.php +++ b/htdocs/contrat/list.php @@ -241,19 +241,7 @@ $sql.= ' AND c.entity IN ('.getEntity('contract').')'; if ($search_product_category > 0) $sql.=" AND cp.fk_categorie = ".$search_product_category; if ($socid) $sql.= " AND s.rowid = ".$db->escape($socid); if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; -if ($month > 0) -{ - if ($year > 0 && empty($day)) - $sql.= " AND c.date_contrat BETWEEN '".$db->idate(dol_get_first_day($year, $month, false))."' AND '".$db->idate(dol_get_last_day($year, $month, false))."'"; - elseif ($year > 0 && ! empty($day)) - $sql.= " AND c.date_contrat BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month, $day, $year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $month, $day, $year))."'"; - else - $sql.= " AND date_format(c.date_contrat, '%m') = '".$month."'"; -} -elseif ($year > 0) -{ - $sql.= " AND c.date_contrat BETWEEN '".$db->idate(dol_get_first_day($year, 1, false))."' AND '".$db->idate(dol_get_last_day($year, 12, false))."'"; -} +$sql.= dolSqlDateFilter('c.date_contrat', $day, $month, $year); if ($search_name) $sql .= natural_search('s.nom', $search_name); if ($search_email) $sql .= natural_search('s.email', $search_email); if ($search_contract) $sql .= natural_search(array('c.rowid', 'c.ref'), $search_contract); diff --git a/htdocs/core/lib/date.lib.php b/htdocs/core/lib/date.lib.php index ea5027de1ee..a748218416f 100644 --- a/htdocs/core/lib/date.lib.php +++ b/htdocs/core/lib/date.lib.php @@ -282,27 +282,28 @@ function convertSecondToTime($iSecond, $format = 'all', $lengthOfDay = 86400, $l /** * Generate a SQL string to make a filter into a range (for second of date until last second of date) * - * @param string $datefield Name of SQL field where apply sql date filter - * @param int $day_date Day date - * @param int $month_date Month date - * @param int $year_date Year date - * @return string $sqldate String with SQL filter + * @param string $datefield Name of SQL field where apply sql date filter + * @param int $day_date Day date + * @param int $month_date Month date + * @param int $year_date Year date + * @param int $excludefirstand Exclude first and + * @return string $sqldate String with SQL filter */ -function dolSqlDateFilter($datefield, $day_date, $month_date, $year_date) +function dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand = 0) { global $db; $sqldate=""; if ($month_date > 0) { if ($year_date > 0 && empty($day_date)) { - $sqldate.= " AND ".$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, $month_date, false)); + $sqldate.= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, $month_date, false)); $sqldate.= "' AND '".$db->idate(dol_get_last_day($year_date, $month_date, false))."'"; } elseif ($year_date > 0 && ! empty($day_date)) { - $sqldate.= " AND ".$datefield." BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month_date, $day_date, $year_date)); + $sqldate.= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month_date, $day_date, $year_date)); $sqldate.= "' AND '".$db->idate(dol_mktime(23, 59, 59, $month_date, $day_date, $year_date))."'"; } else - $sqldate.= " AND date_format( ".$datefield.", '%m') = '".$db->escape($month_date)."'"; + $sqldate.= ($excludefirstand ? "" : " AND ")." date_format( ".$datefield.", '%m') = '".$db->escape($month_date)."'"; } elseif ($year_date > 0){ - $sqldate.= " AND ".$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, 1, false)); + $sqldate.= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, 1, false)); $sqldate.= "' AND '".$db->idate(dol_get_last_day($year_date, 12, false))."'"; } return $sqldate; diff --git a/htdocs/fourn/facture/paiement.php b/htdocs/fourn/facture/paiement.php index 640267e25da..893b92df599 100644 --- a/htdocs/fourn/facture/paiement.php +++ b/htdocs/fourn/facture/paiement.php @@ -823,19 +823,7 @@ if (empty($action) || $action == 'list') $sql.= " WHERE f.entity = ".$conf->entity; if (!$user->rights->societe->client->voir) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; if ($socid > 0) $sql .= ' AND f.fk_soc = '.$socid; - // Search criteria - if ($month > 0) { - if ($year > 0 && empty($day)) - $sql.= " AND p.datep BETWEEN '".$db->idate(dol_get_first_day($year, $month, false))."' AND '".$db->idate(dol_get_last_day($year, $month, false))."'"; - elseif ($year > 0 && ! empty($day)) - $sql.= " AND p.datep BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month, $day, $year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $month, $day, $year))."'"; - else - $sql.= " AND date_format(p.datep, '%m') = '".$month."'"; - } - elseif ($year > 0) - { - $sql.= " AND p.datep BETWEEN '".$db->idate(dol_get_first_day($year, 1, false))."' AND '".$db->idate(dol_get_last_day($year, 12, false))."'"; - } + $sql.= dolSqlDateFilter('p.datep', $day, $month, $year); if ($search_ref) $sql .= natural_search('p.rowid', $search_ref); if ($search_account > 0) $sql .=" AND b.fk_account=".$search_account; if ($search_paymenttype != "") $sql .=" AND c.code='".$db->escape($search_paymenttype)."'"; diff --git a/htdocs/holiday/month_report.php b/htdocs/holiday/month_report.php index 1823e5f0a93..f3740453e7c 100644 --- a/htdocs/holiday/month_report.php +++ b/htdocs/holiday/month_report.php @@ -27,6 +27,7 @@ require '../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php'; require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; // Load translation files required by the page @@ -68,7 +69,6 @@ print '
' . "\n"; $search_month = GETPOST("remonth", 'int')?GETPOST("remonth", 'int'):date("m", time()); $search_year = GETPOST("reyear", 'int')?GETPOST("reyear", 'int'):date("Y", time()); -$month_year = sprintf("%02d", $search_month).'-'.sprintf("%04d", $search_year); $year_month = sprintf("%04d", $search_year).'-'.sprintf("%02d", $search_month); print $formother->select_month($search_month, 'remonth'); diff --git a/htdocs/product/stock/movement_card.php b/htdocs/product/stock/movement_card.php index 85065e8ec2b..291dcc6e456 100644 --- a/htdocs/product/stock/movement_card.php +++ b/htdocs/product/stock/movement_card.php @@ -467,17 +467,7 @@ $sql.= " AND m.fk_entrepot = e.rowid"; $sql.= " AND e.entity IN (".getEntity('stock').")"; if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) $sql.= " AND p.fk_product_type = 0"; if ($id > 0) $sql.= " AND e.rowid ='".$id."'"; -if ($month > 0) -{ - if ($year > 0) - $sql.= " AND m.datem BETWEEN '".$db->idate(dol_get_first_day($year, $month, false))."' AND '".$db->idate(dol_get_last_day($year, $month, false))."'"; - else - $sql.= " AND date_format(m.datem, '%m') = '$month'"; -} -elseif ($year > 0) -{ - $sql.= " AND m.datem BETWEEN '".$db->idate(dol_get_first_day($year, 1, false))."' AND '".$db->idate(dol_get_last_day($year, 12, false))."'"; -} +$sql.= dolSqlDateFilter('m.datem', 0, $month, $year); if ($idproduct > 0) $sql.= " AND p.rowid = '".$idproduct."'"; if (! empty($search_ref)) $sql.= natural_search('m.rowid', $search_ref, 1); if (! empty($search_movement)) $sql.= natural_search('m.label', $search_movement); diff --git a/htdocs/product/stock/movement_list.php b/htdocs/product/stock/movement_list.php index 149804ee966..71e4d95f6c7 100644 --- a/htdocs/product/stock/movement_list.php +++ b/htdocs/product/stock/movement_list.php @@ -445,17 +445,7 @@ $sql.= " AND m.fk_entrepot = e.rowid"; $sql.= " AND e.entity IN (".getEntity('stock').")"; if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) $sql.= " AND p.fk_product_type = 0"; if ($id > 0) $sql.= " AND e.rowid ='".$id."'"; -if ($month > 0) -{ - if ($year > 0) - $sql.= " AND m.datem BETWEEN '".$db->idate(dol_get_first_day($year, $month, false))."' AND '".$db->idate(dol_get_last_day($year, $month, false))."'"; - else - $sql.= " AND date_format(m.datem, '%m') = '$month'"; -} -elseif ($year > 0) -{ - $sql.= " AND m.datem BETWEEN '".$db->idate(dol_get_first_day($year, 1, false))."' AND '".$db->idate(dol_get_last_day($year, 12, false))."'"; -} +$sql.= dolSqlDateFilter('m.datem', 0, $month, $year); if ($idproduct > 0) $sql.= " AND p.rowid = '".$idproduct."'"; if (! empty($search_ref)) $sql.= natural_search('m.rowid', $search_ref, 1); if (! empty($search_movement)) $sql.= natural_search('m.label', $search_movement); diff --git a/htdocs/product/stock/replenishorders.php b/htdocs/product/stock/replenishorders.php index 617d40b5232..59f436cb487 100644 --- a/htdocs/product/stock/replenishorders.php +++ b/htdocs/product/stock/replenishorders.php @@ -131,20 +131,7 @@ if ($sref) $sql .= natural_search('cf.ref', $sref); if ($snom) $sql .= natural_search('s.nom', $snom); if ($suser) $sql .= natural_search('u.login', $suser); if ($sttc) $sql .= natural_search('cf.total_ttc', $sttc, 1); - -if ($search_datemonth > 0) -{ - if ($search_dateyear > 0 && empty($search_dateday)) - $sql.= " AND cf.date_creation BETWEEN '".$db->idate(dol_get_first_day($search_dateyear, $search_datemonth, false))."' AND '".$db->idate(dol_get_last_day($search_dateyear, $search_datemonth, false))."'"; - elseif ($search_dateyear > 0 && ! empty($search_dateday)) - $sql.= " AND cf.date_creation BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_datemonth, $search_dateday, $search_dateyear))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_datemonth, $search_dateday, $search_dateyear))."'"; - else - $sql.= " AND date_format(cf.date_creation, '%m') = '".$search_datemonth."'"; -} -elseif ($search_dateyear > 0) -{ - $sql.= " AND cf.date_creation BETWEEN '".$db->idate(dol_get_first_day($search_dateyear, 1, false))."' AND '".$db->idate(dol_get_last_day($search_dateyear, 12, false))."'"; -} +$sql.= dolSqlDateFilter('cf.date_creation', $search_dateday, $search_datemonth, $search_dateyear); if ($sall) $sql .= natural_search(array('cf.ref','cf.note'), $sall); if (!empty($socid)) $sql .= ' AND s.rowid = ' . $socid; if (GETPOST('statut', 'int')) { diff --git a/htdocs/projet/list.php b/htdocs/projet/list.php index 40bebc5ce6a..62c4edb94d3 100644 --- a/htdocs/projet/list.php +++ b/htdocs/projet/list.php @@ -323,32 +323,8 @@ if ($search_label) $sql .= natural_search('p.title', $search_label); if ($search_societe) $sql .= natural_search('s.nom', $search_societe); if ($search_opp_amount) $sql .= natural_search('p.opp_amount', $search_opp_amount, 1); if ($search_opp_percent) $sql .= natural_search('p.opp_percent', $search_opp_percent, 1); -if ($search_smonth > 0) -{ - if ($search_syear > 0 && empty($search_sday)) - $sql.= " AND p.dateo BETWEEN '".$db->idate(dol_get_first_day($search_syear, $search_smonth, false))."' AND '".$db->idate(dol_get_last_day($search_syear, $search_smonth, false))."'"; - elseif ($search_syear > 0 && ! empty($search_sday)) - $sql.= " AND p.dateo BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_smonth, $search_sday, $search_syear))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_smonth, $search_sday, $search_syear))."'"; - else - $sql.= " AND date_format(p.dateo, '%m') = '".$search_smonth."'"; -} -elseif ($search_syear > 0) -{ - $sql.= " AND p.dateo BETWEEN '".$db->idate(dol_get_first_day($search_syear, 1, false))."' AND '".$db->idate(dol_get_last_day($search_syear, 12, false))."'"; -} -if ($search_emonth > 0) -{ - if ($search_eyear > 0 && empty($search_eday)) - $sql.= " AND p.datee BETWEEN '".$db->idate(dol_get_first_day($search_eyear, $search_emonth, false))."' AND '".$db->idate(dol_get_last_day($search_eyear, $search_emonth, false))."'"; - elseif ($search_eyear > 0 && ! empty($search_eday)) - $sql.= " AND p.datee BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_emonth, $search_eday, $search_eyear))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_emonth, $search_eday, $search_eyear))."'"; - else - $sql.= " AND date_format(p.datee, '%m') = '".$search_emonth."'"; -} -elseif ($search_eyear > 0) -{ - $sql.= " AND p.datee BETWEEN '".$db->idate(dol_get_first_day($search_eyear, 1, false))."' AND '".$db->idate(dol_get_last_day($search_eyear, 12, false))."'"; -} +$sql .= dolSqlDateFilter('p.dateo', $search_sday, $search_smonth, $search_syear); +$sql .= dolSqlDateFilter('p.datee', $search_eday, $search_emonth, $search_eyear); if ($search_all) $sql .= natural_search(array_keys($fieldstosearchall), $search_all); if ($search_status >= 0) { diff --git a/htdocs/projet/tasks.php b/htdocs/projet/tasks.php index 734904e9254..dd29a401bf7 100644 --- a/htdocs/projet/tasks.php +++ b/htdocs/projet/tasks.php @@ -177,35 +177,11 @@ if (!empty($search_tasklabel)) { $morewherefilterarray[]= natural_search('t.label', $search_tasklabel, 0, 1); } -if ($search_dtstartmonth > 0) -{ - if ($search_dtstartyear > 0 && empty($search_dtstartday)) { - $morewherefilterarray[]= " (t.dateo BETWEEN '".$db->idate(dol_get_first_day($search_dtstartyear, $search_dtstartmonth, false))."' AND '".$db->idate(dol_get_last_day($search_dtstartyear, $search_dtstartmonth, false))."')"; - } elseif ($search_dtstartyear > 0 && ! empty($search_dtstartday)) { - $morewherefilterarray[]= " (t.dateo BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_dtstartmonth, $search_dtstartday, $search_dtstartyear))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_dtstartmonth, $search_dtstartday, $search_dtstartyear))."')"; - } else { - $morewherefilterarray[]= " date_format(t.dateo, '%m') = '".$search_dtstartmonth."'"; - } -} -elseif ($search_dtstartyear > 0) -{ - $morewherefilterarray[]= " (t.dateo BETWEEN '".$db->idate(dol_get_first_day($search_dtstartyear, 1, false))."' AND '".$db->idate(dol_get_last_day($search_dtstartyear, 12, false))."')"; -} +$moresql = dolSqlDateFilter('t.dateo', $search_dtstartday, $search_dtstartmonth, $search_dtstartyear, 1); +if ($moresql) $morewherefilterarray[] = $moresql; -if ($search_dtendmonth > 0) -{ - if ($search_dtendyear > 0 && empty($search_dtendday)) { - $morewherefilterarray[]= " (t.datee BETWEEN '".$db->idate(dol_get_first_day($search_dtendyear, $search_dtendmonth, false))."' AND '".$db->idate(dol_get_last_day($search_dtendyear, $search_dtendmonth, false))."')"; - }elseif ($search_dtendyear > 0 && ! empty($search_dtendday)) { - $morewherefilterarray[]= " (t.datee BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_dtendmonth, $search_dtendday, $search_dtendyear))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_dtendmonth, $search_dtendday, $search_dtendyear))."')"; - }else { - $morewherefilterarray[]= " date_format(t.datee, '%m') = '".$search_dtendmonth."'"; - } -} -elseif ($search_dtendyear > 0) -{ - $morewherefilterarray[]= " (t.datee BETWEEN '".$db->idate(dol_get_first_day($search_dtendyear, 1, false))."' AND '".$db->idate(dol_get_last_day($search_dtendyear, 12, false))."')"; -} +$moresql = dolSqlDateFilter('t.datee', $search_dtendday, $search_dtendmonth, $search_dtendyear, 1); +if ($moresql) $morewherefilterarray[] = $moresql; if (!empty($search_planedworkload)) { $morewherefilterarray[]= natural_search('t.planned_workload', $search_planedworkload, 1, 1); diff --git a/htdocs/projet/tasks/list.php b/htdocs/projet/tasks/list.php index ebe506c15d6..7f6999b4b69 100644 --- a/htdocs/projet/tasks/list.php +++ b/htdocs/projet/tasks/list.php @@ -301,32 +301,8 @@ if ($search_project_title) $sql .= natural_search('p.title', $search_project_tit if ($search_task_ref) $sql .= natural_search('t.ref', $search_task_ref); if ($search_task_label) $sql .= natural_search('t.label', $search_task_label); if ($search_societe) $sql .= natural_search('s.nom', $search_societe); -if ($search_smonth > 0) -{ - if ($search_syear > 0 && empty($search_sday)) - $sql.= " AND t.dateo BETWEEN '".$db->idate(dol_get_first_day($search_syear, $search_smonth, false))."' AND '".$db->idate(dol_get_last_day($search_syear, $search_smonth, false))."'"; - elseif ($search_syear > 0 && ! empty($search_sday)) - $sql.= " AND t.dateo BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_smonth, $search_sday, $search_syear))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_smonth, $search_sday, $search_syear))."'"; - else - $sql.= " AND date_format(t.dateo, '%m') = '".$search_smonth."'"; -} -elseif ($search_syear > 0) -{ - $sql.= " AND t.dateo BETWEEN '".$db->idate(dol_get_first_day($search_syear, 1, false))."' AND '".$db->idate(dol_get_last_day($search_syear, 12, false))."'"; -} -if ($search_emonth > 0) -{ - if ($search_eyear > 0 && empty($search_eday)) - $sql.= " AND t.datee BETWEEN '".$db->idate(dol_get_first_day($search_eyear, $search_emonth, false))."' AND '".$db->idate(dol_get_last_day($search_eyear, $search_emonth, false))."'"; - elseif ($search_eyear > 0 && ! empty($search_eday)) - $sql.= " AND t.datee BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_emonth, $search_eday, $search_eyear))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_emonth, $search_eday, $search_eyear))."'"; - else - $sql.= " AND date_format(t.datee, '%m') = '".$search_emonth."'"; -} -elseif ($search_eyear > 0) -{ - $sql.= " AND t.datee BETWEEN '".$db->idate(dol_get_first_day($search_eyear, 1, false))."' AND '".$db->idate(dol_get_last_day($search_eyear, 12, false))."'"; -} +$sql.= dolSqlDateFilter('t.dateo', $search_sday, $search_smonth, $search_syear); +$sql.= dolSqlDateFilter('t.datee', $search_eday, $search_emonth, $search_eyear); if ($search_all) $sql .= natural_search(array_keys($fieldstosearchall), $search_all); if ($search_projectstatus >= 0) { diff --git a/htdocs/projet/tasks/time.php b/htdocs/projet/tasks/time.php index 8547b3d331c..d4c4a6d8473 100644 --- a/htdocs/projet/tasks/time.php +++ b/htdocs/projet/tasks/time.php @@ -899,20 +899,7 @@ if (($id > 0 || ! empty($ref)) || $projectidforalltimes > 0) if ($search_user > 0) $sql .= natural_search('t.fk_user', $search_user); if ($search_valuebilled == '1') $sql .= ' AND t.invoice_id > 0'; if ($search_valuebilled == '0') $sql .= ' AND (t.invoice_id = 0 OR t.invoice_id IS NULL)'; - if ($search_month > 0) - { - if ($search_year > 0 && empty($search_day)) - $sql.= " AND t.task_datehour BETWEEN '".$db->idate(dol_get_first_day($search_year, $search_month, false))."' AND '".$db->idate(dol_get_last_day($search_year, $search_month, false))."'"; - elseif ($search_year > 0 && ! empty($search_day)) - $sql.= " AND t.task_datehour BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_month, $search_day, $search_year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_month, $search_day, $search_year))."'"; - else - $sql.= " AND date_format(t.task_datehour, '%m') = '".$db->escape($search_month)."'"; - } - elseif ($search_year > 0) - { - $sql.= " AND t.task_datehour BETWEEN '".$db->idate(dol_get_first_day($search_year, 1, false))."' AND '".$db->idate(dol_get_last_day($search_year, 12, false))."'"; - } - //$sql .= ' GROUP BY t.rowid, t.fk_task, t.task_date, t.task_datehour, t.task_date_withhour, t.task_duration, t.fk_user, t.note, t.thm, pt.ref, pt.label, u.lastname, u.firstname, u.login, u.photo, u.statut, il.fk_facture'; + $sql .= dolSqlDateFilter('t.task_datehour', $search_day, $search_month, $search_year); $sql .= $db->order($sortfield, $sortorder); // Count total nb of records diff --git a/htdocs/societe/consumption.php b/htdocs/societe/consumption.php index 1c5c4b82a5d..fd8c33209bf 100644 --- a/htdocs/societe/consumption.php +++ b/htdocs/societe/consumption.php @@ -305,19 +305,7 @@ if (!empty($sql_select)) $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".$tables_from; if ($type_element != 'fichinter') $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON d.fk_product = p.rowid '; $sql.= $where; - if ($month > 0) { - if ($year > 0) { - $start = dol_mktime(0, 0, 0, $month, 1, $year); - $end = dol_time_plus_duree($start, 1, 'm') - 1; - $sql.= " AND ".$dateprint." BETWEEN '".$db->idate($start)."' AND '".$db->idate($end)."'"; - } else { - $sql.= " AND date_format(".$dateprint.", '%m') = '".sprintf('%02d', $month)."'"; - } - } elseif ($year > 0) { - $start = dol_mktime(0, 0, 0, 1, 1, $year); - $end = dol_time_plus_duree($start, 1, 'y') - 1; - $sql.= " AND ".$dateprint." BETWEEN '".$db->idate($start)."' AND '".$db->idate($end)."'"; - } + $sql.= dolSqlDateFilter($dateprint, 0, $month, $year); if ($sref) $sql.= " AND ".$doc_number." LIKE '%".$db->escape($sref)."%'"; if ($sprod_fulldescr) { diff --git a/test/phpunit/CodingSqlTest.php b/test/phpunit/CodingSqlTest.php index 1e1d3d1a838..c0f28e4c089 100644 --- a/test/phpunit/CodingSqlTest.php +++ b/test/phpunit/CodingSqlTest.php @@ -182,6 +182,10 @@ class CodingSqlTest extends PHPUnit\Framework\TestCase print __METHOD__." Result for checking we don't have 'NUMERIC(' = ".$result."\n"; $this->assertTrue($result===false, 'Found NUMERIC( into '.$file.'. Bad.'); + $result=strpos($filecontent, 'NUMERIC('); + print __METHOD__." Result for checking we don't have 'curdate(' = ".$result."\n"; + $this->assertTrue($result===false, 'Found curdate( into '.$file.'. Bad. Current date must be generated with PHP.'); + $result=strpos($filecontent, 'integer('); print __METHOD__." Result for checking we don't have 'integer(' = ".$result."\n"; $this->assertTrue($result===false, 'Found value in parenthesis after the integer. It must be integer not integer(x) into '.$file.'. Bad.');