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 if ($field == 'array_options' && is_array($value)) {
666 foreach ($value as $index => $val) {
667 $this->commande->array_options[$index] = $this->_checkValForAPI($field, $val, $this->commande);
668 }
669 continue;
670 }
671 $this->commande->$field = $this->_checkValForAPI($field, $value, $this->commande);
672 }
673
674 // Update availability
675 if (!empty($this->commande->availability_id)) {
676 if ($this->commande->availability($this->commande->availability_id) < 0) {
677 throw new RestException(400, 'Error while updating availability');
678 }
679 }
680
681 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
682 return $this->get($id);
683 } else {
684 throw new RestException(500, $this->commande->error);
685 }
686 }
687
694 public function delete($id)
695 {
696 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
697 throw new RestException(401);
698 }
699 $result = $this->commande->fetch($id);
700 if (!$result) {
701 throw new RestException(404, 'Order not found');
702 }
703
704 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
705 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
706 }
707
708 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
709 throw new RestException(500, 'Error when deleting order : '.$this->commande->error);
710 }
711
712 return array(
713 'success' => array(
714 'code' => 200,
715 'message' => 'Order deleted'
716 )
717 );
718 }
719
742 public function validate($id, $idwarehouse = 0, $notrigger = 0)
743 {
744 if (!DolibarrApiAccess::$user->rights->commande->creer) {
745 throw new RestException(401);
746 }
747 $result = $this->commande->fetch($id);
748 if (!$result) {
749 throw new RestException(404, 'Order not found');
750 }
751
752 $result = $this->commande->fetch_thirdparty(); // do not check result, as failure is not fatal (used only for mail notification substitutes)
753
754 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
755 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
756 }
757
758 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
759 if ($result == 0) {
760 throw new RestException(304, 'Error nothing done. May be object is already validated');
761 }
762 if ($result < 0) {
763 throw new RestException(500, 'Error when validating Order: '.$this->commande->error);
764 }
765 $result = $this->commande->fetch($id);
766
767 $this->commande->fetchObjectLinked();
768
769 //fix #20477 : add online_payment_url
770 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
771 $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
772
773 return $this->_cleanObjectDatas($this->commande);
774 }
775
793 public function reopen($id)
794 {
795 if (!DolibarrApiAccess::$user->rights->commande->creer) {
796 throw new RestException(401);
797 }
798 if (empty($id)) {
799 throw new RestException(400, 'Order ID is mandatory');
800 }
801 $result = $this->commande->fetch($id);
802 if (!$result) {
803 throw new RestException(404, 'Order not found');
804 }
805
806 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
807 if ($result < 0) {
808 throw new RestException(405, $this->commande->error);
809 } elseif ($result == 0) {
810 throw new RestException(304);
811 }
812
813 return $result;
814 }
815
829 public function setinvoiced($id)
830 {
831 if (!DolibarrApiAccess::$user->rights->commande->creer) {
832 throw new RestException(401);
833 }
834 if (empty($id)) {
835 throw new RestException(400, 'Order ID is mandatory');
836 }
837 $result = $this->commande->fetch($id);
838 if (!$result) {
839 throw new RestException(404, 'Order not found');
840 }
841
842 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
843 if ($result < 0) {
844 throw new RestException(400, $this->commande->error);
845 }
846
847 $result = $this->commande->fetch($id);
848 if (!$result) {
849 throw new RestException(404, 'Order not found');
850 }
851
852 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
853 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
854 }
855
856 $this->commande->fetchObjectLinked();
857
858 return $this->_cleanObjectDatas($this->commande);
859 }
860
870 public function close($id, $notrigger = 0)
871 {
872 if (!DolibarrApiAccess::$user->rights->commande->creer) {
873 throw new RestException(401);
874 }
875 $result = $this->commande->fetch($id);
876 if (!$result) {
877 throw new RestException(404, 'Order not found');
878 }
879
880 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
881 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
882 }
883
884 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
885 if ($result == 0) {
886 throw new RestException(304, 'Error nothing done. May be object is already closed');
887 }
888 if ($result < 0) {
889 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
890 }
891
892 $result = $this->commande->fetch($id);
893 if (!$result) {
894 throw new RestException(404, 'Order not found');
895 }
896
897 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
898 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
899 }
900
901 $this->commande->fetchObjectLinked();
902
903 return $this->_cleanObjectDatas($this->commande);
904 }
905
915 public function settodraft($id, $idwarehouse = -1)
916 {
917 if (!DolibarrApiAccess::$user->rights->commande->creer) {
918 throw new RestException(401);
919 }
920 $result = $this->commande->fetch($id);
921 if (!$result) {
922 throw new RestException(404, 'Order not found');
923 }
924
925 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
926 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
927 }
928
929 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
930 if ($result == 0) {
931 throw new RestException(304, 'Nothing done. May be object is already closed');
932 }
933 if ($result < 0) {
934 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
935 }
936
937 $result = $this->commande->fetch($id);
938 if (!$result) {
939 throw new RestException(404, 'Order not found');
940 }
941
942 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
943 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
944 }
945
946 $this->commande->fetchObjectLinked();
947
948 return $this->_cleanObjectDatas($this->commande);
949 }
950
951
965 public function createOrderFromProposal($proposalid)
966 {
967 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
968
969 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
970 throw new RestException(401);
971 }
972 if (!DolibarrApiAccess::$user->rights->commande->creer) {
973 throw new RestException(401);
974 }
975 if (empty($proposalid)) {
976 throw new RestException(400, 'Proposal ID is mandatory');
977 }
978
979 $propal = new Propal($this->db);
980 $result = $propal->fetch($proposalid);
981 if (!$result) {
982 throw new RestException(404, 'Proposal not found');
983 }
984
985 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
986 if ($result < 0) {
987 throw new RestException(405, $this->commande->error);
988 }
989 $this->commande->fetchObjectLinked();
990
991 return $this->_cleanObjectDatas($this->commande);
992 }
993
1007 public function getOrderShipments($id)
1008 {
1009 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1010 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
1011 throw new RestException(401);
1012 }
1013 $obj_ret = array();
1014 $sql = "SELECT e.rowid";
1015 $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
1016 $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
1017 $sql .= " ON e.rowid = edet.fk_expedition";
1018 $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
1019 $sql .= " ON edet.fk_origin_line = cdet.rowid";
1020 $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
1021 $sql .= " ON cdet.fk_commande = c.rowid";
1022 $sql .= " WHERE c.rowid = ".((int) $id);
1023 $sql .= " GROUP BY e.rowid";
1024 $sql .= $this->db->order("e.rowid", "ASC");
1025
1026 dol_syslog("API Rest request");
1027 $result = $this->db->query($sql);
1028
1029 if ($result) {
1030 $num = $this->db->num_rows($result);
1031 if ($num <= 0) {
1032 throw new RestException(404, 'Shipments not found ');
1033 }
1034 $i = 0;
1035 while ($i < $num) {
1036 $obj = $this->db->fetch_object($result);
1037 $shipment_static = new Expedition($this->db);
1038 if ($shipment_static->fetch($obj->rowid)) {
1039 $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
1040 }
1041 $i++;
1042 }
1043 } else {
1044 throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
1045 }
1046 return $obj_ret;
1047 }
1048
1063 public function createOrderShipment($id, $warehouse_id)
1064 {
1065 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1066 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1067 throw new RestException(401);
1068 }
1069 if ($warehouse_id <= 0) {
1070 throw new RestException(404, 'Warehouse not found');
1071 }
1072 $result = $this->commande->fetch($id);
1073 if (!$result) {
1074 throw new RestException(404, 'Order not found');
1075 }
1076 $shipment = new Expedition($this->db);
1077 $shipment->socid = $this->commande->socid;
1078 $shipment->origin_id = $this->commande->id;
1079 $result = $shipment->create(DolibarrApiAccess::$user);
1080 if ($result <= 0) {
1081 throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
1082 }
1083 foreach ($this->commande->lines as $line) {
1084 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1085 if ($result <= 0) {
1086 throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
1087 }
1088 }
1089 return $shipment->id;
1090 }
1091
1092 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1099 protected function _cleanObjectDatas($object)
1100 {
1101 // phpcs:enable
1102 $object = parent::_cleanObjectDatas($object);
1103
1104 unset($object->note);
1105 unset($object->address);
1106 unset($object->barcode_type);
1107 unset($object->barcode_type_code);
1108 unset($object->barcode_type_label);
1109 unset($object->barcode_type_coder);
1110
1111 return $object;
1112 }
1113
1121 private function _validate($data)
1122 {
1123 $commande = array();
1124 foreach (Orders::$FIELDS as $field) {
1125 if (!isset($data[$field])) {
1126 throw new RestException(400, $field." field missing");
1127 }
1128 $commande[$field] = $data[$field];
1129 }
1130 return $commande;
1131 }
1132}
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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:85
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.