diff --git a/htdocs/commande/class/api_orders.class.php b/htdocs/commande/class/api_orders.class.php index b82bd618a42..66e8c2ee957 100644 --- a/htdocs/commande/class/api_orders.class.php +++ b/htdocs/commande/class/api_orders.class.php @@ -156,12 +156,13 @@ class Orders extends DolibarrApi * @param int $page Page number * @param string $thirdparty_ids Thirdparty ids to filter orders of (example '1' or '1,2,3') {@pattern /^[0-9,]*$/i} * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" + * @param string $sqlfilterlines Other criteria to filter answers separated by a comma. Syntax example "(tl.fk_product:=:'17') and (tl.price:<:'250')" * @return array Array of order objects * * @throws RestException 404 Not found * @throws RestException 503 Error */ - public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '') + public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $sqlfilterlines = '') { global $db, $conf; @@ -212,7 +213,16 @@ class Orders extends DolibarrApi throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage); } } - + // Add sql filters for lines + if ($sqlfilterlines) { + $errormessage = ''; + $sql .= " AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX."commandedet AS tl WHERE tl.fk_commande = t.rowid"; + $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilterlines, $errormessage); + $sql .= ")"; + if ($errormessage) { + throw new RestException(400, 'Error when validating parameter sqlfilterlines -> '.$errormessage); + } + } $sql .= $this->db->order($sortfield, $sortorder); if ($limit) { if ($page < 0) {