dolibarr  18.0.6
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 
165  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $sqlfilterlines = '')
166  {
167  global $db, $conf;
168 
169  if (!DolibarrApiAccess::$user->hasRight('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 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
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  // Add sql filters for lines
217  if ($sqlfilterlines) {
218  $errormessage = '';
219  $sql .= " AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX."commandedet AS tl WHERE tl.fk_commande = t.rowid";
220  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilterlines, $errormessage);
221  $sql .= ")";
222  if ($errormessage) {
223  throw new RestException(400, 'Error when validating parameter sqlfilterlines -> '.$errormessage);
224  }
225  }
226  $sql .= $this->db->order($sortfield, $sortorder);
227  if ($limit) {
228  if ($page < 0) {
229  $page = 0;
230  }
231  $offset = $limit * $page;
232 
233  $sql .= $this->db->plimit($limit + 1, $offset);
234  }
235 
236  dol_syslog("API Rest request");
237  $result = $this->db->query($sql);
238 
239  if ($result) {
240  $num = $this->db->num_rows($result);
241  $min = min($num, ($limit <= 0 ? $num : $limit));
242  $i = 0;
243  while ($i < $min) {
244  $obj = $this->db->fetch_object($result);
245  $commande_static = new Commande($this->db);
246  if ($commande_static->fetch($obj->rowid)) {
247  // Add external contacts ids
248  $tmparray = $commande_static->liste_contact(-1, 'external', 1);
249  if (is_array($tmparray)) {
250  $commande_static->contacts_ids = $tmparray;
251  }
252  // Add online_payment_url, cf #20477
253  require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
254  $commande_static->online_payment_url = getOnlinePaymentUrl(0, 'order', $commande_static->ref);
255 
256  $obj_ret[] = $this->_cleanObjectDatas($commande_static);
257  }
258  $i++;
259  }
260  } else {
261  throw new RestException(503, 'Error when retrieve commande list : '.$this->db->lasterror());
262  }
263  if (!count($obj_ret)) {
264  throw new RestException(404, 'No order found');
265  }
266  return $obj_ret;
267  }
268 
277  public function post($request_data = null)
278  {
279  if (!DolibarrApiAccess::$user->rights->commande->creer) {
280  throw new RestException(401, "Insuffisant rights");
281  }
282  // Check mandatory fields
283  $result = $this->_validate($request_data);
284 
285  foreach ($request_data as $field => $value) {
286  $this->commande->$field = $value;
287  }
288  /*if (isset($request_data["lines"])) {
289  $lines = array();
290  foreach ($request_data["lines"] as $line) {
291  array_push($lines, (object) $line);
292  }
293  $this->commande->lines = $lines;
294  }*/
295 
296  if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
297  throw new RestException(500, "Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
298  }
299 
300  return ((int) $this->commande->id);
301  }
302 
312  public function getLines($id)
313  {
314  if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
315  throw new RestException(401);
316  }
317 
318  $result = $this->commande->fetch($id);
319  if (!$result) {
320  throw new RestException(404, 'Order not found');
321  }
322 
323  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
324  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
325  }
326  $this->commande->getLinesArray();
327  $result = array();
328  foreach ($this->commande->lines as $line) {
329  array_push($result, $this->_cleanObjectDatas($line));
330  }
331  return $result;
332  }
333 
344  public function postLine($id, $request_data = null)
345  {
346  if (!DolibarrApiAccess::$user->rights->commande->creer) {
347  throw new RestException(401);
348  }
349 
350  $result = $this->commande->fetch($id);
351  if (!$result) {
352  throw new RestException(404, 'Order not found');
353  }
354 
355  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
356  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
357  }
358 
359  $request_data = (object) $request_data;
360 
361  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
362  $request_data->label = sanitizeVal($request_data->label);
363 
364  $updateRes = $this->commande->addline(
365  $request_data->desc,
366  $request_data->subprice,
367  $request_data->qty,
368  $request_data->tva_tx,
369  $request_data->localtax1_tx,
370  $request_data->localtax2_tx,
371  $request_data->fk_product,
372  $request_data->remise_percent,
373  $request_data->info_bits,
374  $request_data->fk_remise_except,
375  $request_data->price_base_type ? $request_data->price_base_type : 'HT',
376  $request_data->subprice,
377  $request_data->date_start,
378  $request_data->date_end,
379  $request_data->product_type,
380  $request_data->rang,
381  $request_data->special_code,
382  $request_data->fk_parent_line,
383  $request_data->fk_fournprice,
384  $request_data->pa_ht,
385  $request_data->label,
386  $request_data->array_options,
387  $request_data->fk_unit,
388  $request_data->origin,
389  $request_data->origin_id,
390  $request_data->multicurrency_subprice,
391  $request_data->ref_ext
392  );
393 
394  if ($updateRes > 0) {
395  return $updateRes;
396  } else {
397  throw new RestException(400, $this->commande->error);
398  }
399  }
400 
411  public function putLine($id, $lineid, $request_data = null)
412  {
413  if (!DolibarrApiAccess::$user->rights->commande->creer) {
414  throw new RestException(401);
415  }
416 
417  $result = $this->commande->fetch($id);
418  if (!$result) {
419  throw new RestException(404, 'Order not found');
420  }
421 
422  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
423  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
424  }
425 
426  $request_data = (object) $request_data;
427 
428  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
429  $request_data->label = sanitizeVal($request_data->label);
430 
431  $updateRes = $this->commande->updateline(
432  $lineid,
433  $request_data->desc,
434  $request_data->subprice,
435  $request_data->qty,
436  $request_data->remise_percent,
437  $request_data->tva_tx,
438  $request_data->localtax1_tx,
439  $request_data->localtax2_tx,
440  $request_data->price_base_type ? $request_data->price_base_type : 'HT',
441  $request_data->info_bits,
442  $request_data->date_start,
443  $request_data->date_end,
444  $request_data->product_type,
445  $request_data->fk_parent_line,
446  0,
447  $request_data->fk_fournprice,
448  $request_data->pa_ht,
449  $request_data->label,
450  $request_data->special_code,
451  $request_data->array_options,
452  $request_data->fk_unit,
453  $request_data->multicurrency_subprice,
454  0,
455  $request_data->ref_ext,
456  $request_data->rang
457  );
458 
459  if ($updateRes > 0) {
460  $result = $this->get($id);
461  unset($result->line);
462  return $this->_cleanObjectDatas($result);
463  }
464  return false;
465  }
466 
479  public function deleteLine($id, $lineid)
480  {
481  if (!DolibarrApiAccess::$user->rights->commande->creer) {
482  throw new RestException(401);
483  }
484 
485  $result = $this->commande->fetch($id);
486  if (!$result) {
487  throw new RestException(404, 'Order not found');
488  }
489 
490  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
491  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
492  }
493 
494  $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid, $id);
495  if ($updateRes > 0) {
496  return $this->get($id);
497  } else {
498  throw new RestException(405, $this->commande->error);
499  }
500  }
501 
515  public function getContacts($id, $type = '')
516  {
517  if (!DolibarrApiAccess::$user->hasRight('commande', 'lire')) {
518  throw new RestException(401);
519  }
520 
521  $result = $this->commande->fetch($id);
522  if (!$result) {
523  throw new RestException(404, 'Order not found');
524  }
525 
526  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
527  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
528  }
529 
530  $contacts = $this->commande->liste_contact(-1, 'external', 0, $type);
531 
532  return $this->_cleanObjectDatas($contacts);
533  }
534 
548  public function postContact($id, $contactid, $type)
549  {
550  if (!DolibarrApiAccess::$user->rights->commande->creer) {
551  throw new RestException(401);
552  }
553 
554  $result = $this->commande->fetch($id);
555  if (!$result) {
556  throw new RestException(404, 'Order not found');
557  }
558 
559  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
560  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
561  }
562 
563  $result = $this->commande->add_contact($contactid, $type, 'external');
564 
565  if ($result < 0) {
566  throw new RestException(500, 'Error when added the contact');
567  }
568 
569  if ($result == 0) {
570  throw new RestException(304, 'contact already added');
571  }
572 
573  return array(
574  'success' => array(
575  'code' => 200,
576  'message' => 'Contact linked to the order'
577  )
578  );
579  }
580 
596  public function deleteContact($id, $contactid, $type)
597  {
598  if (!DolibarrApiAccess::$user->rights->commande->creer) {
599  throw new RestException(401);
600  }
601 
602  $result = $this->commande->fetch($id);
603  if (!$result) {
604  throw new RestException(404, 'Order not found');
605  }
606 
607  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
608  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
609  }
610 
611  $contacts = $this->commande->liste_contact();
612 
613  foreach ($contacts as $contact) {
614  if ($contact['id'] == $contactid && $contact['code'] == $type) {
615  $result = $this->commande->delete_contact($contact['rowid']);
616 
617  if (!$result) {
618  throw new RestException(500, 'Error when deleted the contact');
619  }
620  }
621  }
622 
623  return array(
624  'success' => array(
625  'code' => 200,
626  'message' => 'Contact unlinked from order'
627  )
628  );
629  }
630 
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  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 
814  public function setinvoiced($id)
815  {
816 
817  if (!DolibarrApiAccess::$user->rights->commande->creer) {
818  throw new RestException(401);
819  }
820  if (empty($id)) {
821  throw new RestException(400, 'Order ID is mandatory');
822  }
823  $result = $this->commande->fetch($id);
824  if (!$result) {
825  throw new RestException(404, 'Order not found');
826  }
827 
828  $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
829  if ($result < 0) {
830  throw new RestException(400, $this->commande->error);
831  }
832 
833  $result = $this->commande->fetch($id);
834  if (!$result) {
835  throw new RestException(404, 'Order not found');
836  }
837 
838  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
839  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
840  }
841 
842  $this->commande->fetchObjectLinked();
843 
844  return $this->_cleanObjectDatas($this->commande);
845  }
846 
856  public function close($id, $notrigger = 0)
857  {
858  if (!DolibarrApiAccess::$user->rights->commande->creer) {
859  throw new RestException(401);
860  }
861  $result = $this->commande->fetch($id);
862  if (!$result) {
863  throw new RestException(404, 'Order not found');
864  }
865 
866  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
867  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
868  }
869 
870  $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
871  if ($result == 0) {
872  throw new RestException(304, 'Error nothing done. May be object is already closed');
873  }
874  if ($result < 0) {
875  throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
876  }
877 
878  $result = $this->commande->fetch($id);
879  if (!$result) {
880  throw new RestException(404, 'Order not found');
881  }
882 
883  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
884  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
885  }
886 
887  $this->commande->fetchObjectLinked();
888 
889  return $this->_cleanObjectDatas($this->commande);
890  }
891 
901  public function settodraft($id, $idwarehouse = -1)
902  {
903  if (!DolibarrApiAccess::$user->rights->commande->creer) {
904  throw new RestException(401);
905  }
906  $result = $this->commande->fetch($id);
907  if (!$result) {
908  throw new RestException(404, 'Order not found');
909  }
910 
911  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
912  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
913  }
914 
915  $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
916  if ($result == 0) {
917  throw new RestException(304, 'Nothing done. May be object is already closed');
918  }
919  if ($result < 0) {
920  throw new RestException(500, 'Error when closing Order: '.$this->commande->error);
921  }
922 
923  $result = $this->commande->fetch($id);
924  if (!$result) {
925  throw new RestException(404, 'Order not found');
926  }
927 
928  if (!DolibarrApi::_checkAccessToResource('commande', $this->commande->id)) {
929  throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
930  }
931 
932  $this->commande->fetchObjectLinked();
933 
934  return $this->_cleanObjectDatas($this->commande);
935  }
936 
937 
951  public function createOrderFromProposal($proposalid)
952  {
953 
954  require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
955 
956  if (!DolibarrApiAccess::$user->hasRight('propal', 'lire')) {
957  throw new RestException(401);
958  }
959  if (!DolibarrApiAccess::$user->rights->commande->creer) {
960  throw new RestException(401);
961  }
962  if (empty($proposalid)) {
963  throw new RestException(400, 'Proposal ID is mandatory');
964  }
965 
966  $propal = new Propal($this->db);
967  $result = $propal->fetch($proposalid);
968  if (!$result) {
969  throw new RestException(404, 'Proposal not found');
970  }
971 
972  $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
973  if ($result < 0) {
974  throw new RestException(405, $this->commande->error);
975  }
976  $this->commande->fetchObjectLinked();
977 
978  return $this->_cleanObjectDatas($this->commande);
979  }
980 
994  public function getOrderShipments($id)
995  {
996  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
997  if (!DolibarrApiAccess::$user->rights->expedition->lire) {
998  throw new RestException(401);
999  }
1000  $obj_ret = array();
1001  $sql = "SELECT e.rowid";
1002  $sql .= " FROM ".MAIN_DB_PREFIX."expedition as e";
1003  $sql .= " JOIN ".MAIN_DB_PREFIX."expeditiondet as edet";
1004  $sql .= " ON e.rowid = edet.fk_expedition";
1005  $sql .= " JOIN ".MAIN_DB_PREFIX."commandedet as cdet";
1006  $sql .= " ON edet.fk_origin_line = cdet.rowid";
1007  $sql .= " JOIN ".MAIN_DB_PREFIX."commande as c";
1008  $sql .= " ON cdet.fk_commande = c.rowid";
1009  $sql .= " WHERE c.rowid = ".((int) $id);
1010  $sql .= " GROUP BY e.rowid";
1011  $sql .= $this->db->order("e.rowid", "ASC");
1012 
1013  dol_syslog("API Rest request");
1014  $result = $this->db->query($sql);
1015 
1016  if ($result) {
1017  $num = $this->db->num_rows($result);
1018  if ($num <= 0) {
1019  throw new RestException(404, 'Shipments not found ');
1020  }
1021  $i = 0;
1022  while ($i < $num) {
1023  $obj = $this->db->fetch_object($result);
1024  $shipment_static = new Expedition($this->db);
1025  if ($shipment_static->fetch($obj->rowid)) {
1026  $obj_ret[] = $this->_cleanObjectDatas($shipment_static);
1027  }
1028  $i++;
1029  }
1030  } else {
1031  throw new RestException(500, 'Error when retrieve shipment list : '.$this->db->lasterror());
1032  }
1033  return $obj_ret;
1034  }
1035 
1050  public function createOrderShipment($id, $warehouse_id)
1051  {
1052  require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php';
1053  if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1054  throw new RestException(401);
1055  }
1056  if ($warehouse_id <= 0) {
1057  throw new RestException(404, 'Warehouse not found');
1058  }
1059  $result = $this->commande->fetch($id);
1060  if (!$result) {
1061  throw new RestException(404, 'Order not found');
1062  }
1063  $shipment = new Expedition($this->db);
1064  $shipment->socid = $this->commande->socid;
1065  $shipment->origin_id = $this->commande->id;
1066  $result = $shipment->create(DolibarrApiAccess::$user);
1067  if ($result <= 0) {
1068  throw new RestException(500, 'Error on creating expedition :'.$this->db->lasterror());
1069  }
1070  foreach ($this->commande->lines as $line) {
1071  $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1072  if ($result <= 0) {
1073  throw new RestException(500, 'Error on creating expedition lines:'.$this->db->lasterror());
1074  }
1075  }
1076  return $shipment->id;
1077  }
1078 
1079  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
1086  protected function _cleanObjectDatas($object)
1087  {
1088  // phpcs:enable
1089  $object = parent::_cleanObjectDatas($object);
1090 
1091  unset($object->note);
1092  unset($object->address);
1093  unset($object->barcode_type);
1094  unset($object->barcode_type_code);
1095  unset($object->barcode_type_label);
1096  unset($object->barcode_type_coder);
1097 
1098  return $object;
1099  }
1100 
1108  private function _validate($data)
1109  {
1110  $commande = array();
1111  foreach (Orders::$FIELDS as $field) {
1112  if (!isset($data[$field])) {
1113  throw new RestException(400, $field." field missing");
1114  }
1115  $commande[$field] = $data[$field];
1116  }
1117  return $commande;
1118  }
1119 }
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.
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)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $sqlfilterlines='')
List orders.
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.