dolibarr  16.0.5
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  if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
212  throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
213  }
214  $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
215  $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
216  }
217 
218  $sql .= $this->db->order($sortfield, $sortorder);
219  if ($limit) {
220  if ($page < 0) {
221  $page = 0;
222  }
223  $offset = $limit * $page;
224 
225  $sql .= $this->db->plimit($limit + 1, $offset);
226  }
227 
228  dol_syslog("API Rest request");
229  $result = $this->db->query($sql);
230 
231  if ($result) {
232  $num = $this->db->num_rows($result);
233  $min = min($num, ($limit <= 0 ? $num : $limit));
234  $i = 0;
235  while ($i < $min) {
236  $obj = $this->db->fetch_object($result);
237  $commande_static = new Commande($this->db);
238  if ($commande_static->fetch($obj->rowid)) {
239  // Add external contacts ids
240  $tmparray = $commande_static->liste_contact(-1, 'external', 1);
241  if (is_array($tmparray)) {
242  $commande_static->contacts_ids = $tmparray;
243  }
244  // Add online_payment_url, cf #20477
245  require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
246  $commande_static->online_payment_url = getOnlinePaymentUrl(0, 'order', $commande_static->ref);
247 
248  $obj_ret[] = $this->_cleanObjectDatas($commande_static);
249  }
250  $i++;
251  }
252  } else {
253  throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
254  }
255  if (!count($obj_ret)) {
256  throw new RestException(404, 'No order found');
257  }
258  return $obj_ret;
259  }
260 
269  public function post($request_data = null)
270  {
271  if (!DolibarrApiAccess::$user->rights->commande->creer) {
272  throw new RestException(401, "Insuffisant rights");
273  }
274  // Check mandatory fields
275  $result = $this->_validate($request_data);
276 
277  foreach ($request_data as $field => $value) {
278  $this->commande->$field = $value;
279  }
280  /*if (isset($request_data["lines"])) {
281  $lines = array();
282  foreach ($request_data["lines"] as $line) {
283  array_push($lines, (object) $line);
284  }
285  $this->commande->lines = $lines;
286  }*/
287 
288  if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
289  throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
290  }
291 
292  return $this->commande->id;
293  }
294 
304  public function getLines($id)
305  {
306  if (!DolibarrApiAccess::$user->rights->commande->lire) {
307  throw new RestException(401);
308  }
309 
310  $result = $this->commande->fetch($id);
311  if (!$result) {
312  throw new RestException(404, 'Order not found');
313  }
314 
315  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
316  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
317  }
318  $this->commande->getLinesArray();
319  $result = array();
320  foreach ($this->commande->lines as $line) {
321  array_push($result, $this->_cleanObjectDatas($line));
322  }
323  return $result;
324  }
325 
336  public function postLine($id, $request_data = null)
337  {
338  if (!DolibarrApiAccess::$user->rights->commande->creer) {
339  throw new RestException(401);
340  }
341 
342  $result = $this->commande->fetch($id);
343  if (!$result) {
344  throw new RestException(404, 'Order not found');
345  }
346 
347  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
348  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
349  }
350 
351  $request_data = (object) $request_data;
352 
353  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
354  $request_data->label = sanitizeVal($request_data->label);
355 
356  $updateRes = $this->commande->addline(
357  $request_data->desc,
358  $request_data->subprice,
359  $request_data->qty,
360  $request_data->tva_tx,
361  $request_data->localtax1_tx,
362  $request_data->localtax2_tx,
363  $request_data->fk_product,
364  $request_data->remise_percent,
365  $request_data->info_bits,
366  $request_data->fk_remise_except,
367  $request_data->price_base_type ? $request_data->price_base_type : 'HT',
368  $request_data->subprice,
369  $request_data->date_start,
370  $request_data->date_end,
371  $request_data->product_type,
372  $request_data->rang,
373  $request_data->special_code,
374  $request_data->fk_parent_line,
375  $request_data->fk_fournprice,
376  $request_data->pa_ht,
377  $request_data->label,
378  $request_data->array_options,
379  $request_data->fk_unit,
380  $request_data->origin,
381  $request_data->origin_id,
382  $request_data->multicurrency_subprice,
383  $request_data->ref_ext
384  );
385 
386  if ($updateRes > 0) {
387  return $updateRes;
388  } else {
389  throw new RestException(400, $this->commande->error);
390  }
391  }
392 
404  public function putLine($id, $lineid, $request_data = null)
405  {
406  if (!DolibarrApiAccess::$user->rights->commande->creer) {
407  throw new RestException(401);
408  }
409 
410  $result = $this->commande->fetch($id);
411  if (!$result) {
412  throw new RestException(404, 'Order not found');
413  }
414 
415  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
416  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
417  }
418 
419  $request_data = (object) $request_data;
420 
421  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
422  $request_data->label = sanitizeVal($request_data->label);
423 
424  $updateRes = $this->commande->updateline(
425  $lineid,
426  $request_data->desc,
427  $request_data->subprice,
428  $request_data->qty,
429  $request_data->remise_percent,
430  $request_data->tva_tx,
431  $request_data->localtax1_tx,
432  $request_data->localtax2_tx,
433  $request_data->price_base_type ? $request_data->price_base_type : 'HT',
434  $request_data->info_bits,
435  $request_data->date_start,
436  $request_data->date_end,
437  $request_data->product_type,
438  $request_data->fk_parent_line,
439  0,
440  $request_data->fk_fournprice,
441  $request_data->pa_ht,
442  $request_data->label,
443  $request_data->special_code,
444  $request_data->array_options,
445  $request_data->fk_unit,
446  $request_data->multicurrency_subprice,
447  0,
448  $request_data->ref_ext,
449  $request_data->rang
450  );
451 
452  if ($updateRes > 0) {
453  $result = $this->get($id);
454  unset($result->line);
455  return $this->_cleanObjectDatas($result);
456  }
457  return false;
458  }
459 
474  public function deleteLine($id, $lineid)
475  {
476  if (!DolibarrApiAccess::$user->rights->commande->creer) {
477  throw new RestException(401);
478  }
479 
480  $result = $this->commande->fetch($id);
481  if (!$result) {
482  throw new RestException(404, 'Order not found');
483  }
484 
485  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
486  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
487  }
488 
489  // TODO Check the lineid $lineid is a line of ojbect
490 
491  $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid);
492  if ($updateRes > 0) {
493  return $this->get($id);
494  } else {
495  throw new RestException(405, $this->commande->error);
496  }
497  }
498 
513  public function getContacts($id, $type = '')
514  {
515  if (!DolibarrApiAccess::$user->rights->commande->lire) {
516  throw new RestException(401);
517  }
518 
519  $result = $this->commande->fetch($id);
520  if (!$result) {
521  throw new RestException(404, 'Order not found');
522  }
523 
524  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
525  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
526  }
527 
528  $contacts = $this->commande->liste_contact(-1, 'external', 0, $type);
529 
530  return $this->_cleanObjectDatas($contacts);
531  }
532 
547  public function postContact($id, $contactid, $type)
548  {
549  if (!DolibarrApiAccess::$user->rights->commande->creer) {
550  throw new RestException(401);
551  }
552 
553  $result = $this->commande->fetch($id);
554  if (!$result) {
555  throw new RestException(404, 'Order not found');
556  }
557 
558  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
559  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
560  }
561 
562  $result = $this->commande->add_contact($contactid, $type, 'external');
563 
564  if ($result < 0) {
565  throw new RestException(500, 'Error when added the contact');
566  }
567 
568  if ($result == 0) {
569  throw new RestException(304, 'contact already added');
570  }
571 
572  return array(
573  'success' => array(
574  'code' => 200,
575  'message' => 'Contact linked to the order'
576  )
577  );
578  }
579 
595  public function deleteContact($id, $contactid, $type)
596  {
597  if (!DolibarrApiAccess::$user->rights->commande->creer) {
598  throw new RestException(401);
599  }
600 
601  $result = $this->commande->fetch($id);
602  if (!$result) {
603  throw new RestException(404, 'Order not found');
604  }
605 
606  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
607  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
608  }
609 
610  $contacts = $this->commande->liste_contact();
611 
612  foreach ($contacts as $contact) {
613  if ($contact['id'] == $contactid && $contact['code'] == $type) {
614  $result = $this->commande->delete_contact($contact['rowid']);
615 
616  if (!$result) {
617  throw new RestException(500, 'Error when deleted the contact');
618  }
619  }
620  }
621 
622  return array(
623  'success' => array(
624  'code' => 200,
625  'message' => 'Contact unlinked from order'
626  )
627  );
628  }
629 
638  public function put($id, $request_data = null)
639  {
640  if (!DolibarrApiAccess::$user->rights->commande->creer) {
641  throw new RestException(401);
642  }
643 
644  $result = $this->commande->fetch($id);
645  if (!$result) {
646  throw new RestException(404, 'Order not found');
647  }
648 
649  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
650  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
651  }
652  foreach ($request_data as $field => $value) {
653  if ($field == 'id') {
654  continue;
655  }
656  $this->commande->$field = $value;
657  }
658 
659  // Update availability
660  if (!empty($this->commande->availability_id)) {
661  if ($this->commande->availability($this->commande->availability_id) < 0) {
662  throw new RestException(400, 'Error while updating availability');
663  }
664  }
665 
666  if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
667  return $this->get($id);
668  } else {
669  throw new RestException(500, $this->commande->error);
670  }
671  }
672 
679  public function delete($id)
680  {
681  if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
682  throw new RestException(401);
683  }
684  $result = $this->commande->fetch($id);
685  if (!$result) {
686  throw new RestException(404, 'Order not found');
687  }
688 
689  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
690  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
691  }
692 
693  if (!$this->commande->delete(DolibarrApiAccess::$user)) {
694  throw new RestException(500, 'Error when deleting order : '.$this->commande->error);
695  }
696 
697  return array(
698  'success' => array(
699  'code' => 200,
700  'message' => 'Order deleted'
701  )
702  );
703  }
704 
727  public function validate($id, $idwarehouse = 0, $notrigger = 0)
728  {
729  if (!DolibarrApiAccess::$user->rights->commande->creer) {
730  throw new RestException(401);
731  }
732  $result = $this->commande->fetch($id);
733  if (!$result) {
734  throw new RestException(404, 'Order not found');
735  }
736 
737  $result = $this->commande->fetch_thirdparty(); // do not check result, as failure is not fatal (used only for mail notification substitutes)
738 
739  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
740  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
741  }
742 
743  $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
744  if ($result == 0) {
745  throw new RestException(304, 'Error nothing done. May be object is already validated');
746  }
747  if ($result < 0) {
748  throw new RestException(500, 'Error when validating Order: '.$this->commande->error);
749  }
750  $result = $this->commande->fetch($id);
751 
752  $this->commande->fetchObjectLinked();
753 
754  //fix #20477 : add online_payment_url
755  require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
756  $this->commande->online_payment_url = getOnlinePaymentUrl(0, 'order', $this->commande->ref);
757 
758  return $this->_cleanObjectDatas($this->commande);
759  }
760 
778  public function reopen($id)
779  {
780 
781  if (!DolibarrApiAccess::$user->rights->commande->creer) {
782  throw new RestException(401);
783  }
784  if (empty($id)) {
785  throw new RestException(400, 'Order ID is mandatory');
786  }
787  $result = $this->commande->fetch($id);
788  if (!$result) {
789  throw new RestException(404, 'Order not found');
790  }
791 
792  $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
793  if ($result < 0) {
794  throw new RestException(405, $this->commande->error);
795  } elseif ($result == 0) {
796  throw new RestException(304);
797  }
798 
799  return $result;
800  }
801 
816  public function setinvoiced($id)
817  {
818 
819  if (!DolibarrApiAccess::$user->rights->commande->creer) {
820  throw new RestException(401);
821  }
822  if (empty($id)) {
823  throw new RestException(400, 'Order ID is mandatory');
824  }
825  $result = $this->commande->fetch($id);
826  if (!$result) {
827  throw new RestException(404, 'Order not found');
828  }
829 
830  $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
831  if ($result < 0) {
832  throw new RestException(400, $this->commande->error);
833  }
834 
835  $result = $this->commande->fetch($id);
836  if (!$result) {
837  throw new RestException(404, 'Order not found');
838  }
839 
840  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
841  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
842  }
843 
844  $this->commande->fetchObjectLinked();
845 
846  return $this->_cleanObjectDatas($this->commande);
847  }
848 
859  public function close($id, $notrigger = 0)
860  {
861  if (!DolibarrApiAccess::$user->rights->commande->creer) {
862  throw new RestException(401);
863  }
864  $result = $this->commande->fetch($id);
865  if (!$result) {
866  throw new RestException(404, 'Order not found');
867  }
868 
869  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
870  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
871  }
872 
873  $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
874  if ($result == 0) {
875  throw new RestException(304, 'Error nothing done. May be object is already closed');
876  }
877  if ($result < 0) {
878  throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
879  }
880 
881  $result = $this->commande->fetch($id);
882  if (!$result) {
883  throw new RestException(404, 'Order not found');
884  }
885 
886  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
887  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
888  }
889 
890  $this->commande->fetchObjectLinked();
891 
892  return $this->_cleanObjectDatas($this->commande);
893  }
894 
905  public function settodraft($id, $idwarehouse = -1)
906  {
907  if (!DolibarrApiAccess::$user->rights->commande->creer) {
908  throw new RestException(401);
909  }
910  $result = $this->commande->fetch($id);
911  if (!$result) {
912  throw new RestException(404, 'Order not found');
913  }
914 
915  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
916  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
917  }
918 
919  $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
920  if ($result == 0) {
921  throw new RestException(304, 'Nothing done. May be object is already closed');
922  }
923  if ($result < 0) {
924  throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
925  }
926 
927  $result = $this->commande->fetch($id);
928  if (!$result) {
929  throw new RestException(404, 'Order not found');
930  }
931 
932  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
933  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
934  }
935 
936  $this->commande->fetchObjectLinked();
937 
938  return $this->_cleanObjectDatas($this->commande);
939  }
940 
941 
956  public function createOrderFromProposal($proposalid)
957  {
958 
959  require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
960 
961  if (!DolibarrApiAccess::$user->rights->propal->lire) {
962  throw new RestException(401);
963  }
964  if (!DolibarrApiAccess::$user->rights->commande->creer) {
965  throw new RestException(401);
966  }
967  if (empty($proposalid)) {
968  throw new RestException(400, 'Proposal ID is mandatory');
969  }
970 
971  $propal = new Propal($this->db);
972  $result = $propal->fetch($proposalid);
973  if (!$result) {
974  throw new RestException(404, 'Proposal not found');
975  }
976 
977  $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
978  if ($result < 0) {
979  throw new RestException(405, $this->commande->error);
980  }
981  $this->commande->fetchObjectLinked();
982 
983  return $this->_cleanObjectDatas($this->commande);
984  }
985 
999  public function getOrderShipments($id)
1000  {
1001  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1002  if (!DolibarrApiAccess::$user->rights->expedition->lire) {
1003  throw new RestException(401);
1004  }
1005  $obj_ret = array();
1006  $sql = "SELECT e.rowid";
1007  $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
1008  $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
1009  $sql .= " ON e.rowid = edet.fk_expedition";
1010  $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
1011  $sql .= " ON edet.fk_origin_line = cdet.rowid";
1012  $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
1013  $sql .= " ON cdet.fk_commande = c.rowid";
1014  $sql .= " WHERE c.rowid = ".((int) $id);
1015  $sql .= " GROUP BY e.rowid";
1016  $sql .= $this->db->order("e.rowid", "ASC");
1017 
1018  dol_syslog("API Rest request");
1019  $result = $this->db->query($sql);
1020 
1021  if ($result) {
1022  $num = $this->db->num_rows($result);
1023  if ($num <= 0) {
1024  throw new RestException(404, 'Shipments not found ');
1025  }
1026  $i = 0;
1027  while ($i < $num) {
1028  $obj = $this->db->fetch_object($result);
1029  $shipment_static = new Expedition($this->db);
1030  if ($shipment_static->fetch($obj->rowid)) {
1031  $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
1032  }
1033  $i++;
1034  }
1035  } else {
1036  throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
1037  }
1038  return $obj_ret;
1039  }
1040 
1055  public function createOrderShipment($id, $warehouse_id)
1056  {
1057  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1058  if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1059  throw new RestException(401);
1060  }
1061  if ($warehouse_id <= 0) {
1062  throw new RestException(404, 'Warehouse not found');
1063  }
1064  $result = $this->commande->fetch($id);
1065  if (!$result) {
1066  throw new RestException(404, 'Order not found');
1067  }
1068  $shipment = new Expedition($this->db);
1069  $shipment->socid = $this->commande->socid;
1070  $result = $shipment->create(DolibarrApiAccess::$user);
1071  if ($result <= 0) {
1072  throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
1073  }
1074  foreach ($this->commande->lines as $line) {
1075  $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1076  if ($result <= 0) {
1077  throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
1078  }
1079  }
1080  return $shipment->id;
1081  }
1082 
1083  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1090  protected function _cleanObjectDatas($object)
1091  {
1092  // phpcs:enable
1093  $object = parent::_cleanObjectDatas($object);
1094 
1095  unset($object->note);
1096  unset($object->address);
1097  unset($object->barcode_type);
1098  unset($object->barcode_type_code);
1099  unset($object->barcode_type_label);
1100  unset($object->barcode_type_coder);
1101 
1102  return $object;
1103  }
1104 
1112  private function _validate($data)
1113  {
1114  $commande = array();
1115  foreach (Orders::$FIELDS as $field) {
1116  if (!isset($data[$field])) {
1117  throw new RestException(400, $field." field missing");
1118  }
1119  $commande[$field] = $data[$field];
1120  }
1121  return $commande;
1122  }
1123 }
Orders\deleteLine
deleteLine($id, $lineid)
Delete a line to given order.
Definition: api_orders.class.php:474
Orders\post
post($request_data=null)
Create a sale order.
Definition: api_orders.class.php:269
db
$conf db
API class for accounts.
Definition: inc.php:41
Orders\__construct
__construct()
Constructor.
Definition: api_orders.class.php:48
Expedition
Class to manage shipments.
Definition: expedition.class.php:52
Orders\setinvoiced
setinvoiced($id)
Classify the order as invoiced.
Definition: api_orders.class.php:816
Orders\postContact
postContact($id, $contactid, $type)
Add a contact type of given order.
Definition: api_orders.class.php:547
Orders\getContacts
getContacts($id, $type='')
Get contacts of given order.
Definition: api_orders.class.php:513
Orders\postLine
postLine($id, $request_data=null)
Add a line to given order.
Definition: api_orders.class.php:336
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:825
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:102
Orders\createOrderShipment
createOrderShipment($id, $warehouse_id)
Create the shipment of an order.
Definition: api_orders.class.php:1055
Orders\getByRef
getByRef($ref, $contact_list=1)
Get properties of an order object by ref.
Definition: api_orders.class.php:84
Orders\settodraft
settodraft($id, $idwarehouse=-1)
Set an order to draft.
Definition: api_orders.class.php:905
Orders\_fetch
_fetch($id, $ref='', $ref_ext='', $contact_list=1)
Get properties of an order object.
Definition: api_orders.class.php:120
Commande
Class to manage customers orders.
Definition: commande.class.php:46
DolibarrApi\_checkFilters
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:310
Orders\_validate
_validate($data)
Validate fields before create or update object.
Definition: api_orders.class.php:1112
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Orders\index
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List orders.
Definition: api_orders.class.php:165
Orders\deleteContact
deleteContact($id, $contactid, $type)
Unlink a contact type of given order.
Definition: api_orders.class.php:595
Orders\reopen
reopen($id)
Tag the order as validated (opened)
Definition: api_orders.class.php:778
Orders\getLines
getLines($id)
Get lines of an order.
Definition: api_orders.class.php:304
Orders\close
close($id, $notrigger=0)
Close an order (Classify it as "Delivered")
Definition: api_orders.class.php:859
Orders\getOrderShipments
getOrderShipments($id)
Get the shipments of an order.
Definition: api_orders.class.php:999
Orders\createOrderFromProposal
createOrderFromProposal($proposalid)
Create an order using an existing proposal.
Definition: api_orders.class.php:956
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:1090
Orders\put
put($id, $request_data=null)
Update order general fields (won't touch lines of order)
Definition: api_orders.class.php:638
Orders\putLine
putLine($id, $lineid, $request_data=null)
Update a line to given order.
Definition: api_orders.class.php:404
Orders\validate
validate($id, $idwarehouse=0, $notrigger=0)
Validate an order.
Definition: api_orders.class.php:727