dolibarr  13.0.2
api_shipments.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.'/expedition/class/expedition.class.php';
22 
29 class Shipments extends DolibarrApi
30 {
31 
35  static $FIELDS = array(
36  'socid',
37  'origin_id',
38  'origin_type',
39  );
40 
44  public $shipment;
45 
49  public function __construct()
50  {
51  global $db, $conf;
52  $this->db = $db;
53  $this->shipment = new Expedition($this->db);
54  }
55 
66  public function get($id)
67  {
68  if (!DolibarrApiAccess::$user->rights->expedition->lire) {
69  throw new RestException(401);
70  }
71 
72  $result = $this->shipment->fetch($id);
73  if (!$result) {
74  throw new RestException(404, 'Shipment not found');
75  }
76 
77  if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
78  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
79  }
80 
81  $this->shipment->fetchObjectLinked();
82  return $this->_cleanObjectDatas($this->shipment);
83  }
84 
85 
86 
102  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
103  {
104  global $db, $conf;
105 
106  $obj_ret = array();
107 
108  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
109  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
110 
111  // If the internal user must only see his customers, force searching by him
112  $search_sale = 0;
113  if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
114 
115  $sql = "SELECT t.rowid";
116  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)
117  $sql .= " FROM ".MAIN_DB_PREFIX."expedition as t";
118 
119  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
120 
121  $sql .= ' WHERE t.entity IN ('.getEntity('expedition').')';
122  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .= " AND t.fk_soc = sc.fk_soc";
123  if ($socids) $sql .= " AND t.fk_soc IN (".$socids.")";
124  if ($search_sale > 0) $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
125  // Insert sale filter
126  if ($search_sale > 0)
127  {
128  $sql .= " AND sc.fk_user = ".$search_sale;
129  }
130  // Add sql filters
131  if ($sqlfilters)
132  {
133  if (!DolibarrApi::_checkFilters($sqlfilters))
134  {
135  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
136  }
137  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
138  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
139  }
140 
141  $sql .= $this->db->order($sortfield, $sortorder);
142  if ($limit) {
143  if ($page < 0)
144  {
145  $page = 0;
146  }
147  $offset = $limit * $page;
148 
149  $sql .= $this->db->plimit($limit + 1, $offset);
150  }
151 
152  dol_syslog("API Rest request");
153  $result = $this->db->query($sql);
154 
155  if ($result)
156  {
157  $num = $this->db->num_rows($result);
158  $min = min($num, ($limit <= 0 ? $num : $limit));
159  $i = 0;
160  while ($i < $min)
161  {
162  $obj = $this->db->fetch_object($result);
163  $shipment_static = new Expedition($this->db);
164  if ($shipment_static->fetch($obj->rowid)) {
165  $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
166  }
167  $i++;
168  }
169  } else {
170  throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
171  }
172  if (!count($obj_ret)) {
173  throw new RestException(404, 'No shipment found');
174  }
175  return $obj_ret;
176  }
177 
184  public function post($request_data = null)
185  {
186  if (!DolibarrApiAccess::$user->rights->expedition->creer) {
187  throw new RestException(401, "Insuffisant rights");
188  }
189  // Check mandatory fields
190  $result = $this->_validate($request_data);
191 
192  foreach ($request_data as $field => $value) {
193  $this->shipment->$field = $value;
194  }
195  if (isset($request_data["lines"])) {
196  $lines = array();
197  foreach ($request_data["lines"] as $line) {
198  array_push($lines, (object) $line);
199  }
200  $this->shipment->lines = $lines;
201  }
202 
203  if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
204  throw new RestException(500, "Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
205  }
206 
207  return $this->shipment->id;
208  }
209 
210  // /**
211  // * Get lines of an shipment
212  // *
213  // * @param int $id Id of shipment
214  // *
215  // * @url GET {id}/lines
216  // *
217  // * @return int
218  // */
219  /*
220  public function getLines($id)
221  {
222  if(! DolibarrApiAccess::$user->rights->expedition->lire) {
223  throw new RestException(401);
224  }
225 
226  $result = $this->shipment->fetch($id);
227  if( ! $result ) {
228  throw new RestException(404, 'Shipment not found');
229  }
230 
231  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
232  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
233  }
234  $this->shipment->getLinesArray();
235  $result = array();
236  foreach ($this->shipment->lines as $line) {
237  array_push($result,$this->_cleanObjectDatas($line));
238  }
239  return $result;
240  }
241  */
242 
243  // /**
244  // * Add a line to given shipment
245  // *
246  // * @param int $id Id of shipment to update
247  // * @param array $request_data ShipmentLine data
248  // *
249  // * @url POST {id}/lines
250  // *
251  // * @return int
252  // */
253  /*
254  public function postLine($id, $request_data = null)
255  {
256  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
257  throw new RestException(401);
258  }
259 
260  $result = $this->shipment->fetch($id);
261  if ( ! $result ) {
262  throw new RestException(404, 'Shipment not found');
263  }
264 
265  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
266  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
267  }
268  $request_data = (object) $request_data;
269  $updateRes = $this->shipment->addline(
270  $request_data->desc,
271  $request_data->subprice,
272  $request_data->qty,
273  $request_data->tva_tx,
274  $request_data->localtax1_tx,
275  $request_data->localtax2_tx,
276  $request_data->fk_product,
277  $request_data->remise_percent,
278  $request_data->info_bits,
279  $request_data->fk_remise_except,
280  'HT',
281  0,
282  $request_data->date_start,
283  $request_data->date_end,
284  $request_data->product_type,
285  $request_data->rang,
286  $request_data->special_code,
287  $fk_parent_line,
288  $request_data->fk_fournprice,
289  $request_data->pa_ht,
290  $request_data->label,
291  $request_data->array_options,
292  $request_data->fk_unit,
293  $request_data->origin,
294  $request_data->origin_id,
295  $request_data->multicurrency_subprice
296  );
297 
298  if ($updateRes > 0) {
299  return $updateRes;
300 
301  }
302  return false;
303  }*/
304 
305  // /**
306  // * Update a line to given shipment
307  // *
308  // * @param int $id Id of shipment to update
309  // * @param int $lineid Id of line to update
310  // * @param array $request_data ShipmentLine data
311  // *
312  // * @url PUT {id}/lines/{lineid}
313  // *
314  // * @return object
315  // */
316  /*
317  public function putLine($id, $lineid, $request_data = null)
318  {
319  if (! DolibarrApiAccess::$user->rights->expedition->creer) {
320  throw new RestException(401);
321  }
322 
323  $result = $this->shipment->fetch($id);
324  if ( ! $result ) {
325  throw new RestException(404, 'Shipment not found');
326  }
327 
328  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
329  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
330  }
331  $request_data = (object) $request_data;
332  $updateRes = $this->shipment->updateline(
333  $lineid,
334  $request_data->desc,
335  $request_data->subprice,
336  $request_data->qty,
337  $request_data->remise_percent,
338  $request_data->tva_tx,
339  $request_data->localtax1_tx,
340  $request_data->localtax2_tx,
341  'HT',
342  $request_data->info_bits,
343  $request_data->date_start,
344  $request_data->date_end,
345  $request_data->product_type,
346  $request_data->fk_parent_line,
347  0,
348  $request_data->fk_fournprice,
349  $request_data->pa_ht,
350  $request_data->label,
351  $request_data->special_code,
352  $request_data->array_options,
353  $request_data->fk_unit,
354  $request_data->multicurrency_subprice
355  );
356 
357  if ($updateRes > 0) {
358  $result = $this->get($id);
359  unset($result->line);
360  return $this->_cleanObjectDatas($result);
361  }
362  return false;
363  }*/
364 
379  public function deleteLine($id, $lineid)
380  {
381  if (!DolibarrApiAccess::$user->rights->expedition->creer) {
382  throw new RestException(401);
383  }
384 
385  $result = $this->shipment->fetch($id);
386  if (!$result) {
387  throw new RestException(404, 'Shipment not found');
388  }
389 
390  if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
391  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
392  }
393 
394  // TODO Check the lineid $lineid is a line of ojbect
395 
396  $request_data = (object) $request_data;
397  $updateRes = $this->shipment->deleteline(DolibarrApiAccess::$user, $lineid);
398  if ($updateRes > 0) {
399  return $this->get($id);
400  } else {
401  throw new RestException(405, $this->shipment->error);
402  }
403  }
404 
413  public function put($id, $request_data = null)
414  {
415  if (!DolibarrApiAccess::$user->rights->expedition->creer) {
416  throw new RestException(401);
417  }
418 
419  $result = $this->shipment->fetch($id);
420  if (!$result) {
421  throw new RestException(404, 'Shipment not found');
422  }
423 
424  if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
425  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
426  }
427  foreach ($request_data as $field => $value) {
428  if ($field == 'id') continue;
429  $this->shipment->$field = $value;
430  }
431 
432  if ($this->shipment->update(DolibarrApiAccess::$user) > 0)
433  {
434  return $this->get($id);
435  } else {
436  throw new RestException(500, $this->shipment->error);
437  }
438  }
439 
447  public function delete($id)
448  {
449  if (!DolibarrApiAccess::$user->rights->expedition->supprimer) {
450  throw new RestException(401);
451  }
452  $result = $this->shipment->fetch($id);
453  if (!$result) {
454  throw new RestException(404, 'Shipment not found');
455  }
456 
457  if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
458  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
459  }
460 
461  if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
462  throw new RestException(500, 'Error when deleting shipment : '.$this->shipment->error);
463  }
464 
465  return array(
466  'success' => array(
467  'code' => 200,
468  'message' => 'Shipment deleted'
469  )
470  );
471  }
472 
492  public function validate($id, $notrigger = 0)
493  {
494  if (!DolibarrApiAccess::$user->rights->expedition->creer) {
495  throw new RestException(401);
496  }
497  $result = $this->shipment->fetch($id);
498  if (!$result) {
499  throw new RestException(404, 'Shipment not found');
500  }
501 
502  if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
503  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
504  }
505 
506  $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
507  if ($result == 0) {
508  throw new RestException(304, 'Error nothing done. May be object is already validated');
509  }
510  if ($result < 0) {
511  throw new RestException(500, 'Error when validating Shipment: '.$this->shipment->error);
512  }
513  $result = $this->shipment->fetch($id);
514  if (!$result) {
515  throw new RestException(404, 'Shipment not found');
516  }
517 
518  if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
519  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
520  }
521 
522  $this->shipment->fetchObjectLinked();
523  return $this->_cleanObjectDatas($this->shipment);
524  }
525 
526 
527  // /**
528  // * Classify the shipment as invoiced
529  // *
530  // * @param int $id Id of the shipment
531  // *
532  // * @url POST {id}/setinvoiced
533  // *
534  // * @return int
535  // *
536  // * @throws RestException 400
537  // * @throws RestException 401
538  // * @throws RestException 404
539  // * @throws RestException 405
540  // */
541  /*
542  public function setinvoiced($id)
543  {
544 
545  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
546  throw new RestException(401);
547  }
548  if(empty($id)) {
549  throw new RestException(400, 'Shipment ID is mandatory');
550  }
551  $result = $this->shipment->fetch($id);
552  if( ! $result ) {
553  throw new RestException(404, 'Shipment not found');
554  }
555 
556  $result = $this->shipment->classifyBilled(DolibarrApiAccess::$user);
557  if( $result < 0) {
558  throw new RestException(400, $this->shipment->error);
559  }
560  return $result;
561  }
562  */
563 
564 
565  // /**
566  // * Create a shipment using an existing order.
567  // *
568  // * @param int $orderid Id of the order
569  // *
570  // * @url POST /createfromorder/{orderid}
571  // *
572  // * @return int
573  // * @throws RestException 400
574  // * @throws RestException 401
575  // * @throws RestException 404
576  // * @throws RestException 405
577  // */
578  /*
579  public function createShipmentFromOrder($orderid)
580  {
581 
582  require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
583 
584  if(! DolibarrApiAccess::$user->rights->expedition->lire) {
585  throw new RestException(401);
586  }
587  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
588  throw new RestException(401);
589  }
590  if(empty($proposalid)) {
591  throw new RestException(400, 'Order ID is mandatory');
592  }
593 
594  $order = new Commande($this->db);
595  $result = $order->fetch($proposalid);
596  if( ! $result ) {
597  throw new RestException(404, 'Order not found');
598  }
599 
600  $result = $this->shipment->createFromOrder($order, DolibarrApiAccess::$user);
601  if( $result < 0) {
602  throw new RestException(405, $this->shipment->error);
603  }
604  $this->shipment->fetchObjectLinked();
605  return $this->_cleanObjectDatas($this->shipment);
606  }
607  */
608 
609  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
616  protected function _cleanObjectDatas($object)
617  {
618  // phpcs:enable
619  $object = parent::_cleanObjectDatas($object);
620 
621  unset($object->thirdparty); // id already returned
622 
623  unset($object->note);
624  unset($object->address);
625  unset($object->barcode_type);
626  unset($object->barcode_type_code);
627  unset($object->barcode_type_label);
628  unset($object->barcode_type_coder);
629 
630  if (!empty($object->lines) && is_array($object->lines))
631  {
632  foreach ($object->lines as $line)
633  {
634  unset($line->tva_tx);
635  unset($line->vat_src_code);
636  unset($line->total_ht);
637  unset($line->total_ttc);
638  unset($line->total_tva);
639  unset($line->total_localtax1);
640  unset($line->total_localtax2);
641  unset($line->remise_percent);
642  }
643  }
644 
645  return $object;
646  }
647 
655  private function _validate($data)
656  {
657  $shipment = array();
658  foreach (Shipments::$FIELDS as $field) {
659  if (!isset($data[$field]))
660  throw new RestException(400, "$field field missing");
661  $shipment[$field] = $data[$field];
662  }
663  return $shipment;
664  }
665 }
_cleanObjectDatas($object)
Classify the shipment as invoiced.
deleteLine($id, $lineid)
Get lines of an shipment.
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
__construct()
Constructor.
Class for API REST v1.
Definition: api.class.php:30
Class to manage shipments.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
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
put($id, $request_data=null)
Update shipment general fields (won&#39;t touch lines of shipment)
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $thirdparty_ids= '', $sqlfilters= '')
List shipments.
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create shipment object.
validate($id, $notrigger=0)
Validate a shipment.