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);
213 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
217 $sql .= $this->
db->order($sortfield, $sortorder);
222 $offset = $limit * $page;
224 $sql .= $this->
db->plimit($limit + 1, $offset);
228 $result = $this->
db->query($sql);
231 $num = $this->
db->num_rows($result);
232 $min = min($num, ($limit <= 0 ? $num : $limit));
235 $obj = $this->
db->fetch_object($result);
237 if ($commande_static->fetch($obj->rowid)) {
239 $tmparray = $commande_static->liste_contact(-1,
'external', 1);
240 if (is_array($tmparray)) {
241 $commande_static->contacts_ids = $tmparray;
244 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
245 $commande_static->online_payment_url = getOnlinePaymentUrl(0,
'order', $commande_static->ref);
252 throw new RestException(503,
'Error when retrieve commande list : '.$this->
db->lasterror());
254 if (!count($obj_ret)) {
255 throw new RestException(404,
'No order found');
268 public function post($request_data =
null)
270 if (!DolibarrApiAccess::$user->rights->commande->creer) {
271 throw new RestException(401,
"Insuffisant rights");
274 $result = $this->
_validate($request_data);
276 foreach ($request_data as $field => $value) {
277 $this->commande->$field = $value;
287 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
288 throw new RestException(500,
"Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
291 return $this->commande->id;
305 if (!DolibarrApiAccess::$user->rights->commande->lire) {
306 throw new RestException(401);
309 $result = $this->commande->fetch($id);
311 throw new RestException(404,
'Order not found');
315 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
317 $this->commande->getLinesArray();
319 foreach ($this->commande->lines as $line) {
335 public function postLine($id, $request_data =
null)
337 if (!DolibarrApiAccess::$user->rights->commande->creer) {
338 throw new RestException(401);
341 $result = $this->commande->fetch($id);
343 throw new RestException(404,
'Order not found');
347 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
350 $request_data = (object) $request_data;
352 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
353 $request_data->label =
sanitizeVal($request_data->label);
355 $updateRes = $this->commande->addline(
357 $request_data->subprice,
359 $request_data->tva_tx,
360 $request_data->localtax1_tx,
361 $request_data->localtax2_tx,
362 $request_data->fk_product,
363 $request_data->remise_percent,
364 $request_data->info_bits,
365 $request_data->fk_remise_except,
366 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
367 $request_data->subprice,
368 $request_data->date_start,
369 $request_data->date_end,
370 $request_data->product_type,
372 $request_data->special_code,
373 $request_data->fk_parent_line,
374 $request_data->fk_fournprice,
375 $request_data->pa_ht,
376 $request_data->label,
377 $request_data->array_options,
378 $request_data->fk_unit,
379 $request_data->origin,
380 $request_data->origin_id,
381 $request_data->multicurrency_subprice,
382 $request_data->ref_ext
385 if ($updateRes > 0) {
388 throw new RestException(400, $this->commande->error);
403 public function putLine($id, $lineid, $request_data =
null)
405 if (!DolibarrApiAccess::$user->rights->commande->creer) {
406 throw new RestException(401);
409 $result = $this->commande->fetch($id);
411 throw new RestException(404,
'Order not found');
415 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
418 $request_data = (object) $request_data;
420 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
421 $request_data->label =
sanitizeVal($request_data->label);
423 $updateRes = $this->commande->updateline(
426 $request_data->subprice,
428 $request_data->remise_percent,
429 $request_data->tva_tx,
430 $request_data->localtax1_tx,
431 $request_data->localtax2_tx,
432 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
433 $request_data->info_bits,
434 $request_data->date_start,
435 $request_data->date_end,
436 $request_data->product_type,
437 $request_data->fk_parent_line,
439 $request_data->fk_fournprice,
440 $request_data->pa_ht,
441 $request_data->label,
442 $request_data->special_code,
443 $request_data->array_options,
444 $request_data->fk_unit,
445 $request_data->multicurrency_subprice,
447 $request_data->ref_ext,
451 if ($updateRes > 0) {
452 $result = $this->
get($id);
453 unset($result->line);
475 if (!DolibarrApiAccess::$user->rights->commande->creer) {
476 throw new RestException(401);
479 $result = $this->commande->fetch($id);
481 throw new RestException(404,
'Order not found');
485 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
490 $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid);
491 if ($updateRes > 0) {
492 return $this->
get($id);
494 throw new RestException(405, $this->commande->error);
514 if (!DolibarrApiAccess::$user->rights->commande->lire) {
515 throw new RestException(401);
518 $result = $this->commande->fetch($id);
520 throw new RestException(404,
'Order not found');
524 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
527 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
548 if (!DolibarrApiAccess::$user->rights->commande->creer) {
549 throw new RestException(401);
552 $result = $this->commande->fetch($id);
554 throw new RestException(404,
'Order not found');
558 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
561 $result = $this->commande->add_contact($contactid, $type,
'external');
564 throw new RestException(500,
'Error when added the contact');
568 throw new RestException(304,
'contact already added');
574 'message' =>
'Contact linked to the order'
596 if (!DolibarrApiAccess::$user->rights->commande->creer) {
597 throw new RestException(401);
600 $result = $this->commande->fetch($id);
602 throw new RestException(404,
'Order not found');
606 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
609 $contacts = $this->commande->liste_contact();
611 foreach ($contacts as $contact) {
612 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
613 $result = $this->commande->delete_contact($contact[
'rowid']);
616 throw new RestException(500,
'Error when deleted the contact');
624 'message' =>
'Contact unlinked from order'
637 public function put($id, $request_data =
null)
639 if (!DolibarrApiAccess::$user->rights->commande->creer) {
640 throw new RestException(401);
643 $result = $this->commande->fetch($id);
645 throw new RestException(404,
'Order not found');
649 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
651 foreach ($request_data as $field => $value) {
652 if ($field ==
'id') {
655 $this->commande->$field = $value;
659 if (!empty($this->commande->availability_id)) {
660 if ($this->commande->availability($this->commande->availability_id) < 0) {
661 throw new RestException(400,
'Error while updating availability');
665 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
666 return $this->
get($id);
668 throw new RestException(500, $this->commande->error);
678 public function delete($id)
680 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
681 throw new RestException(401);
683 $result = $this->commande->fetch($id);
685 throw new RestException(404,
'Order not found');
689 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
692 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
693 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
699 'message' =>
'Order deleted'
726 public function validate($id, $idwarehouse = 0, $notrigger = 0)
728 if (!DolibarrApiAccess::$user->rights->commande->creer) {
729 throw new RestException(401);
731 $result = $this->commande->fetch($id);
733 throw new RestException(404,
'Order not found');
736 $result = $this->commande->fetch_thirdparty();
739 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
742 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
744 throw new RestException(304,
'Error nothing done. May be object is already validated');
747 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
749 $result = $this->commande->fetch($id);
751 $this->commande->fetchObjectLinked();
754 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
755 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
780 if (!DolibarrApiAccess::$user->rights->commande->creer) {
781 throw new RestException(401);
784 throw new RestException(400,
'Order ID is mandatory');
786 $result = $this->commande->fetch($id);
788 throw new RestException(404,
'Order not found');
791 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
793 throw new RestException(405, $this->commande->error);
794 } elseif ($result == 0) {
795 throw new RestException(304);
818 if (!DolibarrApiAccess::$user->rights->commande->creer) {
819 throw new RestException(401);
822 throw new RestException(400,
'Order ID is mandatory');
824 $result = $this->commande->fetch($id);
826 throw new RestException(404,
'Order not found');
829 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
831 throw new RestException(400, $this->commande->error);
834 $result = $this->commande->fetch($id);
836 throw new RestException(404,
'Order not found');
840 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
843 $this->commande->fetchObjectLinked();
858 public function close($id, $notrigger = 0)
860 if (!DolibarrApiAccess::$user->rights->commande->creer) {
861 throw new RestException(401);
863 $result = $this->commande->fetch($id);
865 throw new RestException(404,
'Order not found');
869 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
872 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
874 throw new RestException(304,
'Error nothing done. May be object is already closed');
877 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
880 $result = $this->commande->fetch($id);
882 throw new RestException(404,
'Order not found');
886 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
889 $this->commande->fetchObjectLinked();
906 if (!DolibarrApiAccess::$user->rights->commande->creer) {
907 throw new RestException(401);
909 $result = $this->commande->fetch($id);
911 throw new RestException(404,
'Order not found');
915 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
918 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
920 throw new RestException(304,
'Nothing done. May be object is already closed');
923 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
926 $result = $this->commande->fetch($id);
928 throw new RestException(404,
'Order not found');
932 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
935 $this->commande->fetchObjectLinked();
958 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
960 if (!DolibarrApiAccess::$user->rights->propal->lire) {
961 throw new RestException(401);
963 if (!DolibarrApiAccess::$user->rights->commande->creer) {
964 throw new RestException(401);
966 if (empty($proposalid)) {
967 throw new RestException(400,
'Proposal ID is mandatory');
971 $result = $propal->fetch($proposalid);
973 throw new RestException(404,
'Proposal not found');
976 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
978 throw new RestException(405, $this->commande->error);
980 $this->commande->fetchObjectLinked();
1000 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1001 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
1002 throw new RestException(401);
1005 $sql =
"SELECT e.rowid";
1006 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
1007 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
1008 $sql .=
" ON e.rowid = edet.fk_expedition";
1009 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
1010 $sql .=
" ON edet.fk_origin_line = cdet.rowid";
1011 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
1012 $sql .=
" ON cdet.fk_commande = c.rowid";
1013 $sql .=
" WHERE c.rowid = ".((int) $id);
1014 $sql .=
" GROUP BY e.rowid";
1015 $sql .= $this->
db->order(
"e.rowid",
"ASC");
1018 $result = $this->
db->query($sql);
1021 $num = $this->
db->num_rows($result);
1023 throw new RestException(404,
'Shipments not found ');
1027 $obj = $this->
db->fetch_object($result);
1029 if ($shipment_static->fetch($obj->rowid)) {
1035 throw new RestException(500,
'Error when retrieve shipment list : '.$this->
db->lasterror());
1056 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1057 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1058 throw new RestException(401);
1060 if ($warehouse_id <= 0) {
1061 throw new RestException(404,
'Warehouse not found');
1063 $result = $this->commande->fetch($id);
1065 throw new RestException(404,
'Order not found');
1068 $shipment->socid = $this->commande->socid;
1069 $result = $shipment->create(DolibarrApiAccess::$user);
1071 throw new RestException(500,
'Error on creating expedition :'.$this->
db->lasterror());
1073 foreach ($this->commande->lines as $line) {
1074 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1076 throw new RestException(500,
'Error on creating expedition lines:'.$this->
db->lasterror());
1079 return $shipment->id;
1092 $object = parent::_cleanObjectDatas($object);
1094 unset($object->note);
1095 unset($object->address);
1096 unset($object->barcode_type);
1097 unset($object->barcode_type_code);
1098 unset($object->barcode_type_label);
1099 unset($object->barcode_type_coder);
1113 $commande = array();
1114 foreach (Orders::$FIELDS as $field) {
1115 if (!isset($data[$field])) {
1116 throw new RestException(400, $field.
" field missing");
1118 $commande[$field] = $data[$field];
Class to manage customers orders.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
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 to 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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List orders.
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.
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, &$error='')
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.
$conf db
API class for accounts.