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