dolibarr  17.0.4
api_orders.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3  * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
19 use Luracast\Restler\RestException;
20 
21 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
22 
29 class Orders extends DolibarrApi
30 {
31 
35  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('', $ref, '', $contact_list);
87  }
88 
102  public function getByRefExt($ref_ext, $contact_list = 1)
103  {
104  return $this->_fetch('', '', $ref_ext, $contact_list);
105  }
106 
120  private function _fetch($id, $ref = '', $ref_ext = '', $contact_list = 1)
121  {
122  if (!DolibarrApiAccess::$user->rights->commande->lire) {
123  throw new RestException(401);
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(401, '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 
165  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
166  {
167  global $db, $conf;
168 
169  if (!DolibarrApiAccess::$user->rights->commande->lire) {
170  throw new RestException(401);
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->rights->societe->client->voir && !$socids) {
181  $search_sale = DolibarrApiAccess::$user->id;
182  }
183 
184  $sql = "SELECT t.rowid";
185  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
186  $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)
187  }
188  $sql .= " FROM ".MAIN_DB_PREFIX."commande as t";
189 
190  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
191  $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
192  }
193 
194  $sql .= ' WHERE t.entity IN ('.getEntity('commande').')';
195  if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
196  $sql .= " AND t.fk_soc = sc.fk_soc";
197  }
198  if ($socids) {
199  $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
200  }
201  if ($search_sale > 0) {
202  $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
203  }
204  // Insert sale filter
205  if ($search_sale > 0) {
206  $sql .= " AND sc.fk_user = ".((int) $search_sale);
207  }
208  // Add sql filters
209  if ($sqlfilters) {
210  $errormessage = '';
211  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
212  if ($errormessage) {
213  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
214  }
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 
403  public function putLine($id, $lineid, $request_data = null)
404  {
405  if (!DolibarrApiAccess::$user->rights->commande->creer) {
406  throw new RestException(401);
407  }
408 
409  $result = $this->commande->fetch($id);
410  if (!$result) {
411  throw new RestException(404, 'Order not found');
412  }
413 
414  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
415  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
416  }
417 
418  $request_data = (object) $request_data;
419 
420  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
421  $request_data->label = sanitizeVal($request_data->label);
422 
423  $updateRes = $this->commande->updateline(
424  $lineid,
425  $request_data->desc,
426  $request_data->subprice,
427  $request_data->qty,
428  $request_data->remise_percent,
429  $request_data->tva_tx,
430  $request_data->localtax1_tx,
431  $request_data->localtax2_tx,
432  $request_data->price_base_type ? $request_data->price_base_type : 'HT',
433  $request_data->info_bits,
434  $request_data->date_start,
435  $request_data->date_end,
436  $request_data->product_type,
437  $request_data->fk_parent_line,
438  0,
439  $request_data->fk_fournprice,
440  $request_data->pa_ht,
441  $request_data->label,
442  $request_data->special_code,
443  $request_data->array_options,
444  $request_data->fk_unit,
445  $request_data->multicurrency_subprice,
446  0,
447  $request_data->ref_ext,
448  $request_data->rang
449  );
450 
451  if ($updateRes > 0) {
452  $result = $this->get($id);
453  unset($result->line);
454  return $this->_cleanObjectDatas($result);
455  }
456  return false;
457  }
458 
473  public function deleteLine($id, $lineid)
474  {
475  if (!DolibarrApiAccess::$user->rights->commande->creer) {
476  throw new RestException(401);
477  }
478 
479  $result = $this->commande->fetch($id);
480  if (!$result) {
481  throw new RestException(404, 'Order not found');
482  }
483 
484  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
485  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
486  }
487 
488  // TODO Check the lineid $lineid is a line of ojbect
489 
490  $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid);
491  if ($updateRes > 0) {
492  return $this->get($id);
493  } else {
494  throw new RestException(405, $this->commande->error);
495  }
496  }
497 
512  public function getContacts($id, $type = '')
513  {
514  if (!DolibarrApiAccess::$user->rights->commande->lire) {
515  throw new RestException(401);
516  }
517 
518  $result = $this->commande->fetch($id);
519  if (!$result) {
520  throw new RestException(404, 'Order not found');
521  }
522 
523  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
524  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
525  }
526 
527  $contacts = $this->commande->liste_contact(-1, 'external', 0, $type);
528 
529  return $this->_cleanObjectDatas($contacts);
530  }
531 
546  public function postContact($id, $contactid, $type)
547  {
548  if (!DolibarrApiAccess::$user->rights->commande->creer) {
549  throw new RestException(401);
550  }
551 
552  $result = $this->commande->fetch($id);
553  if (!$result) {
554  throw new RestException(404, 'Order not found');
555  }
556 
557  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
558  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
559  }
560 
561  $result = $this->commande->add_contact($contactid, $type, 'external');
562 
563  if ($result < 0) {
564  throw new RestException(500, 'Error when added the contact');
565  }
566 
567  if ($result == 0) {
568  throw new RestException(304, 'contact already added');
569  }
570 
571  return array(
572  'success' => array(
573  'code' => 200,
574  'message' => 'Contact linked to the order'
575  )
576  );
577  }
578 
594  public function deleteContact($id, $contactid, $type)
595  {
596  if (!DolibarrApiAccess::$user->rights->commande->creer) {
597  throw new RestException(401);
598  }
599 
600  $result = $this->commande->fetch($id);
601  if (!$result) {
602  throw new RestException(404, 'Order not found');
603  }
604 
605  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
606  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
607  }
608 
609  $contacts = $this->commande->liste_contact();
610 
611  foreach ($contacts as $contact) {
612  if ($contact['id'] == $contactid && $contact['code'] == $type) {
613  $result = $this->commande->delete_contact($contact['rowid']);
614 
615  if (!$result) {
616  throw new RestException(500, 'Error when deleted the contact');
617  }
618  }
619  }
620 
621  return array(
622  'success' => array(
623  'code' => 200,
624  'message' => 'Contact unlinked from order'
625  )
626  );
627  }
628 
637  public function put($id, $request_data = null)
638  {
639  if (!DolibarrApiAccess::$user->rights->commande->creer) {
640  throw new RestException(401);
641  }
642 
643  $result = $this->commande->fetch($id);
644  if (!$result) {
645  throw new RestException(404, 'Order not found');
646  }
647 
648  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
649  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
650  }
651  foreach ($request_data as $field => $value) {
652  if ($field == 'id') {
653  continue;
654  }
655  $this->commande->$field = $value;
656  }
657 
658  // Update availability
659  if (!empty($this->commande->availability_id)) {
660  if ($this->commande->availability($this->commande->availability_id) < 0) {
661  throw new RestException(400, 'Error while updating availability');
662  }
663  }
664 
665  if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
666  return $this->get($id);
667  } else {
668  throw new RestException(500, $this->commande->error);
669  }
670  }
671 
678  public function delete($id)
679  {
680  if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
681  throw new RestException(401);
682  }
683  $result = $this->commande->fetch($id);
684  if (!$result) {
685  throw new RestException(404, 'Order not found');
686  }
687 
688  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
689  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
690  }
691 
692  if (!$this->commande->delete(DolibarrApiAccess::$user)) {
693  throw new RestException(500, 'Error when deleting order : '.$this->commande->error);
694  }
695 
696  return array(
697  'success' => array(
698  'code' => 200,
699  'message' => 'Order deleted'
700  )
701  );
702  }
703 
726  public function validate($id, $idwarehouse = 0, $notrigger = 0)
727  {
728  if (!DolibarrApiAccess::$user->rights->commande->creer) {
729  throw new RestException(401);
730  }
731  $result = $this->commande->fetch($id);
732  if (!$result) {
733  throw new RestException(404, 'Order not found');
734  }
735 
736  $result = $this->commande->fetch_thirdparty(); // do not check result, as failure is not fatal (used only for mail notification substitutes)
737 
738  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
739  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
740  }
741 
742  $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
743  if ($result == 0) {
744  throw new RestException(304, 'Error nothing done. May be object is already validated');
745  }
746  if ($result < 0) {
747  throw new RestException(500, 'Error when validating Order: '.$this->commande->error);
748  }
749  $result = $this->commande->fetch($id);
750 
751  $this->commande->fetchObjectLinked();
752 
753  //fix #20477 : add online_payment_url
754  require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
755  $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
756 
757  return $this->_cleanObjectDatas($this->commande);
758  }
759 
777  public function reopen($id)
778  {
779 
780  if (!DolibarrApiAccess::$user->rights->commande->creer) {
781  throw new RestException(401);
782  }
783  if (empty($id)) {
784  throw new RestException(400, 'Order ID is mandatory');
785  }
786  $result = $this->commande->fetch($id);
787  if (!$result) {
788  throw new RestException(404, 'Order not found');
789  }
790 
791  $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
792  if ($result < 0) {
793  throw new RestException(405, $this->commande->error);
794  } elseif ($result == 0) {
795  throw new RestException(304);
796  }
797 
798  return $result;
799  }
800 
815  public function setinvoiced($id)
816  {
817 
818  if (!DolibarrApiAccess::$user->rights->commande->creer) {
819  throw new RestException(401);
820  }
821  if (empty($id)) {
822  throw new RestException(400, 'Order ID is mandatory');
823  }
824  $result = $this->commande->fetch($id);
825  if (!$result) {
826  throw new RestException(404, 'Order not found');
827  }
828 
829  $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
830  if ($result < 0) {
831  throw new RestException(400, $this->commande->error);
832  }
833 
834  $result = $this->commande->fetch($id);
835  if (!$result) {
836  throw new RestException(404, 'Order not found');
837  }
838 
839  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
840  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
841  }
842 
843  $this->commande->fetchObjectLinked();
844 
845  return $this->_cleanObjectDatas($this->commande);
846  }
847 
858  public function close($id, $notrigger = 0)
859  {
860  if (!DolibarrApiAccess::$user->rights->commande->creer) {
861  throw new RestException(401);
862  }
863  $result = $this->commande->fetch($id);
864  if (!$result) {
865  throw new RestException(404, 'Order not found');
866  }
867 
868  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
869  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
870  }
871 
872  $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
873  if ($result == 0) {
874  throw new RestException(304, 'Error nothing done. May be object is already closed');
875  }
876  if ($result < 0) {
877  throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
878  }
879 
880  $result = $this->commande->fetch($id);
881  if (!$result) {
882  throw new RestException(404, 'Order not found');
883  }
884 
885  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
886  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
887  }
888 
889  $this->commande->fetchObjectLinked();
890 
891  return $this->_cleanObjectDatas($this->commande);
892  }
893 
904  public function settodraft($id, $idwarehouse = -1)
905  {
906  if (!DolibarrApiAccess::$user->rights->commande->creer) {
907  throw new RestException(401);
908  }
909  $result = $this->commande->fetch($id);
910  if (!$result) {
911  throw new RestException(404, 'Order not found');
912  }
913 
914  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
915  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
916  }
917 
918  $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
919  if ($result == 0) {
920  throw new RestException(304, 'Nothing done. May be object is already closed');
921  }
922  if ($result < 0) {
923  throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
924  }
925 
926  $result = $this->commande->fetch($id);
927  if (!$result) {
928  throw new RestException(404, 'Order not found');
929  }
930 
931  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
932  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
933  }
934 
935  $this->commande->fetchObjectLinked();
936 
937  return $this->_cleanObjectDatas($this->commande);
938  }
939 
940 
955  public function createOrderFromProposal($proposalid)
956  {
957 
958  require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
959 
960  if (!DolibarrApiAccess::$user->rights->propal->lire) {
961  throw new RestException(401);
962  }
963  if (!DolibarrApiAccess::$user->rights->commande->creer) {
964  throw new RestException(401);
965  }
966  if (empty($proposalid)) {
967  throw new RestException(400, 'Proposal ID is mandatory');
968  }
969 
970  $propal = new Propal($this->db);
971  $result = $propal->fetch($proposalid);
972  if (!$result) {
973  throw new RestException(404, 'Proposal not found');
974  }
975 
976  $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
977  if ($result < 0) {
978  throw new RestException(405, $this->commande->error);
979  }
980  $this->commande->fetchObjectLinked();
981 
982  return $this->_cleanObjectDatas($this->commande);
983  }
984 
998  public function getOrderShipments($id)
999  {
1000  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1001  if (!DolibarrApiAccess::$user->rights->expedition->lire) {
1002  throw new RestException(401);
1003  }
1004  $obj_ret = array();
1005  $sql = "SELECT e.rowid";
1006  $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
1007  $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
1008  $sql .= " ON e.rowid = edet.fk_expedition";
1009  $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
1010  $sql .= " ON edet.fk_origin_line = cdet.rowid";
1011  $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
1012  $sql .= " ON cdet.fk_commande = c.rowid";
1013  $sql .= " WHERE c.rowid = ".((int) $id);
1014  $sql .= " GROUP BY e.rowid";
1015  $sql .= $this->db->order("e.rowid", "ASC");
1016 
1017  dol_syslog("API Rest request");
1018  $result = $this->db->query($sql);
1019 
1020  if ($result) {
1021  $num = $this->db->num_rows($result);
1022  if ($num <= 0) {
1023  throw new RestException(404, 'Shipments not found ');
1024  }
1025  $i = 0;
1026  while ($i < $num) {
1027  $obj = $this->db->fetch_object($result);
1028  $shipment_static = new Expedition($this->db);
1029  if ($shipment_static->fetch($obj->rowid)) {
1030  $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
1031  }
1032  $i++;
1033  }
1034  } else {
1035  throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
1036  }
1037  return $obj_ret;
1038  }
1039 
1054  public function createOrderShipment($id, $warehouse_id)
1055  {
1056  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1057  if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1058  throw new RestException(401);
1059  }
1060  if ($warehouse_id <= 0) {
1061  throw new RestException(404, 'Warehouse not found');
1062  }
1063  $result = $this->commande->fetch($id);
1064  if (!$result) {
1065  throw new RestException(404, 'Order not found');
1066  }
1067  $shipment = new Expedition($this->db);
1068  $shipment->socid = $this->commande->socid;
1069  $result = $shipment->create(DolibarrApiAccess::$user);
1070  if ($result <= 0) {
1071  throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
1072  }
1073  foreach ($this->commande->lines as $line) {
1074  $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1075  if ($result <= 0) {
1076  throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
1077  }
1078  }
1079  return $shipment->id;
1080  }
1081 
1082  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1089  protected function _cleanObjectDatas($object)
1090  {
1091  // phpcs:enable
1092  $object = parent::_cleanObjectDatas($object);
1093 
1094  unset($object->note);
1095  unset($object->address);
1096  unset($object->barcode_type);
1097  unset($object->barcode_type_code);
1098  unset($object->barcode_type_label);
1099  unset($object->barcode_type_coder);
1100 
1101  return $object;
1102  }
1103 
1111  private function _validate($data)
1112  {
1113  $commande = array();
1114  foreach (Orders::$FIELDS as $field) {
1115  if (!isset($data[$field])) {
1116  throw new RestException(400, $field." field missing");
1117  }
1118  $commande[$field] = $data[$field];
1119  }
1120  return $commande;
1121  }
1122 }
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
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 to 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.
forgeSQLFromUniversalSearchCriteria($filter, &$error='')
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.
$conf db
API class for accounts.
Definition: inc.php:41