dolibarr 18.0.6
api_orders.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
19use Luracast\Restler\RestException;
20
21require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
22
29class Orders extends DolibarrApi
30{
34 static $FIELDS = array(
35 'socid',
36 'date'
37 );
38
42 public $commande;
43
47 public function __construct()
48 {
49 global $db, $conf;
50 $this->db = $db;
51 $this->commande = new Commande($this->db);
52 }
53
65 public function get($id, $contact_list = 1)
66 {
67 return $this->_fetch($id, '', '', $contact_list);
68 }
69
83 public function getByRef($ref, $contact_list = 1)
84 {
85 return $this->_fetch('', $ref, '', $contact_list);
86 }
87
101 public function getByRefExt($ref_ext, $contact_list = 1)
102 {
103 return $this->_fetch('', '', $ref_ext, $contact_list);
104 }
105
119 private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
120 {
121 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
122 throw new RestException(401);
123 }
124
125 $result = $this->commande->fetch($id, $ref, $ref_ext);
126 if (!$result) {
127 throw new RestException(404, 'Order not found');
128 }
129
130 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
131 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
132 }
133
134 // Add external contacts ids
135 $tmparray = $this->commande->liste_contact(-1, 'external', $contact_list);
136 if (is_array($tmparray)) {
137 $this->commande->contacts_ids = $tmparray;
138 }
139 $this->commande->fetchObjectLinked();
140
141 // Add online_payment_url, cf #20477
142 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
143 $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
144
145 return $this->_cleanObjectDatas($this->commande);
146 }
147
165 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $sqlfilterlines = '')
166 {
167 global $db, $conf;
168
169 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
170 throw new RestException(401);
171 }
172
173 $obj_ret = array();
174
175 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
176 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
177
178 // If the internal user must only see his customers, force searching by him
179 $search_sale = 0;
180 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
181 $search_sale = DolibarrApiAccess::$user->id;
182 }
183
184 $sql = "SELECT t.rowid";
185 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
186 $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)
187 }
188 $sql .= " FROM ".MAIN_DB_PREFIX."commande AS t LEFT JOIN ".MAIN_DB_PREFIX."commande_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
189
190 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
191 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
192 }
193
194 $sql .= ' WHERE t.entity IN ('.getEntity('commande').')';
195 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
196 $sql .= " AND t.fk_soc = sc.fk_soc";
197 }
198 if ($socids) {
199 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
200 }
201 if ($search_sale > 0) {
202 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
203 }
204 // Insert sale filter
205 if ($search_sale > 0) {
206 $sql .= " AND sc.fk_user = ".((int) $search_sale);
207 }
208 // Add sql filters
209 if ($sqlfilters) {
210 $errormessage = '';
211 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
212 if ($errormessage) {
213 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
214 }
215 }
216 // Add sql filters for lines
217 if ($sqlfilterlines) {
218 $errormessage = '';
219 $sql .= " AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX."commandedet AS tl WHERE tl.fk_commande = t.rowid";
220 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilterlines, $errormessage);
221 $sql .= ")";
222 if ($errormessage) {
223 throw new RestException(400, 'Error when validating parameter sqlfilterlines -> '.$errormessage);
224 }
225 }
226 $sql .= $this->db->order($sortfield, $sortorder);
227 if ($limit) {
228 if ($page < 0) {
229 $page = 0;
230 }
231 $offset = $limit * $page;
232
233 $sql .= $this->db->plimit($limit + 1, $offset);
234 }
235
236 dol_syslog("API Rest request");
237 $result = $this->db->query($sql);
238
239 if ($result) {
240 $num = $this->db->num_rows($result);
241 $min = min($num, ($limit <= 0 ? $num : $limit));
242 $i = 0;
243 while ($i < $min) {
244 $obj = $this->db->fetch_object($result);
245 $commande_static = new Commande($this->db);
246 if ($commande_static->fetch($obj->rowid)) {
247 // Add external contacts ids
248 $tmparray = $commande_static->liste_contact(-1, 'external', 1);
249 if (is_array($tmparray)) {
250 $commande_static->contacts_ids = $tmparray;
251 }
252 // Add online_payment_url, cf #20477
253 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
254 $commande_static->online_payment_url = getOnlinePaymentUrl(0, 'order', $commande_static->ref);
255
256 $obj_ret[] = $this->_cleanObjectDatas($commande_static);
257 }
258 $i++;
259 }
260 } else {
261 throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
262 }
263 if (!count($obj_ret)) {
264 throw new RestException(404, 'No order found');
265 }
266 return $obj_ret;
267 }
268
277 public function post($request_data = null)
278 {
279 if (!DolibarrApiAccess::$user->rights->commande->creer) {
280 throw new RestException(401, "Insuffisant rights");
281 }
282 // Check mandatory fields
283 $result = $this->_validate($request_data);
284
285 foreach ($request_data as $field => $value) {
286 $this->commande->$field = $value;
287 }
288 /*if (isset($request_data["lines"])) {
289 $lines = array();
290 foreach ($request_data["lines"] as $line) {
291 array_push($lines, (object) $line);
292 }
293 $this->commande->lines = $lines;
294 }*/
295
296 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
297 throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
298 }
299
300 return ((int) $this->commande->id);
301 }
302
312 public function getLines($id)
313 {
314 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
315 throw new RestException(401);
316 }
317
318 $result = $this->commande->fetch($id);
319 if (!$result) {
320 throw new RestException(404, 'Order not found');
321 }
322
323 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
324 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
325 }
326 $this->commande->getLinesArray();
327 $result = array();
328 foreach ($this->commande->lines as $line) {
329 array_push($result, $this->_cleanObjectDatas($line));
330 }
331 return $result;
332 }
333
344 public function postLine($id, $request_data = null)
345 {
346 if (!DolibarrApiAccess::$user->rights->commande->creer) {
347 throw new RestException(401);
348 }
349
350 $result = $this->commande->fetch($id);
351 if (!$result) {
352 throw new RestException(404, 'Order not found');
353 }
354
355 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
356 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
357 }
358
359 $request_data = (object) $request_data;
360
361 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
362 $request_data->label = sanitizeVal($request_data->label);
363
364 $updateRes = $this->commande->addline(
365 $request_data->desc,
366 $request_data->subprice,
367 $request_data->qty,
368 $request_data->tva_tx,
369 $request_data->localtax1_tx,
370 $request_data->localtax2_tx,
371 $request_data->fk_product,
372 $request_data->remise_percent,
373 $request_data->info_bits,
374 $request_data->fk_remise_except,
375 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
376 $request_data->subprice,
377 $request_data->date_start,
378 $request_data->date_end,
379 $request_data->product_type,
380 $request_data->rang,
381 $request_data->special_code,
382 $request_data->fk_parent_line,
383 $request_data->fk_fournprice,
384 $request_data->pa_ht,
385 $request_data->label,
386 $request_data->array_options,
387 $request_data->fk_unit,
388 $request_data->origin,
389 $request_data->origin_id,
390 $request_data->multicurrency_subprice,
391 $request_data->ref_ext
392 );
393
394 if ($updateRes > 0) {
395 return $updateRes;
396 } else {
397 throw new RestException(400, $this->commande->error);
398 }
399 }
400
411 public function putLine($id, $lineid, $request_data = null)
412 {
413 if (!DolibarrApiAccess::$user->rights->commande->creer) {
414 throw new RestException(401);
415 }
416
417 $result = $this->commande->fetch($id);
418 if (!$result) {
419 throw new RestException(404, 'Order not found');
420 }
421
422 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
423 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
424 }
425
426 $request_data = (object) $request_data;
427
428 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
429 $request_data->label = sanitizeVal($request_data->label);
430
431 $updateRes = $this->commande->updateline(
432 $lineid,
433 $request_data->desc,
434 $request_data->subprice,
435 $request_data->qty,
436 $request_data->remise_percent,
437 $request_data->tva_tx,
438 $request_data->localtax1_tx,
439 $request_data->localtax2_tx,
440 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
441 $request_data->info_bits,
442 $request_data->date_start,
443 $request_data->date_end,
444 $request_data->product_type,
445 $request_data->fk_parent_line,
446 0,
447 $request_data->fk_fournprice,
448 $request_data->pa_ht,
449 $request_data->label,
450 $request_data->special_code,
451 $request_data->array_options,
452 $request_data->fk_unit,
453 $request_data->multicurrency_subprice,
454 0,
455 $request_data->ref_ext,
456 $request_data->rang
457 );
458
459 if ($updateRes > 0) {
460 $result = $this->get($id);
461 unset($result->line);
462 return $this->_cleanObjectDatas($result);
463 }
464 return false;
465 }
466
479 public function deleteLine($id, $lineid)
480 {
481 if (!DolibarrApiAccess::$user->rights->commande->creer) {
482 throw new RestException(401);
483 }
484
485 $result = $this->commande->fetch($id);
486 if (!$result) {
487 throw new RestException(404, 'Order not found');
488 }
489
490 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
491 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
492 }
493
494 $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid, $id);
495 if ($updateRes > 0) {
496 return $this->get($id);
497 } else {
498 throw new RestException(405, $this->commande->error);
499 }
500 }
501
515 public function getContacts($id, $type = '')
516 {
517 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
518 throw new RestException(401);
519 }
520
521 $result = $this->commande->fetch($id);
522 if (!$result) {
523 throw new RestException(404, 'Order not found');
524 }
525
526 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
527 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
528 }
529
530 $contacts = $this->commande->liste_contact(-1, 'external', 0, $type);
531
532 return $this->_cleanObjectDatas($contacts);
533 }
534
548 public function postContact($id, $contactid, $type)
549 {
550 if (!DolibarrApiAccess::$user->rights->commande->creer) {
551 throw new RestException(401);
552 }
553
554 $result = $this->commande->fetch($id);
555 if (!$result) {
556 throw new RestException(404, 'Order not found');
557 }
558
559 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
560 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
561 }
562
563 $result = $this->commande->add_contact($contactid, $type, 'external');
564
565 if ($result < 0) {
566 throw new RestException(500, 'Error when added the contact');
567 }
568
569 if ($result == 0) {
570 throw new RestException(304, 'contact already added');
571 }
572
573 return array(
574 'success' => array(
575 'code' => 200,
576 'message' => 'Contact linked to the order'
577 )
578 );
579 }
580
596 public function deleteContact($id, $contactid, $type)
597 {
598 if (!DolibarrApiAccess::$user->rights->commande->creer) {
599 throw new RestException(401);
600 }
601
602 $result = $this->commande->fetch($id);
603 if (!$result) {
604 throw new RestException(404, 'Order not found');
605 }
606
607 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
608 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
609 }
610
611 $contacts = $this->commande->liste_contact();
612
613 foreach ($contacts as $contact) {
614 if ($contact['id'] == $contactid && $contact['code'] == $type) {
615 $result = $this->commande->delete_contact($contact['rowid']);
616
617 if (!$result) {
618 throw new RestException(500, 'Error when deleted the contact');
619 }
620 }
621 }
622
623 return array(
624 'success' => array(
625 'code' => 200,
626 'message' => 'Contact unlinked from order'
627 )
628 );
629 }
630
638 public function put($id, $request_data = null)
639 {
640 if (!DolibarrApiAccess::$user->rights->commande->creer) {
641 throw new RestException(401);
642 }
643
644 $result = $this->commande->fetch($id);
645 if (!$result) {
646 throw new RestException(404, 'Order not found');
647 }
648
649 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
650 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
651 }
652 foreach ($request_data as $field => $value) {
653 if ($field == 'id') {
654 continue;
655 }
656 if ($field == 'array_options' && is_array($value)) {
657 foreach ($value as $index => $val) {
658 $this->commande->array_options[$index] = $this->_checkValForAPI($field, $val, $this->commande);
659 }
660 continue;
661 }
662 $this->commande->$field = $value;
663 }
664
665 // Update availability
666 if (!empty($this->commande->availability_id)) {
667 if ($this->commande->availability($this->commande->availability_id) < 0) {
668 throw new RestException(400, 'Error while updating availability');
669 }
670 }
671
672 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
673 return $this->get($id);
674 } else {
675 throw new RestException(500, $this->commande->error);
676 }
677 }
678
685 public function delete($id)
686 {
687 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
688 throw new RestException(401);
689 }
690 $result = $this->commande->fetch($id);
691 if (!$result) {
692 throw new RestException(404, 'Order not found');
693 }
694
695 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
696 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
697 }
698
699 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
700 throw new RestException(500, 'Error when deleting order : '.$this->commande->error);
701 }
702
703 return array(
704 'success' => array(
705 'code' => 200,
706 'message' => 'Order deleted'
707 )
708 );
709 }
710
733 public function validate($id, $idwarehouse = 0, $notrigger = 0)
734 {
735 if (!DolibarrApiAccess::$user->rights->commande->creer) {
736 throw new RestException(401);
737 }
738 $result = $this->commande->fetch($id);
739 if (!$result) {
740 throw new RestException(404, 'Order not found');
741 }
742
743 $result = $this->commande->fetch_thirdparty(); // do not check result, as failure is not fatal (used only for mail notification substitutes)
744
745 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
746 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
747 }
748
749 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
750 if ($result == 0) {
751 throw new RestException(304, 'Error nothing done. May be object is already validated');
752 }
753 if ($result < 0) {
754 throw new RestException(500, 'Error when validating Order: '.$this->commande->error);
755 }
756 $result = $this->commande->fetch($id);
757
758 $this->commande->fetchObjectLinked();
759
760 //fix #20477 : add online_payment_url
761 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
762 $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
763
764 return $this->_cleanObjectDatas($this->commande);
765 }
766
784 public function reopen($id)
785 {
786 if (!DolibarrApiAccess::$user->rights->commande->creer) {
787 throw new RestException(401);
788 }
789 if (empty($id)) {
790 throw new RestException(400, 'Order ID is mandatory');
791 }
792 $result = $this->commande->fetch($id);
793 if (!$result) {
794 throw new RestException(404, 'Order not found');
795 }
796
797 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
798 if ($result < 0) {
799 throw new RestException(405, $this->commande->error);
800 } elseif ($result == 0) {
801 throw new RestException(304);
802 }
803
804 return $result;
805 }
806
820 public function setinvoiced($id)
821 {
822
823 if (!DolibarrApiAccess::$user->rights->commande->creer) {
824 throw new RestException(401);
825 }
826 if (empty($id)) {
827 throw new RestException(400, 'Order ID is mandatory');
828 }
829 $result = $this->commande->fetch($id);
830 if (!$result) {
831 throw new RestException(404, 'Order not found');
832 }
833
834 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
835 if ($result < 0) {
836 throw new RestException(400, $this->commande->error);
837 }
838
839 $result = $this->commande->fetch($id);
840 if (!$result) {
841 throw new RestException(404, 'Order not found');
842 }
843
844 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
845 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
846 }
847
848 $this->commande->fetchObjectLinked();
849
850 return $this->_cleanObjectDatas($this->commande);
851 }
852
862 public function close($id, $notrigger = 0)
863 {
864 if (!DolibarrApiAccess::$user->rights->commande->creer) {
865 throw new RestException(401);
866 }
867 $result = $this->commande->fetch($id);
868 if (!$result) {
869 throw new RestException(404, 'Order not found');
870 }
871
872 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
873 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
874 }
875
876 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
877 if ($result == 0) {
878 throw new RestException(304, 'Error nothing done. May be object is already closed');
879 }
880 if ($result < 0) {
881 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
882 }
883
884 $result = $this->commande->fetch($id);
885 if (!$result) {
886 throw new RestException(404, 'Order not found');
887 }
888
889 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
890 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
891 }
892
893 $this->commande->fetchObjectLinked();
894
895 return $this->_cleanObjectDatas($this->commande);
896 }
897
907 public function settodraft($id, $idwarehouse = -1)
908 {
909 if (!DolibarrApiAccess::$user->rights->commande->creer) {
910 throw new RestException(401);
911 }
912 $result = $this->commande->fetch($id);
913 if (!$result) {
914 throw new RestException(404, 'Order not found');
915 }
916
917 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
918 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
919 }
920
921 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
922 if ($result == 0) {
923 throw new RestException(304, 'Nothing done. May be object is already closed');
924 }
925 if ($result < 0) {
926 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
927 }
928
929 $result = $this->commande->fetch($id);
930 if (!$result) {
931 throw new RestException(404, 'Order not found');
932 }
933
934 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
935 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
936 }
937
938 $this->commande->fetchObjectLinked();
939
940 return $this->_cleanObjectDatas($this->commande);
941 }
942
943
957 public function createOrderFromProposal($proposalid)
958 {
959
960 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
961
962 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
963 throw new RestException(401);
964 }
965 if (!DolibarrApiAccess::$user->rights->commande->creer) {
966 throw new RestException(401);
967 }
968 if (empty($proposalid)) {
969 throw new RestException(400, 'Proposal ID is mandatory');
970 }
971
972 $propal = new Propal($this->db);
973 $result = $propal->fetch($proposalid);
974 if (!$result) {
975 throw new RestException(404, 'Proposal not found');
976 }
977
978 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
979 if ($result < 0) {
980 throw new RestException(405, $this->commande->error);
981 }
982 $this->commande->fetchObjectLinked();
983
984 return $this->_cleanObjectDatas($this->commande);
985 }
986
1000 public function getOrderShipments($id)
1001 {
1002 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1003 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
1004 throw new RestException(401);
1005 }
1006 $obj_ret = array();
1007 $sql = "SELECT e.rowid";
1008 $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
1009 $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
1010 $sql .= " ON e.rowid = edet.fk_expedition";
1011 $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
1012 $sql .= " ON edet.fk_origin_line = cdet.rowid";
1013 $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
1014 $sql .= " ON cdet.fk_commande = c.rowid";
1015 $sql .= " WHERE c.rowid = ".((int) $id);
1016 $sql .= " GROUP BY e.rowid";
1017 $sql .= $this->db->order("e.rowid", "ASC");
1018
1019 dol_syslog("API Rest request");
1020 $result = $this->db->query($sql);
1021
1022 if ($result) {
1023 $num = $this->db->num_rows($result);
1024 if ($num <= 0) {
1025 throw new RestException(404, 'Shipments not found ');
1026 }
1027 $i = 0;
1028 while ($i < $num) {
1029 $obj = $this->db->fetch_object($result);
1030 $shipment_static = new Expedition($this->db);
1031 if ($shipment_static->fetch($obj->rowid)) {
1032 $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
1033 }
1034 $i++;
1035 }
1036 } else {
1037 throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
1038 }
1039 return $obj_ret;
1040 }
1041
1056 public function createOrderShipment($id, $warehouse_id)
1057 {
1058 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1059 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1060 throw new RestException(401);
1061 }
1062 if ($warehouse_id <= 0) {
1063 throw new RestException(404, 'Warehouse not found');
1064 }
1065 $result = $this->commande->fetch($id);
1066 if (!$result) {
1067 throw new RestException(404, 'Order not found');
1068 }
1069 $shipment = new Expedition($this->db);
1070 $shipment->socid = $this->commande->socid;
1071 $shipment->origin_id = $this->commande->id;
1072 $result = $shipment->create(DolibarrApiAccess::$user);
1073 if ($result <= 0) {
1074 throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
1075 }
1076 foreach ($this->commande->lines as $line) {
1077 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1078 if ($result <= 0) {
1079 throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
1080 }
1081 }
1082 return $shipment->id;
1083 }
1084
1085 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1092 protected function _cleanObjectDatas($object)
1093 {
1094 // phpcs:enable
1095 $object = parent::_cleanObjectDatas($object);
1096
1097 unset($object->note);
1098 unset($object->address);
1099 unset($object->barcode_type);
1100 unset($object->barcode_type_code);
1101 unset($object->barcode_type_label);
1102 unset($object->barcode_type_coder);
1103
1104 return $object;
1105 }
1106
1114 private function _validate($data)
1115 {
1116 $commande = array();
1117 foreach (Orders::$FIELDS as $field) {
1118 if (!isset($data[$field])) {
1119 throw new RestException(400, $field." field missing");
1120 }
1121 $commande[$field] = $data[$field];
1122 }
1123 return $commande;
1124 }
1125}
Class to manage customers orders.
Class for API REST v1.
Definition api.class.php:31
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:86
Class to manage shipments.
deleteContact($id, $contactid, $type)
Unlink a contact type of given order.
__construct()
Constructor.
_validate($data)
Validate fields before create or update object.
deleteLine($id, $lineid)
Delete a line of a given order.
getByRef($ref, $contact_list=1)
Get properties of an order object by ref.
close($id, $notrigger=0)
Close an order (Classify it as "Delivered")
getByRefExt($ref_ext, $contact_list=1)
Get properties of an order object by ref_ext.
_cleanObjectDatas($object)
Clean sensible object datas.
put($id, $request_data=null)
Update order general fields (won't touch lines of order)
getLines($id)
Get lines of an order.
postContact($id, $contactid, $type)
Add a contact type of given order.
reopen($id)
Tag the order as validated (opened)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $sqlfilterlines='')
List orders.
setinvoiced($id)
Classify the order as invoiced.
getContacts($id, $type='')
Get contacts of given order.
postLine($id, $request_data=null)
Add a line to given order.
post($request_data=null)
Create a sale order.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an order.
createOrderFromProposal($proposalid)
Create an order using an existing proposal.
putLine($id, $lineid, $request_data=null)
Update a line to given order.
getOrderShipments($id)
Get the shipments of an order.
settodraft($id, $idwarehouse=-1)
Set an order to draft.
createOrderShipment($id, $warehouse_id)
Create the shipment of an order.
_fetch($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an order object.
Class to manage proposals.
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.