dolibarr  7.0.0-beta
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 <http://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  function __construct()
50  {
51  global $db, $conf;
52  $this->db = $db;
53  $this->shipment = new Expedition($this->db);
54  }
55 
66  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  function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '') {
103  global $db, $conf;
104 
105  $obj_ret = array();
106 
107  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
108  $socids = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $thirdparty_ids;
109 
110  // If the internal user must only see his customers, force searching by him
111  $search_sale = 0;
112  if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
113 
114  $sql = "SELECT t.rowid";
115  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)
116  $sql.= " FROM ".MAIN_DB_PREFIX."expedition as t";
117 
118  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
119 
120  $sql.= ' WHERE t.entity IN ('.getEntity('expedition').')';
121  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
122  if ($socids) $sql.= " AND t.fk_soc IN (".$socids.")";
123  if ($search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
124  // Insert sale filter
125  if ($search_sale > 0)
126  {
127  $sql .= " AND sc.fk_user = ".$search_sale;
128  }
129  // Add sql filters
130  if ($sqlfilters)
131  {
132  if (! DolibarrApi::_checkFilters($sqlfilters))
133  {
134  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
135  }
136  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
137  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
138  }
139 
140  $sql.= $db->order($sortfield, $sortorder);
141  if ($limit) {
142  if ($page < 0)
143  {
144  $page = 0;
145  }
146  $offset = $limit * $page;
147 
148  $sql.= $db->plimit($limit + 1, $offset);
149  }
150 
151  dol_syslog("API Rest request");
152  $result = $db->query($sql);
153 
154  if ($result)
155  {
156  $num = $db->num_rows($result);
157  $min = min($num, ($limit <= 0 ? $num : $limit));
158  $i=0;
159  while ($i < $min)
160  {
161  $obj = $db->fetch_object($result);
162  $shipment_static = new Expedition($db);
163  if($shipment_static->fetch($obj->rowid)) {
164  $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
165  }
166  $i++;
167  }
168  }
169  else {
170  throw new RestException(503, 'Error when retrieve commande list : '.$db->lasterror());
171  }
172  if( ! count($obj_ret)) {
173  throw new RestException(404, 'No shipment found');
174  }
175  return $obj_ret;
176  }
177 
184  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 
219  /*
220  function getLines($id) {
221  if(! DolibarrApiAccess::$user->rights->expedition->lire) {
222  throw new RestException(401);
223  }
224 
225  $result = $this->shipment->fetch($id);
226  if( ! $result ) {
227  throw new RestException(404, 'Shipment not found');
228  }
229 
230  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
231  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
232  }
233  $this->shipment->getLinesArray();
234  $result = array();
235  foreach ($this->shipment->lines as $line) {
236  array_push($result,$this->_cleanObjectDatas($line));
237  }
238  return $result;
239  }
240  */
241 
252  /*
253  function postLine($id, $request_data = NULL) {
254  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
255  throw new RestException(401);
256  }
257 
258  $result = $this->shipment->fetch($id);
259  if( ! $result ) {
260  throw new RestException(404, 'Shipment not found');
261  }
262 
263  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
264  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
265  }
266  $request_data = (object) $request_data;
267  $updateRes = $this->shipment->addline(
268  $request_data->desc,
269  $request_data->subprice,
270  $request_data->qty,
271  $request_data->tva_tx,
272  $request_data->localtax1_tx,
273  $request_data->localtax2_tx,
274  $request_data->fk_product,
275  $request_data->remise_percent,
276  $request_data->info_bits,
277  $request_data->fk_remise_except,
278  'HT',
279  0,
280  $request_data->date_start,
281  $request_data->date_end,
282  $request_data->product_type,
283  $request_data->rang,
284  $request_data->special_code,
285  $fk_parent_line,
286  $request_data->fk_fournprice,
287  $request_data->pa_ht,
288  $request_data->label,
289  $request_data->array_options,
290  $request_data->fk_unit,
291  $request_data->origin,
292  $request_data->origin_id,
293  $request_data->multicurrency_subprice
294  );
295 
296  if ($updateRes > 0) {
297  return $updateRes;
298 
299  }
300  return false;
301  }*/
302 
314  /*
315  function putLine($id, $lineid, $request_data = NULL) {
316  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
317  throw new RestException(401);
318  }
319 
320  $result = $this->shipment->fetch($id);
321  if( ! $result ) {
322  throw new RestException(404, 'Shipment not found');
323  }
324 
325  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
326  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
327  }
328  $request_data = (object) $request_data;
329  $updateRes = $this->shipment->updateline(
330  $lineid,
331  $request_data->desc,
332  $request_data->subprice,
333  $request_data->qty,
334  $request_data->remise_percent,
335  $request_data->tva_tx,
336  $request_data->localtax1_tx,
337  $request_data->localtax2_tx,
338  'HT',
339  $request_data->info_bits,
340  $request_data->date_start,
341  $request_data->date_end,
342  $request_data->product_type,
343  $request_data->fk_parent_line,
344  0,
345  $request_data->fk_fournprice,
346  $request_data->pa_ht,
347  $request_data->label,
348  $request_data->special_code,
349  $request_data->array_options,
350  $request_data->fk_unit,
351  $request_data->multicurrency_subprice
352  );
353 
354  if ($updateRes > 0) {
355  $result = $this->get($id);
356  unset($result->line);
357  return $this->_cleanObjectDatas($result);
358  }
359  return false;
360  }*/
361 
375  function deleteLine($id, $lineid) {
376  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
377  throw new RestException(401);
378  }
379 
380  $result = $this->shipment->fetch($id);
381  if( ! $result ) {
382  throw new RestException(404, 'Shipment not found');
383  }
384 
385  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
386  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
387  }
388 
389  // TODO Check the lineid $lineid is a line of ojbect
390 
391  $request_data = (object) $request_data;
392  $updateRes = $this->shipment->deleteline(DolibarrApiAccess::$user, $lineid);
393  if ($updateRes > 0) {
394  return $this->get($id);
395  }
396  else
397  {
398  throw new RestException(405, $this->shipment->error);
399  }
400  }
401 
410  function put($id, $request_data = NULL) {
411  if (! DolibarrApiAccess::$user->rights->expedition->creer) {
412  throw new RestException(401);
413  }
414 
415  $result = $this->shipment->fetch($id);
416  if (! $result) {
417  throw new RestException(404, 'Shipment not found');
418  }
419 
420  if (! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
421  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
422  }
423  foreach($request_data as $field => $value) {
424  if ($field == 'id') continue;
425  $this->shipment->$field = $value;
426  }
427 
428  if ($this->shipment->update(DolibarrApiAccess::$user) > 0)
429  {
430  return $this->get($id);
431  }
432  else
433  {
434  throw new RestException(500, $this->shipment->error);
435  }
436  }
437 
445  function delete($id)
446  {
447  if(! DolibarrApiAccess::$user->rights->shipment->supprimer) {
448  throw new RestException(401);
449  }
450  $result = $this->shipment->fetch($id);
451  if( ! $result ) {
452  throw new RestException(404, 'Shipment not found');
453  }
454 
455  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
456  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
457  }
458 
459  if( ! $this->shipment->delete(DolibarrApiAccess::$user)) {
460  throw new RestException(500, 'Error when deleting shipment : '.$this->shipment->error);
461  }
462 
463  return array(
464  'success' => array(
465  'code' => 200,
466  'message' => 'Shipment deleted'
467  )
468  );
469 
470  }
471 
491  function validate($id, $notrigger=0)
492  {
493  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
494  throw new RestException(401);
495  }
496  $result = $this->shipment->fetch($id);
497  if( ! $result ) {
498  throw new RestException(404, 'Shipment not found');
499  }
500 
501  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
502  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
503  }
504 
505  $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
506  if ($result == 0) {
507  throw new RestException(304, 'Error nothing done. May be object is already validated');
508  }
509  if ($result < 0) {
510  throw new RestException(500, 'Error when validating Shipment: '.$this->shipment->error);
511  }
512  $result = $this->shipment->fetch($id);
513  if( ! $result ) {
514  throw new RestException(404, 'Shipment not found');
515  }
516 
517  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
518  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
519  }
520 
521  $this->shipment->fetchObjectLinked();
522  return $this->_cleanObjectDatas($this->shipment);
523  }
524 
525 
540 /* function setinvoiced($id) {
541 
542  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
543  throw new RestException(401);
544  }
545  if(empty($id)) {
546  throw new RestException(400, 'Shipment ID is mandatory');
547  }
548  $result = $this->shipment->fetch($id);
549  if( ! $result ) {
550  throw new RestException(404, 'Shipment not found');
551  }
552 
553  $result = $this->shipment->classifyBilled(DolibarrApiAccess::$user);
554  if( $result < 0) {
555  throw new RestException(400, $this->shipment->error);
556  }
557  return $result;
558  }
559 */
560 
561 
575  /*
576  function createShipmentFromOrder($orderid) {
577 
578  require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
579 
580  if(! DolibarrApiAccess::$user->rights->expedition->lire) {
581  throw new RestException(401);
582  }
583  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
584  throw new RestException(401);
585  }
586  if(empty($proposalid)) {
587  throw new RestException(400, 'Order ID is mandatory');
588  }
589 
590  $order = new Commande($this->db);
591  $result = $order->fetch($proposalid);
592  if( ! $result ) {
593  throw new RestException(404, 'Order not found');
594  }
595 
596  $result = $this->shipment->createFromOrder($order, DolibarrApiAccess::$user);
597  if( $result < 0) {
598  throw new RestException(405, $this->shipment->error);
599  }
600  $this->shipment->fetchObjectLinked();
601  return $this->_cleanObjectDatas($this->shipment);
602  }
603  */
604 
611  function _cleanObjectDatas($object) {
612 
613  $object = parent::_cleanObjectDatas($object);
614 
615  unset($object->thirdparty); // id already returned
616 
617  unset($object->note);
618  unset($object->address);
619  unset($object->barcode_type);
620  unset($object->barcode_type_code);
621  unset($object->barcode_type_label);
622  unset($object->barcode_type_coder);
623 
624  if (! empty($object->lines) && is_array($object->lines))
625  {
626  foreach ($object->lines as $line)
627  {
628  unset($line->tva_tx);
629  unset($line->vat_src_code);
630  unset($line->total_ht);
631  unset($line->total_ttc);
632  unset($line->total_tva);
633  unset($line->total_localtax1);
634  unset($line->total_localtax2);
635  unset($line->remise_percent);
636  }
637  }
638 
639  return $object;
640  }
641 
649  function _validate($data)
650  {
651  $shipment = array();
652  foreach (Shipments::$FIELDS as $field) {
653  if (!isset($data[$field]))
654  throw new RestException(400, "$field field missing");
655  $shipment[$field] = $data[$field];
656 
657  }
658  return $shipment;
659  }
660 }
_cleanObjectDatas($object)
Classify the shipment as invoiced.
deleteLine($id, $lineid)
Get lines of an shipment.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:246
__construct()
Constructor.
Class for API REST v1.
Definition: api.class.php:29
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
put($id, $request_data=NULL)
Update shipment general fields (won't touch lines of shipment)
Class to manage shipments.
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
_validate($data)
Validate fields before create or update object.
post($request_data=NULL)
Create shipment object.
validate($id, $notrigger=0)
Validate a shipment.