dolibarr  18.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 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
22 
29 class Orders extends DolibarrApi
30 {
34  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->rights->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 
164  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
165  {
166  global $db, $conf;
167 
168  if (!DolibarrApiAccess::$user->rights->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";
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 
216  $sql .= $this->db->order($sortfield, $sortorder);
217  if ($limit) {
218  if ($page < 0) {
219  $page = 0;
220  }
221  $offset = $limit * $page;
222 
223  $sql .= $this->db->plimit($limit + 1, $offset);
224  }
225 
226  dol_syslog("API Rest request");
227  $result = $this->db->query($sql);
228 
229  if ($result) {
230  $num = $this->db->num_rows($result);
231  $min = min($num, ($limit <= 0 ? $num : $limit));
232  $i = 0;
233  while ($i < $min) {
234  $obj = $this->db->fetch_object($result);
235  $commande_static = new Commande($this->db);
236  if ($commande_static->fetch($obj->rowid)) {
237  // Add external contacts ids
238  $tmparray = $commande_static->liste_contact(-1, 'external', 1);
239  if (is_array($tmparray)) {
240  $commande_static->contacts_ids = $tmparray;
241  }
242  // Add online_payment_url, cf #20477
243  require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
244  $commande_static->online_payment_url = getOnlinePaymentUrl(0, 'order', $commande_static->ref);
245 
246  $obj_ret[] = $this->_cleanObjectDatas($commande_static);
247  }
248  $i++;
249  }
250  } else {
251  throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
252  }
253  if (!count($obj_ret)) {
254  throw new RestException(404, 'No order found');
255  }
256  return $obj_ret;
257  }
258 
267  public function post($request_data = null)
268  {
269  if (!DolibarrApiAccess::$user->rights->commande->creer) {
270  throw new RestException(401, "Insuffisant rights");
271  }
272  // Check mandatory fields
273  $result = $this->_validate($request_data);
274 
275  foreach ($request_data as $field => $value) {
276  $this->commande->$field = $value;
277  }
278  /*if (isset($request_data["lines"])) {
279  $lines = array();
280  foreach ($request_data["lines"] as $line) {
281  array_push($lines, (object) $line);
282  }
283  $this->commande->lines = $lines;
284  }*/
285 
286  if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
287  throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
288  }
289 
290  return $this->commande->id;
291  }
292 
302  public function getLines($id)
303  {
304  if (!DolibarrApiAccess::$user->rights->commande->lire) {
305  throw new RestException(401);
306  }
307 
308  $result = $this->commande->fetch($id);
309  if (!$result) {
310  throw new RestException(404, 'Order not found');
311  }
312 
313  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
314  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
315  }
316  $this->commande->getLinesArray();
317  $result = array();
318  foreach ($this->commande->lines as $line) {
319  array_push($result, $this->_cleanObjectDatas($line));
320  }
321  return $result;
322  }
323 
334  public function postLine($id, $request_data = null)
335  {
336  if (!DolibarrApiAccess::$user->rights->commande->creer) {
337  throw new RestException(401);
338  }
339 
340  $result = $this->commande->fetch($id);
341  if (!$result) {
342  throw new RestException(404, 'Order not found');
343  }
344 
345  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
346  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
347  }
348 
349  $request_data = (object) $request_data;
350 
351  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
352  $request_data->label = sanitizeVal($request_data->label);
353 
354  $updateRes = $this->commande->addline(
355  $request_data->desc,
356  $request_data->subprice,
357  $request_data->qty,
358  $request_data->tva_tx,
359  $request_data->localtax1_tx,
360  $request_data->localtax2_tx,
361  $request_data->fk_product,
362  $request_data->remise_percent,
363  $request_data->info_bits,
364  $request_data->fk_remise_except,
365  $request_data->price_base_type ? $request_data->price_base_type : 'HT',
366  $request_data->subprice,
367  $request_data->date_start,
368  $request_data->date_end,
369  $request_data->product_type,
370  $request_data->rang,
371  $request_data->special_code,
372  $request_data->fk_parent_line,
373  $request_data->fk_fournprice,
374  $request_data->pa_ht,
375  $request_data->label,
376  $request_data->array_options,
377  $request_data->fk_unit,
378  $request_data->origin,
379  $request_data->origin_id,
380  $request_data->multicurrency_subprice,
381  $request_data->ref_ext
382  );
383 
384  if ($updateRes > 0) {
385  return $updateRes;
386  } else {
387  throw new RestException(400, $this->commande->error);
388  }
389  }
390 
401  public function putLine($id, $lineid, $request_data = null)
402  {
403  if (!DolibarrApiAccess::$user->rights->commande->creer) {
404  throw new RestException(401);
405  }
406 
407  $result = $this->commande->fetch($id);
408  if (!$result) {
409  throw new RestException(404, 'Order not found');
410  }
411 
412  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
413  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
414  }
415 
416  $request_data = (object) $request_data;
417 
418  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
419  $request_data->label = sanitizeVal($request_data->label);
420 
421  $updateRes = $this->commande->updateline(
422  $lineid,
423  $request_data->desc,
424  $request_data->subprice,
425  $request_data->qty,
426  $request_data->remise_percent,
427  $request_data->tva_tx,
428  $request_data->localtax1_tx,
429  $request_data->localtax2_tx,
430  $request_data->price_base_type ? $request_data->price_base_type : 'HT',
431  $request_data->info_bits,
432  $request_data->date_start,
433  $request_data->date_end,
434  $request_data->product_type,
435  $request_data->fk_parent_line,
436  0,
437  $request_data->fk_fournprice,
438  $request_data->pa_ht,
439  $request_data->label,
440  $request_data->special_code,
441  $request_data->array_options,
442  $request_data->fk_unit,
443  $request_data->multicurrency_subprice,
444  0,
445  $request_data->ref_ext,
446  $request_data->rang
447  );
448 
449  if ($updateRes > 0) {
450  $result = $this->get($id);
451  unset($result->line);
452  return $this->_cleanObjectDatas($result);
453  }
454  return false;
455  }
456 
469  public function deleteLine($id, $lineid)
470  {
471  if (!DolibarrApiAccess::$user->rights->commande->creer) {
472  throw new RestException(401);
473  }
474 
475  $result = $this->commande->fetch($id);
476  if (!$result) {
477  throw new RestException(404, 'Order not found');
478  }
479 
480  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
481  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
482  }
483 
484  $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid, $id);
485  if ($updateRes > 0) {
486  return $this->get($id);
487  } else {
488  throw new RestException(405, $this->commande->error);
489  }
490  }
491 
505  public function getContacts($id, $type = '')
506  {
507  if (!DolibarrApiAccess::$user->rights->commande->lire) {
508  throw new RestException(401);
509  }
510 
511  $result = $this->commande->fetch($id);
512  if (!$result) {
513  throw new RestException(404, 'Order not found');
514  }
515 
516  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
517  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
518  }
519 
520  $contacts = $this->commande->liste_contact(-1, 'external', 0, $type);
521 
522  return $this->_cleanObjectDatas($contacts);
523  }
524 
538  public function postContact($id, $contactid, $type)
539  {
540  if (!DolibarrApiAccess::$user->rights->commande->creer) {
541  throw new RestException(401);
542  }
543 
544  $result = $this->commande->fetch($id);
545  if (!$result) {
546  throw new RestException(404, 'Order not found');
547  }
548 
549  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
550  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
551  }
552 
553  $result = $this->commande->add_contact($contactid, $type, 'external');
554 
555  if ($result < 0) {
556  throw new RestException(500, 'Error when added the contact');
557  }
558 
559  if ($result == 0) {
560  throw new RestException(304, 'contact already added');
561  }
562 
563  return array(
564  'success' => array(
565  'code' => 200,
566  'message' => 'Contact linked to the order'
567  )
568  );
569  }
570 
586  public function deleteContact($id, $contactid, $type)
587  {
588  if (!DolibarrApiAccess::$user->rights->commande->creer) {
589  throw new RestException(401);
590  }
591 
592  $result = $this->commande->fetch($id);
593  if (!$result) {
594  throw new RestException(404, 'Order not found');
595  }
596 
597  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
598  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
599  }
600 
601  $contacts = $this->commande->liste_contact();
602 
603  foreach ($contacts as $contact) {
604  if ($contact['id'] == $contactid && $contact['code'] == $type) {
605  $result = $this->commande->delete_contact($contact['rowid']);
606 
607  if (!$result) {
608  throw new RestException(500, 'Error when deleted the contact');
609  }
610  }
611  }
612 
613  return array(
614  'success' => array(
615  'code' => 200,
616  'message' => 'Contact unlinked from order'
617  )
618  );
619  }
620 
628  public function put($id, $request_data = null)
629  {
630  if (!DolibarrApiAccess::$user->rights->commande->creer) {
631  throw new RestException(401);
632  }
633 
634  $result = $this->commande->fetch($id);
635  if (!$result) {
636  throw new RestException(404, 'Order not found');
637  }
638 
639  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
640  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
641  }
642  foreach ($request_data as $field => $value) {
643  if ($field == 'id') {
644  continue;
645  }
646  $this->commande->$field = $value;
647  }
648 
649  // Update availability
650  if (!empty($this->commande->availability_id)) {
651  if ($this->commande->availability($this->commande->availability_id) < 0) {
652  throw new RestException(400, 'Error while updating availability');
653  }
654  }
655 
656  if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
657  return $this->get($id);
658  } else {
659  throw new RestException(500, $this->commande->error);
660  }
661  }
662 
669  public function delete($id)
670  {
671  if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
672  throw new RestException(401);
673  }
674  $result = $this->commande->fetch($id);
675  if (!$result) {
676  throw new RestException(404, 'Order not found');
677  }
678 
679  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
680  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
681  }
682 
683  if (!$this->commande->delete(DolibarrApiAccess::$user)) {
684  throw new RestException(500, 'Error when deleting order : '.$this->commande->error);
685  }
686 
687  return array(
688  'success' => array(
689  'code' => 200,
690  'message' => 'Order deleted'
691  )
692  );
693  }
694 
716  public function validate($id, $idwarehouse = 0, $notrigger = 0)
717  {
718  if (!DolibarrApiAccess::$user->rights->commande->creer) {
719  throw new RestException(401);
720  }
721  $result = $this->commande->fetch($id);
722  if (!$result) {
723  throw new RestException(404, 'Order not found');
724  }
725 
726  $result = $this->commande->fetch_thirdparty(); // do not check result, as failure is not fatal (used only for mail notification substitutes)
727 
728  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
729  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
730  }
731 
732  $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
733  if ($result == 0) {
734  throw new RestException(304, 'Error nothing done. May be object is already validated');
735  }
736  if ($result < 0) {
737  throw new RestException(500, 'Error when validating Order: '.$this->commande->error);
738  }
739  $result = $this->commande->fetch($id);
740 
741  $this->commande->fetchObjectLinked();
742 
743  //fix #20477 : add online_payment_url
744  require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
745  $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
746 
747  return $this->_cleanObjectDatas($this->commande);
748  }
749 
767  public function reopen($id)
768  {
769  if (!DolibarrApiAccess::$user->rights->commande->creer) {
770  throw new RestException(401);
771  }
772  if (empty($id)) {
773  throw new RestException(400, 'Order ID is mandatory');
774  }
775  $result = $this->commande->fetch($id);
776  if (!$result) {
777  throw new RestException(404, 'Order not found');
778  }
779 
780  $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
781  if ($result < 0) {
782  throw new RestException(405, $this->commande->error);
783  } elseif ($result == 0) {
784  throw new RestException(304);
785  }
786 
787  return $result;
788  }
789 
803  public function setinvoiced($id)
804  {
805 
806  if (!DolibarrApiAccess::$user->rights->commande->creer) {
807  throw new RestException(401);
808  }
809  if (empty($id)) {
810  throw new RestException(400, 'Order ID is mandatory');
811  }
812  $result = $this->commande->fetch($id);
813  if (!$result) {
814  throw new RestException(404, 'Order not found');
815  }
816 
817  $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
818  if ($result < 0) {
819  throw new RestException(400, $this->commande->error);
820  }
821 
822  $result = $this->commande->fetch($id);
823  if (!$result) {
824  throw new RestException(404, 'Order not found');
825  }
826 
827  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
828  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
829  }
830 
831  $this->commande->fetchObjectLinked();
832 
833  return $this->_cleanObjectDatas($this->commande);
834  }
835 
845  public function close($id, $notrigger = 0)
846  {
847  if (!DolibarrApiAccess::$user->rights->commande->creer) {
848  throw new RestException(401);
849  }
850  $result = $this->commande->fetch($id);
851  if (!$result) {
852  throw new RestException(404, 'Order not found');
853  }
854 
855  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
856  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
857  }
858 
859  $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
860  if ($result == 0) {
861  throw new RestException(304, 'Error nothing done. May be object is already closed');
862  }
863  if ($result < 0) {
864  throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
865  }
866 
867  $result = $this->commande->fetch($id);
868  if (!$result) {
869  throw new RestException(404, 'Order not found');
870  }
871 
872  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
873  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
874  }
875 
876  $this->commande->fetchObjectLinked();
877 
878  return $this->_cleanObjectDatas($this->commande);
879  }
880 
890  public function settodraft($id, $idwarehouse = -1)
891  {
892  if (!DolibarrApiAccess::$user->rights->commande->creer) {
893  throw new RestException(401);
894  }
895  $result = $this->commande->fetch($id);
896  if (!$result) {
897  throw new RestException(404, 'Order not found');
898  }
899 
900  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
901  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
902  }
903 
904  $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
905  if ($result == 0) {
906  throw new RestException(304, 'Nothing done. May be object is already closed');
907  }
908  if ($result < 0) {
909  throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
910  }
911 
912  $result = $this->commande->fetch($id);
913  if (!$result) {
914  throw new RestException(404, 'Order not found');
915  }
916 
917  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
918  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
919  }
920 
921  $this->commande->fetchObjectLinked();
922 
923  return $this->_cleanObjectDatas($this->commande);
924  }
925 
926 
940  public function createOrderFromProposal($proposalid)
941  {
942 
943  require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
944 
945  if (!DolibarrApiAccess::$user->rights->propal->lire) {
946  throw new RestException(401);
947  }
948  if (!DolibarrApiAccess::$user->rights->commande->creer) {
949  throw new RestException(401);
950  }
951  if (empty($proposalid)) {
952  throw new RestException(400, 'Proposal ID is mandatory');
953  }
954 
955  $propal = new Propal($this->db);
956  $result = $propal->fetch($proposalid);
957  if (!$result) {
958  throw new RestException(404, 'Proposal not found');
959  }
960 
961  $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
962  if ($result < 0) {
963  throw new RestException(405, $this->commande->error);
964  }
965  $this->commande->fetchObjectLinked();
966 
967  return $this->_cleanObjectDatas($this->commande);
968  }
969 
983  public function getOrderShipments($id)
984  {
985  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
986  if (!DolibarrApiAccess::$user->rights->expedition->lire) {
987  throw new RestException(401);
988  }
989  $obj_ret = array();
990  $sql = "SELECT e.rowid";
991  $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
992  $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
993  $sql .= " ON e.rowid = edet.fk_expedition";
994  $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
995  $sql .= " ON edet.fk_origin_line = cdet.rowid";
996  $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
997  $sql .= " ON cdet.fk_commande = c.rowid";
998  $sql .= " WHERE c.rowid = ".((int) $id);
999  $sql .= " GROUP BY e.rowid";
1000  $sql .= $this->db->order("e.rowid", "ASC");
1001 
1002  dol_syslog("API Rest request");
1003  $result = $this->db->query($sql);
1004 
1005  if ($result) {
1006  $num = $this->db->num_rows($result);
1007  if ($num <= 0) {
1008  throw new RestException(404, 'Shipments not found ');
1009  }
1010  $i = 0;
1011  while ($i < $num) {
1012  $obj = $this->db->fetch_object($result);
1013  $shipment_static = new Expedition($this->db);
1014  if ($shipment_static->fetch($obj->rowid)) {
1015  $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
1016  }
1017  $i++;
1018  }
1019  } else {
1020  throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
1021  }
1022  return $obj_ret;
1023  }
1024 
1039  public function createOrderShipment($id, $warehouse_id)
1040  {
1041  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1042  if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1043  throw new RestException(401);
1044  }
1045  if ($warehouse_id <= 0) {
1046  throw new RestException(404, 'Warehouse not found');
1047  }
1048  $result = $this->commande->fetch($id);
1049  if (!$result) {
1050  throw new RestException(404, 'Order not found');
1051  }
1052  $shipment = new Expedition($this->db);
1053  $shipment->socid = $this->commande->socid;
1054  $shipment->origin_id = $this->commande->id;
1055  $result = $shipment->create(DolibarrApiAccess::$user);
1056  if ($result <= 0) {
1057  throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
1058  }
1059  foreach ($this->commande->lines as $line) {
1060  $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1061  if ($result <= 0) {
1062  throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
1063  }
1064  }
1065  return $shipment->id;
1066  }
1067 
1068  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1075  protected function _cleanObjectDatas($object)
1076  {
1077  // phpcs:enable
1078  $object = parent::_cleanObjectDatas($object);
1079 
1080  unset($object->note);
1081  unset($object->address);
1082  unset($object->barcode_type);
1083  unset($object->barcode_type_code);
1084  unset($object->barcode_type_label);
1085  unset($object->barcode_type_coder);
1086 
1087  return $object;
1088  }
1089 
1097  private function _validate($data)
1098  {
1099  $commande = array();
1100  foreach (Orders::$FIELDS as $field) {
1101  if (!isset($data[$field])) {
1102  throw new RestException(400, $field." field missing");
1103  }
1104  $commande[$field] = $data[$field];
1105  }
1106  return $commande;
1107  }
1108 }
Orders\deleteLine
deleteLine($id, $lineid)
Delete a line of a given order.
Definition: api_orders.class.php:469
Orders\post
post($request_data=null)
Create a sale order.
Definition: api_orders.class.php:267
db
$conf db
API class for accounts.
Definition: inc.php:41
Orders\__construct
__construct()
Constructor.
Definition: api_orders.class.php:47
Expedition
Class to manage shipments.
Definition: expedition.class.php:52
Orders\setinvoiced
setinvoiced($id)
Classify the order as invoiced.
Definition: api_orders.class.php:803
Orders\postContact
postContact($id, $contactid, $type)
Add a contact type of given order.
Definition: api_orders.class.php:538
$sql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:745
Orders\getContacts
getContacts($id, $type='')
Get contacts of given order.
Definition: api_orders.class.php:505
Orders\postLine
postLine($id, $request_data=null)
Add a line to given order.
Definition: api_orders.class.php:334
sanitizeVal
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
Definition: functions.lib.php:877
DolibarrApi\_checkAccessToResource
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:283
DolibarrApi
Class for API REST v1.
Definition: api.class.php:30
Orders\getByRefExt
getByRefExt($ref_ext, $contact_list=1)
Get properties of an order object by ref_ext.
Definition: api_orders.class.php:101
Orders\createOrderShipment
createOrderShipment($id, $warehouse_id)
Create the shipment of an order.
Definition: api_orders.class.php:1039
Orders\getByRef
getByRef($ref, $contact_list=1)
Get properties of an order object by ref.
Definition: api_orders.class.php:83
Orders\settodraft
settodraft($id, $idwarehouse=-1)
Set an order to draft.
Definition: api_orders.class.php:890
Orders\_fetch
_fetch($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an order object.
Definition: api_orders.class.php:119
Commande
Class to manage customers orders.
Definition: commande.class.php:46
Orders\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_orders.class.php:1097
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1639
forgeSQLFromUniversalSearchCriteria
forgeSQLFromUniversalSearchCriteria($filter, &$error='')
forgeSQLFromUniversalSearchCriteria
Definition: functions.lib.php:11589
Orders\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List orders.
Definition: api_orders.class.php:164
Orders\deleteContact
deleteContact($id, $contactid, $type)
Unlink a contact type of given order.
Definition: api_orders.class.php:586
Orders\reopen
reopen($id)
Tag the order as validated (opened)
Definition: api_orders.class.php:767
Orders\getLines
getLines($id)
Get lines of an order.
Definition: api_orders.class.php:302
Orders\close
close($id, $notrigger=0)
Close an order (Classify it as "Delivered")
Definition: api_orders.class.php:845
Orders\getOrderShipments
getOrderShipments($id)
Get the shipments of an order.
Definition: api_orders.class.php:983
Orders\createOrderFromProposal
createOrderFromProposal($proposalid)
Create an order using an existing proposal.
Definition: api_orders.class.php:940
Propal
Class to manage proposals.
Definition: propal.class.php:52
Orders
Definition: api_orders.class.php:29
Orders\_cleanObjectDatas
_cleanObjectDatas($object)
Clean sensible object datas.
Definition: api_orders.class.php:1075
Orders\put
put($id, $request_data=null)
Update order general fields (won't touch lines of order)
Definition: api_orders.class.php:628
Orders\putLine
putLine($id, $lineid, $request_data=null)
Update a line to given order.
Definition: api_orders.class.php:401
Orders\validate
validate($id, $idwarehouse=0, $notrigger=0)
Validate an order.
Definition: api_orders.class.php:716