dolibarr  13.0.2
api_supplier_orders.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.'/fourn/class/fournisseur.commande.class.php';
22 
30 {
35  static $FIELDS = array(
36  'socid'
37  );
38 
42  public $order;
43 
47  public function __construct()
48  {
49  global $db, $conf;
50  $this->db = $db;
51  $this->order = new CommandeFournisseur($this->db);
52  }
53 
64  public function get($id)
65  {
66  if (!DolibarrApiAccess::$user->rights->fournisseur->commande->lire) {
67  throw new RestException(401);
68  }
69 
70  $result = $this->order->fetch($id);
71  if (!$result) {
72  throw new RestException(404, 'Supplier order not found');
73  }
74 
75  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, '', 'commande')) {
76  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
77  }
78 
79  $this->order->fetchObjectLinked();
80  return $this->_cleanObjectDatas($this->order);
81  }
82 
100  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $product_ids = '', $status = '', $sqlfilters = '')
101  {
102  global $db, $conf;
103 
104  $obj_ret = array();
105 
106  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
107  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
108 
109  // If the internal user must only see his customers, force searching by him
110  $search_sale = 0;
111  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
112 
113  $sql = "SELECT t.rowid";
114  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $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)
115  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as t";
116 
117  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
118 
119  if (!empty($product_ids)) $sql .= ", ".MAIN_DB_PREFIX."commande_fournisseurdet as cd"; // We need this table joined to the select in order to filter by product
120 
121  $sql .= ' WHERE t.entity IN ('.getEntity('supplier_order').')';
122  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.fk_soc = sc.fk_soc";
123  if (!empty($product_ids)) $sql .= " AND cd.fk_commande = t.rowid AND cd.fk_product IN (".$product_ids.")";
124  if ($socids) $sql .= " AND t.fk_soc IN (".$socids.")";
125  if ($search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
126 
127  // Filter by status
128  if ($status == 'draft') $sql .= " AND t.fk_statut IN (0)";
129  if ($status == 'validated') $sql .= " AND t.fk_statut IN (1)";
130  if ($status == 'approved') $sql .= " AND t.fk_statut IN (2)";
131  if ($status == 'running') $sql .= " AND t.fk_statut IN (3)";
132  if ($status == 'received_start') $sql .= " AND t.fk_statut IN (4)";
133  if ($status == 'received_end') $sql .= " AND t.fk_statut IN (5)";
134  if ($status == 'cancelled') $sql .= " AND t.fk_statut IN (6,7)";
135  if ($status == 'refused') $sql .= " AND t.fk_statut IN (9)";
136  // Insert sale filter
137  if ($search_sale > 0)
138  {
139  $sql .= " AND sc.fk_user = ".$search_sale;
140  }
141  // Add sql filters
142  if ($sqlfilters)
143  {
144  if (!DolibarrApi::_checkFilters($sqlfilters))
145  {
146  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
147  }
148  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
149  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
150  }
151 
152  $sql .= $this->db->order($sortfield, $sortorder);
153  if ($limit) {
154  if ($page < 0)
155  {
156  $page = 0;
157  }
158  $offset = $limit * $page;
159 
160  $sql .= $this->db->plimit($limit + 1, $offset);
161  }
162 
163  $result = $this->db->query($sql);
164  if ($result)
165  {
166  $i = 0;
167  $num = $this->db->num_rows($result);
168  $min = min($num, ($limit <= 0 ? $num : $limit));
169  while ($i < $min)
170  {
171  $obj = $this->db->fetch_object($result);
172  $order_static = new CommandeFournisseur($this->db);
173  if ($order_static->fetch($obj->rowid)) {
174  $obj_ret[] = $this->_cleanObjectDatas($order_static);
175  }
176  $i++;
177  }
178  } else {
179  throw new RestException(503, 'Error when retrieve supplier order list : '.$this->db->lasterror());
180  }
181  if (!count($obj_ret)) {
182  throw new RestException(404, 'No supplier order found');
183  }
184  return $obj_ret;
185  }
186 
195  public function post($request_data = null)
196  {
197  if (!DolibarrApiAccess::$user->rights->fournisseur->commande->creer) {
198  throw new RestException(401, "Insuffisant rights");
199  }
200  // Check mandatory fields
201  $result = $this->_validate($request_data);
202 
203  foreach ($request_data as $field => $value) {
204  $this->order->$field = $value;
205  }
206  if (!array_keys($request_data, 'date')) {
207  $this->order->date = dol_now();
208  }
209  /* We keep lines as an array
210  if (isset($request_data["lines"])) {
211  $lines = array();
212  foreach ($request_data["lines"] as $line) {
213  array_push($lines, (object) $line);
214  }
215  $this->order->lines = $lines;
216  }*/
217 
218  if ($this->order->create(DolibarrApiAccess::$user) < 0) {
219  throw new RestException(500, "Error creating order", array_merge(array($this->order->error), $this->order->errors));
220  }
221  return $this->order->id;
222  }
223 
231  public function put($id, $request_data = null)
232  {
233  if (!DolibarrApiAccess::$user->rights->fournisseur->commande->creer) {
234  throw new RestException(401);
235  }
236 
237  $result = $this->order->fetch($id);
238  if (!$result) {
239  throw new RestException(404, 'Supplier order not found');
240  }
241 
242  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, '', 'commande')) {
243  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
244  }
245 
246  foreach ($request_data as $field => $value) {
247  if ($field == 'id') continue;
248  $this->order->$field = $value;
249  }
250 
251  if ($this->order->update($id, DolibarrApiAccess::$user))
252  return $this->get($id);
253 
254  return false;
255  }
256 
263  public function delete($id)
264  {
265  if (!DolibarrApiAccess::$user->rights->fournisseur->commande->supprimer) {
266  throw new RestException(401);
267  }
268  $result = $this->order->fetch($id);
269  if (!$result) {
270  throw new RestException(404, 'Supplier order not found');
271  }
272 
273  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, '', 'commande')) {
274  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
275  }
276 
277  if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
278  throw new RestException(500);
279  }
280 
281  return array(
282  'success' => array(
283  'code' => 200,
284  'message' => 'Supplier order deleted'
285  )
286  );
287  }
288 
289 
308  public function validate($id, $idwarehouse = 0, $notrigger = 0)
309  {
310  if (!DolibarrApiAccess::$user->rights->fournisseur->commande->creer) {
311  throw new RestException(401);
312  }
313  $result = $this->order->fetch($id);
314  if (!$result) {
315  throw new RestException(404, 'Order not found');
316  }
317 
318  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, '', 'commande')) {
319  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
320  }
321 
322  $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
323  if ($result == 0) {
324  throw new RestException(304, 'Error nothing done. May be object is already validated');
325  }
326  if ($result < 0) {
327  throw new RestException(500, 'Error when validating Order: '.$this->order->error);
328  }
329 
330  return array(
331  'success' => array(
332  'code' => 200,
333  'message' => 'Order validated (Ref='.$this->order->ref.')'
334  )
335  );
336  }
337 
338  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
345  protected function _cleanObjectDatas($object)
346  {
347  // phpcs:enable
348  $object = parent::_cleanObjectDatas($object);
349 
350  unset($object->rowid);
351  unset($object->barcode_type);
352  unset($object->barcode_type_code);
353  unset($object->barcode_type_label);
354  unset($object->barcode_type_coder);
355 
356  return $object;
357  }
358 
367  private function _validate($data)
368  {
369  $order = array();
370  foreach (SupplierOrders::$FIELDS as $field) {
371  if (!isset($data[$field]))
372  throw new RestException(400, "$field field missing");
373  $order[$field] = $data[$field];
374  }
375  return $order;
376  }
377 }
_validate($data)
Validate fields before create or update object.
dol_now($mode= 'auto')
Return date for now.
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
Class for API REST v1.
Definition: api.class.php:30
validate($id, $idwarehouse=0, $notrigger=0)
Validate an order.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $thirdparty_ids= '', $product_ids= '', $status= '', $sqlfilters= '')
List orders.
Class to manage predefined suppliers products.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
Definition: api.class.php:252
post($request_data=null)
Create supplier order object.
_cleanObjectDatas($object)
Clean sensible object datas.
put($id, $request_data=null)
Update supplier order.