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