dolibarr 19.0.3
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 $this->shipment->$field = $value;
461 }
462
463 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
464 return $this->get($id);
465 } else {
466 throw new RestException(500, $this->shipment->error);
467 }
468 }
469
477 public function delete($id)
478 {
479 if (!DolibarrApiAccess::$user->rights->expedition->supprimer) {
480 throw new RestException(401);
481 }
482 $result = $this->shipment->fetch($id);
483 if (!$result) {
484 throw new RestException(404, 'Shipment not found');
485 }
486
487 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
488 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
489 }
490
491 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
492 throw new RestException(500, 'Error when deleting shipment : '.$this->shipment->error);
493 }
494
495 return array(
496 'success' => array(
497 'code' => 200,
498 'message' => 'Shipment deleted'
499 )
500 );
501 }
502
522 public function validate($id, $notrigger = 0)
523 {
524 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
525 throw new RestException(401);
526 }
527 $result = $this->shipment->fetch($id);
528 if (!$result) {
529 throw new RestException(404, 'Shipment not found');
530 }
531
532 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
533 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
534 }
535
536 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
537 if ($result == 0) {
538 throw new RestException(304, 'Error nothing done. May be object is already validated');
539 }
540 if ($result < 0) {
541 throw new RestException(500, 'Error when validating Shipment: '.$this->shipment->error);
542 }
543
544 // Reload shipment
545 $result = $this->shipment->fetch($id);
546
547 $this->shipment->fetchObjectLinked();
548 return $this->_cleanObjectDatas($this->shipment);
549 }
550
551
552 // /**
553 // * Classify the shipment as invoiced
554 // *
555 // * @param int $id Id of the shipment
556 // *
557 // * @url POST {id}/setinvoiced
558 // *
559 // * @return int
560 // *
561 // * @throws RestException 400
562 // * @throws RestException 401
563 // * @throws RestException 404
564 // * @throws RestException 405
565 // */
566 /*
567 public function setinvoiced($id)
568 {
569
570 if(! DolibarrApiAccess::$user->rights->expedition->creer) {
571 throw new RestException(401);
572 }
573 if(empty($id)) {
574 throw new RestException(400, 'Shipment ID is mandatory');
575 }
576 $result = $this->shipment->fetch($id);
577 if( ! $result ) {
578 throw new RestException(404, 'Shipment not found');
579 }
580
581 $result = $this->shipment->classifyBilled(DolibarrApiAccess::$user);
582 if( $result < 0) {
583 throw new RestException(400, $this->shipment->error);
584 }
585 return $result;
586 }
587 */
588
589
590 // /**
591 // * Create a shipment using an existing order.
592 // *
593 // * @param int $orderid Id of the order
594 // *
595 // * @url POST /createfromorder/{orderid}
596 // *
597 // * @return int
598 // * @throws RestException 400
599 // * @throws RestException 401
600 // * @throws RestException 404
601 // * @throws RestException 405
602 // */
603 /*
604 public function createShipmentFromOrder($orderid)
605 {
606
607 require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
608
609 if(! DolibarrApiAccess::$user->rights->expedition->lire) {
610 throw new RestException(401);
611 }
612 if(! DolibarrApiAccess::$user->rights->expedition->creer) {
613 throw new RestException(401);
614 }
615 if(empty($proposalid)) {
616 throw new RestException(400, 'Order ID is mandatory');
617 }
618
619 $order = new Commande($this->db);
620 $result = $order->fetch($proposalid);
621 if( ! $result ) {
622 throw new RestException(404, 'Order not found');
623 }
624
625 $result = $this->shipment->createFromOrder($order, DolibarrApiAccess::$user);
626 if( $result < 0) {
627 throw new RestException(405, $this->shipment->error);
628 }
629 $this->shipment->fetchObjectLinked();
630 return $this->_cleanObjectDatas($this->shipment);
631 }
632 */
633
644 public function close($id, $notrigger = 0)
645 {
646 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
647 throw new RestException(401);
648 }
649
650 $result = $this->shipment->fetch($id);
651 if (!$result) {
652 throw new RestException(404, 'Shipment not found');
653 }
654
655 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
656 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
657 }
658
659 $result = $this->shipment->setClosed();
660 if ($result == 0) {
661 throw new RestException(304, 'Error nothing done. May be object is already closed');
662 }
663 if ($result < 0) {
664 throw new RestException(500, 'Error when closing Order: '.$this->shipment->error);
665 }
666
667 // Reload shipment
668 $result = $this->shipment->fetch($id);
669
670 $this->shipment->fetchObjectLinked();
671
672 return $this->_cleanObjectDatas($this->shipment);
673 }
674
675 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
682 protected function _cleanObjectDatas($object)
683 {
684 // phpcs:enable
685 $object = parent::_cleanObjectDatas($object);
686
687 unset($object->thirdparty); // id already returned
688
689 unset($object->note);
690 unset($object->address);
691 unset($object->barcode_type);
692 unset($object->barcode_type_code);
693 unset($object->barcode_type_label);
694 unset($object->barcode_type_coder);
695
696 if (!empty($object->lines) && is_array($object->lines)) {
697 foreach ($object->lines as $line) {
698 if (is_array($line->detail_batch)) {
699 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
700 unset($line->detail_batch[$keytmp2]->db);
701 }
702 }
703 unset($line->tva_tx);
704 unset($line->vat_src_code);
705 unset($line->total_ht);
706 unset($line->total_ttc);
707 unset($line->total_tva);
708 unset($line->total_localtax1);
709 unset($line->total_localtax2);
710 unset($line->remise_percent);
711 }
712 }
713
714 return $object;
715 }
716
724 private function _validate($data)
725 {
726 $shipment = array();
727 foreach (Shipments::$FIELDS as $field) {
728 if (!isset($data[$field])) {
729 throw new RestException(400, "$field field missing");
730 }
731 $shipment[$field] = $data[$field];
732 }
733 return $shipment;
734 }
735}
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.
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.