dolibarr 21.0.0-alpha
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
167 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $sqlfilterlines = '', $properties = '', $pagination_data = false)
168 {
169 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
170 throw new RestException(403);
171 }
172
173 $obj_ret = array();
174
175 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
176 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
177
178 // If the internal user must only see his customers, force searching by him
179 $search_sale = 0;
180 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
181 $search_sale = DolibarrApiAccess::$user->id;
182 }
183
184 $sql = "SELECT t.rowid";
185 $sql .= " FROM ".MAIN_DB_PREFIX."commande AS t";
186 $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
187 $sql .= ' WHERE t.entity IN ('.getEntity('commande').')';
188 if ($socids) {
189 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
190 }
191 // Search on sale representative
192 if ($search_sale && $search_sale != '-1') {
193 if ($search_sale == -2) {
194 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
195 } elseif ($search_sale > 0) {
196 $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).")";
197 }
198 }
199 // Add sql filters
200 if ($sqlfilters) {
201 $errormessage = '';
202 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
203 if ($errormessage) {
204 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
205 }
206 }
207 // Add sql filters for lines
208 if ($sqlfilterlines) {
209 $errormessage = '';
210 $sql .= " AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX."commandedet AS tl WHERE tl.fk_commande = t.rowid";
211 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilterlines, $errormessage);
212 $sql .= ")";
213 if ($errormessage) {
214 throw new RestException(400, 'Error when validating parameter sqlfilterlines -> '.$errormessage);
215 }
216 }
217
218 //this query will return total orders with the filters given
219 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
220
221 $sql .= $this->db->order($sortfield, $sortorder);
222 if ($limit) {
223 if ($page < 0) {
224 $page = 0;
225 }
226 $offset = $limit * $page;
227
228 $sql .= $this->db->plimit($limit + 1, $offset);
229 }
230
231 dol_syslog("API Rest request");
232 $result = $this->db->query($sql);
233
234 if ($result) {
235 $num = $this->db->num_rows($result);
236 $min = min($num, ($limit <= 0 ? $num : $limit));
237 $i = 0;
238 while ($i < $min) {
239 $obj = $this->db->fetch_object($result);
240 $commande_static = new Commande($this->db);
241 if ($commande_static->fetch($obj->rowid)) {
242 // Add external contacts ids
243 $tmparray = $commande_static->liste_contact(-1, 'external', 1);
244 if (is_array($tmparray)) {
245 $commande_static->contacts_ids = $tmparray;
246 }
247 // Add online_payment_url, cf #20477
248 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
249 $commande_static->online_payment_url = getOnlinePaymentUrl(0, 'order', $commande_static->ref);
250
251 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($commande_static), $properties);
252 }
253 $i++;
254 }
255 } else {
256 throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
257 }
258
259 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
260 if ($pagination_data) {
261 $totalsResult = $this->db->query($sqlTotals);
262 $total = $this->db->fetch_object($totalsResult)->total;
263
264 $tmp = $obj_ret;
265 $obj_ret = [];
266
267 $obj_ret['data'] = $tmp;
268 $obj_ret['pagination'] = [
269 'total' => (int) $total,
270 'page' => $page, //count starts from 0
271 'page_count' => ceil((int) $total / $limit),
272 'limit' => $limit
273 ];
274 }
275
276 return $obj_ret;
277 }
278
287 public function post($request_data = null)
288 {
289 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
290 throw new RestException(403, "Insuffisant rights");
291 }
292 // Check mandatory fields
293 $result = $this->_validate($request_data);
294
295 foreach ($request_data as $field => $value) {
296 if ($field === 'caller') {
297 // 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
298 $this->commande->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
299 continue;
300 }
301
302 $this->commande->$field = $this->_checkValForAPI($field, $value, $this->commande);
303 }
304 /*if (isset($request_data["lines"])) {
305 $lines = array();
306 foreach ($request_data["lines"] as $line) {
307 array_push($lines, (object) $line);
308 }
309 $this->commande->lines = $lines;
310 }*/
311
312 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
313 throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
314 }
315
316 return ((int) $this->commande->id);
317 }
318
328 public function getLines($id)
329 {
330 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
331 throw new RestException(403);
332 }
333
334 $result = $this->commande->fetch($id);
335 if (!$result) {
336 throw new RestException(404, 'Order not found');
337 }
338
339 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
340 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
341 }
342 $this->commande->getLinesArray();
343 $result = array();
344 foreach ($this->commande->lines as $line) {
345 array_push($result, $this->_cleanObjectDatas($line));
346 }
347 return $result;
348 }
349
360 public function postLine($id, $request_data = null)
361 {
362 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
363 throw new RestException(403);
364 }
365
366 $result = $this->commande->fetch($id);
367 if (!$result) {
368 throw new RestException(404, 'Order not found');
369 }
370
371 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
372 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
373 }
374
375 $request_data = (object) $request_data;
376
377 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
378 $request_data->label = sanitizeVal($request_data->label);
379
380 $updateRes = $this->commande->addline(
381 $request_data->desc,
382 $request_data->subprice,
383 $request_data->qty,
384 $request_data->tva_tx,
385 $request_data->localtax1_tx,
386 $request_data->localtax2_tx,
387 $request_data->fk_product,
388 $request_data->remise_percent,
389 $request_data->info_bits,
390 $request_data->fk_remise_except,
391 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
392 $request_data->subprice,
393 $request_data->date_start,
394 $request_data->date_end,
395 $request_data->product_type,
396 $request_data->rang,
397 $request_data->special_code,
398 $request_data->fk_parent_line,
399 $request_data->fk_fournprice,
400 $request_data->pa_ht,
401 $request_data->label,
402 $request_data->array_options,
403 $request_data->fk_unit,
404 $request_data->origin,
405 $request_data->origin_id,
406 $request_data->multicurrency_subprice,
407 $request_data->ref_ext
408 );
409
410 if ($updateRes > 0) {
411 return $updateRes;
412 } else {
413 throw new RestException(400, $this->commande->error);
414 }
415 }
416
427 public function putLine($id, $lineid, $request_data = null)
428 {
429 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
430 throw new RestException(403);
431 }
432
433 $result = $this->commande->fetch($id);
434 if (!$result) {
435 throw new RestException(404, 'Order not found');
436 }
437
438 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
439 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
440 }
441
442 $request_data = (object) $request_data;
443
444 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
445 $request_data->label = sanitizeVal($request_data->label);
446
447 $updateRes = $this->commande->updateline(
448 $lineid,
449 $request_data->desc,
450 $request_data->subprice,
451 $request_data->qty,
452 $request_data->remise_percent,
453 $request_data->tva_tx,
454 $request_data->localtax1_tx,
455 $request_data->localtax2_tx,
456 $request_data->price_base_type ? $request_data->price_base_type : 'HT',
457 $request_data->info_bits,
458 $request_data->date_start,
459 $request_data->date_end,
460 $request_data->product_type,
461 $request_data->fk_parent_line,
462 0,
463 $request_data->fk_fournprice,
464 $request_data->pa_ht,
465 $request_data->label,
466 $request_data->special_code,
467 $request_data->array_options,
468 $request_data->fk_unit,
469 $request_data->multicurrency_subprice,
470 0,
471 $request_data->ref_ext,
472 $request_data->rang
473 );
474
475 if ($updateRes > 0) {
476 $result = $this->get($id);
477 unset($result->line);
478 return $this->_cleanObjectDatas($result);
479 }
480 return false;
481 }
482
495 public function deleteLine($id, $lineid)
496 {
497 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
498 throw new RestException(403);
499 }
500
501 $result = $this->commande->fetch($id);
502 if (!$result) {
503 throw new RestException(404, 'Order not found');
504 }
505
506 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
507 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
508 }
509
510 $updateRes = $this->commande->deleteLine(DolibarrApiAccess::$user, $lineid, $id);
511 if ($updateRes > 0) {
512 return $this->get($id);
513 } else {
514 throw new RestException(405, $this->commande->error);
515 }
516 }
517
531 public function getContacts($id, $type = '')
532 {
533 if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
534 throw new RestException(403);
535 }
536
537 $result = $this->commande->fetch($id);
538 if (!$result) {
539 throw new RestException(404, 'Order not found');
540 }
541
542 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
543 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
544 }
545
546 $contacts = $this->commande->liste_contact(-1, 'external', 0, $type);
547
548 return $this->_cleanObjectDatas($contacts);
549 }
550
564 public function postContact($id, $contactid, $type)
565 {
566 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
567 throw new RestException(403);
568 }
569
570 $result = $this->commande->fetch($id);
571 if (!$result) {
572 throw new RestException(404, 'Order not found');
573 }
574
575 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
576 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
577 }
578
579 $result = $this->commande->add_contact($contactid, $type, 'external');
580
581 if ($result < 0) {
582 throw new RestException(500, 'Error when added the contact');
583 }
584
585 if ($result == 0) {
586 throw new RestException(304, 'contact already added');
587 }
588
589 return array(
590 'success' => array(
591 'code' => 200,
592 'message' => 'Contact linked to the order'
593 )
594 );
595 }
596
612 public function deleteContact($id, $contactid, $type)
613 {
614 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
615 throw new RestException(403);
616 }
617
618 $result = $this->commande->fetch($id);
619 if (!$result) {
620 throw new RestException(404, 'Order not found');
621 }
622
623 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
624 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
625 }
626
627 $contacts = $this->commande->liste_contact();
628
629 foreach ($contacts as $contact) {
630 if ($contact['id'] == $contactid && $contact['code'] == $type) {
631 $result = $this->commande->delete_contact($contact['rowid']);
632
633 if (!$result) {
634 throw new RestException(500, 'Error when deleted the contact');
635 }
636 }
637 }
638
639 return array(
640 'success' => array(
641 'code' => 200,
642 'message' => 'Contact unlinked from order'
643 )
644 );
645 }
646
654 public function put($id, $request_data = null)
655 {
656 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
657 throw new RestException(403);
658 }
659
660 $result = $this->commande->fetch($id);
661 if (!$result) {
662 throw new RestException(404, 'Order not found');
663 }
664
665 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
666 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
667 }
668 foreach ($request_data as $field => $value) {
669 if ($field == 'id') {
670 continue;
671 }
672 if ($field === 'caller') {
673 // 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
674 $this->commande->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
675 continue;
676 }
677 if ($field == 'array_options' && is_array($value)) {
678 foreach ($value as $index => $val) {
679 $this->commande->array_options[$index] = $this->_checkValForAPI($field, $val, $this->commande);
680 }
681 continue;
682 }
683
684 $this->commande->$field = $this->_checkValForAPI($field, $value, $this->commande);
685 }
686
687 // Update availability
688 if (!empty($this->commande->availability_id)) {
689 if ($this->commande->availability($this->commande->availability_id) < 0) {
690 throw new RestException(400, 'Error while updating availability');
691 }
692 }
693
694 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
695 return $this->get($id);
696 } else {
697 throw new RestException(500, $this->commande->error);
698 }
699 }
700
707 public function delete($id)
708 {
709 if (!DolibarrApiAccess::$user->hasRight('commande', 'supprimer')) {
710 throw new RestException(403);
711 }
712 $result = $this->commande->fetch($id);
713 if (!$result) {
714 throw new RestException(404, 'Order not found');
715 }
716
717 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
718 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
719 }
720
721 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
722 throw new RestException(500, 'Error when deleting order : '.$this->commande->error);
723 }
724
725 return array(
726 'success' => array(
727 'code' => 200,
728 'message' => 'Order deleted'
729 )
730 );
731 }
732
755 public function validate($id, $idwarehouse = 0, $notrigger = 0)
756 {
757 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
758 throw new RestException(403);
759 }
760 $result = $this->commande->fetch($id);
761 if (!$result) {
762 throw new RestException(404, 'Order not found');
763 }
764
765 $result = $this->commande->fetch_thirdparty(); // do not check result, as failure is not fatal (used only for mail notification substitutes)
766
767 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
768 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
769 }
770
771 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
772 if ($result == 0) {
773 throw new RestException(304, 'Error nothing done. May be object is already validated');
774 }
775 if ($result < 0) {
776 throw new RestException(500, 'Error when validating Order: '.$this->commande->error);
777 }
778 $result = $this->commande->fetch($id);
779
780 $this->commande->fetchObjectLinked();
781
782 //fix #20477 : add online_payment_url
783 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
784 $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
785
786 return $this->_cleanObjectDatas($this->commande);
787 }
788
806 public function reopen($id)
807 {
808 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
809 throw new RestException(403);
810 }
811 if (empty($id)) {
812 throw new RestException(400, 'Order ID is mandatory');
813 }
814 $result = $this->commande->fetch($id);
815 if (!$result) {
816 throw new RestException(404, 'Order not found');
817 }
818
819 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
820 if ($result < 0) {
821 throw new RestException(405, $this->commande->error);
822 } elseif ($result == 0) {
823 throw new RestException(304);
824 }
825
826 return $result;
827 }
828
842 public function setinvoiced($id)
843 {
844 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
845 throw new RestException(403);
846 }
847 if (empty($id)) {
848 throw new RestException(400, 'Order ID is mandatory');
849 }
850 $result = $this->commande->fetch($id);
851 if (!$result) {
852 throw new RestException(404, 'Order not found');
853 }
854
855 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
856 if ($result < 0) {
857 throw new RestException(400, $this->commande->error);
858 }
859
860 $result = $this->commande->fetch($id);
861 if (!$result) {
862 throw new RestException(404, 'Order not found');
863 }
864
865 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
866 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
867 }
868
869 $this->commande->fetchObjectLinked();
870
871 return $this->_cleanObjectDatas($this->commande);
872 }
873
883 public function close($id, $notrigger = 0)
884 {
885 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
886 throw new RestException(403);
887 }
888 $result = $this->commande->fetch($id);
889 if (!$result) {
890 throw new RestException(404, 'Order not found');
891 }
892
893 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
894 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
895 }
896
897 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
898 if ($result == 0) {
899 throw new RestException(304, 'Error nothing done. May be object is already closed');
900 }
901 if ($result < 0) {
902 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
903 }
904
905 $result = $this->commande->fetch($id);
906 if (!$result) {
907 throw new RestException(404, 'Order not found');
908 }
909
910 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
911 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
912 }
913
914 $this->commande->fetchObjectLinked();
915
916 return $this->_cleanObjectDatas($this->commande);
917 }
918
928 public function settodraft($id, $idwarehouse = -1)
929 {
930 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
931 throw new RestException(403);
932 }
933 $result = $this->commande->fetch($id);
934 if (!$result) {
935 throw new RestException(404, 'Order not found');
936 }
937
938 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
939 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
940 }
941
942 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
943 if ($result == 0) {
944 throw new RestException(304, 'Nothing done. May be object is already closed');
945 }
946 if ($result < 0) {
947 throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
948 }
949
950 $result = $this->commande->fetch($id);
951 if (!$result) {
952 throw new RestException(404, 'Order not found');
953 }
954
955 if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
956 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
957 }
958
959 $this->commande->fetchObjectLinked();
960
961 return $this->_cleanObjectDatas($this->commande);
962 }
963
964
978 public function createOrderFromProposal($proposalid)
979 {
980 require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
981
982 if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
983 throw new RestException(403);
984 }
985 if (!DolibarrApiAccess::$user->hasRight('commande', 'creer')) {
986 throw new RestException(403);
987 }
988 if (empty($proposalid)) {
989 throw new RestException(400, 'Proposal ID is mandatory');
990 }
991
992 $propal = new Propal($this->db);
993 $result = $propal->fetch($proposalid);
994 if (!$result) {
995 throw new RestException(404, 'Proposal not found');
996 }
997
998 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
999 if ($result < 0) {
1000 throw new RestException(405, $this->commande->error);
1001 }
1002 $this->commande->fetchObjectLinked();
1003
1004 return $this->_cleanObjectDatas($this->commande);
1005 }
1006
1020 public function getOrderShipments($id)
1021 {
1022 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1023 if (!DolibarrApiAccess::$user->hasRight('expedition', 'lire')) {
1024 throw new RestException(403);
1025 }
1026 $obj_ret = array();
1027 $sql = "SELECT e.rowid";
1028 $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
1029 $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
1030 $sql .= " ON e.rowid = edet.fk_expedition";
1031 $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
1032 $sql .= " ON edet.fk_elementdet = cdet.rowid";
1033 $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
1034 $sql .= " ON cdet.fk_commande = c.rowid";
1035 $sql .= " WHERE c.rowid = ".((int) $id);
1036 $sql .= " GROUP BY e.rowid";
1037 $sql .= $this->db->order("e.rowid", "ASC");
1038
1039 dol_syslog("API Rest request");
1040 $result = $this->db->query($sql);
1041
1042 if ($result) {
1043 $num = $this->db->num_rows($result);
1044 if ($num <= 0) {
1045 throw new RestException(404, 'Shipments not found ');
1046 }
1047 $i = 0;
1048 while ($i < $num) {
1049 $obj = $this->db->fetch_object($result);
1050 $shipment_static = new Expedition($this->db);
1051 if ($shipment_static->fetch($obj->rowid)) {
1052 $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
1053 }
1054 $i++;
1055 }
1056 } else {
1057 throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
1058 }
1059 return $obj_ret;
1060 }
1061
1076 public function createOrderShipment($id, $warehouse_id)
1077 {
1078 require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1079 if (!DolibarrApiAccess::$user->hasRight('expedition', 'creer')) {
1080 throw new RestException(403);
1081 }
1082 if ($warehouse_id <= 0) {
1083 throw new RestException(404, 'Warehouse not found');
1084 }
1085 $result = $this->commande->fetch($id);
1086 if (!$result) {
1087 throw new RestException(404, 'Order not found');
1088 }
1089 $shipment = new Expedition($this->db);
1090 $shipment->socid = $this->commande->socid;
1091 $shipment->origin_id = $this->commande->id;
1092 $result = $shipment->create(DolibarrApiAccess::$user);
1093 if ($result <= 0) {
1094 throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
1095 }
1096 foreach ($this->commande->lines as $line) {
1097 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1098 if ($result <= 0) {
1099 throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
1100 }
1101 }
1102 return $shipment->id;
1103 }
1104
1105 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1112 protected function _cleanObjectDatas($object)
1113 {
1114 // phpcs:enable
1115 $object = parent::_cleanObjectDatas($object);
1116
1117 unset($object->note);
1118 unset($object->address);
1119 unset($object->barcode_type);
1120 unset($object->barcode_type_code);
1121 unset($object->barcode_type_label);
1122 unset($object->barcode_type_coder);
1123
1124 return $object;
1125 }
1126
1134 private function _validate($data)
1135 {
1136 $commande = array();
1137 foreach (Orders::$FIELDS as $field) {
1138 if (!isset($data[$field])) {
1139 throw new RestException(400, $field." field missing");
1140 }
1141 $commande[$field] = $data[$field];
1142 }
1143 return $commande;
1144 }
1145}
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='', $pagination_data=false)
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.