dolibarr  17.0.4
api_supplier_proposals.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3  * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/supplier_proposal/class/supplier_proposal.class.php';
22 
23 
31 {
32 
36  public static $FIELDS = array(
37  'socid'
38  );
39 
43  public $supplier_proposal;
44 
48  public function __construct()
49  {
50  global $db, $conf;
51  $this->db = $db;
52  $this->supplier_proposal = new SupplierProposal($this->db);
53  }
54 
65  public function get($id)
66  {
67  if (!DolibarrApiAccess::$user->rights->supplier_proposal->lire) {
68  throw new RestException(401);
69  }
70 
71  $result = $this->supplier_proposal->fetch($id);
72  if (!$result) {
73  throw new RestException(404, 'Supplier Proposal not found');
74  }
75 
76  if (!DolibarrApi::_checkAccessToResource('supplier_proposal', $this->propal->id)) {
77  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
78  }
79 
80  $this->supplier_proposal->fetchObjectLinked();
81  return $this->_cleanObjectDatas($this->supplier_proposal);
82  }
83 
97  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
98  {
99  global $db, $conf;
100 
101  if (!DolibarrApiAccess::$user->rights->supplier_proposal->lire) {
102  throw new RestException(401);
103  }
104 
105  $obj_ret = array();
106 
107  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
108  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
109 
110  // If the internal user must only see his customers, force searching by him
111  $search_sale = 0;
112  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
113  $search_sale = DolibarrApiAccess::$user->id;
114  }
115 
116  $sql = "SELECT t.rowid";
117  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
118  $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
119  }
120  $sql .= " FROM ".MAIN_DB_PREFIX."supplier_proposal as t";
121 
122  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
123  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
124  }
125 
126  $sql .= ' WHERE t.entity IN ('.getEntity('propal').')';
127  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
128  $sql .= " AND t.fk_soc = sc.fk_soc";
129  }
130  if ($socids) {
131  $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
132  }
133  if ($search_sale > 0) {
134  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
135  }
136  // Insert sale filter
137  if ($search_sale > 0) {
138  $sql .= " AND sc.fk_user = ".((int) $search_sale);
139  }
140  // Add sql filters
141  if ($sqlfilters) {
142  $errormessage = '';
143  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
144  if ($errormessage) {
145  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
146  }
147  }
148 
149  $sql .= $this->db->order($sortfield, $sortorder);
150  if ($limit) {
151  if ($page < 0) {
152  $page = 0;
153  }
154  $offset = $limit * $page;
155 
156  $sql .= $this->db->plimit($limit + 1, $offset);
157  }
158 
159  $result = $this->db->query($sql);
160 
161  if ($result) {
162  $num = $this->db->num_rows($result);
163  $min = min($num, ($limit <= 0 ? $num : $limit));
164  $i = 0;
165  while ($i < $min) {
166  $obj = $this->db->fetch_object($result);
167  $propal_static = new SupplierProposal($this->db);
168  if ($propal_static->fetch($obj->rowid)) {
169  $obj_ret[] = $this->_cleanObjectDatas($propal_static);
170  }
171  $i++;
172  }
173  } else {
174  throw new RestException(503, 'Error when retrieving supplier proposal list : '.$this->db->lasterror());
175  }
176  if (!count($obj_ret)) {
177  throw new RestException(404, 'No supplier proposal found');
178  }
179  return $obj_ret;
180  }
181 
182 
190  private function _validate($data)
191  {
192  $propal = array();
193  foreach (SupplierProposals::$FIELDS as $field) {
194  if (!isset($data[$field])) {
195  throw new RestException(400, "$field field missing");
196  }
197  $propal[$field] = $data[$field];
198  }
199  return $propal;
200  }
201 
202 
203  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
210  protected function _cleanObjectDatas($object)
211  {
212  // phpcs:enable
213  $object = parent::_cleanObjectDatas($object);
214 
215  unset($object->name);
216  unset($object->lastname);
217  unset($object->firstname);
218  unset($object->civility_id);
219  unset($object->address);
220  unset($object->datec);
221  unset($object->datev);
222 
223  return $object;
224  }
225 }
Class for API REST v1.
Definition: api.class.php:31
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
Class to manage price ask supplier.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List supplier proposals.
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before create or update object.
forgeSQLFromUniversalSearchCriteria($filter, &$error='')
forgeSQLFromUniversalSearchCriteria
$conf db
API class for accounts.
Definition: inc.php:41