dolibarr 21.0.0-beta
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20use Luracast\Restler\RestException;
21
22require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
23
30class Orders extends DolibarrApi
31{
35 public static $FIELDS = array(
36 'socid',
37 'date'
38 );
39
43 public $commande;
44
48 public function __construct()
49 {
50 global $db;
51
52 $this->db = $db;
53 $this->commande = new Commande($this->db);
54 }
55
67 public function get($id, $contact_list = 1)
68 {
69 return $this->_fetch($id, '', '', $contact_list);
70 }
71
85 public function getByRef($ref, $contact_list = 1)
86 {
87 return $this->_fetch(0, $ref, '', $contact_list);
88 }
89
103 public function getByRefExt($ref_ext, $contact_list = 1)
104 {
105 return $this->_fetch(0, '', $ref_ext, $contact_list);
106 }
107
121 private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
122 {
123 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
124 throw new RestException(403);
125 }
126
127 $result = $this->commande->fetch($id, $ref, $ref_ext);
128 if (!$result) {
129 throw new RestException(404, 'Order not found');
130 }
131
132 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
133 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
134 }
135
136 // Add external contacts ids
137 $tmparray = $this->commande->liste_contact(-1, 'external', $contact_list);
138 if (is_array($tmparray)) {
139 $this->commande->contacts_ids = $tmparray;
140 }
141 $this->commande->fetchObjectLinked();
142
143 // Add online_payment_url, cf #20477
144 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
145 $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
146
147 return $this->_cleanObjectDatas($this->commande);
148 }
149
170 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $sqlfilterlines = '', $properties = '', $pagination_data = false, $loadlinkedobjects = 0)
171 {
172 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
173 throw new RestException(403);
174 }
175
176 $obj_ret = array();
177
178 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
179 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
180
181 // If the internal user must only see his customers, force searching by him
182 $search_sale = 0;
183 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
184 $search_sale = DolibarrApiAccess::$user->id;
185 }
186
187 $sql = "SELECT t.rowid";
188 $sql .= " FROM ".MAIN_DB_PREFIX."commande AS t";
189 $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
190 $sql .= ' WHERE t.entity IN ('.getEntity('commande').')';
191 if ($socids) {
192 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
193 }
194 // Search on sale representative
195 if ($search_sale && $search_sale != '-1') {
196 if ($search_sale == -2) {
197 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
198 } elseif ($search_sale > 0) {
199 $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).")";
200 }
201 }
202 // Add sql filters
203 if ($sqlfilters) {
204 $errormessage = '';
205 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
206 if ($errormessage) {
207 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
208 }
209 }
210 // Add sql filters for lines
211 if ($sqlfilterlines) {
212 $errormessage = '';
213 $sql .= " AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX."commandedet AS tl WHERE tl.fk_commande = t.rowid";
214 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilterlines, $errormessage);
215 $sql .= ")";
216 if ($errormessage) {
217 throw new RestException(400, 'Error when validating parameter sqlfilterlines -> '.$errormessage);
218 }
219 }
220
221 //this query will return total orders with the filters given
222 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
223
224 $sql .= $this->db->order($sortfield, $sortorder);
225 if ($limit) {
226 if ($page < 0) {
227 $page = 0;
228 }
229 $offset = $limit * $page;
230
231 $sql .= $this->db->plimit($limit + 1, $offset);
232 }
233
234 dol_syslog("API Rest request");
235 $result = $this->db->query($sql);
236
237 if ($result) {
238 $num = $this->db->num_rows($result);
239 $min = min($num, ($limit <= 0 ? $num : $limit));
240 $i = 0;
241 while ($i < $min) {
242 $obj = $this->db->fetch_object($result);
243 $commande_static = new Commande($this->db);
244 if ($commande_static->fetch($obj->rowid) > 0) {
245 // Add external contacts ids
246 $tmparray = $commande_static->liste_contact(-1, 'external', 1);
247 if (is_array($tmparray)) {
248 $commande_static->contacts_ids = $tmparray;
249 }
250
251 if ($loadlinkedobjects) {
252 // retrieve linked objects
253 $commande_static->fetchObjectLinked();
254 }
255
256 // Add online_payment_url, cf #20477
257 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
258 $commande_static->online_payment_url = getOnlinePaymentUrl(0, 'order', $commande_static->ref);
259
260 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($commande_static), $properties);
261 }
262 $i++;
263 }
264 } else {
265 throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
266 }
267
268 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
269 if ($pagination_data) {
270 $totalsResult = $this->db->query($sqlTotals);
271 $total = $this->db->fetch_object($totalsResult)->total;
272
273 $tmp = $obj_ret;
274 $obj_ret = [];
275
276 $obj_ret['data'] = $tmp;
277 $obj_ret['pagination'] = [
278 'total' => (int) $total,
279 'page' => $page, //count starts from 0
280 'page_count' => ceil((int) $total / $limit),
281 'limit' => $limit
282 ];
283 }
284
285 return $obj_ret;
286 }
287
296 public function post($request_data = null)
297 {
298 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
299 throw new RestException(403, "Insuffisant rights");
300 }
301 // Check mandatory fields
302 $result = $this->_validate($request_data);
303
304 foreach ($request_data as $field => $value) {
305 if ($field === 'caller') {
306 // 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
307 $this->commande->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
308 continue;
309 }
310
311 $this->commande->$field = $this->_checkValForAPI($field, $value, $this->commande);
312 }
313 /*if (isset($request_data["lines"])) {
314 $lines = array();
315 foreach ($request_data["lines"] as $line) {
316 array_push($lines, (object) $line);
317 }
318 $this->commande->lines = $lines;
319 }*/
320
321 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
322 throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
323 }
324
325 return ((int) $this->commande->id);
326 }
327
337 public function getLines($id)
338 {
339 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
340 throw new RestException(403);
341 }
342
343 $result = $this->commande->fetch($id);
344 if (!$result) {
345 throw new RestException(404, 'Order not found');
346 }
347
348 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
349 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
350 }
351 $this->commande->getLinesArray();
352 $result = array();
353 foreach ($this->commande->lines as $line) {
354 array_push($result, $this->_cleanObjectDatas($line));
355 }
356 return $result;
357 }
358
369 public function postLine($id, $request_data = null)
370 {
371 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
372 throw new RestException(403);
373 }
374
375 $result = $this->commande->fetch($id);
376 if (!$result) {
377 throw new RestException(404, 'Order not found');
378 }
379
380 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
381 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
382 }
383
384 $request_data = (object) $request_data;
385
386 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
387 $request_data->label = sanitizeVal($request_data->label);
388
389 $updateRes = $this->commande->addline(
390 $request_data->desc,
391 $request_data->subprice,
392 $request_data->qty,
393 $request_data->tva_tx,
394 $request_data->localtax1_tx,
395 $request_data->localtax2_tx,
396 $request_data->fk_product,
397 $request_data->remise_percent,
398 $request_data->info_bits,
399 $request_data->fk_remise_except,
400 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
401 $request_data->subprice,
402 $request_data->date_start,
403 $request_data->date_end,
404 $request_data->product_type,
405 $request_data->rang,
406 $request_data->special_code,
407 $request_data->fk_parent_line,
408 $request_data->fk_fournprice,
409 $request_data->pa_ht,
410 $request_data->label,
411 $request_data->array_options,
412 $request_data->fk_unit,
413 $request_data->origin,
414 $request_data->origin_id,
415 $request_data->multicurrency_subprice,
416 $request_data->ref_ext
417 );
418
419 if ($updateRes > 0) {
420 return $updateRes;
421 } else {
422 throw new RestException(400, $this->commande->error);
423 }
424 }
425
436 public function putLine($id, $lineid, $request_data = null)
437 {
438 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
439 throw new RestException(403);
440 }
441
442 $result = $this->commande->fetch($id);
443 if (!$result) {
444 throw new RestException(404, 'Order not found');
445 }
446
447 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
448 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
449 }
450
451 $request_data = (object) $request_data;
452
453 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
454 $request_data->label = sanitizeVal($request_data->label);
455
456 $updateRes = $this->commande->updateline(
457 $lineid,
458 $request_data->desc,
459 $request_data->subprice,
460 $request_data->qty,
461 $request_data->remise_percent,
462 $request_data->tva_tx,
463 $request_data->localtax1_tx,
464 $request_data->localtax2_tx,
465 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
466 $request_data->info_bits,
467 $request_data->date_start,
468 $request_data->date_end,
469 $request_data->product_type,
470 $request_data->fk_parent_line,
471 0,
472 $request_data->fk_fournprice,
473 $request_data->pa_ht,
474 $request_data->label,
475 $request_data->special_code,
476 $request_data->array_options,
477 $request_data->fk_unit,
478 $request_data->multicurrency_subprice,
479 0,
480 $request_data->ref_ext,
481 $request_data->rang
482 );
483
484 if ($updateRes > 0) {
485 $result = $this->get($id);
486 unset($result->line);
487 return $this->_cleanObjectDatas($result);
488 }
489 return false;
490 }
491
504 public function deleteLine($id, $lineid)
505 {
506 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
507 throw new RestException(403);
508 }
509
510 $result = $this->commande->fetch($id);
511 if (!$result) {
512 throw new RestException(404, 'Order not found');
513 }
514
515 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
516 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
517 }
518
519 $updateRes = $this->commande->deleteLine(DolibarrApiAccess::$user, $lineid, $id);
520 if ($updateRes > 0) {
521 return $this->get($id);
522 } else {
523 throw new RestException(405, $this->commande->error);
524 }
525 }
526
540 public function getContacts($id, $type = '')
541 {
542 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
543 throw new RestException(403);
544 }
545
546 $result = $this->commande->fetch($id);
547 if (!$result) {
548 throw new RestException(404, 'Order not found');
549 }
550
551 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
552 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
553 }
554
555 $contacts = $this->commande->liste_contact(-1, 'external', 0, $type);
556
557 return $this->_cleanObjectDatas($contacts);
558 }
559
573 public function postContact($id, $contactid, $type)
574 {
575 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
576 throw new RestException(403);
577 }
578
579 $result = $this->commande->fetch($id);
580 if (!$result) {
581 throw new RestException(404, 'Order not found');
582 }
583
584 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
585 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
586 }
587
588 $result = $this->commande->add_contact($contactid, $type, 'external');
589
590 if ($result < 0) {
591 throw new RestException(500, 'Error when added the contact');
592 }
593
594 if ($result == 0) {
595 throw new RestException(304, 'contact already added');
596 }
597
598 return array(
599 'success' => array(
600 'code' => 200,
601 'message' => 'Contact linked to the order'
602 )
603 );
604 }
605
621 public function deleteContact($id, $contactid, $type)
622 {
623 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
624 throw new RestException(403);
625 }
626
627 $result = $this->commande->fetch($id);
628 if (!$result) {
629 throw new RestException(404, 'Order not found');
630 }
631
632 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
633 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
634 }
635
636 $contacts = $this->commande->liste_contact();
637
638 foreach ($contacts as $contact) {
639 if ($contact['id'] == $contactid && $contact['code'] == $type) {
640 $result = $this->commande->delete_contact($contact['rowid']);
641
642 if (!$result) {
643 throw new RestException(500, 'Error when deleted the contact');
644 }
645 }
646 }
647
648 return array(
649 'success' => array(
650 'code' => 200,
651 'message' => 'Contact unlinked from order'
652 )
653 );
654 }
655
663 public function put($id, $request_data = null)
664 {
665 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
666 throw new RestException(403);
667 }
668
669 $result = $this->commande->fetch($id);
670 if (!$result) {
671 throw new RestException(404, 'Order not found');
672 }
673
674 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
675 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
676 }
677 foreach ($request_data as $field => $value) {
678 if ($field == 'id') {
679 continue;
680 }
681 if ($field === 'caller') {
682 // 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
683 $this->commande->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
684 continue;
685 }
686 if ($field == 'array_options' && is_array($value)) {
687 foreach ($value as $index => $val) {
688 $this->commande->array_options[$index] = $this->_checkValForAPI($field, $val, $this->commande);
689 }
690 continue;
691 }
692
693 $this->commande->$field = $this->_checkValForAPI($field, $value, $this->commande);
694 }
695
696 // Update availability
697 if (!empty($this->commande->availability_id)) {
698 if ($this->commande->availability($this->commande->availability_id) < 0) {
699 throw new RestException(400, 'Error while updating availability');
700 }
701 }
702
703 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
704 return $this->get($id);
705 } else {
706 throw new RestException(500, $this->commande->error);
707 }
708 }
709
716 public function delete($id)
717 {
718 if (!DolibarrApiAccess::$user->hasRight('commande', 'supprimer')) {
719 throw new RestException(403);
720 }
721 $result = $this->commande->fetch($id);
722 if (!$result) {
723 throw new RestException(404, 'Order not found');
724 }
725
726 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
727 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
728 }
729
730 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
731 throw new RestException(500, 'Error when deleting order : '.$this->commande->error);
732 }
733
734 return array(
735 'success' => array(
736 'code' => 200,
737 'message' => 'Order deleted'
738 )
739 );
740 }
741
764 public function validate($id, $idwarehouse = 0, $notrigger = 0)
765 {
766 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
767 throw new RestException(403);
768 }
769 $result = $this->commande->fetch($id);
770 if (!$result) {
771 throw new RestException(404, 'Order not found');
772 }
773
774 $result = $this->commande->fetch_thirdparty(); // do not check result, as failure is not fatal (used only for mail notification substitutes)
775
776 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
777 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
778 }
779
780 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
781 if ($result == 0) {
782 throw new RestException(304, 'Error nothing done. May be object is already validated');
783 }
784 if ($result < 0) {
785 throw new RestException(500, 'Error when validating Order: '.$this->commande->error);
786 }
787 $result = $this->commande->fetch($id);
788
789 $this->commande->fetchObjectLinked();
790
791 //fix #20477 : add online_payment_url
792 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
793 $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
794
795 return $this->_cleanObjectDatas($this->commande);
796 }
797
815 public function reopen($id)
816 {
817 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
818 throw new RestException(403);
819 }
820 if (empty($id)) {
821 throw new RestException(400, 'Order ID is mandatory');
822 }
823 $result = $this->commande->fetch($id);
824 if (!$result) {
825 throw new RestException(404, 'Order not found');
826 }
827
828 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
829 if ($result < 0) {
830 throw new RestException(405, $this->commande->error);
831 } elseif ($result == 0) {
832 throw new RestException(304);
833 }
834
835 return $result;
836 }
837
851 public function setinvoiced($id)
852 {
853 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
854 throw new RestException(403);
855 }
856 if (empty($id)) {
857 throw new RestException(400, 'Order ID is mandatory');
858 }
859 $result = $this->commande->fetch($id);
860 if (!$result) {
861 throw new RestException(404, 'Order not found');
862 }
863
864 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
865 if ($result < 0) {
866 throw new RestException(400, $this->commande->error);
867 }
868
869 $result = $this->commande->fetch($id);
870 if (!$result) {
871 throw new RestException(404, 'Order not found');
872 }
873
874 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
875 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
876 }
877
878 $this->commande->fetchObjectLinked();
879
880 return $this->_cleanObjectDatas($this->commande);
881 }
882
892 public function close($id, $notrigger = 0)
893 {
894 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
895 throw new RestException(403);
896 }
897 $result = $this->commande->fetch($id);
898 if (!$result) {
899 throw new RestException(404, 'Order not found');
900 }
901
902 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
903 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
904 }
905
906 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
907 if ($result == 0) {
908 throw new RestException(304, 'Error nothing done. May be object is already closed');
909 }
910 if ($result < 0) {
911 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
912 }
913
914 $result = $this->commande->fetch($id);
915 if (!$result) {
916 throw new RestException(404, 'Order not found');
917 }
918
919 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
920 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
921 }
922
923 $this->commande->fetchObjectLinked();
924
925 return $this->_cleanObjectDatas($this->commande);
926 }
927
937 public function settodraft($id, $idwarehouse = -1)
938 {
939 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
940 throw new RestException(403);
941 }
942 $result = $this->commande->fetch($id);
943 if (!$result) {
944 throw new RestException(404, 'Order not found');
945 }
946
947 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
948 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
949 }
950
951 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
952 if ($result == 0) {
953 throw new RestException(304, 'Nothing done. May be object is already closed');
954 }
955 if ($result < 0) {
956 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
957 }
958
959 $result = $this->commande->fetch($id);
960 if (!$result) {
961 throw new RestException(404, 'Order not found');
962 }
963
964 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
965 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
966 }
967
968 $this->commande->fetchObjectLinked();
969
970 return $this->_cleanObjectDatas($this->commande);
971 }
972
973
987 public function createOrderFromProposal($proposalid)
988 {
989 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
990
991 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
992 throw new RestException(403);
993 }
994 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
995 throw new RestException(403);
996 }
997 if (empty($proposalid)) {
998 throw new RestException(400, 'Proposal ID is mandatory');
999 }
1000
1001 $propal = new Propal($this->db);
1002 $result = $propal->fetch($proposalid);
1003 if (!$result) {
1004 throw new RestException(404, 'Proposal not found');
1005 }
1006
1007 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
1008 if ($result < 0) {
1009 throw new RestException(405, $this->commande->error);
1010 }
1011 $this->commande->fetchObjectLinked();
1012
1013 return $this->_cleanObjectDatas($this->commande);
1014 }
1015
1029 public function getOrderShipments($id)
1030 {
1031 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1032 if (!DolibarrApiAccess::$user->hasRight('expedition', 'lire')) {
1033 throw new RestException(403);
1034 }
1035 $obj_ret = array();
1036 $sql = "SELECT e.rowid";
1037 $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
1038 $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
1039 $sql .= " ON e.rowid = edet.fk_expedition";
1040 $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
1041 $sql .= " ON edet.fk_elementdet = cdet.rowid";
1042 $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
1043 $sql .= " ON cdet.fk_commande = c.rowid";
1044 $sql .= " WHERE c.rowid = ".((int) $id);
1045 $sql .= " GROUP BY e.rowid";
1046 $sql .= $this->db->order("e.rowid", "ASC");
1047
1048 dol_syslog("API Rest request");
1049 $result = $this->db->query($sql);
1050
1051 if ($result) {
1052 $num = $this->db->num_rows($result);
1053 if ($num <= 0) {
1054 throw new RestException(404, 'Shipments not found ');
1055 }
1056 $i = 0;
1057 while ($i < $num) {
1058 $obj = $this->db->fetch_object($result);
1059 $shipment_static = new Expedition($this->db);
1060 if ($shipment_static->fetch($obj->rowid)) {
1061 $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
1062 }
1063 $i++;
1064 }
1065 } else {
1066 throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
1067 }
1068 return $obj_ret;
1069 }
1070
1085 public function createOrderShipment($id, $warehouse_id)
1086 {
1087 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1088 if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
1089 throw new RestException(403);
1090 }
1091 if ($warehouse_id <= 0) {
1092 throw new RestException(404, 'Warehouse not found');
1093 }
1094 $result = $this->commande->fetch($id);
1095 if (!$result) {
1096 throw new RestException(404, 'Order not found');
1097 }
1098 $shipment = new Expedition($this->db);
1099 $shipment->socid = $this->commande->socid;
1100 $shipment->origin_id = $this->commande->id;
1101 $shipment->origin = $this->commande->element;
1102 $result = $shipment->create(DolibarrApiAccess::$user);
1103 if ($result <= 0) {
1104 throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
1105 }
1106 foreach ($this->commande->lines as $line) {
1107 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1108 if ($result <= 0) {
1109 throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
1110 }
1111 }
1112 return $shipment->id;
1113 }
1114
1115 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1122 protected function _cleanObjectDatas($object)
1123 {
1124 // phpcs:enable
1125 $object = parent::_cleanObjectDatas($object);
1126
1127 unset($object->note);
1128 unset($object->address);
1129 unset($object->barcode_type);
1130 unset($object->barcode_type_code);
1131 unset($object->barcode_type_label);
1132 unset($object->barcode_type_coder);
1133
1134 return $object;
1135 }
1136
1144 private function _validate($data)
1145 {
1146 $commande = array();
1147 foreach (Orders::$FIELDS as $field) {
1148 if (!isset($data[$field])) {
1149 throw new RestException(400, $field." field missing");
1150 }
1151 $commande[$field] = $data[$field];
1152 }
1153 return $commande;
1154 }
1155}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $sqlfilterlines='', $properties='', $pagination_data=false, $loadlinkedobjects=0)
List orders.
_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.
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.