dolibarr 19.0.4
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
30{
34 public static $FIELDS = array(
35 'socid',
36 'origin_id',
37 'origin_type',
38 );
39
43 public $shipment;
44
48 public function __construct()
49 {
50 global $db, $conf;
51 $this->db = $db;
52 $this->shipment = new Expedition($this->db);
53 }
54
65 public function get($id)
66 {
67 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
68 throw new RestException(401);
69 }
70
71 $result = $this->shipment->fetch($id);
72 if (!$result) {
73 throw new RestException(404, 'Shipment not found');
74 }
75
76 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
77 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
78 }
79
80 $this->shipment->fetchObjectLinked();
81 return $this->_cleanObjectDatas($this->shipment);
82 }
83
84
85
102 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '')
103 {
104 global $db, $conf;
105
106 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
107 throw new RestException(401);
108 }
109
110 $obj_ret = array();
111
112 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
113 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
114
115 // If the internal user must only see his customers, force searching by him
116 $search_sale = 0;
117 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
118 $search_sale = DolibarrApiAccess::$user->id;
119 }
120
121 $sql = "SELECT t.rowid";
122 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
123 $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)
124 }
125 $sql .= " FROM ".MAIN_DB_PREFIX."expedition AS t LEFT JOIN ".MAIN_DB_PREFIX."expedition_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
126
127 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
128 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
129 }
130
131 $sql .= ' WHERE t.entity IN ('.getEntity('expedition').')';
132 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
133 $sql .= " AND t.fk_soc = sc.fk_soc";
134 }
135 if ($socids) {
136 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
137 }
138 if ($search_sale > 0) {
139 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
140 }
141 // Insert sale filter
142 if ($search_sale > 0) {
143 $sql .= " AND sc.fk_user = ".((int) $search_sale);
144 }
145 // Add sql filters
146 if ($sqlfilters) {
147 $errormessage = '';
148 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
149 if ($errormessage) {
150 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
151 }
152 }
153
154 $sql .= $this->db->order($sortfield, $sortorder);
155 if ($limit) {
156 if ($page < 0) {
157 $page = 0;
158 }
159 $offset = $limit * $page;
160
161 $sql .= $this->db->plimit($limit + 1, $offset);
162 }
163
164 dol_syslog("API Rest request");
165 $result = $this->db->query($sql);
166
167 if ($result) {
168 $num = $this->db->num_rows($result);
169 $min = min($num, ($limit <= 0 ? $num : $limit));
170 $i = 0;
171 while ($i < $min) {
172 $obj = $this->db->fetch_object($result);
173 $shipment_static = new Expedition($this->db);
174 if ($shipment_static->fetch($obj->rowid)) {
175 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($shipment_static), $properties);
176 }
177 $i++;
178 }
179 } else {
180 throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
181 }
182
183 return $obj_ret;
184 }
185
192 public function post($request_data = null)
193 {
194 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
195 throw new RestException(401, "Insuffisant rights");
196 }
197 // Check mandatory fields
198 $result = $this->_validate($request_data);
199
200 foreach ($request_data as $field => $value) {
201 if ($field === 'caller') {
202 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
203 $this->shipment->context['caller'] = $request_data['caller'];
204 continue;
205 }
206
207 $this->shipment->$field = $value;
208 }
209 if (isset($request_data["lines"])) {
210 $lines = array();
211 foreach ($request_data["lines"] as $line) {
212 array_push($lines, (object) $line);
213 }
214 $this->shipment->lines = $lines;
215 }
216
217 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
218 throw new RestException(500, "Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
219 }
220
221 return $this->shipment->id;
222 }
223
224 // /**
225 // * Get lines of an shipment
226 // *
227 // * @param int $id Id of shipment
228 // *
229 // * @url GET {id}/lines
230 // *
231 // * @return int
232 // */
233 /*
234 public function getLines($id)
235 {
236 if(! DolibarrApiAccess::$user->rights->expedition->lire) {
237 throw new RestException(401);
238 }
239
240 $result = $this->shipment->fetch($id);
241 if( ! $result ) {
242 throw new RestException(404, 'Shipment not found');
243 }
244
245 if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
246 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
247 }
248 $this->shipment->getLinesArray();
249 $result = array();
250 foreach ($this->shipment->lines as $line) {
251 array_push($result,$this->_cleanObjectDatas($line));
252 }
253 return $result;
254 }
255 */
256
257 // /**
258 // * Add a line to given shipment
259 // *
260 // * @param int $id Id of shipment to update
261 // * @param array $request_data ShipmentLine data
262 // *
263 // * @url POST {id}/lines
264 // *
265 // * @return int
266 // */
267 /*
268 public function postLine($id, $request_data = null)
269 {
270 if(! DolibarrApiAccess::$user->rights->expedition->creer) {
271 throw new RestException(401);
272 }
273
274 $result = $this->shipment->fetch($id);
275 if ( ! $result ) {
276 throw new RestException(404, 'Shipment not found');
277 }
278
279 if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
280 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
281 }
282
283 $request_data = (object) $request_data;
284
285 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
286 $request_data->label = sanitizeVal($request_data->label);
287
288 $updateRes = $this->shipment->addline(
289 $request_data->desc,
290 $request_data->subprice,
291 $request_data->qty,
292 $request_data->tva_tx,
293 $request_data->localtax1_tx,
294 $request_data->localtax2_tx,
295 $request_data->fk_product,
296 $request_data->remise_percent,
297 $request_data->info_bits,
298 $request_data->fk_remise_except,
299 'HT',
300 0,
301 $request_data->date_start,
302 $request_data->date_end,
303 $request_data->product_type,
304 $request_data->rang,
305 $request_data->special_code,
306 $fk_parent_line,
307 $request_data->fk_fournprice,
308 $request_data->pa_ht,
309 $request_data->label,
310 $request_data->array_options,
311 $request_data->fk_unit,
312 $request_data->origin,
313 $request_data->origin_id,
314 $request_data->multicurrency_subprice
315 );
316
317 if ($updateRes > 0) {
318 return $updateRes;
319
320 }
321 return false;
322 }*/
323
324 // /**
325 // * Update a line to given shipment
326 // *
327 // * @param int $id Id of shipment to update
328 // * @param int $lineid Id of line to update
329 // * @param array $request_data ShipmentLine data
330 // *
331 // * @url PUT {id}/lines/{lineid}
332 // *
333 // * @return object
334 // */
335 /*
336 public function putLine($id, $lineid, $request_data = null)
337 {
338 if (! DolibarrApiAccess::$user->rights->expedition->creer) {
339 throw new RestException(401);
340 }
341
342 $result = $this->shipment->fetch($id);
343 if ( ! $result ) {
344 throw new RestException(404, 'Shipment not found');
345 }
346
347 if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
348 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
349 }
350
351 $request_data = (object) $request_data;
352
353 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
354 $request_data->label = sanitizeVal($request_data->label);
355
356 $updateRes = $this->shipment->updateline(
357 $lineid,
358 $request_data->desc,
359 $request_data->subprice,
360 $request_data->qty,
361 $request_data->remise_percent,
362 $request_data->tva_tx,
363 $request_data->localtax1_tx,
364 $request_data->localtax2_tx,
365 'HT',
366 $request_data->info_bits,
367 $request_data->date_start,
368 $request_data->date_end,
369 $request_data->product_type,
370 $request_data->fk_parent_line,
371 0,
372 $request_data->fk_fournprice,
373 $request_data->pa_ht,
374 $request_data->label,
375 $request_data->special_code,
376 $request_data->array_options,
377 $request_data->fk_unit,
378 $request_data->multicurrency_subprice
379 );
380
381 if ($updateRes > 0) {
382 $result = $this->get($id);
383 unset($result->line);
384 return $this->_cleanObjectDatas($result);
385 }
386 return false;
387 }*/
388
403 public function deleteLine($id, $lineid)
404 {
405 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
406 throw new RestException(401);
407 }
408
409 $result = $this->shipment->fetch($id);
410 if (!$result) {
411 throw new RestException(404, 'Shipment not found');
412 }
413
414 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
415 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
416 }
417
418 // TODO Check the lineid $lineid is a line of ojbect
419
420 $updateRes = $this->shipment->deleteline(DolibarrApiAccess::$user, $lineid);
421 if ($updateRes > 0) {
422 return $this->get($id);
423 } else {
424 throw new RestException(405, $this->shipment->error);
425 }
426 }
427
436 public function put($id, $request_data = null)
437 {
438 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
439 throw new RestException(401);
440 }
441
442 $result = $this->shipment->fetch($id);
443 if (!$result) {
444 throw new RestException(404, 'Shipment not found');
445 }
446
447 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
448 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
449 }
450 foreach ($request_data as $field => $value) {
451 if ($field == 'id') {
452 continue;
453 }
454 if ($field === 'caller') {
455 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
456 $this->shipment->context['caller'] = $request_data['caller'];
457 continue;
458 }
459
460 if ($field == 'array_options' && is_array($value)) {
461 foreach ($value as $index => $val) {
462 $this->shipment->array_options[$index] = $this->_checkValForAPI($field, $val, $this->shipment);
463 }
464 continue;
465 }
466 $this->shipment->$field = $this->_checkValForAPI($field, $value, $this->shipment);
467 }
468
469 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
470 return $this->get($id);
471 } else {
472 throw new RestException(500, $this->shipment->error);
473 }
474 }
475
483 public function delete($id)
484 {
485 if (!DolibarrApiAccess::$user->rights->expedition->supprimer) {
486 throw new RestException(401);
487 }
488 $result = $this->shipment->fetch($id);
489 if (!$result) {
490 throw new RestException(404, 'Shipment not found');
491 }
492
493 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
494 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 }
496
497 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
498 throw new RestException(500, 'Error when deleting shipment : '.$this->shipment->error);
499 }
500
501 return array(
502 'success' => array(
503 'code' => 200,
504 'message' => 'Shipment deleted'
505 )
506 );
507 }
508
528 public function validate($id, $notrigger = 0)
529 {
530 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
531 throw new RestException(401);
532 }
533 $result = $this->shipment->fetch($id);
534 if (!$result) {
535 throw new RestException(404, 'Shipment not found');
536 }
537
538 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
539 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
540 }
541
542 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
543 if ($result == 0) {
544 throw new RestException(304, 'Error nothing done. May be object is already validated');
545 }
546 if ($result < 0) {
547 throw new RestException(500, 'Error when validating Shipment: '.$this->shipment->error);
548 }
549
550 // Reload shipment
551 $result = $this->shipment->fetch($id);
552
553 $this->shipment->fetchObjectLinked();
554 return $this->_cleanObjectDatas($this->shipment);
555 }
556
557
558 // /**
559 // * Classify the shipment as invoiced
560 // *
561 // * @param int $id Id of the shipment
562 // *
563 // * @url POST {id}/setinvoiced
564 // *
565 // * @return int
566 // *
567 // * @throws RestException 400
568 // * @throws RestException 401
569 // * @throws RestException 404
570 // * @throws RestException 405
571 // */
572 /*
573 public function setinvoiced($id)
574 {
575
576 if(! DolibarrApiAccess::$user->rights->expedition->creer) {
577 throw new RestException(401);
578 }
579 if(empty($id)) {
580 throw new RestException(400, 'Shipment ID is mandatory');
581 }
582 $result = $this->shipment->fetch($id);
583 if( ! $result ) {
584 throw new RestException(404, 'Shipment not found');
585 }
586
587 $result = $this->shipment->classifyBilled(DolibarrApiAccess::$user);
588 if( $result < 0) {
589 throw new RestException(400, $this->shipment->error);
590 }
591 return $result;
592 }
593 */
594
595
596 // /**
597 // * Create a shipment using an existing order.
598 // *
599 // * @param int $orderid Id of the order
600 // *
601 // * @url POST /createfromorder/{orderid}
602 // *
603 // * @return int
604 // * @throws RestException 400
605 // * @throws RestException 401
606 // * @throws RestException 404
607 // * @throws RestException 405
608 // */
609 /*
610 public function createShipmentFromOrder($orderid)
611 {
612
613 require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
614
615 if(! DolibarrApiAccess::$user->rights->expedition->lire) {
616 throw new RestException(401);
617 }
618 if(! DolibarrApiAccess::$user->rights->expedition->creer) {
619 throw new RestException(401);
620 }
621 if(empty($proposalid)) {
622 throw new RestException(400, 'Order ID is mandatory');
623 }
624
625 $order = new Commande($this->db);
626 $result = $order->fetch($proposalid);
627 if( ! $result ) {
628 throw new RestException(404, 'Order not found');
629 }
630
631 $result = $this->shipment->createFromOrder($order, DolibarrApiAccess::$user);
632 if( $result < 0) {
633 throw new RestException(405, $this->shipment->error);
634 }
635 $this->shipment->fetchObjectLinked();
636 return $this->_cleanObjectDatas($this->shipment);
637 }
638 */
639
650 public function close($id, $notrigger = 0)
651 {
652 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
653 throw new RestException(401);
654 }
655
656 $result = $this->shipment->fetch($id);
657 if (!$result) {
658 throw new RestException(404, 'Shipment not found');
659 }
660
661 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
662 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
663 }
664
665 $result = $this->shipment->setClosed();
666 if ($result == 0) {
667 throw new RestException(304, 'Error nothing done. May be object is already closed');
668 }
669 if ($result < 0) {
670 throw new RestException(500, 'Error when closing Order: '.$this->shipment->error);
671 }
672
673 // Reload shipment
674 $result = $this->shipment->fetch($id);
675
676 $this->shipment->fetchObjectLinked();
677
678 return $this->_cleanObjectDatas($this->shipment);
679 }
680
681 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
688 protected function _cleanObjectDatas($object)
689 {
690 // phpcs:enable
691 $object = parent::_cleanObjectDatas($object);
692
693 unset($object->thirdparty); // id already returned
694
695 unset($object->note);
696 unset($object->address);
697 unset($object->barcode_type);
698 unset($object->barcode_type_code);
699 unset($object->barcode_type_label);
700 unset($object->barcode_type_coder);
701
702 if (!empty($object->lines) && is_array($object->lines)) {
703 foreach ($object->lines as $line) {
704 if (is_array($line->detail_batch)) {
705 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
706 unset($line->detail_batch[$keytmp2]->db);
707 }
708 }
709 unset($line->tva_tx);
710 unset($line->vat_src_code);
711 unset($line->total_ht);
712 unset($line->total_ttc);
713 unset($line->total_tva);
714 unset($line->total_localtax1);
715 unset($line->total_localtax2);
716 unset($line->remise_percent);
717 }
718 }
719
720 return $object;
721 }
722
730 private function _validate($data)
731 {
732 $shipment = array();
733 foreach (Shipments::$FIELDS as $field) {
734 if (!isset($data[$field])) {
735 throw new RestException(400, "$field field missing");
736 }
737 $shipment[$field] = $data[$field];
738 }
739 return $shipment;
740 }
741}
Class for API REST v1.
Definition api.class.php:31
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:85
Class to manage shipments.
close($id, $notrigger=0)
Classify the shipment as invoiced.
_validate($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update shipment general fields (won't touch lines of shipment)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List shipments.
validate($id, $notrigger=0)
Validate a shipment.
__construct()
Constructor.
post($request_data=null)
Create shipment object.
_cleanObjectDatas($object)
Clean sensible object datas.
deleteLine($id, $lineid)
Get lines of an shipment.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.