19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
34 static $FIELDS = array(
65 public function get($id, $contact_list = 1)
67 return $this->
_fetch($id,
'',
'', $contact_list);
83 public function getByRef($ref, $contact_list = 1)
85 return $this->
_fetch(
'', $ref,
'', $contact_list);
103 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
119 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
121 if (!DolibarrApiAccess::$user->rights->commande->lire) {
122 throw new RestException(401);
125 $result = $this->commande->fetch($id, $ref, $ref_ext);
127 throw new RestException(404,
'Order not found');
131 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
135 $tmparray = $this->commande->liste_contact(-1,
'external', $contact_list);
136 if (is_array($tmparray)) {
137 $this->commande->contacts_ids = $tmparray;
139 $this->commande->fetchObjectLinked();
142 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
143 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
164 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'')
168 if (!DolibarrApiAccess::$user->rights->commande->lire) {
169 throw new RestException(401);
175 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
179 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
180 $search_sale = DolibarrApiAccess::$user->id;
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";
187 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as t";
189 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
190 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
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";
198 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
200 if ($search_sale > 0) {
201 $sql .=
" AND t.rowid = sc.fk_soc";
204 if ($search_sale > 0) {
205 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
212 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
216 $sql .= $this->
db->order($sortfield, $sortorder);
221 $offset = $limit * $page;
223 $sql .= $this->
db->plimit($limit + 1, $offset);
227 $result = $this->
db->query(
$sql);
230 $num = $this->
db->num_rows($result);
231 $min = min($num, ($limit <= 0 ? $num : $limit));
234 $obj = $this->
db->fetch_object($result);
236 if ($commande_static->fetch($obj->rowid)) {
238 $tmparray = $commande_static->liste_contact(-1,
'external', 1);
239 if (is_array($tmparray)) {
240 $commande_static->contacts_ids = $tmparray;
243 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
244 $commande_static->online_payment_url = getOnlinePaymentUrl(0,
'order', $commande_static->ref);
251 throw new RestException(503,
'Error when retrieve commande list : '.$this->
db->lasterror());
253 if (!count($obj_ret)) {
254 throw new RestException(404,
'No order found');
267 public function post($request_data =
null)
269 if (!DolibarrApiAccess::$user->rights->commande->creer) {
270 throw new RestException(401,
"Insuffisant rights");
273 $result = $this->
_validate($request_data);
275 foreach ($request_data as $field => $value) {
276 $this->commande->$field = $value;
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));
290 return $this->commande->id;
304 if (!DolibarrApiAccess::$user->rights->commande->lire) {
305 throw new RestException(401);
308 $result = $this->commande->fetch($id);
310 throw new RestException(404,
'Order not found');
314 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
316 $this->commande->getLinesArray();
318 foreach ($this->commande->lines as $line) {
334 public function postLine($id, $request_data =
null)
336 if (!DolibarrApiAccess::$user->rights->commande->creer) {
337 throw new RestException(401);
340 $result = $this->commande->fetch($id);
342 throw new RestException(404,
'Order not found');
346 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
349 $request_data = (object) $request_data;
351 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
352 $request_data->label =
sanitizeVal($request_data->label);
354 $updateRes = $this->commande->addline(
356 $request_data->subprice,
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,
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
384 if ($updateRes > 0) {
387 throw new RestException(400, $this->commande->error);
401 public function putLine($id, $lineid, $request_data =
null)
403 if (!DolibarrApiAccess::$user->rights->commande->creer) {
404 throw new RestException(401);
407 $result = $this->commande->fetch($id);
409 throw new RestException(404,
'Order not found');
413 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
416 $request_data = (object) $request_data;
418 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
419 $request_data->label =
sanitizeVal($request_data->label);
421 $updateRes = $this->commande->updateline(
424 $request_data->subprice,
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,
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,
445 $request_data->ref_ext,
449 if ($updateRes > 0) {
450 $result = $this->
get($id);
451 unset($result->line);
471 if (!DolibarrApiAccess::$user->rights->commande->creer) {
472 throw new RestException(401);
475 $result = $this->commande->fetch($id);
477 throw new RestException(404,
'Order not found');
481 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
484 $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid, $id);
485 if ($updateRes > 0) {
486 return $this->
get($id);
488 throw new RestException(405, $this->commande->error);
507 if (!DolibarrApiAccess::$user->rights->commande->lire) {
508 throw new RestException(401);
511 $result = $this->commande->fetch($id);
513 throw new RestException(404,
'Order not found');
517 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
520 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
540 if (!DolibarrApiAccess::$user->rights->commande->creer) {
541 throw new RestException(401);
544 $result = $this->commande->fetch($id);
546 throw new RestException(404,
'Order not found');
550 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
553 $result = $this->commande->add_contact($contactid, $type,
'external');
556 throw new RestException(500,
'Error when added the contact');
560 throw new RestException(304,
'contact already added');
566 'message' =>
'Contact linked to the order'
588 if (!DolibarrApiAccess::$user->rights->commande->creer) {
589 throw new RestException(401);
592 $result = $this->commande->fetch($id);
594 throw new RestException(404,
'Order not found');
598 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
601 $contacts = $this->commande->liste_contact();
603 foreach ($contacts as $contact) {
604 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
605 $result = $this->commande->delete_contact($contact[
'rowid']);
608 throw new RestException(500,
'Error when deleted the contact');
616 'message' =>
'Contact unlinked from order'
628 public function put($id, $request_data =
null)
630 if (!DolibarrApiAccess::$user->rights->commande->creer) {
631 throw new RestException(401);
634 $result = $this->commande->fetch($id);
636 throw new RestException(404,
'Order not found');
640 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
642 foreach ($request_data as $field => $value) {
643 if ($field ==
'id') {
646 $this->commande->$field = $value;
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');
656 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
657 return $this->
get($id);
659 throw new RestException(500, $this->commande->error);
669 public function delete($id)
671 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
672 throw new RestException(401);
674 $result = $this->commande->fetch($id);
676 throw new RestException(404,
'Order not found');
680 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
683 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
684 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
690 'message' =>
'Order deleted'
716 public function validate($id, $idwarehouse = 0, $notrigger = 0)
718 if (!DolibarrApiAccess::$user->rights->commande->creer) {
719 throw new RestException(401);
721 $result = $this->commande->fetch($id);
723 throw new RestException(404,
'Order not found');
726 $result = $this->commande->fetch_thirdparty();
729 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
732 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
734 throw new RestException(304,
'Error nothing done. May be object is already validated');
737 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
739 $result = $this->commande->fetch($id);
741 $this->commande->fetchObjectLinked();
744 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
745 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
769 if (!DolibarrApiAccess::$user->rights->commande->creer) {
770 throw new RestException(401);
773 throw new RestException(400,
'Order ID is mandatory');
775 $result = $this->commande->fetch($id);
777 throw new RestException(404,
'Order not found');
780 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
782 throw new RestException(405, $this->commande->error);
783 } elseif ($result == 0) {
784 throw new RestException(304);
806 if (!DolibarrApiAccess::$user->rights->commande->creer) {
807 throw new RestException(401);
810 throw new RestException(400,
'Order ID is mandatory');
812 $result = $this->commande->fetch($id);
814 throw new RestException(404,
'Order not found');
817 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
819 throw new RestException(400, $this->commande->error);
822 $result = $this->commande->fetch($id);
824 throw new RestException(404,
'Order not found');
828 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
831 $this->commande->fetchObjectLinked();
845 public function close($id, $notrigger = 0)
847 if (!DolibarrApiAccess::$user->rights->commande->creer) {
848 throw new RestException(401);
850 $result = $this->commande->fetch($id);
852 throw new RestException(404,
'Order not found');
856 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
859 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
861 throw new RestException(304,
'Error nothing done. May be object is already closed');
864 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
867 $result = $this->commande->fetch($id);
869 throw new RestException(404,
'Order not found');
873 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
876 $this->commande->fetchObjectLinked();
892 if (!DolibarrApiAccess::$user->rights->commande->creer) {
893 throw new RestException(401);
895 $result = $this->commande->fetch($id);
897 throw new RestException(404,
'Order not found');
901 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
904 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
906 throw new RestException(304,
'Nothing done. May be object is already closed');
909 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
912 $result = $this->commande->fetch($id);
914 throw new RestException(404,
'Order not found');
918 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
921 $this->commande->fetchObjectLinked();
943 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
945 if (!DolibarrApiAccess::$user->rights->propal->lire) {
946 throw new RestException(401);
948 if (!DolibarrApiAccess::$user->rights->commande->creer) {
949 throw new RestException(401);
951 if (empty($proposalid)) {
952 throw new RestException(400,
'Proposal ID is mandatory');
956 $result = $propal->fetch($proposalid);
958 throw new RestException(404,
'Proposal not found');
961 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
963 throw new RestException(405, $this->commande->error);
965 $this->commande->fetchObjectLinked();
985 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
986 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
987 throw new RestException(401);
990 $sql =
"SELECT e.rowid";
991 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
992 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
993 $sql .=
" ON e.rowid = edet.fk_expedition";
994 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
995 $sql .=
" ON edet.fk_origin_line = cdet.rowid";
996 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
997 $sql .=
" ON cdet.fk_commande = c.rowid";
998 $sql .=
" WHERE c.rowid = ".((int) $id);
999 $sql .=
" GROUP BY e.rowid";
1000 $sql .= $this->
db->order(
"e.rowid",
"ASC");
1003 $result = $this->
db->query(
$sql);
1006 $num = $this->
db->num_rows($result);
1008 throw new RestException(404,
'Shipments not found ');
1012 $obj = $this->
db->fetch_object($result);
1014 if ($shipment_static->fetch($obj->rowid)) {
1020 throw new RestException(500,
'Error when retrieve shipment list : '.$this->
db->lasterror());
1041 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1042 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1043 throw new RestException(401);
1045 if ($warehouse_id <= 0) {
1046 throw new RestException(404,
'Warehouse not found');
1048 $result = $this->commande->fetch($id);
1050 throw new RestException(404,
'Order not found');
1053 $shipment->socid = $this->commande->socid;
1054 $shipment->origin_id = $this->commande->id;
1055 $result = $shipment->create(DolibarrApiAccess::$user);
1057 throw new RestException(500,
'Error on creating expedition :'.$this->
db->lasterror());
1059 foreach ($this->commande->lines as $line) {
1060 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1062 throw new RestException(500,
'Error on creating expedition lines:'.$this->
db->lasterror());
1065 return $shipment->id;
1078 $object = parent::_cleanObjectDatas($object);
1080 unset($object->note);
1081 unset($object->address);
1082 unset($object->barcode_type);
1083 unset($object->barcode_type_code);
1084 unset($object->barcode_type_label);
1085 unset($object->barcode_type_coder);
1099 $commande = array();
1100 foreach (Orders::$FIELDS as $field) {
1101 if (!isset($data[$field])) {
1102 throw new RestException(400, $field.
" field missing");
1104 $commande[$field] = $data[$field];