19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
34 public 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);
166 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $sqlfilterlines =
'', $properties =
'')
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 if ($sqlfilterlines) {
218 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commandedet AS tl WHERE tl.fk_commande = t.rowid";
222 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
225 $sql .= $this->db->order($sortfield, $sortorder);
230 $offset = $limit * $page;
232 $sql .= $this->db->plimit($limit + 1, $offset);
236 $result = $this->db->query($sql);
239 $num = $this->db->num_rows($result);
240 $min = min($num, ($limit <= 0 ? $num : $limit));
243 $obj = $this->db->fetch_object($result);
244 $commande_static =
new Commande($this->db);
245 if ($commande_static->fetch($obj->rowid)) {
247 $tmparray = $commande_static->liste_contact(-1,
'external', 1);
248 if (is_array($tmparray)) {
249 $commande_static->contacts_ids = $tmparray;
252 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
253 $commande_static->online_payment_url = getOnlinePaymentUrl(0,
'order', $commande_static->ref);
260 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
274 public function post($request_data =
null)
276 if (!DolibarrApiAccess::$user->rights->commande->creer) {
277 throw new RestException(401,
"Insuffisant rights");
280 $result = $this->
_validate($request_data);
282 foreach ($request_data as $field => $value) {
283 if ($field ===
'caller') {
285 $this->commande->context[
'caller'] = $request_data[
'caller'];
289 $this->commande->$field = $value;
299 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
300 throw new RestException(500,
"Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
303 return ((
int) $this->commande->id);
317 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
318 throw new RestException(401);
321 $result = $this->commande->fetch($id);
323 throw new RestException(404,
'Order not found');
327 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
329 $this->commande->getLinesArray();
331 foreach ($this->commande->lines as $line) {
347 public function postLine($id, $request_data =
null)
349 if (!DolibarrApiAccess::$user->rights->commande->creer) {
350 throw new RestException(401);
353 $result = $this->commande->fetch($id);
355 throw new RestException(404,
'Order not found');
359 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
362 $request_data = (object) $request_data;
364 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
365 $request_data->label =
sanitizeVal($request_data->label);
367 $updateRes = $this->commande->addline(
369 $request_data->subprice,
371 $request_data->tva_tx,
372 $request_data->localtax1_tx,
373 $request_data->localtax2_tx,
374 $request_data->fk_product,
375 $request_data->remise_percent,
376 $request_data->info_bits,
377 $request_data->fk_remise_except,
378 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
379 $request_data->subprice,
380 $request_data->date_start,
381 $request_data->date_end,
382 $request_data->product_type,
384 $request_data->special_code,
385 $request_data->fk_parent_line,
386 $request_data->fk_fournprice,
387 $request_data->pa_ht,
388 $request_data->label,
389 $request_data->array_options,
390 $request_data->fk_unit,
391 $request_data->origin,
392 $request_data->origin_id,
393 $request_data->multicurrency_subprice,
394 $request_data->ref_ext
397 if ($updateRes > 0) {
400 throw new RestException(400, $this->commande->error);
414 public function putLine($id, $lineid, $request_data =
null)
416 if (!DolibarrApiAccess::$user->rights->commande->creer) {
417 throw new RestException(401);
420 $result = $this->commande->fetch($id);
422 throw new RestException(404,
'Order not found');
426 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
429 $request_data = (object) $request_data;
431 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
432 $request_data->label =
sanitizeVal($request_data->label);
434 $updateRes = $this->commande->updateline(
437 $request_data->subprice,
439 $request_data->remise_percent,
440 $request_data->tva_tx,
441 $request_data->localtax1_tx,
442 $request_data->localtax2_tx,
443 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
444 $request_data->info_bits,
445 $request_data->date_start,
446 $request_data->date_end,
447 $request_data->product_type,
448 $request_data->fk_parent_line,
450 $request_data->fk_fournprice,
451 $request_data->pa_ht,
452 $request_data->label,
453 $request_data->special_code,
454 $request_data->array_options,
455 $request_data->fk_unit,
456 $request_data->multicurrency_subprice,
458 $request_data->ref_ext,
462 if ($updateRes > 0) {
463 $result = $this->
get($id);
464 unset($result->line);
484 if (!DolibarrApiAccess::$user->rights->commande->creer) {
485 throw new RestException(401);
488 $result = $this->commande->fetch($id);
490 throw new RestException(404,
'Order not found');
494 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
497 $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid, $id);
498 if ($updateRes > 0) {
499 return $this->
get($id);
501 throw new RestException(405, $this->commande->error);
520 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
521 throw new RestException(401);
524 $result = $this->commande->fetch($id);
526 throw new RestException(404,
'Order not found');
530 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
533 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
553 if (!DolibarrApiAccess::$user->rights->commande->creer) {
554 throw new RestException(401);
557 $result = $this->commande->fetch($id);
559 throw new RestException(404,
'Order not found');
563 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
566 $result = $this->commande->add_contact($contactid, $type,
'external');
569 throw new RestException(500,
'Error when added the contact');
573 throw new RestException(304,
'contact already added');
579 'message' =>
'Contact linked to the order'
601 if (!DolibarrApiAccess::$user->rights->commande->creer) {
602 throw new RestException(401);
605 $result = $this->commande->fetch($id);
607 throw new RestException(404,
'Order not found');
611 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
614 $contacts = $this->commande->liste_contact();
616 foreach ($contacts as $contact) {
617 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
618 $result = $this->commande->delete_contact($contact[
'rowid']);
621 throw new RestException(500,
'Error when deleted the contact');
629 'message' =>
'Contact unlinked from order'
641 public function put($id, $request_data =
null)
643 if (!DolibarrApiAccess::$user->rights->commande->creer) {
644 throw new RestException(401);
647 $result = $this->commande->fetch($id);
649 throw new RestException(404,
'Order not found');
653 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
655 foreach ($request_data as $field => $value) {
656 if ($field ==
'id') {
659 if ($field ===
'caller') {
661 $this->commande->context[
'caller'] = $request_data[
'caller'];
665 if ($field ==
'array_options' && is_array($value)) {
666 foreach ($value as $index => $val) {
667 $this->commande->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->commande);
671 $this->commande->$field = $this->
_checkValForAPI($field, $value, $this->commande);
675 if (!empty($this->commande->availability_id)) {
676 if ($this->commande->availability($this->commande->availability_id) < 0) {
677 throw new RestException(400,
'Error while updating availability');
681 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
682 return $this->
get($id);
684 throw new RestException(500, $this->commande->error);
694 public function delete($id)
696 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
697 throw new RestException(401);
699 $result = $this->commande->fetch($id);
701 throw new RestException(404,
'Order not found');
705 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
708 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
709 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
715 'message' =>
'Order deleted'
742 public function validate($id, $idwarehouse = 0, $notrigger = 0)
744 if (!DolibarrApiAccess::$user->rights->commande->creer) {
745 throw new RestException(401);
747 $result = $this->commande->fetch($id);
749 throw new RestException(404,
'Order not found');
752 $result = $this->commande->fetch_thirdparty();
755 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
758 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
760 throw new RestException(304,
'Error nothing done. May be object is already validated');
763 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
765 $result = $this->commande->fetch($id);
767 $this->commande->fetchObjectLinked();
770 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
771 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
795 if (!DolibarrApiAccess::$user->rights->commande->creer) {
796 throw new RestException(401);
799 throw new RestException(400,
'Order ID is mandatory');
801 $result = $this->commande->fetch($id);
803 throw new RestException(404,
'Order not found');
806 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
808 throw new RestException(405, $this->commande->error);
809 } elseif ($result == 0) {
810 throw new RestException(304);
831 if (!DolibarrApiAccess::$user->rights->commande->creer) {
832 throw new RestException(401);
835 throw new RestException(400,
'Order ID is mandatory');
837 $result = $this->commande->fetch($id);
839 throw new RestException(404,
'Order not found');
842 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
844 throw new RestException(400, $this->commande->error);
847 $result = $this->commande->fetch($id);
849 throw new RestException(404,
'Order not found');
853 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
856 $this->commande->fetchObjectLinked();
870 public function close($id, $notrigger = 0)
872 if (!DolibarrApiAccess::$user->rights->commande->creer) {
873 throw new RestException(401);
875 $result = $this->commande->fetch($id);
877 throw new RestException(404,
'Order not found');
881 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
884 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
886 throw new RestException(304,
'Error nothing done. May be object is already closed');
889 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
892 $result = $this->commande->fetch($id);
894 throw new RestException(404,
'Order not found');
898 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
901 $this->commande->fetchObjectLinked();
917 if (!DolibarrApiAccess::$user->rights->commande->creer) {
918 throw new RestException(401);
920 $result = $this->commande->fetch($id);
922 throw new RestException(404,
'Order not found');
926 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
929 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
931 throw new RestException(304,
'Nothing done. May be object is already closed');
934 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
937 $result = $this->commande->fetch($id);
939 throw new RestException(404,
'Order not found');
943 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
946 $this->commande->fetchObjectLinked();
967 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
969 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
970 throw new RestException(401);
972 if (!DolibarrApiAccess::$user->rights->commande->creer) {
973 throw new RestException(401);
975 if (empty($proposalid)) {
976 throw new RestException(400,
'Proposal ID is mandatory');
979 $propal =
new Propal($this->db);
980 $result = $propal->fetch($proposalid);
982 throw new RestException(404,
'Proposal not found');
985 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
987 throw new RestException(405, $this->commande->error);
989 $this->commande->fetchObjectLinked();
1009 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1010 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
1011 throw new RestException(401);
1014 $sql =
"SELECT e.rowid";
1015 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
1016 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
1017 $sql .=
" ON e.rowid = edet.fk_expedition";
1018 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
1019 $sql .=
" ON edet.fk_origin_line = cdet.rowid";
1020 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
1021 $sql .=
" ON cdet.fk_commande = c.rowid";
1022 $sql .=
" WHERE c.rowid = ".((int) $id);
1023 $sql .=
" GROUP BY e.rowid";
1024 $sql .= $this->db->order(
"e.rowid",
"ASC");
1027 $result = $this->db->query($sql);
1030 $num = $this->db->num_rows($result);
1032 throw new RestException(404,
'Shipments not found ');
1036 $obj = $this->db->fetch_object($result);
1037 $shipment_static =
new Expedition($this->db);
1038 if ($shipment_static->fetch($obj->rowid)) {
1044 throw new RestException(500,
'Error when retrieve shipment list : '.$this->db->lasterror());
1065 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1066 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1067 throw new RestException(401);
1069 if ($warehouse_id <= 0) {
1070 throw new RestException(404,
'Warehouse not found');
1072 $result = $this->commande->fetch($id);
1074 throw new RestException(404,
'Order not found');
1077 $shipment->socid = $this->commande->socid;
1078 $shipment->origin_id = $this->commande->id;
1079 $result = $shipment->create(DolibarrApiAccess::$user);
1081 throw new RestException(500,
'Error on creating expedition :'.$this->db->lasterror());
1083 foreach ($this->commande->lines as $line) {
1084 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1086 throw new RestException(500,
'Error on creating expedition lines:'.$this->db->lasterror());
1089 return $shipment->id;
1102 $object = parent::_cleanObjectDatas($object);
1104 unset($object->note);
1105 unset($object->address);
1106 unset($object->barcode_type);
1107 unset($object->barcode_type_code);
1108 unset($object->barcode_type_label);
1109 unset($object->barcode_type_coder);
1123 $commande = array();
1124 foreach (Orders::$FIELDS as $field) {
1125 if (!isset($data[$field])) {
1126 throw new RestException(400, $field.
" field missing");
1128 $commande[$field] = $data[$field];
Class to manage customers orders.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
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)
setinvoiced($id)
Classify the order as invoiced.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $sqlfilterlines='', $properties='')
List orders.
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.
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.