19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
35 static $FIELDS = array(
66 public function get($id, $contact_list = 1)
68 return $this->
_fetch($id,
'',
'', $contact_list);
84 public function getByRef($ref, $contact_list = 1)
86 return $this->
_fetch(
'', $ref,
'', $contact_list);
104 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
120 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
122 if (!DolibarrApiAccess::$user->rights->commande->lire) {
123 throw new RestException(401);
126 $result = $this->commande->fetch($id, $ref, $ref_ext);
128 throw new RestException(404,
'Order not found');
132 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
136 $tmparray = $this->commande->liste_contact(-1,
'external', $contact_list);
137 if (is_array($tmparray)) {
138 $this->commande->contacts_ids = $tmparray;
140 $this->commande->fetchObjectLinked();
143 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
144 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
165 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'')
169 if (!DolibarrApiAccess::$user->rights->commande->lire) {
170 throw new RestException(401);
176 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
180 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
181 $search_sale = DolibarrApiAccess::$user->id;
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";
188 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as t";
190 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
191 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
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";
199 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
201 if ($search_sale > 0) {
202 $sql .=
" AND t.rowid = sc.fk_soc";
205 if ($search_sale > 0) {
206 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
212 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
214 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
215 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
218 $sql .= $this->
db->order($sortfield, $sortorder);
223 $offset = $limit * $page;
225 $sql .= $this->
db->plimit($limit + 1, $offset);
229 $result = $this->
db->query($sql);
232 $num = $this->
db->num_rows($result);
233 $min = min($num, ($limit <= 0 ? $num : $limit));
236 $obj = $this->
db->fetch_object($result);
238 if ($commande_static->fetch($obj->rowid)) {
240 $tmparray = $commande_static->liste_contact(-1,
'external', 1);
241 if (is_array($tmparray)) {
242 $commande_static->contacts_ids = $tmparray;
245 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
246 $commande_static->online_payment_url = getOnlinePaymentUrl(0,
'order', $commande_static->ref);
253 throw new RestException(503,
'Error when retrieve commande list : '.$this->
db->lasterror());
255 if (!count($obj_ret)) {
256 throw new RestException(404,
'No order found');
269 public function post($request_data =
null)
271 if (!DolibarrApiAccess::$user->rights->commande->creer) {
272 throw new RestException(401,
"Insuffisant rights");
275 $result = $this->
_validate($request_data);
277 foreach ($request_data as $field => $value) {
278 $this->commande->$field = $value;
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));
292 return $this->commande->id;
306 if (!DolibarrApiAccess::$user->rights->commande->lire) {
307 throw new RestException(401);
310 $result = $this->commande->fetch($id);
312 throw new RestException(404,
'Order not found');
316 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
318 $this->commande->getLinesArray();
320 foreach ($this->commande->lines as $line) {
336 public function postLine($id, $request_data =
null)
338 if (!DolibarrApiAccess::$user->rights->commande->creer) {
339 throw new RestException(401);
342 $result = $this->commande->fetch($id);
344 throw new RestException(404,
'Order not found');
348 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
351 $request_data = (object) $request_data;
353 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
354 $request_data->label =
sanitizeVal($request_data->label);
356 $updateRes = $this->commande->addline(
358 $request_data->subprice,
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,
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
386 if ($updateRes > 0) {
389 throw new RestException(400, $this->commande->error);
404 public function putLine($id, $lineid, $request_data =
null)
406 if (!DolibarrApiAccess::$user->rights->commande->creer) {
407 throw new RestException(401);
410 $result = $this->commande->fetch($id);
412 throw new RestException(404,
'Order not found');
416 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
419 $request_data = (object) $request_data;
421 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
422 $request_data->label =
sanitizeVal($request_data->label);
424 $updateRes = $this->commande->updateline(
427 $request_data->subprice,
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,
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,
448 $request_data->ref_ext,
452 if ($updateRes > 0) {
453 $result = $this->
get($id);
454 unset($result->line);
476 if (!DolibarrApiAccess::$user->rights->commande->creer) {
477 throw new RestException(401);
480 $result = $this->commande->fetch($id);
482 throw new RestException(404,
'Order not found');
486 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
491 $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid);
492 if ($updateRes > 0) {
493 return $this->
get($id);
495 throw new RestException(405, $this->commande->error);
515 if (!DolibarrApiAccess::$user->rights->commande->lire) {
516 throw new RestException(401);
519 $result = $this->commande->fetch($id);
521 throw new RestException(404,
'Order not found');
525 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
528 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
549 if (!DolibarrApiAccess::$user->rights->commande->creer) {
550 throw new RestException(401);
553 $result = $this->commande->fetch($id);
555 throw new RestException(404,
'Order not found');
559 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
562 $result = $this->commande->add_contact($contactid, $type,
'external');
565 throw new RestException(500,
'Error when added the contact');
569 throw new RestException(304,
'contact already added');
575 'message' =>
'Contact linked to the order'
597 if (!DolibarrApiAccess::$user->rights->commande->creer) {
598 throw new RestException(401);
601 $result = $this->commande->fetch($id);
603 throw new RestException(404,
'Order not found');
607 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
610 $contacts = $this->commande->liste_contact();
612 foreach ($contacts as $contact) {
613 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
614 $result = $this->commande->delete_contact($contact[
'rowid']);
617 throw new RestException(500,
'Error when deleted the contact');
625 'message' =>
'Contact unlinked from order'
638 public function put($id, $request_data =
null)
640 if (!DolibarrApiAccess::$user->rights->commande->creer) {
641 throw new RestException(401);
644 $result = $this->commande->fetch($id);
646 throw new RestException(404,
'Order not found');
650 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
652 foreach ($request_data as $field => $value) {
653 if ($field ==
'id') {
656 $this->commande->$field = $value;
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');
666 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
667 return $this->
get($id);
669 throw new RestException(500, $this->commande->error);
679 public function delete($id)
681 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
682 throw new RestException(401);
684 $result = $this->commande->fetch($id);
686 throw new RestException(404,
'Order not found');
690 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
693 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
694 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
700 'message' =>
'Order deleted'
727 public function validate($id, $idwarehouse = 0, $notrigger = 0)
729 if (!DolibarrApiAccess::$user->rights->commande->creer) {
730 throw new RestException(401);
732 $result = $this->commande->fetch($id);
734 throw new RestException(404,
'Order not found');
737 $result = $this->commande->fetch_thirdparty();
740 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
743 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
745 throw new RestException(304,
'Error nothing done. May be object is already validated');
748 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
750 $result = $this->commande->fetch($id);
752 $this->commande->fetchObjectLinked();
755 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
756 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
781 if (!DolibarrApiAccess::$user->rights->commande->creer) {
782 throw new RestException(401);
785 throw new RestException(400,
'Order ID is mandatory');
787 $result = $this->commande->fetch($id);
789 throw new RestException(404,
'Order not found');
792 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
794 throw new RestException(405, $this->commande->error);
795 } elseif ($result == 0) {
796 throw new RestException(304);
819 if (!DolibarrApiAccess::$user->rights->commande->creer) {
820 throw new RestException(401);
823 throw new RestException(400,
'Order ID is mandatory');
825 $result = $this->commande->fetch($id);
827 throw new RestException(404,
'Order not found');
830 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
832 throw new RestException(400, $this->commande->error);
835 $result = $this->commande->fetch($id);
837 throw new RestException(404,
'Order not found');
841 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
844 $this->commande->fetchObjectLinked();
859 public function close($id, $notrigger = 0)
861 if (!DolibarrApiAccess::$user->rights->commande->creer) {
862 throw new RestException(401);
864 $result = $this->commande->fetch($id);
866 throw new RestException(404,
'Order not found');
870 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
873 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
875 throw new RestException(304,
'Error nothing done. May be object is already closed');
878 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
881 $result = $this->commande->fetch($id);
883 throw new RestException(404,
'Order not found');
887 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
890 $this->commande->fetchObjectLinked();
907 if (!DolibarrApiAccess::$user->rights->commande->creer) {
908 throw new RestException(401);
910 $result = $this->commande->fetch($id);
912 throw new RestException(404,
'Order not found');
916 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
919 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
921 throw new RestException(304,
'Nothing done. May be object is already closed');
924 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
927 $result = $this->commande->fetch($id);
929 throw new RestException(404,
'Order not found');
933 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
936 $this->commande->fetchObjectLinked();
959 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
961 if (!DolibarrApiAccess::$user->rights->propal->lire) {
962 throw new RestException(401);
964 if (!DolibarrApiAccess::$user->rights->commande->creer) {
965 throw new RestException(401);
967 if (empty($proposalid)) {
968 throw new RestException(400,
'Proposal ID is mandatory');
972 $result = $propal->fetch($proposalid);
974 throw new RestException(404,
'Proposal not found');
977 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
979 throw new RestException(405, $this->commande->error);
981 $this->commande->fetchObjectLinked();
1001 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1002 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
1003 throw new RestException(401);
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");
1019 $result = $this->
db->query($sql);
1022 $num = $this->
db->num_rows($result);
1024 throw new RestException(404,
'Shipments not found ');
1028 $obj = $this->
db->fetch_object($result);
1030 if ($shipment_static->fetch($obj->rowid)) {
1036 throw new RestException(500,
'Error when retrieve shipment list : '.$this->
db->lasterror());
1057 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1058 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1059 throw new RestException(401);
1061 if ($warehouse_id <= 0) {
1062 throw new RestException(404,
'Warehouse not found');
1064 $result = $this->commande->fetch($id);
1066 throw new RestException(404,
'Order not found');
1069 $shipment->socid = $this->commande->socid;
1070 $result = $shipment->create(DolibarrApiAccess::$user);
1072 throw new RestException(500,
'Error on creating expedition :'.$this->
db->lasterror());
1074 foreach ($this->commande->lines as $line) {
1075 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1077 throw new RestException(500,
'Error on creating expedition lines:'.$this->
db->lasterror());
1080 return $shipment->id;
1093 $object = parent::_cleanObjectDatas($object);
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);
1114 $commande = array();
1115 foreach (Orders::$FIELDS as $field) {
1116 if (!isset($data[$field])) {
1117 throw new RestException(400, $field.
" field missing");
1119 $commande[$field] = $data[$field];