Merge pull request #19927 from aspangaro/16a10

NEW Proposal - Add filter on proposal get lines
This commit is contained in:
Laurent Destailleur 2022-01-26 11:38:25 +01:00 committed by GitHub
commit ebdea44de9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 25 deletions

View File

@ -275,13 +275,16 @@ class Proposals extends DolibarrApi
* Get lines of a commercial proposal * Get lines of a commercial proposal
* *
* @param int $id Id of commercial proposal * @param int $id Id of commercial proposal
* @param string $sqlfilters Other criteria to filter answers separated by a comma. d is the alias for proposal lines table, p is the alias for product table. "Syntax example "(p.ref:like:'SO-%') and (d.date_start:<:'20220101')"
* *
* @url GET {id}/lines * @url GET {id}/lines
* *
* @return int * @return int
*/ */
public function getLines($id) public function getLines($id, $sqlfilters = '')
{ {
$filters = "";
if (!DolibarrApiAccess::$user->rights->propal->lire) { if (!DolibarrApiAccess::$user->rights->propal->lire) {
throw new RestException(401); throw new RestException(401);
} }
@ -294,7 +297,16 @@ class Proposals extends DolibarrApi
if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) { if (!DolibarrApi::_checkAccessToResource('propal', $this->propal->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
$this->propal->getLinesArray();
if (!empty($sqlfilters)) {
if (!DolibarrApi::_checkFilters($sqlfilters)) {
throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
}
$regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
$filters = " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
}
$this->propal->getLinesArray($filters);
$result = array(); $result = array();
foreach ($this->propal->lines as $line) { foreach ($this->propal->lines as $line) {
array_push($result, $this->_cleanObjectDatas($line)); array_push($result, $this->_cleanObjectDatas($line));

View File

@ -15,6 +15,8 @@
* Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com> * Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
* Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr> * Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
* Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es> * Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
* Copyright (C) 2022 ATM Consulting <contact@atm-consulting.fr>
* Copyright (C) 2022 OpenDSI <support@open-dsi.fr>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -1716,10 +1718,11 @@ class Propal extends CommonObject
* *
* @param int $only_product Return only physical products * @param int $only_product Return only physical products
* @param int $loadalsotranslation Return translation for products * @param int $loadalsotranslation Return translation for products
* @param string $filters Filter on other fields
* *
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function fetch_lines($only_product = 0, $loadalsotranslation = 0) public function fetch_lines($only_product = 0, $loadalsotranslation = 0, $filters = '')
{ {
global $langs, $conf; global $langs, $conf;
// phpcs:enable // phpcs:enable
@ -1738,6 +1741,9 @@ class Propal extends CommonObject
if ($only_product) { if ($only_product) {
$sql .= ' AND p.fk_product_type = 0'; $sql .= ' AND p.fk_product_type = 0';
} }
if ($filters) {
$sql .= $filters;
}
$sql .= ' ORDER by d.rang'; $sql .= ' ORDER by d.rang';
dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG); dol_syslog(get_class($this)."::fetch_lines", LOG_DEBUG);
@ -3689,12 +3695,13 @@ class Propal extends CommonObject
/** /**
* Retrieve an array of proposal lines * Retrieve an array of proposal lines
* @param string $filters Filter on other fields
* *
* @return int >0 if OK, <0 if KO * @return int >0 if OK, <0 if KO
*/ */
public function getLinesArray() public function getLinesArray($filters = '')
{ {
return $this->fetch_lines(); return $this->fetch_lines(0, 0, $filters);
} }
/** /**