19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
34 static $FIELDS = array(
51 $this->commande =
new Commande($this->db);
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->hasRight(
'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->hasRight(
'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 LEFT JOIN ".MAIN_DB_PREFIX.
"commande_extrafields AS ef ON (ef.fk_object = t.rowid)";
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);
235 $commande_static =
new Commande($this->db);
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->hasRight(
'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->hasRight(
'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'
717 public function validate($id, $idwarehouse = 0, $notrigger = 0)
719 if (!DolibarrApiAccess::$user->rights->commande->creer) {
720 throw new RestException(401);
722 $result = $this->commande->fetch($id);
724 throw new RestException(404,
'Order not found');
727 $result = $this->commande->fetch_thirdparty();
730 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
733 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
735 throw new RestException(304,
'Error nothing done. May be object is already validated');
738 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
740 $result = $this->commande->fetch($id);
742 $this->commande->fetchObjectLinked();
745 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
746 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
770 if (!DolibarrApiAccess::$user->rights->commande->creer) {
771 throw new RestException(401);
774 throw new RestException(400,
'Order ID is mandatory');
776 $result = $this->commande->fetch($id);
778 throw new RestException(404,
'Order not found');
781 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
783 throw new RestException(405, $this->commande->error);
784 } elseif ($result == 0) {
785 throw new RestException(304);
807 if (!DolibarrApiAccess::$user->rights->commande->creer) {
808 throw new RestException(401);
811 throw new RestException(400,
'Order ID is mandatory');
813 $result = $this->commande->fetch($id);
815 throw new RestException(404,
'Order not found');
818 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
820 throw new RestException(400, $this->commande->error);
823 $result = $this->commande->fetch($id);
825 throw new RestException(404,
'Order not found');
829 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
832 $this->commande->fetchObjectLinked();
846 public function close($id, $notrigger = 0)
848 if (!DolibarrApiAccess::$user->rights->commande->creer) {
849 throw new RestException(401);
851 $result = $this->commande->fetch($id);
853 throw new RestException(404,
'Order not found');
857 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
860 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
862 throw new RestException(304,
'Error nothing done. May be object is already closed');
865 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
868 $result = $this->commande->fetch($id);
870 throw new RestException(404,
'Order not found');
874 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
877 $this->commande->fetchObjectLinked();
893 if (!DolibarrApiAccess::$user->rights->commande->creer) {
894 throw new RestException(401);
896 $result = $this->commande->fetch($id);
898 throw new RestException(404,
'Order not found');
902 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
905 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
907 throw new RestException(304,
'Nothing done. May be object is already closed');
910 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
913 $result = $this->commande->fetch($id);
915 throw new RestException(404,
'Order not found');
919 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
922 $this->commande->fetchObjectLinked();
944 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
946 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
947 throw new RestException(401);
949 if (!DolibarrApiAccess::$user->rights->commande->creer) {
950 throw new RestException(401);
952 if (empty($proposalid)) {
953 throw new RestException(400,
'Proposal ID is mandatory');
956 $propal =
new Propal($this->db);
957 $result = $propal->fetch($proposalid);
959 throw new RestException(404,
'Proposal not found');
962 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
964 throw new RestException(405, $this->commande->error);
966 $this->commande->fetchObjectLinked();
986 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
987 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
988 throw new RestException(401);
991 $sql =
"SELECT e.rowid";
992 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
993 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
994 $sql .=
" ON e.rowid = edet.fk_expedition";
995 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
996 $sql .=
" ON edet.fk_origin_line = cdet.rowid";
997 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
998 $sql .=
" ON cdet.fk_commande = c.rowid";
999 $sql .=
" WHERE c.rowid = ".((int) $id);
1000 $sql .=
" GROUP BY e.rowid";
1001 $sql .= $this->db->order(
"e.rowid",
"ASC");
1004 $result = $this->db->query(
$sql);
1007 $num = $this->db->num_rows($result);
1009 throw new RestException(404,
'Shipments not found ');
1013 $obj = $this->db->fetch_object($result);
1014 $shipment_static =
new Expedition($this->db);
1015 if ($shipment_static->fetch($obj->rowid)) {
1021 throw new RestException(500,
'Error when retrieve shipment list : '.$this->db->lasterror());
1042 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1043 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1044 throw new RestException(401);
1046 if ($warehouse_id <= 0) {
1047 throw new RestException(404,
'Warehouse not found');
1049 $result = $this->commande->fetch($id);
1051 throw new RestException(404,
'Order not found');
1054 $shipment->socid = $this->commande->socid;
1055 $shipment->origin_id = $this->commande->id;
1056 $result = $shipment->create(DolibarrApiAccess::$user);
1058 throw new RestException(500,
'Error on creating expedition :'.$this->db->lasterror());
1060 foreach ($this->commande->lines as $line) {
1061 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1063 throw new RestException(500,
'Error on creating expedition lines:'.$this->db->lasterror());
1066 return $shipment->id;
1079 $object = parent::_cleanObjectDatas($object);
1081 unset($object->note);
1082 unset($object->address);
1083 unset($object->barcode_type);
1084 unset($object->barcode_type_code);
1085 unset($object->barcode_type_label);
1086 unset($object->barcode_type_coder);
1100 $commande = array();
1101 foreach (Orders::$FIELDS as $field) {
1102 if (!isset($data[$field])) {
1103 throw new RestException(400, $field.
" field missing");
1105 $commande[$field] = $data[$field];