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