dolibarr  19.0.0-dev
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->hasRight('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->hasRight('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 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 
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->hasRight('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->hasRight('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 
717  public function validate($id, $idwarehouse = 0, $notrigger = 0)
718  {
719  if (!DolibarrApiAccess::$user->rights->commande->creer) {
720  throw new RestException(401);
721  }
722  $result = $this->commande->fetch($id);
723  if (!$result) {
724  throw new RestException(404, 'Order not found');
725  }
726 
727  $result = $this->commande->fetch_thirdparty(); // do not check result, as failure is not fatal (used only for mail notification substitutes)
728 
729  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
730  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
731  }
732 
733  $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
734  if ($result == 0) {
735  throw new RestException(304, 'Error nothing done. May be object is already validated');
736  }
737  if ($result < 0) {
738  throw new RestException(500, 'Error when validating Order: '.$this->commande->error);
739  }
740  $result = $this->commande->fetch($id);
741 
742  $this->commande->fetchObjectLinked();
743 
744  //fix #20477 : add online_payment_url
745  require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
746  $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
747 
748  return $this->_cleanObjectDatas($this->commande);
749  }
750 
768  public function reopen($id)
769  {
770  if (!DolibarrApiAccess::$user->rights->commande->creer) {
771  throw new RestException(401);
772  }
773  if (empty($id)) {
774  throw new RestException(400, 'Order ID is mandatory');
775  }
776  $result = $this->commande->fetch($id);
777  if (!$result) {
778  throw new RestException(404, 'Order not found');
779  }
780 
781  $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
782  if ($result < 0) {
783  throw new RestException(405, $this->commande->error);
784  } elseif ($result == 0) {
785  throw new RestException(304);
786  }
787 
788  return $result;
789  }
790 
804  public function setinvoiced($id)
805  {
806 
807  if (!DolibarrApiAccess::$user->rights->commande->creer) {
808  throw new RestException(401);
809  }
810  if (empty($id)) {
811  throw new RestException(400, 'Order ID is mandatory');
812  }
813  $result = $this->commande->fetch($id);
814  if (!$result) {
815  throw new RestException(404, 'Order not found');
816  }
817 
818  $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
819  if ($result < 0) {
820  throw new RestException(400, $this->commande->error);
821  }
822 
823  $result = $this->commande->fetch($id);
824  if (!$result) {
825  throw new RestException(404, 'Order not found');
826  }
827 
828  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
829  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
830  }
831 
832  $this->commande->fetchObjectLinked();
833 
834  return $this->_cleanObjectDatas($this->commande);
835  }
836 
846  public function close($id, $notrigger = 0)
847  {
848  if (!DolibarrApiAccess::$user->rights->commande->creer) {
849  throw new RestException(401);
850  }
851  $result = $this->commande->fetch($id);
852  if (!$result) {
853  throw new RestException(404, 'Order not found');
854  }
855 
856  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
857  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
858  }
859 
860  $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
861  if ($result == 0) {
862  throw new RestException(304, 'Error nothing done. May be object is already closed');
863  }
864  if ($result < 0) {
865  throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
866  }
867 
868  $result = $this->commande->fetch($id);
869  if (!$result) {
870  throw new RestException(404, 'Order not found');
871  }
872 
873  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
874  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
875  }
876 
877  $this->commande->fetchObjectLinked();
878 
879  return $this->_cleanObjectDatas($this->commande);
880  }
881 
891  public function settodraft($id, $idwarehouse = -1)
892  {
893  if (!DolibarrApiAccess::$user->rights->commande->creer) {
894  throw new RestException(401);
895  }
896  $result = $this->commande->fetch($id);
897  if (!$result) {
898  throw new RestException(404, 'Order not found');
899  }
900 
901  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
902  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
903  }
904 
905  $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
906  if ($result == 0) {
907  throw new RestException(304, 'Nothing done. May be object is already closed');
908  }
909  if ($result < 0) {
910  throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
911  }
912 
913  $result = $this->commande->fetch($id);
914  if (!$result) {
915  throw new RestException(404, 'Order not found');
916  }
917 
918  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
919  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
920  }
921 
922  $this->commande->fetchObjectLinked();
923 
924  return $this->_cleanObjectDatas($this->commande);
925  }
926 
927 
941  public function createOrderFromProposal($proposalid)
942  {
943 
944  require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
945 
946  if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
947  throw new RestException(401);
948  }
949  if (!DolibarrApiAccess::$user->rights->commande->creer) {
950  throw new RestException(401);
951  }
952  if (empty($proposalid)) {
953  throw new RestException(400, 'Proposal ID is mandatory');
954  }
955 
956  $propal = new Propal($this->db);
957  $result = $propal->fetch($proposalid);
958  if (!$result) {
959  throw new RestException(404, 'Proposal not found');
960  }
961 
962  $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
963  if ($result < 0) {
964  throw new RestException(405, $this->commande->error);
965  }
966  $this->commande->fetchObjectLinked();
967 
968  return $this->_cleanObjectDatas($this->commande);
969  }
970 
984  public function getOrderShipments($id)
985  {
986  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
987  if (!DolibarrApiAccess::$user->rights->expedition->lire) {
988  throw new RestException(401);
989  }
990  $obj_ret = array();
991  $sql = "SELECT e.rowid";
992  $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
993  $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
994  $sql .= " ON e.rowid = edet.fk_expedition";
995  $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
996  $sql .= " ON edet.fk_origin_line = cdet.rowid";
997  $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
998  $sql .= " ON cdet.fk_commande = c.rowid";
999  $sql .= " WHERE c.rowid = ".((int) $id);
1000  $sql .= " GROUP BY e.rowid";
1001  $sql .= $this->db->order("e.rowid", "ASC");
1002 
1003  dol_syslog("API Rest request");
1004  $result = $this->db->query($sql);
1005 
1006  if ($result) {
1007  $num = $this->db->num_rows($result);
1008  if ($num <= 0) {
1009  throw new RestException(404, 'Shipments not found ');
1010  }
1011  $i = 0;
1012  while ($i < $num) {
1013  $obj = $this->db->fetch_object($result);
1014  $shipment_static = new Expedition($this->db);
1015  if ($shipment_static->fetch($obj->rowid)) {
1016  $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
1017  }
1018  $i++;
1019  }
1020  } else {
1021  throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
1022  }
1023  return $obj_ret;
1024  }
1025 
1040  public function createOrderShipment($id, $warehouse_id)
1041  {
1042  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1043  if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1044  throw new RestException(401);
1045  }
1046  if ($warehouse_id <= 0) {
1047  throw new RestException(404, 'Warehouse not found');
1048  }
1049  $result = $this->commande->fetch($id);
1050  if (!$result) {
1051  throw new RestException(404, 'Order not found');
1052  }
1053  $shipment = new Expedition($this->db);
1054  $shipment->socid = $this->commande->socid;
1055  $shipment->origin_id = $this->commande->id;
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:282
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.
if(isModEnabled('facture') && $user->hasRight('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') && $user->hasRight('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:746
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.