dolibarr 20.0.5
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->hasRight('expedition', 'lire')) {
68 throw new RestException(403);
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(403, '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 if (!DolibarrApiAccess::$user->hasRight('expedition', 'lire')) {
105 throw new RestException(403);
106 }
107
108 $obj_ret = array();
109
110 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
111 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
112
113 // If the internal user must only see his customers, force searching by him
114 $search_sale = 0;
115 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
116 $search_sale = DolibarrApiAccess::$user->id;
117 }
118
119 $sql = "SELECT t.rowid";
120 $sql .= " FROM ".MAIN_DB_PREFIX."expedition AS t";
121 $sql .= " 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
122 $sql .= ' WHERE t.entity IN ('.getEntity('expedition').')';
123 if ($socids) {
124 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
125 }
126 // Search on sale representative
127 if ($search_sale && $search_sale != '-1') {
128 if ($search_sale == -2) {
129 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
130 } elseif ($search_sale > 0) {
131 $sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
132 }
133 }
134 // Add sql filters
135 if ($sqlfilters) {
136 $errormessage = '';
137 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
138 if ($errormessage) {
139 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
140 }
141 }
142
143 $sql .= $this->db->order($sortfield, $sortorder);
144 if ($limit) {
145 if ($page < 0) {
146 $page = 0;
147 }
148 $offset = $limit * $page;
149
150 $sql .= $this->db->plimit($limit + 1, $offset);
151 }
152
153 dol_syslog("API Rest request");
154 $result = $this->db->query($sql);
155
156 if ($result) {
157 $num = $this->db->num_rows($result);
158 $min = min($num, ($limit <= 0 ? $num : $limit));
159 $i = 0;
160 while ($i < $min) {
161 $obj = $this->db->fetch_object($result);
162 $shipment_static = new Expedition($this->db);
163 if ($shipment_static->fetch($obj->rowid)) {
164 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($shipment_static), $properties);
165 }
166 $i++;
167 }
168 } else {
169 throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
170 }
171
172 return $obj_ret;
173 }
174
181 public function post($request_data = null)
182 {
183 if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
184 throw new RestException(403, "Insuffisant rights");
185 }
186 // Check mandatory fields
187 $result = $this->_validate($request_data);
188
189 foreach ($request_data as $field => $value) {
190 if ($field === 'caller') {
191 // 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 with the caller
192 $this->shipment->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
193 continue;
194 }
195
196 $this->shipment->$field = $this->_checkValForAPI($field, $value, $this->shipment);
197 }
198 if (isset($request_data["lines"])) {
199 $lines = array();
200 foreach ($request_data["lines"] as $line) {
201 $shipmentline = new ExpeditionLigne($this->db);
202
203 $shipmentline->entrepot_id = $line['entrepot_id'];
204 $shipmentline->fk_element = $line['fk_element'] ?? $line['origin_id']; // example: order id. this->origin is 'commande'
205 $shipmentline->origin_line_id = $line['fk_elementdet'] ?? $line['origin_line_id']; // example: order id
206 $shipmentline->fk_elementdet = $line['fk_elementdet'] ?? $line['origin_line_id']; // example: order line id
207 $shipmentline->origin_type = $line['element_type'] ?? $line['origin_type']; // example 'commande' or 'order'
208 $shipmentline->element_type = $line['element_type'] ?? $line['origin_type']; // example 'commande' or 'order'
209 $shipmentline->qty = $line['qty'];
210 $shipmentline->rang = $line['rang'];
211 $shipmentline->array_options = $line['array_options'];
212 $shipmentline->detail_batch = $line['detail_batch'];
213
214 $lines[] = $shipmentline;
215 }
216 $this->shipment->lines = $lines;
217 }
218
219 if ($this->shipment->create(DolibarrApiAccess::$user) < 0) {
220 throw new RestException(500, "Error creating shipment", array_merge(array($this->shipment->error), $this->shipment->errors));
221 }
222
223 return $this->shipment->id;
224 }
225
226 // /**
227 // * Get lines of an shipment
228 // *
229 // * @param int $id Id of shipment
230 // *
231 // * @url GET {id}/lines
232 // *
233 // * @return int
234 // */
235 /*
236 public function getLines($id)
237 {
238 if(! DolibarrApiAccess::$user->hasRight('expedition', 'lire')) {
239 throw new RestException(403);
240 }
241
242 $result = $this->shipment->fetch($id);
243 if( ! $result ) {
244 throw new RestException(404, 'Shipment not found');
245 }
246
247 if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
248 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
249 }
250 $this->shipment->getLinesArray();
251 $result = array();
252 foreach ($this->shipment->lines as $line) {
253 array_push($result,$this->_cleanObjectDatas($line));
254 }
255 return $result;
256 }
257 */
258
259 // /**
260 // * Add a line to given shipment
261 // *
262 // * @param int $id Id of shipment to update
263 // * @param array $request_data ShipmentLine data
264 // *
265 // * @url POST {id}/lines
266 // *
267 // * @return int
268 // */
269 /*
270 public function postLine($id, $request_data = null)
271 {
272 if(! DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
273 throw new RestException(403);
274 }
275
276 $result = $this->shipment->fetch($id);
277 if ( ! $result ) {
278 throw new RestException(404, 'Shipment not found');
279 }
280
281 if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
282 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
283 }
284
285 $request_data = (object) $request_data;
286
287 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
288 $request_data->label = sanitizeVal($request_data->label);
289
290 $updateRes = $this->shipment->addline(
291 $request_data->desc,
292 $request_data->subprice,
293 $request_data->qty,
294 $request_data->tva_tx,
295 $request_data->localtax1_tx,
296 $request_data->localtax2_tx,
297 $request_data->fk_product,
298 $request_data->remise_percent,
299 $request_data->info_bits,
300 $request_data->fk_remise_except,
301 'HT',
302 0,
303 $request_data->date_start,
304 $request_data->date_end,
305 $request_data->product_type,
306 $request_data->rang,
307 $request_data->special_code,
308 $fk_parent_line,
309 $request_data->fk_fournprice,
310 $request_data->pa_ht,
311 $request_data->label,
312 $request_data->array_options,
313 $request_data->fk_unit,
314 $request_data->origin,
315 $request_data->origin_id,
316 $request_data->multicurrency_subprice
317 );
318
319 if ($updateRes > 0) {
320 return $updateRes;
321
322 }
323 return false;
324 }*/
325
326 // /**
327 // * Update a line to given shipment
328 // *
329 // * @param int $id Id of shipment to update
330 // * @param int $lineid Id of line to update
331 // * @param array $request_data ShipmentLine data
332 // *
333 // * @url PUT {id}/lines/{lineid}
334 // *
335 // * @return object
336 // */
337 /*
338 public function putLine($id, $lineid, $request_data = null)
339 {
340 if (! DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
341 throw new RestException(403);
342 }
343
344 $result = $this->shipment->fetch($id);
345 if ( ! $result ) {
346 throw new RestException(404, 'Shipment not found');
347 }
348
349 if( ! DolibarrApi::_checkAccessToResource('expedition',$this->shipment->id)) {
350 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
351 }
352
353 $request_data = (object) $request_data;
354
355 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
356 $request_data->label = sanitizeVal($request_data->label);
357
358 $updateRes = $this->shipment->updateline(
359 $lineid,
360 $request_data->desc,
361 $request_data->subprice,
362 $request_data->qty,
363 $request_data->remise_percent,
364 $request_data->tva_tx,
365 $request_data->localtax1_tx,
366 $request_data->localtax2_tx,
367 'HT',
368 $request_data->info_bits,
369 $request_data->date_start,
370 $request_data->date_end,
371 $request_data->product_type,
372 $request_data->fk_parent_line,
373 0,
374 $request_data->fk_fournprice,
375 $request_data->pa_ht,
376 $request_data->label,
377 $request_data->special_code,
378 $request_data->array_options,
379 $request_data->fk_unit,
380 $request_data->multicurrency_subprice
381 );
382
383 if ($updateRes > 0) {
384 $result = $this->get($id);
385 unset($result->line);
386 return $this->_cleanObjectDatas($result);
387 }
388 return false;
389 }*/
390
405 public function deleteLine($id, $lineid)
406 {
407 if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
408 throw new RestException(403);
409 }
410
411 $result = $this->shipment->fetch($id);
412 if (!$result) {
413 throw new RestException(404, 'Shipment not found');
414 }
415
416 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
417 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
418 }
419
420 // TODO Check the lineid $lineid is a line of object
421
422 $updateRes = $this->shipment->deleteLine(DolibarrApiAccess::$user, $lineid);
423 if ($updateRes > 0) {
424 return array(
425 'success' => array(
426 'code' => 200,
427 'message' => 'line ' .$lineid. ' deleted'
428 )
429 );
430 } else {
431 throw new RestException(405, $this->shipment->error);
432 }
433 }
434
442 public function put($id, $request_data = null)
443 {
444 if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
445 throw new RestException(403);
446 }
447
448 $result = $this->shipment->fetch($id);
449 if (!$result) {
450 throw new RestException(404, 'Shipment not found');
451 }
452
453 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
454 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
455 }
456 foreach ($request_data as $field => $value) {
457 if ($field == 'id') {
458 continue;
459 }
460 if ($field === 'caller') {
461 // 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 with the caller
462 $this->shipment->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
463 continue;
464 }
465
466 if ($field == 'array_options' && is_array($value)) {
467 foreach ($value as $index => $val) {
468 $this->shipment->array_options[$index] = $this->_checkValForAPI($field, $val, $this->shipment);
469 }
470 continue;
471 }
472 $this->shipment->$field = $this->_checkValForAPI($field, $value, $this->shipment);
473 }
474
475 if ($this->shipment->update(DolibarrApiAccess::$user) > 0) {
476 return $this->get($id);
477 } else {
478 throw new RestException(500, $this->shipment->error);
479 }
480 }
481
489 public function delete($id)
490 {
491 if (!DolibarrApiAccess::$user->hasRight('expedition', 'supprimer')) {
492 throw new RestException(403);
493 }
494 $result = $this->shipment->fetch($id);
495 if (!$result) {
496 throw new RestException(404, 'Shipment not found');
497 }
498
499 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
500 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
501 }
502
503 if (!$this->shipment->delete(DolibarrApiAccess::$user)) {
504 throw new RestException(500, 'Error when deleting shipment : '.$this->shipment->error);
505 }
506
507 return array(
508 'success' => array(
509 'code' => 200,
510 'message' => 'Shipment deleted'
511 )
512 );
513 }
514
534 public function validate($id, $notrigger = 0)
535 {
536 if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
537 throw new RestException(403);
538 }
539 $result = $this->shipment->fetch($id);
540 if (!$result) {
541 throw new RestException(404, 'Shipment not found');
542 }
543
544 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
545 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
546 }
547
548 $result = $this->shipment->valid(DolibarrApiAccess::$user, $notrigger);
549 if ($result == 0) {
550 throw new RestException(304, 'Error nothing done. May be object is already validated');
551 }
552 if ($result < 0) {
553 throw new RestException(500, 'Error when validating Shipment: '.$this->shipment->error);
554 }
555
556 // Reload shipment
557 $result = $this->shipment->fetch($id);
558
559 $this->shipment->fetchObjectLinked();
560 return $this->_cleanObjectDatas($this->shipment);
561 }
562
563
564 // /**
565 // * Classify the shipment as invoiced
566 // *
567 // * @param int $id Id of the shipment
568 // *
569 // * @url POST {id}/setinvoiced
570 // *
571 // * @return int
572 // *
573 // * @throws RestException 400
574 // * @throws RestException 401
575 // * @throws RestException 404
576 // * @throws RestException 405
577 // */
578 /*
579 public function setinvoiced($id)
580 {
581
582 if(! DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
583 throw new RestException(403);
584 }
585 if(empty($id)) {
586 throw new RestException(400, 'Shipment ID is mandatory');
587 }
588 $result = $this->shipment->fetch($id);
589 if( ! $result ) {
590 throw new RestException(404, 'Shipment not found');
591 }
592
593 $result = $this->shipment->classifyBilled(DolibarrApiAccess::$user);
594 if( $result < 0) {
595 throw new RestException(400, $this->shipment->error);
596 }
597 return $result;
598 }
599 */
600
601
602 // /**
603 // * Create a shipment using an existing order.
604 // *
605 // * @param int $orderid Id of the order
606 // *
607 // * @url POST /createfromorder/{orderid}
608 // *
609 // * @return int
610 // * @throws RestException 400
611 // * @throws RestException 401
612 // * @throws RestException 404
613 // * @throws RestException 405
614 // */
615 /*
616 public function createShipmentFromOrder($orderid)
617 {
618
619 require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
620
621 if(! DolibarrApiAccess::$user->hasRight('expedition', 'lire')) {
622 throw new RestException(403);
623 }
624 if(! DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
625 throw new RestException(403);
626 }
627 if(empty($proposalid)) {
628 throw new RestException(400, 'Order ID is mandatory');
629 }
630
631 $order = new Commande($this->db);
632 $result = $order->fetch($proposalid);
633 if( ! $result ) {
634 throw new RestException(404, 'Order not found');
635 }
636
637 $result = $this->shipment->createFromOrder($order, DolibarrApiAccess::$user);
638 if( $result < 0) {
639 throw new RestException(405, $this->shipment->error);
640 }
641 $this->shipment->fetchObjectLinked();
642 return $this->_cleanObjectDatas($this->shipment);
643 }
644 */
645
656 public function close($id, $notrigger = 0)
657 {
658 if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
659 throw new RestException(403);
660 }
661
662 $result = $this->shipment->fetch($id);
663 if (!$result) {
664 throw new RestException(404, 'Shipment not found');
665 }
666
667 if (!DolibarrApi::_checkAccessToResource('expedition', $this->shipment->id)) {
668 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
669 }
670
671 $result = $this->shipment->setClosed();
672 if ($result == 0) {
673 throw new RestException(304, 'Error nothing done. May be object is already closed');
674 }
675 if ($result < 0) {
676 throw new RestException(500, 'Error when closing Order: '.$this->shipment->error);
677 }
678
679 // Reload shipment
680 $result = $this->shipment->fetch($id);
681
682 $this->shipment->fetchObjectLinked();
683
684 return $this->_cleanObjectDatas($this->shipment);
685 }
686
687 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
694 protected function _cleanObjectDatas($object)
695 {
696 // phpcs:enable
697 $object = parent::_cleanObjectDatas($object);
698
699 unset($object->canvas);
700
701 unset($object->thirdparty); // id already returned
702
703 unset($object->note);
704 unset($object->address);
705 unset($object->barcode_type);
706 unset($object->barcode_type_code);
707 unset($object->barcode_type_label);
708 unset($object->barcode_type_coder);
709
710 if (!empty($object->lines) && is_array($object->lines)) {
711 foreach ($object->lines as $line) {
712 if (is_array($line->detail_batch)) {
713 foreach ($line->detail_batch as $keytmp2 => $valtmp2) {
714 unset($line->detail_batch[$keytmp2]->db);
715 }
716 }
717 unset($line->canvas);
718
719 unset($line->tva_tx);
720 unset($line->vat_src_code);
721 unset($line->total_ht);
722 unset($line->total_ttc);
723 unset($line->total_tva);
724 unset($line->total_localtax1);
725 unset($line->total_localtax2);
726 unset($line->remise_percent);
727 }
728 }
729
730 return $object;
731 }
732
740 private function _validate($data)
741 {
742 $shipment = array();
743 foreach (Shipments::$FIELDS as $field) {
744 if (!isset($data[$field])) {
745 throw new RestException(400, "$field field missing");
746 }
747 $shipment[$field] = $data[$field];
748 }
749 return $shipment;
750 }
751}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class for API REST v1.
Definition api.class.php:30
_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:82
Class to manage shipments.
Class to manage lines of shipment.
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
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.