dolibarr 20.0.2
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 public 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(403);
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(403, '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
166 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $sqlfilterlines = '', $properties = '')
167 {
168 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
169 throw new RestException(403);
170 }
171
172 $obj_ret = array();
173
174 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
175 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
176
177 // If the internal user must only see his customers, force searching by him
178 $search_sale = 0;
179 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
180 $search_sale = DolibarrApiAccess::$user->id;
181 }
182
183 $sql = "SELECT t.rowid";
184 $sql .= " FROM ".MAIN_DB_PREFIX."commande AS t";
185 $sql .= " 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
186 $sql .= ' WHERE t.entity IN ('.getEntity('commande').')';
187 if ($socids) {
188 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
189 }
190 // Search on sale representative
191 if ($search_sale && $search_sale != '-1') {
192 if ($search_sale == -2) {
193 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
194 } elseif ($search_sale > 0) {
195 $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).")";
196 }
197 }
198 // Add sql filters
199 if ($sqlfilters) {
200 $errormessage = '';
201 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
202 if ($errormessage) {
203 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
204 }
205 }
206 // Add sql filters for lines
207 if ($sqlfilterlines) {
208 $errormessage = '';
209 $sql .= " AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX."commandedet AS tl WHERE tl.fk_commande = t.rowid";
210 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilterlines, $errormessage);
211 $sql .= ")";
212 if ($errormessage) {
213 throw new RestException(400, 'Error when validating parameter sqlfilterlines -> '.$errormessage);
214 }
215 }
216 $sql .= $this->db->order($sortfield, $sortorder);
217 if ($limit) {
218 if ($page < 0) {
219 $page = 0;
220 }
221 $offset = $limit * $page;
222
223 $sql .= $this->db->plimit($limit + 1, $offset);
224 }
225
226 dol_syslog("API Rest request");
227 $result = $this->db->query($sql);
228
229 if ($result) {
230 $num = $this->db->num_rows($result);
231 $min = min($num, ($limit <= 0 ? $num : $limit));
232 $i = 0;
233 while ($i < $min) {
234 $obj = $this->db->fetch_object($result);
235 $commande_static = new Commande($this->db);
236 if ($commande_static->fetch($obj->rowid)) {
237 // Add external contacts ids
238 $tmparray = $commande_static->liste_contact(-1, 'external', 1);
239 if (is_array($tmparray)) {
240 $commande_static->contacts_ids = $tmparray;
241 }
242 // Add online_payment_url, cf #20477
243 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
244 $commande_static->online_payment_url = getOnlinePaymentUrl(0, 'order', $commande_static->ref);
245
246 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($commande_static), $properties);
247 }
248 $i++;
249 }
250 } else {
251 throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
252 }
253
254 return $obj_ret;
255 }
256
265 public function post($request_data = null)
266 {
267 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
268 throw new RestException(403, "Insuffisant rights");
269 }
270 // Check mandatory fields
271 $result = $this->_validate($request_data);
272
273 foreach ($request_data as $field => $value) {
274 if ($field === 'caller') {
275 // 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
276 $this->commande->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
277 continue;
278 }
279
280 $this->commande->$field = $this->_checkValForAPI($field, $value, $this->commande);
281 }
282 /*if (isset($request_data["lines"])) {
283 $lines = array();
284 foreach ($request_data["lines"] as $line) {
285 array_push($lines, (object) $line);
286 }
287 $this->commande->lines = $lines;
288 }*/
289
290 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
291 throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
292 }
293
294 return ((int) $this->commande->id);
295 }
296
306 public function getLines($id)
307 {
308 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
309 throw new RestException(403);
310 }
311
312 $result = $this->commande->fetch($id);
313 if (!$result) {
314 throw new RestException(404, 'Order not found');
315 }
316
317 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
318 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
319 }
320 $this->commande->getLinesArray();
321 $result = array();
322 foreach ($this->commande->lines as $line) {
323 array_push($result, $this->_cleanObjectDatas($line));
324 }
325 return $result;
326 }
327
338 public function postLine($id, $request_data = null)
339 {
340 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
341 throw new RestException(403);
342 }
343
344 $result = $this->commande->fetch($id);
345 if (!$result) {
346 throw new RestException(404, 'Order not found');
347 }
348
349 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->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->commande->addline(
359 $request_data->desc,
360 $request_data->subprice,
361 $request_data->qty,
362 $request_data->tva_tx,
363 $request_data->localtax1_tx,
364 $request_data->localtax2_tx,
365 $request_data->fk_product,
366 $request_data->remise_percent,
367 $request_data->info_bits,
368 $request_data->fk_remise_except,
369 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
370 $request_data->subprice,
371 $request_data->date_start,
372 $request_data->date_end,
373 $request_data->product_type,
374 $request_data->rang,
375 $request_data->special_code,
376 $request_data->fk_parent_line,
377 $request_data->fk_fournprice,
378 $request_data->pa_ht,
379 $request_data->label,
380 $request_data->array_options,
381 $request_data->fk_unit,
382 $request_data->origin,
383 $request_data->origin_id,
384 $request_data->multicurrency_subprice,
385 $request_data->ref_ext
386 );
387
388 if ($updateRes > 0) {
389 return $updateRes;
390 } else {
391 throw new RestException(400, $this->commande->error);
392 }
393 }
394
405 public function putLine($id, $lineid, $request_data = null)
406 {
407 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
408 throw new RestException(403);
409 }
410
411 $result = $this->commande->fetch($id);
412 if (!$result) {
413 throw new RestException(404, 'Order not found');
414 }
415
416 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
417 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
418 }
419
420 $request_data = (object) $request_data;
421
422 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
423 $request_data->label = sanitizeVal($request_data->label);
424
425 $updateRes = $this->commande->updateline(
426 $lineid,
427 $request_data->desc,
428 $request_data->subprice,
429 $request_data->qty,
430 $request_data->remise_percent,
431 $request_data->tva_tx,
432 $request_data->localtax1_tx,
433 $request_data->localtax2_tx,
434 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
435 $request_data->info_bits,
436 $request_data->date_start,
437 $request_data->date_end,
438 $request_data->product_type,
439 $request_data->fk_parent_line,
440 0,
441 $request_data->fk_fournprice,
442 $request_data->pa_ht,
443 $request_data->label,
444 $request_data->special_code,
445 $request_data->array_options,
446 $request_data->fk_unit,
447 $request_data->multicurrency_subprice,
448 0,
449 $request_data->ref_ext,
450 $request_data->rang
451 );
452
453 if ($updateRes > 0) {
454 $result = $this->get($id);
455 unset($result->line);
456 return $this->_cleanObjectDatas($result);
457 }
458 return false;
459 }
460
473 public function deleteLine($id, $lineid)
474 {
475 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
476 throw new RestException(403);
477 }
478
479 $result = $this->commande->fetch($id);
480 if (!$result) {
481 throw new RestException(404, 'Order not found');
482 }
483
484 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
485 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
486 }
487
488 $updateRes = $this->commande->deleteLine(DolibarrApiAccess::$user, $lineid, $id);
489 if ($updateRes > 0) {
490 return $this->get($id);
491 } else {
492 throw new RestException(405, $this->commande->error);
493 }
494 }
495
509 public function getContacts($id, $type = '')
510 {
511 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
512 throw new RestException(403);
513 }
514
515 $result = $this->commande->fetch($id);
516 if (!$result) {
517 throw new RestException(404, 'Order not found');
518 }
519
520 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
521 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
522 }
523
524 $contacts = $this->commande->liste_contact(-1, 'external', 0, $type);
525
526 return $this->_cleanObjectDatas($contacts);
527 }
528
542 public function postContact($id, $contactid, $type)
543 {
544 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
545 throw new RestException(403);
546 }
547
548 $result = $this->commande->fetch($id);
549 if (!$result) {
550 throw new RestException(404, 'Order not found');
551 }
552
553 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
554 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
555 }
556
557 $result = $this->commande->add_contact($contactid, $type, 'external');
558
559 if ($result < 0) {
560 throw new RestException(500, 'Error when added the contact');
561 }
562
563 if ($result == 0) {
564 throw new RestException(304, 'contact already added');
565 }
566
567 return array(
568 'success' => array(
569 'code' => 200,
570 'message' => 'Contact linked to the order'
571 )
572 );
573 }
574
590 public function deleteContact($id, $contactid, $type)
591 {
592 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
593 throw new RestException(403);
594 }
595
596 $result = $this->commande->fetch($id);
597 if (!$result) {
598 throw new RestException(404, 'Order not found');
599 }
600
601 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
602 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
603 }
604
605 $contacts = $this->commande->liste_contact();
606
607 foreach ($contacts as $contact) {
608 if ($contact['id'] == $contactid && $contact['code'] == $type) {
609 $result = $this->commande->delete_contact($contact['rowid']);
610
611 if (!$result) {
612 throw new RestException(500, 'Error when deleted the contact');
613 }
614 }
615 }
616
617 return array(
618 'success' => array(
619 'code' => 200,
620 'message' => 'Contact unlinked from order'
621 )
622 );
623 }
624
632 public function put($id, $request_data = null)
633 {
634 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
635 throw new RestException(403);
636 }
637
638 $result = $this->commande->fetch($id);
639 if (!$result) {
640 throw new RestException(404, 'Order not found');
641 }
642
643 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
644 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
645 }
646 foreach ($request_data as $field => $value) {
647 if ($field == 'id') {
648 continue;
649 }
650 if ($field === 'caller') {
651 // 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
652 $this->commande->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
653 continue;
654 }
655 if ($field == 'array_options' && is_array($value)) {
656 foreach ($value as $index => $val) {
657 $this->commande->array_options[$index] = $this->_checkValForAPI($field, $val, $this->commande);
658 }
659 continue;
660 }
661
662 $this->commande->$field = $this->_checkValForAPI($field, $value, $this->commande);
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->hasRight('commande', 'supprimer')) {
688 throw new RestException(403);
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(403, '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->hasRight('commande', 'creer')) {
736 throw new RestException(403);
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(403, '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->hasRight('commande', 'creer')) {
787 throw new RestException(403);
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 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
823 throw new RestException(403);
824 }
825 if (empty($id)) {
826 throw new RestException(400, 'Order ID is mandatory');
827 }
828 $result = $this->commande->fetch($id);
829 if (!$result) {
830 throw new RestException(404, 'Order not found');
831 }
832
833 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
834 if ($result < 0) {
835 throw new RestException(400, $this->commande->error);
836 }
837
838 $result = $this->commande->fetch($id);
839 if (!$result) {
840 throw new RestException(404, 'Order not found');
841 }
842
843 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
844 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
845 }
846
847 $this->commande->fetchObjectLinked();
848
849 return $this->_cleanObjectDatas($this->commande);
850 }
851
861 public function close($id, $notrigger = 0)
862 {
863 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
864 throw new RestException(403);
865 }
866 $result = $this->commande->fetch($id);
867 if (!$result) {
868 throw new RestException(404, 'Order not found');
869 }
870
871 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
872 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
873 }
874
875 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
876 if ($result == 0) {
877 throw new RestException(304, 'Error nothing done. May be object is already closed');
878 }
879 if ($result < 0) {
880 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
881 }
882
883 $result = $this->commande->fetch($id);
884 if (!$result) {
885 throw new RestException(404, 'Order not found');
886 }
887
888 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
889 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
890 }
891
892 $this->commande->fetchObjectLinked();
893
894 return $this->_cleanObjectDatas($this->commande);
895 }
896
906 public function settodraft($id, $idwarehouse = -1)
907 {
908 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
909 throw new RestException(403);
910 }
911 $result = $this->commande->fetch($id);
912 if (!$result) {
913 throw new RestException(404, 'Order not found');
914 }
915
916 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
917 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
918 }
919
920 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
921 if ($result == 0) {
922 throw new RestException(304, 'Nothing done. May be object is already closed');
923 }
924 if ($result < 0) {
925 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
926 }
927
928 $result = $this->commande->fetch($id);
929 if (!$result) {
930 throw new RestException(404, 'Order not found');
931 }
932
933 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
934 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
935 }
936
937 $this->commande->fetchObjectLinked();
938
939 return $this->_cleanObjectDatas($this->commande);
940 }
941
942
956 public function createOrderFromProposal($proposalid)
957 {
958 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
959
960 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
961 throw new RestException(403);
962 }
963 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
964 throw new RestException(403);
965 }
966 if (empty($proposalid)) {
967 throw new RestException(400, 'Proposal ID is mandatory');
968 }
969
970 $propal = new Propal($this->db);
971 $result = $propal->fetch($proposalid);
972 if (!$result) {
973 throw new RestException(404, 'Proposal not found');
974 }
975
976 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
977 if ($result < 0) {
978 throw new RestException(405, $this->commande->error);
979 }
980 $this->commande->fetchObjectLinked();
981
982 return $this->_cleanObjectDatas($this->commande);
983 }
984
998 public function getOrderShipments($id)
999 {
1000 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1001 if (!DolibarrApiAccess::$user->hasRight('expedition', 'lire')) {
1002 throw new RestException(403);
1003 }
1004 $obj_ret = array();
1005 $sql = "SELECT e.rowid";
1006 $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
1007 $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
1008 $sql .= " ON e.rowid = edet.fk_expedition";
1009 $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
1010 $sql .= " ON edet.fk_elementdet = cdet.rowid";
1011 $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
1012 $sql .= " ON cdet.fk_commande = c.rowid";
1013 $sql .= " WHERE c.rowid = ".((int) $id);
1014 $sql .= " GROUP BY e.rowid";
1015 $sql .= $this->db->order("e.rowid", "ASC");
1016
1017 dol_syslog("API Rest request");
1018 $result = $this->db->query($sql);
1019
1020 if ($result) {
1021 $num = $this->db->num_rows($result);
1022 if ($num <= 0) {
1023 throw new RestException(404, 'Shipments not found ');
1024 }
1025 $i = 0;
1026 while ($i < $num) {
1027 $obj = $this->db->fetch_object($result);
1028 $shipment_static = new Expedition($this->db);
1029 if ($shipment_static->fetch($obj->rowid)) {
1030 $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
1031 }
1032 $i++;
1033 }
1034 } else {
1035 throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
1036 }
1037 return $obj_ret;
1038 }
1039
1054 public function createOrderShipment($id, $warehouse_id)
1055 {
1056 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1057 if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
1058 throw new RestException(403);
1059 }
1060 if ($warehouse_id <= 0) {
1061 throw new RestException(404, 'Warehouse not found');
1062 }
1063 $result = $this->commande->fetch($id);
1064 if (!$result) {
1065 throw new RestException(404, 'Order not found');
1066 }
1067 $shipment = new Expedition($this->db);
1068 $shipment->socid = $this->commande->socid;
1069 $shipment->origin_id = $this->commande->id;
1070 $result = $shipment->create(DolibarrApiAccess::$user);
1071 if ($result <= 0) {
1072 throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
1073 }
1074 foreach ($this->commande->lines as $line) {
1075 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1076 if ($result <= 0) {
1077 throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
1078 }
1079 }
1080 return $shipment->id;
1081 }
1082
1083 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1090 protected function _cleanObjectDatas($object)
1091 {
1092 // phpcs:enable
1093 $object = parent::_cleanObjectDatas($object);
1094
1095 unset($object->note);
1096 unset($object->address);
1097 unset($object->barcode_type);
1098 unset($object->barcode_type_code);
1099 unset($object->barcode_type_label);
1100 unset($object->barcode_type_coder);
1101
1102 return $object;
1103 }
1104
1112 private function _validate($data)
1113 {
1114 $commande = array();
1115 foreach (Orders::$FIELDS as $field) {
1116 if (!isset($data[$field])) {
1117 throw new RestException(400, $field." field missing");
1118 }
1119 $commande[$field] = $data[$field];
1120 }
1121 return $commande;
1122 }
1123}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class to manage customers orders.
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.
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)
setinvoiced($id)
Classify the order as invoiced.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $sqlfilterlines='', $properties='')
List orders.
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.