dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
17  */
18 
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  function __construct()
48  {
49  global $db, $conf;
50  $this->db = $db;
51  $this->order = new CommandeFournisseur($this->db);
52  }
53 
64  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 
99  function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids='', $status='', $sqlfilters = '')
100  {
101  global $db, $conf;
102 
103  $obj_ret = array();
104 
105  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
106  $socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids;
107 
108  // If the internal user must only see his customers, force searching by him
109  $search_sale = 0;
110  if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
111 
112  $sql = "SELECT t.rowid";
113  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $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)
114  $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as t";
115 
116  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $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
117 
118  $sql.= ' WHERE t.entity IN ('.getEntity('supplier_order').')';
119  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
120  if ($socids) $sql.= " AND t.fk_soc IN (".$socids.")";
121  if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
122 
123  // Filter by status
124  if ($status == 'draft') $sql.= " AND t.fk_statut IN (0)";
125  if ($status == 'validated') $sql.= " AND t.fk_statut IN (1)";
126  if ($status == 'approved') $sql.= " AND t.fk_statut IN (2)";
127  if ($status == 'running') $sql.= " AND t.fk_statut IN (3)";
128  if ($status == 'received_start') $sql.= " AND t.fk_statut IN (4)";
129  if ($status == 'received_end') $sql.= " AND t.fk_statut IN (5)";
130  if ($status == 'cancelled') $sql.= " AND t.fk_statut IN (6,7)";
131  if ($status == 'refused') $sql.= " AND t.fk_statut IN (9)";
132  // Insert sale filter
133  if ($search_sale > 0)
134  {
135  $sql .= " AND sc.fk_user = ".$search_sale;
136  }
137  // Add sql filters
138  if ($sqlfilters)
139  {
140  if (! DolibarrApi::_checkFilters($sqlfilters))
141  {
142  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
143  }
144  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
145  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
146  }
147 
148  $sql.= $db->order($sortfield, $sortorder);
149  if ($limit) {
150  if ($page < 0)
151  {
152  $page = 0;
153  }
154  $offset = $limit * $page;
155 
156  $sql.= $db->plimit($limit + 1, $offset);
157  }
158 
159  $result = $db->query($sql);
160  if ($result)
161  {
162  $i = 0;
163  $num = $db->num_rows($result);
164  $min = min($num, ($limit <= 0 ? $num : $limit));
165  while ($i < $min)
166  {
167  $obj = $db->fetch_object($result);
168  $order_static = new CommandeFournisseur($db);
169  if($order_static->fetch($obj->rowid)) {
170  $obj_ret[] = $this->_cleanObjectDatas($order_static);
171  }
172  $i++;
173  }
174  }
175  else {
176  throw new RestException(503, 'Error when retrieve supplier order list : '.$db->lasterror());
177  }
178  if( ! count($obj_ret)) {
179  throw new RestException(404, 'No supplier order found');
180  }
181  return $obj_ret;
182  }
183 
190  function post($request_data = null)
191  {
192  if(! DolibarrApiAccess::$user->rights->fournisseur->commande->creer) {
193  throw new RestException(401, "Insuffisant rights");
194  }
195  // Check mandatory fields
196  $result = $this->_validate($request_data);
197 
198  foreach($request_data as $field => $value) {
199  $this->order->$field = $value;
200  }
201  if(! array_keys($request_data,'date')) {
202  $this->order->date = dol_now();
203  }
204  /* We keep lines as an array
205  if (isset($request_data["lines"])) {
206  $lines = array();
207  foreach ($request_data["lines"] as $line) {
208  array_push($lines, (object) $line);
209  }
210  $this->order->lines = $lines;
211  }*/
212 
213  if ($this->order->create(DolibarrApiAccess::$user) < 0) {
214  throw new RestException(500, "Error creating order", array_merge(array($this->order->error), $this->order->errors));
215  }
216  return $this->order->id;
217  }
218 
226  function put($id, $request_data = null)
227  {
228  if(! DolibarrApiAccess::$user->rights->fournisseur->commande->creer) {
229  throw new RestException(401);
230  }
231 
232  $result = $this->order->fetch($id);
233  if( ! $result ) {
234  throw new RestException(404, 'Supplier order not found');
235  }
236 
237  if( ! DolibarrApi::_checkAccessToResource('fournisseur',$this->order->id,'','commande')) {
238  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
239  }
240 
241  foreach($request_data as $field => $value) {
242  if ($field == 'id') continue;
243  $this->order->$field = $value;
244  }
245 
246  if($this->order->update($id, DolibarrApiAccess::$user))
247  return $this->get ($id);
248 
249  return false;
250  }
251 
258  function delete($id)
259  {
260  if(! DolibarrApiAccess::$user->rights->fournisseur->commande->supprimer) {
261  throw new RestException(401);
262  }
263  $result = $this->order->fetch($id);
264  if( ! $result ) {
265  throw new RestException(404, 'Supplier order not found');
266  }
267 
268  if( ! DolibarrApi::_checkAccessToResource('fournisseur',$this->order->id,'','commande')) {
269  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
270  }
271 
272  if( $this->order->delete(DolibarrApiAccess::$user) < 0)
273  {
274  throw new RestException(500);
275  }
276 
277  return array(
278  'success' => array(
279  'code' => 200,
280  'message' => 'Supplier order deleted'
281  )
282  );
283  }
284 
285 
304  function validate($id, $idwarehouse=0, $notrigger=0)
305  {
306  if(! DolibarrApiAccess::$user->rights->fournisseur->commande->creer) {
307  throw new RestException(401);
308  }
309  $result = $this->order->fetch($id);
310  if( ! $result ) {
311  throw new RestException(404, 'Order not found');
312  }
313 
314  if( ! DolibarrApi::_checkAccessToResource('fournisseur',$this->order->id,'','commande')) {
315  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
316  }
317 
318  $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
319  if ($result == 0) {
320  throw new RestException(304, 'Error nothing done. May be object is already validated');
321  }
322  if ($result < 0) {
323  throw new RestException(500, 'Error when validating Order: '.$this->order->error);
324  }
325 
326  return array(
327  'success' => array(
328  'code' => 200,
329  'message' => 'Order validated (Ref='.$this->order->ref.')'
330  )
331  );
332  }
333 
340  function _cleanObjectDatas($object)
341  {
342 
343  $object = parent::_cleanObjectDatas($object);
344 
345  unset($object->rowid);
346  unset($object->barcode_type);
347  unset($object->barcode_type_code);
348  unset($object->barcode_type_label);
349  unset($object->barcode_type_coder);
350 
351  return $object;
352  }
353 
362  function _validate($data)
363  {
364  $order = array();
365  foreach (SupplierOrders::$FIELDS as $field) {
366  if (!isset($data[$field]))
367  throw new RestException(400, "$field field missing");
368  $order[$field] = $data[$field];
369  }
370  return $order;
371  }
372 }
_validate($data)
Validate fields before create or update object.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:251
Class for API REST v1.
Definition: api.class.php:29
validate($id, $idwarehouse=0, $notrigger=0)
Validate an order.
dol_now($mode='gmt')
Return date for now.
Class to manage predefined suppliers products.
post($request_data=null)
Create supplier order object.
_cleanObjectDatas($object)
Clean sensible object datas.
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:225
put($id, $request_data=null)
Update supplier order.