dolibarr  16.0.5
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  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
180  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
181  }
182  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
183  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
184  }
185 
186  $sql .= $this->db->order($sortfield, $sortorder);
187  if ($limit) {
188  if ($page < 0) {
189  $page = 0;
190  }
191  $offset = $limit * $page;
192 
193  $sql .= $this->db->plimit($limit + 1, $offset);
194  }
195 
196  $result = $this->db->query($sql);
197  if ($result) {
198  $i = 0;
199  $num = $this->db->num_rows($result);
200  $min = min($num, ($limit <= 0 ? $num : $limit));
201  while ($i < $min) {
202  $obj = $this->db->fetch_object($result);
203  $order_static = new CommandeFournisseur($this->db);
204  if ($order_static->fetch($obj->rowid)) {
205  $obj_ret[] = $this->_cleanObjectDatas($order_static);
206  }
207  $i++;
208  }
209  } else {
210  throw new RestException(503, 'Error when retrieve supplier order list : '.$this->db->lasterror());
211  }
212  if (!count($obj_ret)) {
213  throw new RestException(404, 'No supplier order found');
214  }
215  return $obj_ret;
216  }
217 
226  public function post($request_data = null)
227  {
228  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
229  throw new RestException(401, "Insuffisant rights");
230  }
231  // Check mandatory fields
232  $result = $this->_validate($request_data);
233 
234  foreach ($request_data as $field => $value) {
235  $this->order->$field = $value;
236  }
237  if (!array_keys($request_data, 'date')) {
238  $this->order->date = dol_now();
239  }
240  /* We keep lines as an array
241  if (isset($request_data["lines"])) {
242  $lines = array();
243  foreach ($request_data["lines"] as $line) {
244  array_push($lines, (object) $line);
245  }
246  $this->order->lines = $lines;
247  }*/
248 
249  if ($this->order->create(DolibarrApiAccess::$user) < 0) {
250  throw new RestException(500, "Error creating order", array_merge(array($this->order->error), $this->order->errors));
251  }
252  return $this->order->id;
253  }
254 
262  public function put($id, $request_data = null)
263  {
264  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
265  throw new RestException(401);
266  }
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_fournisseur', 'commande')) {
274  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
275  }
276 
277  foreach ($request_data as $field => $value) {
278  if ($field == 'id') {
279  continue;
280  }
281  $this->order->$field = $value;
282  }
283 
284  if ($this->order->update(DolibarrApiAccess::$user)) {
285  return $this->get($id);
286  }
287 
288  return false;
289  }
290 
297  public function delete($id)
298  {
299  if (!DolibarrApiAccess::$user->rights->fournisseur->commande->supprimer) {
300  throw new RestException(401);
301  }
302  $result = $this->order->fetch($id);
303  if (!$result) {
304  throw new RestException(404, 'Supplier order not found');
305  }
306 
307  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
308  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
309  }
310 
311  if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
312  throw new RestException(500, 'Error when deleting order');
313  }
314 
315  return array(
316  'success' => array(
317  'code' => 200,
318  'message' => 'Supplier order deleted'
319  )
320  );
321  }
322 
323 
342  public function validate($id, $idwarehouse = 0, $notrigger = 0)
343  {
344  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
345  throw new RestException(401);
346  }
347  $result = $this->order->fetch($id);
348  if (!$result) {
349  throw new RestException(404, 'Order not found');
350  }
351 
352  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
353  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
354  }
355 
356  $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
357  if ($result == 0) {
358  throw new RestException(304, 'Error nothing done. May be object is already validated');
359  }
360  if ($result < 0) {
361  throw new RestException(500, 'Error when validating Order: '.$this->order->error);
362  }
363 
364  return array(
365  'success' => array(
366  'code' => 200,
367  'message' => 'Order validated (Ref='.$this->order->ref.')'
368  )
369  );
370  }
371 
390  public function approve($id, $idwarehouse = 0, $secondlevel = 0)
391  {
392  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
393  throw new RestException(401);
394  }
395  $result = $this->order->fetch($id);
396  if (!$result) {
397  throw new RestException(404, 'Order not found');
398  }
399 
400  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
401  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
402  }
403 
404  $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
405  if ($result == 0) {
406  throw new RestException(304, 'Error nothing done. May be object is already approved');
407  }
408  if ($result < 0) {
409  throw new RestException(500, 'Error when approve Order: '.$this->order->error);
410  }
411 
412  return array(
413  'success' => array(
414  'code' => 200,
415  'message' => 'Order approved (Ref='.$this->order->ref.')'
416  )
417  );
418  }
419 
420 
441  public function makeOrder($id, $date, $method, $comment = '')
442  {
443  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
444  throw new RestException(401);
445  }
446  $result = $this->order->fetch($id);
447  if (!$result) {
448  throw new RestException(404, 'Order not found');
449  }
450 
451  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
452  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
453  }
454 
455  $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
456  if ($result == 0) {
457  throw new RestException(304, 'Error nothing done. May be object is already sent');
458  }
459  if ($result < 0) {
460  throw new RestException(500, 'Error when sending Order: '.$this->order->error);
461  }
462 
463  return array(
464  'success' => array(
465  'code' => 200,
466  'message' => 'Order sent (Ref='.$this->order->ref.')'
467  )
468  );
469  }
470 
504  public function receiveOrder($id, $closeopenorder, $comment, $lines)
505  {
506  if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
507  throw new RestException(401);
508  }
509  $result = $this->order->fetch($id);
510  if (!$result) {
511  throw new RestException(404, 'Order not found');
512  }
513 
514  if (!DolibarrApi::_checkAccessToResource('fournisseur', $this->order->id, 'commande_fournisseur', 'commande')) {
515  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
516  }
517 
518  foreach ($lines as $line) {
519  $lineObj =(object) $line;
520 
521  $result=$this->order->dispatchProduct(DolibarrApiAccess::$user,
522  $lineObj->fk_product,
523  $lineObj->qty,
524  $lineObj->warehouse,
525  $lineObj->price,
526  $lineObj->comment,
527  $lineObj->eatby,
528  $lineObj->sellby,
529  $lineObj->batch,
530  $lineObj->id,
531  $lineObj->notrigger);
532 
533  if ($result < 0) {
534  throw new RestException(500, 'Error dispatch order line '.$line->id.': '.$this->order->error);
535  }
536  }
537 
538  $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
539 
540  if ($result == 0) {
541  throw new RestException(304, 'Error nothing done. May be object is already dispatched');
542  }
543  if ($result < 0) {
544  throw new RestException(500, 'Error when receivce order: '.$this->order->error);
545  }
546 
547  return array(
548  'success' => array(
549  'code' => 200,
550  'message' => 'Order received (Ref='.$this->order->ref.')'
551  )
552  );
553  }
554 
555  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
562  protected function _cleanObjectDatas($object)
563  {
564  // phpcs:enable
565  $object = parent::_cleanObjectDatas($object);
566 
567  unset($object->rowid);
568  unset($object->barcode_type);
569  unset($object->barcode_type_code);
570  unset($object->barcode_type_label);
571  unset($object->barcode_type_coder);
572 
573  return $object;
574  }
575 
584  private function _validate($data)
585  {
586  $order = array();
587  foreach (SupplierOrders::$FIELDS as $field) {
588  if (!isset($data[$field])) {
589  throw new RestException(400, "$field field missing");
590  }
591  $order[$field] = $data[$field];
592  }
593  return $order;
594  }
595 }
db
$conf db
API class for accounts.
Definition: inc.php:41
SupplierOrders\put
put($id, $request_data=null)
Update supplier order.
Definition: api_supplier_orders.class.php:262
SupplierOrders\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_supplier_orders.class.php:584
SupplierOrders\receiveOrder
receiveOrder($id, $closeopenorder, $comment, $lines)
Receives the order, dispatches products.
Definition: api_supplier_orders.class.php:504
DolibarrApi\_checkAccessToResource
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
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
SupplierOrders\post
post($request_data=null)
Create supplier order object.
Definition: api_supplier_orders.class.php:226
SupplierOrders\approve
approve($id, $idwarehouse=0, $secondlevel=0)
Approve an order.
Definition: api_supplier_orders.class.php:390
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
SupplierOrders\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $product_ids='', $status='', $sqlfilters='')
List orders.
Definition: api_supplier_orders.class.php:100
SupplierOrders\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_supplier_orders.class.php:562
SupplierOrders\__construct
__construct()
Constructor.
Definition: api_supplier_orders.class.php:47
CommandeFournisseur
Class to manage predefined suppliers products.
Definition: fournisseur.commande.class.php:47
SupplierOrders\makeOrder
makeOrder($id, $date, $method, $comment='')
Sends an order to the vendor.
Definition: api_supplier_orders.class.php:441
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
SupplierOrders
Definition: api_supplier_orders.class.php:29
SupplierOrders\validate
validate($id, $idwarehouse=0, $notrigger=0)
Validate an order.
Definition: api_supplier_orders.class.php:342