dolibarr  9.0.0
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 
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  {
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->societe_id ? DolibarrApiAccess::$user->societe_id : $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.= $db->order($sortfield, $sortorder);
142  if ($limit) {
143  if ($page < 0)
144  {
145  $page = 0;
146  }
147  $offset = $limit * $page;
148 
149  $sql.= $db->plimit($limit + 1, $offset);
150  }
151 
152  dol_syslog("API Rest request");
153  $result = $db->query($sql);
154 
155  if ($result)
156  {
157  $num = $db->num_rows($result);
158  $min = min($num, ($limit <= 0 ? $num : $limit));
159  $i=0;
160  while ($i < $min)
161  {
162  $obj = $db->fetch_object($result);
163  $shipment_static = new Expedition($db);
164  if($shipment_static->fetch($obj->rowid)) {
165  $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
166  }
167  $i++;
168  }
169  }
170  else {
171  throw new RestException(503, 'Error when retrieve commande list : '.$db->lasterror());
172  }
173  if( ! count($obj_ret)) {
174  throw new RestException(404, 'No shipment found');
175  }
176  return $obj_ret;
177  }
178 
185  function post($request_data = null)
186  {
187  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
188  throw new RestException(401, "Insuffisant rights");
189  }
190  // Check mandatory fields
191  $result = $this->_validate($request_data);
192 
193  foreach($request_data as $field => $value) {
194  $this->shipment->$field = $value;
195  }
196  if (isset($request_data["lines"])) {
197  $lines = array();
198  foreach ($request_data["lines"] as $line) {
199  array_push($lines, (object) $line);
200  }
201  $this->shipment->lines = $lines;
202  }
203 
204  if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
205  throw new RestException(500, "Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
206  }
207 
208  return $this->shipment->id;
209  }
210 
220  /*
221  function getLines($id)
222  {
223  if(! DolibarrApiAccess::$user->rights->expedition->lire) {
224  throw new RestException(401);
225  }
226 
227  $result = $this->shipment->fetch($id);
228  if( ! $result ) {
229  throw new RestException(404, 'Shipment not found');
230  }
231 
232  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
233  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
234  }
235  $this->shipment->getLinesArray();
236  $result = array();
237  foreach ($this->shipment->lines as $line) {
238  array_push($result,$this->_cleanObjectDatas($line));
239  }
240  return $result;
241  }
242  */
243 
254  /*
255  function postLine($id, $request_data = null)
256  {
257  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
258  throw new RestException(401);
259  }
260 
261  $result = $this->shipment->fetch($id);
262  if( ! $result ) {
263  throw new RestException(404, 'Shipment not found');
264  }
265 
266  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
267  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
268  }
269  $request_data = (object) $request_data;
270  $updateRes = $this->shipment->addline(
271  $request_data->desc,
272  $request_data->subprice,
273  $request_data->qty,
274  $request_data->tva_tx,
275  $request_data->localtax1_tx,
276  $request_data->localtax2_tx,
277  $request_data->fk_product,
278  $request_data->remise_percent,
279  $request_data->info_bits,
280  $request_data->fk_remise_except,
281  'HT',
282  0,
283  $request_data->date_start,
284  $request_data->date_end,
285  $request_data->product_type,
286  $request_data->rang,
287  $request_data->special_code,
288  $fk_parent_line,
289  $request_data->fk_fournprice,
290  $request_data->pa_ht,
291  $request_data->label,
292  $request_data->array_options,
293  $request_data->fk_unit,
294  $request_data->origin,
295  $request_data->origin_id,
296  $request_data->multicurrency_subprice
297  );
298 
299  if ($updateRes > 0) {
300  return $updateRes;
301 
302  }
303  return false;
304  }*/
305 
317  /*
318  function putLine($id, $lineid, $request_data = null)
319  {
320  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
321  throw new RestException(401);
322  }
323 
324  $result = $this->shipment->fetch($id);
325  if( ! $result ) {
326  throw new RestException(404, 'Shipment not found');
327  }
328 
329  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
330  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
331  }
332  $request_data = (object) $request_data;
333  $updateRes = $this->shipment->updateline(
334  $lineid,
335  $request_data->desc,
336  $request_data->subprice,
337  $request_data->qty,
338  $request_data->remise_percent,
339  $request_data->tva_tx,
340  $request_data->localtax1_tx,
341  $request_data->localtax2_tx,
342  'HT',
343  $request_data->info_bits,
344  $request_data->date_start,
345  $request_data->date_end,
346  $request_data->product_type,
347  $request_data->fk_parent_line,
348  0,
349  $request_data->fk_fournprice,
350  $request_data->pa_ht,
351  $request_data->label,
352  $request_data->special_code,
353  $request_data->array_options,
354  $request_data->fk_unit,
355  $request_data->multicurrency_subprice
356  );
357 
358  if ($updateRes > 0) {
359  $result = $this->get($id);
360  unset($result->line);
361  return $this->_cleanObjectDatas($result);
362  }
363  return false;
364  }*/
365 
379  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  }
401  else
402  {
403  throw new RestException(405, $this->shipment->error);
404  }
405  }
406 
415  function put($id, $request_data = null)
416  {
417  if (! DolibarrApiAccess::$user->rights->expedition->creer) {
418  throw new RestException(401);
419  }
420 
421  $result = $this->shipment->fetch($id);
422  if (! $result) {
423  throw new RestException(404, 'Shipment not found');
424  }
425 
426  if (! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
427  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
428  }
429  foreach($request_data as $field => $value) {
430  if ($field == 'id') continue;
431  $this->shipment->$field = $value;
432  }
433 
434  if ($this->shipment->update(DolibarrApiAccess::$user) > 0)
435  {
436  return $this->get($id);
437  }
438  else
439  {
440  throw new RestException(500, $this->shipment->error);
441  }
442  }
443 
451  function delete($id)
452  {
453  if(! DolibarrApiAccess::$user->rights->shipment->supprimer) {
454  throw new RestException(401);
455  }
456  $result = $this->shipment->fetch($id);
457  if( ! $result ) {
458  throw new RestException(404, 'Shipment not found');
459  }
460 
461  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
462  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
463  }
464 
465  if( ! $this->shipment->delete(DolibarrApiAccess::$user)) {
466  throw new RestException(500, 'Error when deleting shipment : '.$this->shipment->error);
467  }
468 
469  return array(
470  'success' => array(
471  'code' => 200,
472  'message' => 'Shipment deleted'
473  )
474  );
475  }
476 
496  function validate($id, $notrigger=0)
497  {
498  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
499  throw new RestException(401);
500  }
501  $result = $this->shipment->fetch($id);
502  if( ! $result ) {
503  throw new RestException(404, 'Shipment not found');
504  }
505 
506  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
507  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
508  }
509 
510  $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
511  if ($result == 0) {
512  throw new RestException(304, 'Error nothing done. May be object is already validated');
513  }
514  if ($result < 0) {
515  throw new RestException(500, 'Error when validating Shipment: '.$this->shipment->error);
516  }
517  $result = $this->shipment->fetch($id);
518  if( ! $result ) {
519  throw new RestException(404, 'Shipment not found');
520  }
521 
522  if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
523  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
524  }
525 
526  $this->shipment->fetchObjectLinked();
527  return $this->_cleanObjectDatas($this->shipment);
528  }
529 
530 
545 /* function setinvoiced($id)
546  {
547 
548  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
549  throw new RestException(401);
550  }
551  if(empty($id)) {
552  throw new RestException(400, 'Shipment ID is mandatory');
553  }
554  $result = $this->shipment->fetch($id);
555  if( ! $result ) {
556  throw new RestException(404, 'Shipment not found');
557  }
558 
559  $result = $this->shipment->classifyBilled(DolibarrApiAccess::$user);
560  if( $result < 0) {
561  throw new RestException(400, $this->shipment->error);
562  }
563  return $result;
564  }
565 */
566 
567 
581  /*
582  function createShipmentFromOrder($orderid)
583  {
584 
585  require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
586 
587  if(! DolibarrApiAccess::$user->rights->expedition->lire) {
588  throw new RestException(401);
589  }
590  if(! DolibarrApiAccess::$user->rights->expedition->creer) {
591  throw new RestException(401);
592  }
593  if(empty($proposalid)) {
594  throw new RestException(400, 'Order ID is mandatory');
595  }
596 
597  $order = new Commande($this->db);
598  $result = $order->fetch($proposalid);
599  if( ! $result ) {
600  throw new RestException(404, 'Order not found');
601  }
602 
603  $result = $this->shipment->createFromOrder($order, DolibarrApiAccess::$user);
604  if( $result < 0) {
605  throw new RestException(405, $this->shipment->error);
606  }
607  $this->shipment->fetchObjectLinked();
608  return $this->_cleanObjectDatas($this->shipment);
609  }
610  */
611 
618  function _cleanObjectDatas($object)
619  {
620 
621  $object = parent::_cleanObjectDatas($object);
622 
623  unset($object->thirdparty); // id already returned
624 
625  unset($object->note);
626  unset($object->address);
627  unset($object->barcode_type);
628  unset($object->barcode_type_code);
629  unset($object->barcode_type_label);
630  unset($object->barcode_type_coder);
631 
632  if (! empty($object->lines) && is_array($object->lines))
633  {
634  foreach ($object->lines as $line)
635  {
636  unset($line->tva_tx);
637  unset($line->vat_src_code);
638  unset($line->total_ht);
639  unset($line->total_ttc);
640  unset($line->total_tva);
641  unset($line->total_localtax1);
642  unset($line->total_localtax2);
643  unset($line->remise_percent);
644  }
645  }
646 
647  return $object;
648  }
649 
657  function _validate($data)
658  {
659  $shipment = array();
660  foreach (Shipments::$FIELDS as $field) {
661  if (!isset($data[$field]))
662  throw new RestException(400, "$field field missing");
663  $shipment[$field] = $data[$field];
664  }
665  return $shipment;
666  }
667 }
_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:251
__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.
Class to manage shipments.
put($id, $request_data=null)
Update shipment general fields (won&#39;t touch lines of shipment)
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:225
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create shipment object.
validate($id, $notrigger=0)
Validate a shipment.