From a14a63fdad4d0f13469c9424c872405fb03ab526 Mon Sep 17 00:00:00 2001 From: Thomas Negre Date: Thu, 3 Feb 2022 10:01:20 +0100 Subject: [PATCH] Contrat->getListOfContracts(): allow to sort contracts by contract status, product category, and line status. --- htdocs/contrat/class/contrat.class.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/htdocs/contrat/class/contrat.class.php b/htdocs/contrat/class/contrat.class.php index 4e4437c1b2c..e6c73164f26 100644 --- a/htdocs/contrat/class/contrat.class.php +++ b/htdocs/contrat/class/contrat.class.php @@ -2130,19 +2130,27 @@ class Contrat extends CommonObject /** * Return list of other contracts for same company than current contract * - * @param string $option 'all' or 'others' - * @return array|int Array of contracts id or <0 if error + * @param string $option 'all' or 'others' + * @param array $status sort contracts having these status + * @param array $product_categories sort contracts containing these product categories + * @param array $line_status sort contracts where lines have these status + * @return array|int Array of contracts id or <0 if error */ - public function getListOfContracts($option = 'all') + public function getListOfContracts($option = 'all', $status = [], $product_categories = [], $line_status = [] ) { $tab = array(); $sql = "SELECT c.rowid, c.ref"; $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c"; - $sql .= " WHERE fk_soc =".((int) $this->socid); - if ($option == 'others') { - $sql .= " AND c.rowid <> ".((int) $this->id); + if (!empty($product_categories)) { + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd ON cd.fk_contrat = c.rowid"; + $sql .= " INNER JOIN ".MAIN_DB_PREFIX."categorie_product as cp ON cp.fk_product = cd.fk_product AND cp.fk_categorie IN (".implode(', ', $product_categories).")"; } + $sql .= " WHERE c.fk_soc =".((int) $this->socid); + $sql .= ($option == 'others') ? " AND c.rowid <> ".((int) $this->id) : ""; + $sql .= (!empty($status)) ? " AND c.statut IN (".implode(', ', $status).")" : ""; + $sql .= (!empty($line_status)) ? " AND cd.statut IN (".implode(', ', $line_status).")" : ""; + $sql .= " GROUP BY c.rowid"; dol_syslog(get_class($this)."::getOtherContracts()", LOG_DEBUG); $resql = $this->db->query($sql);