dolibarr 19.0.4
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(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
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(401);
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->rights->societe->client->voir && !$socids) {
180 $search_sale = DolibarrApiAccess::$user->id;
181 }
182
183 $sql = "SELECT t.rowid";
184 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
185 $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)
186 }
187 $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
188
189 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
190 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
191 }
192
193 $sql .= ' WHERE t.entity IN ('.getEntity('commande').')';
194 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
195 $sql .= " AND t.fk_soc = sc.fk_soc";
196 }
197 if ($socids) {
198 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
199 }
200 if ($search_sale > 0) {
201 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
202 }
203 // Insert sale filter
204 if ($search_sale > 0) {
205 $sql .= " AND sc.fk_user = ".((int) $search_sale);
206 }
207 // Add sql filters
208 if ($sqlfilters) {
209 $errormessage = '';
210 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
211 if ($errormessage) {
212 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
213 }
214 }
215 // Add sql filters for lines
216 if ($sqlfilterlines) {
217 $errormessage = '';
218 $sql .= " AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX."commandedet AS tl WHERE tl.fk_commande = t.rowid";
219 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilterlines, $errormessage);
220 $sql .= ")";
221 if ($errormessage) {
222 throw new RestException(400, 'Error when validating parameter sqlfilterlines -> '.$errormessage);
223 }
224 }
225 $sql .= $this->db->order($sortfield, $sortorder);
226 if ($limit) {
227 if ($page < 0) {
228 $page = 0;
229 }
230 $offset = $limit * $page;
231
232 $sql .= $this->db->plimit($limit + 1, $offset);
233 }
234
235 dol_syslog("API Rest request");
236 $result = $this->db->query($sql);
237
238 if ($result) {
239 $num = $this->db->num_rows($result);
240 $min = min($num, ($limit <= 0 ? $num : $limit));
241 $i = 0;
242 while ($i < $min) {
243 $obj = $this->db->fetch_object($result);
244 $commande_static = new Commande($this->db);
245 if ($commande_static->fetch($obj->rowid)) {
246 // Add external contacts ids
247 $tmparray = $commande_static->liste_contact(-1, 'external', 1);
248 if (is_array($tmparray)) {
249 $commande_static->contacts_ids = $tmparray;
250 }
251 // Add online_payment_url, cf #20477
252 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
253 $commande_static->online_payment_url = getOnlinePaymentUrl(0, 'order', $commande_static->ref);
254
255 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($commande_static), $properties);
256 }
257 $i++;
258 }
259 } else {
260 throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
261 }
262
263 return $obj_ret;
264 }
265
274 public function post($request_data = null)
275 {
276 if (!DolibarrApiAccess::$user->rights->commande->creer) {
277 throw new RestException(401, "Insuffisant rights");
278 }
279 // Check mandatory fields
280 $result = $this->_validate($request_data);
281
282 foreach ($request_data as $field => $value) {
283 if ($field === 'caller') {
284 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
285 $this->commande->context['caller'] = $request_data['caller'];
286 continue;
287 }
288
289 $this->commande->$field = $value;
290 }
291 /*if (isset($request_data["lines"])) {
292 $lines = array();
293 foreach ($request_data["lines"] as $line) {
294 array_push($lines, (object) $line);
295 }
296 $this->commande->lines = $lines;
297 }*/
298
299 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
300 throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
301 }
302
303 return ((int) $this->commande->id);
304 }
305
315 public function getLines($id)
316 {
317 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
318 throw new RestException(401);
319 }
320
321 $result = $this->commande->fetch($id);
322 if (!$result) {
323 throw new RestException(404, 'Order not found');
324 }
325
326 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
327 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
328 }
329 $this->commande->getLinesArray();
330 $result = array();
331 foreach ($this->commande->lines as $line) {
332 array_push($result, $this->_cleanObjectDatas($line));
333 }
334 return $result;
335 }
336
347 public function postLine($id, $request_data = null)
348 {
349 if (!DolibarrApiAccess::$user->rights->commande->creer) {
350 throw new RestException(401);
351 }
352
353 $result = $this->commande->fetch($id);
354 if (!$result) {
355 throw new RestException(404, 'Order not found');
356 }
357
358 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
359 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
360 }
361
362 $request_data = (object) $request_data;
363
364 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
365 $request_data->label = sanitizeVal($request_data->label);
366
367 $updateRes = $this->commande->addline(
368 $request_data->desc,
369 $request_data->subprice,
370 $request_data->qty,
371 $request_data->tva_tx,
372 $request_data->localtax1_tx,
373 $request_data->localtax2_tx,
374 $request_data->fk_product,
375 $request_data->remise_percent,
376 $request_data->info_bits,
377 $request_data->fk_remise_except,
378 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
379 $request_data->subprice,
380 $request_data->date_start,
381 $request_data->date_end,
382 $request_data->product_type,
383 $request_data->rang,
384 $request_data->special_code,
385 $request_data->fk_parent_line,
386 $request_data->fk_fournprice,
387 $request_data->pa_ht,
388 $request_data->label,
389 $request_data->array_options,
390 $request_data->fk_unit,
391 $request_data->origin,
392 $request_data->origin_id,
393 $request_data->multicurrency_subprice,
394 $request_data->ref_ext
395 );
396
397 if ($updateRes > 0) {
398 return $updateRes;
399 } else {
400 throw new RestException(400, $this->commande->error);
401 }
402 }
403
414 public function putLine($id, $lineid, $request_data = null)
415 {
416 if (!DolibarrApiAccess::$user->rights->commande->creer) {
417 throw new RestException(401);
418 }
419
420 $result = $this->commande->fetch($id);
421 if (!$result) {
422 throw new RestException(404, 'Order not found');
423 }
424
425 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
426 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
427 }
428
429 $request_data = (object) $request_data;
430
431 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
432 $request_data->label = sanitizeVal($request_data->label);
433
434 $updateRes = $this->commande->updateline(
435 $lineid,
436 $request_data->desc,
437 $request_data->subprice,
438 $request_data->qty,
439 $request_data->remise_percent,
440 $request_data->tva_tx,
441 $request_data->localtax1_tx,
442 $request_data->localtax2_tx,
443 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
444 $request_data->info_bits,
445 $request_data->date_start,
446 $request_data->date_end,
447 $request_data->product_type,
448 $request_data->fk_parent_line,
449 0,
450 $request_data->fk_fournprice,
451 $request_data->pa_ht,
452 $request_data->label,
453 $request_data->special_code,
454 $request_data->array_options,
455 $request_data->fk_unit,
456 $request_data->multicurrency_subprice,
457 0,
458 $request_data->ref_ext,
459 $request_data->rang
460 );
461
462 if ($updateRes > 0) {
463 $result = $this->get($id);
464 unset($result->line);
465 return $this->_cleanObjectDatas($result);
466 }
467 return false;
468 }
469
482 public function deleteLine($id, $lineid)
483 {
484 if (!DolibarrApiAccess::$user->rights->commande->creer) {
485 throw new RestException(401);
486 }
487
488 $result = $this->commande->fetch($id);
489 if (!$result) {
490 throw new RestException(404, 'Order not found');
491 }
492
493 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
494 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 }
496
497 $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid, $id);
498 if ($updateRes > 0) {
499 return $this->get($id);
500 } else {
501 throw new RestException(405, $this->commande->error);
502 }
503 }
504
518 public function getContacts($id, $type = '')
519 {
520 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
521 throw new RestException(401);
522 }
523
524 $result = $this->commande->fetch($id);
525 if (!$result) {
526 throw new RestException(404, 'Order not found');
527 }
528
529 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
530 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
531 }
532
533 $contacts = $this->commande->liste_contact(-1, 'external', 0, $type);
534
535 return $this->_cleanObjectDatas($contacts);
536 }
537
551 public function postContact($id, $contactid, $type)
552 {
553 if (!DolibarrApiAccess::$user->rights->commande->creer) {
554 throw new RestException(401);
555 }
556
557 $result = $this->commande->fetch($id);
558 if (!$result) {
559 throw new RestException(404, 'Order not found');
560 }
561
562 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
563 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
564 }
565
566 $result = $this->commande->add_contact($contactid, $type, 'external');
567
568 if ($result < 0) {
569 throw new RestException(500, 'Error when added the contact');
570 }
571
572 if ($result == 0) {
573 throw new RestException(304, 'contact already added');
574 }
575
576 return array(
577 'success' => array(
578 'code' => 200,
579 'message' => 'Contact linked to the order'
580 )
581 );
582 }
583
599 public function deleteContact($id, $contactid, $type)
600 {
601 if (!DolibarrApiAccess::$user->rights->commande->creer) {
602 throw new RestException(401);
603 }
604
605 $result = $this->commande->fetch($id);
606 if (!$result) {
607 throw new RestException(404, 'Order not found');
608 }
609
610 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
611 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
612 }
613
614 $contacts = $this->commande->liste_contact();
615
616 foreach ($contacts as $contact) {
617 if ($contact['id'] == $contactid && $contact['code'] == $type) {
618 $result = $this->commande->delete_contact($contact['rowid']);
619
620 if (!$result) {
621 throw new RestException(500, 'Error when deleted the contact');
622 }
623 }
624 }
625
626 return array(
627 'success' => array(
628 'code' => 200,
629 'message' => 'Contact unlinked from order'
630 )
631 );
632 }
633
641 public function put($id, $request_data = null)
642 {
643 if (!DolibarrApiAccess::$user->rights->commande->creer) {
644 throw new RestException(401);
645 }
646
647 $result = $this->commande->fetch($id);
648 if (!$result) {
649 throw new RestException(404, 'Order not found');
650 }
651
652 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
653 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
654 }
655 foreach ($request_data as $field => $value) {
656 if ($field == 'id') {
657 continue;
658 }
659 if ($field === 'caller') {
660 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
661 $this->commande->context['caller'] = $request_data['caller'];
662 continue;
663 }
664
665 $this->commande->$field = $value;
666 }
667
668 // Update availability
669 if (!empty($this->commande->availability_id)) {
670 if ($this->commande->availability($this->commande->availability_id) < 0) {
671 throw new RestException(400, 'Error while updating availability');
672 }
673 }
674
675 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
676 return $this->get($id);
677 } else {
678 throw new RestException(500, $this->commande->error);
679 }
680 }
681
688 public function delete($id)
689 {
690 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
691 throw new RestException(401);
692 }
693 $result = $this->commande->fetch($id);
694 if (!$result) {
695 throw new RestException(404, 'Order not found');
696 }
697
698 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
699 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
700 }
701
702 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
703 throw new RestException(500, 'Error when deleting order : '.$this->commande->error);
704 }
705
706 return array(
707 'success' => array(
708 'code' => 200,
709 'message' => 'Order deleted'
710 )
711 );
712 }
713
736 public function validate($id, $idwarehouse = 0, $notrigger = 0)
737 {
738 if (!DolibarrApiAccess::$user->rights->commande->creer) {
739 throw new RestException(401);
740 }
741 $result = $this->commande->fetch($id);
742 if (!$result) {
743 throw new RestException(404, 'Order not found');
744 }
745
746 $result = $this->commande->fetch_thirdparty(); // do not check result, as failure is not fatal (used only for mail notification substitutes)
747
748 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
749 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
750 }
751
752 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
753 if ($result == 0) {
754 throw new RestException(304, 'Error nothing done. May be object is already validated');
755 }
756 if ($result < 0) {
757 throw new RestException(500, 'Error when validating Order: '.$this->commande->error);
758 }
759 $result = $this->commande->fetch($id);
760
761 $this->commande->fetchObjectLinked();
762
763 //fix #20477 : add online_payment_url
764 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
765 $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
766
767 return $this->_cleanObjectDatas($this->commande);
768 }
769
787 public function reopen($id)
788 {
789 if (!DolibarrApiAccess::$user->rights->commande->creer) {
790 throw new RestException(401);
791 }
792 if (empty($id)) {
793 throw new RestException(400, 'Order ID is mandatory');
794 }
795 $result = $this->commande->fetch($id);
796 if (!$result) {
797 throw new RestException(404, 'Order not found');
798 }
799
800 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
801 if ($result < 0) {
802 throw new RestException(405, $this->commande->error);
803 } elseif ($result == 0) {
804 throw new RestException(304);
805 }
806
807 return $result;
808 }
809
823 public function setinvoiced($id)
824 {
825 if (!DolibarrApiAccess::$user->rights->commande->creer) {
826 throw new RestException(401);
827 }
828 if (empty($id)) {
829 throw new RestException(400, 'Order ID is mandatory');
830 }
831 $result = $this->commande->fetch($id);
832 if (!$result) {
833 throw new RestException(404, 'Order not found');
834 }
835
836 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
837 if ($result < 0) {
838 throw new RestException(400, $this->commande->error);
839 }
840
841 $result = $this->commande->fetch($id);
842 if (!$result) {
843 throw new RestException(404, 'Order not found');
844 }
845
846 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
847 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
848 }
849
850 $this->commande->fetchObjectLinked();
851
852 return $this->_cleanObjectDatas($this->commande);
853 }
854
864 public function close($id, $notrigger = 0)
865 {
866 if (!DolibarrApiAccess::$user->rights->commande->creer) {
867 throw new RestException(401);
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(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
876 }
877
878 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
879 if ($result == 0) {
880 throw new RestException(304, 'Error nothing done. May be object is already closed');
881 }
882 if ($result < 0) {
883 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
884 }
885
886 $result = $this->commande->fetch($id);
887 if (!$result) {
888 throw new RestException(404, 'Order not found');
889 }
890
891 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
892 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
893 }
894
895 $this->commande->fetchObjectLinked();
896
897 return $this->_cleanObjectDatas($this->commande);
898 }
899
909 public function settodraft($id, $idwarehouse = -1)
910 {
911 if (!DolibarrApiAccess::$user->rights->commande->creer) {
912 throw new RestException(401);
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(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
921 }
922
923 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
924 if ($result == 0) {
925 throw new RestException(304, 'Nothing done. May be object is already closed');
926 }
927 if ($result < 0) {
928 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
929 }
930
931 $result = $this->commande->fetch($id);
932 if (!$result) {
933 throw new RestException(404, 'Order not found');
934 }
935
936 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
937 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
938 }
939
940 $this->commande->fetchObjectLinked();
941
942 return $this->_cleanObjectDatas($this->commande);
943 }
944
945
959 public function createOrderFromProposal($proposalid)
960 {
961 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
962
963 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
964 throw new RestException(401);
965 }
966 if (!DolibarrApiAccess::$user->rights->commande->creer) {
967 throw new RestException(401);
968 }
969 if (empty($proposalid)) {
970 throw new RestException(400, 'Proposal ID is mandatory');
971 }
972
973 $propal = new Propal($this->db);
974 $result = $propal->fetch($proposalid);
975 if (!$result) {
976 throw new RestException(404, 'Proposal not found');
977 }
978
979 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
980 if ($result < 0) {
981 throw new RestException(405, $this->commande->error);
982 }
983 $this->commande->fetchObjectLinked();
984
985 return $this->_cleanObjectDatas($this->commande);
986 }
987
1001 public function getOrderShipments($id)
1002 {
1003 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1004 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
1005 throw new RestException(401);
1006 }
1007 $obj_ret = array();
1008 $sql = "SELECT e.rowid";
1009 $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
1010 $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
1011 $sql .= " ON e.rowid = edet.fk_expedition";
1012 $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
1013 $sql .= " ON edet.fk_origin_line = cdet.rowid";
1014 $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
1015 $sql .= " ON cdet.fk_commande = c.rowid";
1016 $sql .= " WHERE c.rowid = ".((int) $id);
1017 $sql .= " GROUP BY e.rowid";
1018 $sql .= $this->db->order("e.rowid", "ASC");
1019
1020 dol_syslog("API Rest request");
1021 $result = $this->db->query($sql);
1022
1023 if ($result) {
1024 $num = $this->db->num_rows($result);
1025 if ($num <= 0) {
1026 throw new RestException(404, 'Shipments not found ');
1027 }
1028 $i = 0;
1029 while ($i < $num) {
1030 $obj = $this->db->fetch_object($result);
1031 $shipment_static = new Expedition($this->db);
1032 if ($shipment_static->fetch($obj->rowid)) {
1033 $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
1034 }
1035 $i++;
1036 }
1037 } else {
1038 throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
1039 }
1040 return $obj_ret;
1041 }
1042
1057 public function createOrderShipment($id, $warehouse_id)
1058 {
1059 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1060 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1061 throw new RestException(401);
1062 }
1063 if ($warehouse_id <= 0) {
1064 throw new RestException(404, 'Warehouse not found');
1065 }
1066 $result = $this->commande->fetch($id);
1067 if (!$result) {
1068 throw new RestException(404, 'Order not found');
1069 }
1070 $shipment = new Expedition($this->db);
1071 $shipment->socid = $this->commande->socid;
1072 $shipment->origin_id = $this->commande->id;
1073 $result = $shipment->create(DolibarrApiAccess::$user);
1074 if ($result <= 0) {
1075 throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
1076 }
1077 foreach ($this->commande->lines as $line) {
1078 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1079 if ($result <= 0) {
1080 throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
1081 }
1082 }
1083 return $shipment->id;
1084 }
1085
1086 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1093 protected function _cleanObjectDatas($object)
1094 {
1095 // phpcs:enable
1096 $object = parent::_cleanObjectDatas($object);
1097
1098 unset($object->note);
1099 unset($object->address);
1100 unset($object->barcode_type);
1101 unset($object->barcode_type_code);
1102 unset($object->barcode_type_label);
1103 unset($object->barcode_type_coder);
1104
1105 return $object;
1106 }
1107
1115 private function _validate($data)
1116 {
1117 $commande = array();
1118 foreach (Orders::$FIELDS as $field) {
1119 if (!isset($data[$field])) {
1120 throw new RestException(400, $field." field missing");
1121 }
1122 $commande[$field] = $data[$field];
1123 }
1124 return $commande;
1125 }
1126}
Class to manage customers orders.
Class for API REST v1.
Definition api.class.php:31
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Class to manage shipments.
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.