dolibarr  17.0.4
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  public 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_fournisseur', '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  if (!DolibarrApiAccess::$user->rights->fournisseur->commande->lire) {
105  throw new RestException(401);
106  }
107 
108  $obj_ret = array();
109 
110  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
111  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
112 
113  // If the internal user must only see his customers, force searching by him
114  $search_sale = 0;
115  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
116  $search_sale = DolibarrApiAccess::$user->id;
117  }
118 
119  $sql = "SELECT t.rowid";
120  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
121  $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)
122  }
123  $sql .= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as t";
124 
125  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
126  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
127  }
128 
129  if (!empty($product_ids)) {
130  $sql .= ", ".MAIN_DB_PREFIX."commande_fournisseurdet as cd"; // We need this table joined to the select in order to filter by product
131  }
132 
133  $sql .= ' WHERE t.entity IN ('.getEntity('supplier_order').')';
134  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
135  $sql .= " AND t.fk_soc = sc.fk_soc";
136  }
137  if (!empty($product_ids)) {
138  $sql .= " AND cd.fk_commande = t.rowid AND cd.fk_product IN (".$this->db->sanitize($product_ids).")";
139  }
140  if ($socids) {
141  $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
142  }
143  if ($search_sale > 0) {
144  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
145  }
146 
147  // Filter by status
148  if ($status == 'draft') {
149  $sql .= " AND t.fk_statut IN (0)";
150  }
151  if ($status == 'validated') {
152  $sql .= " AND t.fk_statut IN (1)";
153  }
154  if ($status == 'approved') {
155  $sql .= " AND t.fk_statut IN (2)";
156  }
157  if ($status == 'running') {
158  $sql .= " AND t.fk_statut IN (3)";
159  }
160  if ($status == 'received_start') {
161  $sql .= " AND t.fk_statut IN (4)";
162  }
163  if ($status == 'received_end') {
164  $sql .= " AND t.fk_statut IN (5)";
165  }
166  if ($status == 'cancelled') {
167  $sql .= " AND t.fk_statut IN (6,7)";
168  }
169  if ($status == 'refused') {
170  $sql .= " AND t.fk_statut IN (9)";
171  }
172  // Insert sale filter
173  if ($search_sale > 0) {
174  $sql .= " AND sc.fk_user = ".((int) $search_sale);
175  }
176  // Add sql filters
177  if ($sqlfilters) {
178  $errormessage = '';
179  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
180  if ($errormessage) {
181  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
182  }
183  }
184 
185  $sql .= $this->db->order($sortfield, $sortorder);
186  if ($limit) {
187  if ($page < 0) {
188  $page = 0;
189  }
190  $offset = $limit * $page;
191 
192  $sql .= $this->db->plimit($limit + 1, $offset);
193  }
194 
195  $result = $this->db->query($sql);
196  if ($result) {
197  $i = 0;
198  $num = $this->db->num_rows($result);
199  $min = min($num, ($limit <= 0 ? $num : $limit));
200  while ($i < $min) {
201  $obj = $this->db->fetch_object($result);
202  $order_static = new CommandeFournisseur($this->db);
203  if ($order_static->fetch($obj->rowid)) {
204  $obj_ret[] = $this->_cleanObjectDatas($order_static);
205  }
206  $i++;
207  }
208  } else {
209  throw new RestException(503, 'Error when retrieve supplier order list : '.$this->db->lasterror());
210  }
211  if (!count($obj_ret)) {
212  throw new RestException(404, 'No supplier order found');
213  }
214  return $obj_ret;
215  }
216 
225  public function post($request_data = null)
226  {
227  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
228  throw new RestException(401, "Insuffisant rights");
229  }
230  // Check mandatory fields
231  $result = $this->_validate($request_data);
232 
233  foreach ($request_data as $field => $value) {
234  $this->order->$field = $value;
235  }
236  if (!array_keys($request_data, 'date')) {
237  $this->order->date = dol_now();
238  }
239  /* We keep lines as an array
240  if (isset($request_data["lines"])) {
241  $lines = array();
242  foreach ($request_data["lines"] as $line) {
243  array_push($lines, (object) $line);
244  }
245  $this->order->lines = $lines;
246  }*/
247 
248  if ($this->order->create(DolibarrApiAccess::$user) < 0) {
249  throw new RestException(500, "Error creating order", array_merge(array($this->order->error), $this->order->errors));
250  }
251  return $this->order->id;
252  }
253 
261  public function put($id, $request_data = null)
262  {
263  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
264  throw new RestException(401);
265  }
266 
267  $result = $this->order->fetch($id);
268  if (!$result) {
269  throw new RestException(404, 'Supplier order not found');
270  }
271 
272  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
273  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
274  }
275 
276  foreach ($request_data as $field => $value) {
277  if ($field == 'id') {
278  continue;
279  }
280  $this->order->$field = $value;
281  }
282 
283  if ($this->order->update(DolibarrApiAccess::$user)) {
284  return $this->get($id);
285  }
286 
287  return false;
288  }
289 
296  public function delete($id)
297  {
298  if (!DolibarrApiAccess::$user->rights->fournisseur->commande->supprimer) {
299  throw new RestException(401);
300  }
301  $result = $this->order->fetch($id);
302  if (!$result) {
303  throw new RestException(404, 'Supplier order not found');
304  }
305 
306  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
307  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
308  }
309 
310  if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
311  throw new RestException(500, 'Error when deleting order');
312  }
313 
314  return array(
315  'success' => array(
316  'code' => 200,
317  'message' => 'Supplier order deleted'
318  )
319  );
320  }
321 
322 
341  public function validate($id, $idwarehouse = 0, $notrigger = 0)
342  {
343  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
344  throw new RestException(401);
345  }
346  $result = $this->order->fetch($id);
347  if (!$result) {
348  throw new RestException(404, 'Order not found');
349  }
350 
351  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
352  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
353  }
354 
355  $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
356  if ($result == 0) {
357  throw new RestException(304, 'Error nothing done. May be object is already validated');
358  }
359  if ($result < 0) {
360  throw new RestException(500, 'Error when validating Order: '.$this->order->error);
361  }
362 
363  return array(
364  'success' => array(
365  'code' => 200,
366  'message' => 'Order validated (Ref='.$this->order->ref.')'
367  )
368  );
369  }
370 
389  public function approve($id, $idwarehouse = 0, $secondlevel = 0)
390  {
391  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
392  throw new RestException(401);
393  }
394  $result = $this->order->fetch($id);
395  if (!$result) {
396  throw new RestException(404, 'Order not found');
397  }
398 
399  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
400  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
401  }
402 
403  $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
404  if ($result == 0) {
405  throw new RestException(304, 'Error nothing done. May be object is already approved');
406  }
407  if ($result < 0) {
408  throw new RestException(500, 'Error when approve Order: '.$this->order->error);
409  }
410 
411  return array(
412  'success' => array(
413  'code' => 200,
414  'message' => 'Order approved (Ref='.$this->order->ref.')'
415  )
416  );
417  }
418 
419 
440  public function makeOrder($id, $date, $method, $comment = '')
441  {
442  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
443  throw new RestException(401);
444  }
445  $result = $this->order->fetch($id);
446  if (!$result) {
447  throw new RestException(404, 'Order not found');
448  }
449 
450  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
451  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
452  }
453 
454  $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
455  if ($result == 0) {
456  throw new RestException(304, 'Error nothing done. May be object is already sent');
457  }
458  if ($result < 0) {
459  throw new RestException(500, 'Error when sending Order: '.$this->order->error);
460  }
461 
462  return array(
463  'success' => array(
464  'code' => 200,
465  'message' => 'Order sent (Ref='.$this->order->ref.')'
466  )
467  );
468  }
469 
503  public function receiveOrder($id, $closeopenorder, $comment, $lines)
504  {
505  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
506  throw new RestException(401);
507  }
508  $result = $this->order->fetch($id);
509  if (!$result) {
510  throw new RestException(404, 'Order not found');
511  }
512 
513  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
514  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
515  }
516 
517  foreach ($lines as $line) {
518  $lineObj =(object) $line;
519 
520  $result=$this->order->dispatchProduct(DolibarrApiAccess::$user,
521  $lineObj->fk_product,
522  $lineObj->qty,
523  $lineObj->warehouse,
524  $lineObj->price,
525  $lineObj->comment,
526  $lineObj->eatby,
527  $lineObj->sellby,
528  $lineObj->batch,
529  $lineObj->id,
530  $lineObj->notrigger);
531 
532  if ($result < 0) {
533  throw new RestException(500, 'Error dispatch order line '.$line->id.': '.$this->order->error);
534  }
535  }
536 
537  $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
538 
539  if ($result == 0) {
540  throw new RestException(304, 'Error nothing done. May be object is already dispatched');
541  }
542  if ($result < 0) {
543  throw new RestException(500, 'Error when receivce order: '.$this->order->error);
544  }
545 
546  return array(
547  'success' => array(
548  'code' => 200,
549  'message' => 'Order received (Ref='.$this->order->ref.')'
550  )
551  );
552  }
553 
554  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
561  protected function _cleanObjectDatas($object)
562  {
563  // phpcs:enable
564  $object = parent::_cleanObjectDatas($object);
565 
566  unset($object->rowid);
567  unset($object->barcode_type);
568  unset($object->barcode_type_code);
569  unset($object->barcode_type_label);
570  unset($object->barcode_type_coder);
571 
572  return $object;
573  }
574 
583  private function _validate($data)
584  {
585  $order = array();
586  foreach (SupplierOrders::$FIELDS as $field) {
587  if (!isset($data[$field])) {
588  throw new RestException(400, "$field field missing");
589  }
590  $order[$field] = $data[$field];
591  }
592  return $order;
593  }
594 }
Class to manage predefined suppliers products.
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
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.
put($id, $request_data=null)
Update supplier order.
post($request_data=null)
Create supplier order object.
_cleanObjectDatas($object)
Clean sensible object datas.
makeOrder($id, $date, $method, $comment='')
Sends an order to the vendor.
_validate($data)
Validate fields before create or update object.
approve($id, $idwarehouse=0, $secondlevel=0)
Approve an order.
receiveOrder($id, $closeopenorder, $comment, $lines)
Receives the order, dispatches products.
forgeSQLFromUniversalSearchCriteria($filter, &$error='')
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
$conf db
API class for accounts.
Definition: inc.php:41