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 $this->commande->$field = $value;
669 if (!empty($this->commande->availability_id)) {
670 if ($this->commande->availability($this->commande->availability_id) < 0) {
671 throw new RestException(400,
'Error while updating availability');
675 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
676 return $this->
get($id);
678 throw new RestException(500, $this->commande->error);
688 public function delete($id)
690 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
691 throw new RestException(401);
693 $result = $this->commande->fetch($id);
695 throw new RestException(404,
'Order not found');
699 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
702 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
703 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
709 'message' =>
'Order deleted'
736 public function validate($id, $idwarehouse = 0, $notrigger = 0)
738 if (!DolibarrApiAccess::$user->rights->commande->creer) {
739 throw new RestException(401);
741 $result = $this->commande->fetch($id);
743 throw new RestException(404,
'Order not found');
746 $result = $this->commande->fetch_thirdparty();
749 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
752 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
754 throw new RestException(304,
'Error nothing done. May be object is already validated');
757 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
759 $result = $this->commande->fetch($id);
761 $this->commande->fetchObjectLinked();
764 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
765 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
789 if (!DolibarrApiAccess::$user->rights->commande->creer) {
790 throw new RestException(401);
793 throw new RestException(400,
'Order ID is mandatory');
795 $result = $this->commande->fetch($id);
797 throw new RestException(404,
'Order not found');
800 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
802 throw new RestException(405, $this->commande->error);
803 } elseif ($result == 0) {
804 throw new RestException(304);
825 if (!DolibarrApiAccess::$user->rights->commande->creer) {
826 throw new RestException(401);
829 throw new RestException(400,
'Order ID is mandatory');
831 $result = $this->commande->fetch($id);
833 throw new RestException(404,
'Order not found');
836 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
838 throw new RestException(400, $this->commande->error);
841 $result = $this->commande->fetch($id);
843 throw new RestException(404,
'Order not found');
847 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
850 $this->commande->fetchObjectLinked();
864 public function close($id, $notrigger = 0)
866 if (!DolibarrApiAccess::$user->rights->commande->creer) {
867 throw new RestException(401);
869 $result = $this->commande->fetch($id);
871 throw new RestException(404,
'Order not found');
875 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
878 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
880 throw new RestException(304,
'Error nothing done. May be object is already closed');
883 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
886 $result = $this->commande->fetch($id);
888 throw new RestException(404,
'Order not found');
892 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
895 $this->commande->fetchObjectLinked();
911 if (!DolibarrApiAccess::$user->rights->commande->creer) {
912 throw new RestException(401);
914 $result = $this->commande->fetch($id);
916 throw new RestException(404,
'Order not found');
920 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
923 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
925 throw new RestException(304,
'Nothing done. May be object is already closed');
928 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
931 $result = $this->commande->fetch($id);
933 throw new RestException(404,
'Order not found');
937 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
940 $this->commande->fetchObjectLinked();
961 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
963 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
964 throw new RestException(401);
966 if (!DolibarrApiAccess::$user->rights->commande->creer) {
967 throw new RestException(401);
969 if (empty($proposalid)) {
970 throw new RestException(400,
'Proposal ID is mandatory');
973 $propal =
new Propal($this->db);
974 $result = $propal->fetch($proposalid);
976 throw new RestException(404,
'Proposal not found');
979 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
981 throw new RestException(405, $this->commande->error);
983 $this->commande->fetchObjectLinked();
1003 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1004 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
1005 throw new RestException(401);
1008 $sql =
"SELECT e.rowid";
1009 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
1010 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
1011 $sql .=
" ON e.rowid = edet.fk_expedition";
1012 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
1013 $sql .=
" ON edet.fk_origin_line = cdet.rowid";
1014 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
1015 $sql .=
" ON cdet.fk_commande = c.rowid";
1016 $sql .=
" WHERE c.rowid = ".((int) $id);
1017 $sql .=
" GROUP BY e.rowid";
1018 $sql .= $this->db->order(
"e.rowid",
"ASC");
1021 $result = $this->db->query($sql);
1024 $num = $this->db->num_rows($result);
1026 throw new RestException(404,
'Shipments not found ');
1030 $obj = $this->db->fetch_object($result);
1031 $shipment_static =
new Expedition($this->db);
1032 if ($shipment_static->fetch($obj->rowid)) {
1038 throw new RestException(500,
'Error when retrieve shipment list : '.$this->db->lasterror());
1059 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1060 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1061 throw new RestException(401);
1063 if ($warehouse_id <= 0) {
1064 throw new RestException(404,
'Warehouse not found');
1066 $result = $this->commande->fetch($id);
1068 throw new RestException(404,
'Order not found');
1071 $shipment->socid = $this->commande->socid;
1072 $shipment->origin_id = $this->commande->id;
1073 $result = $shipment->create(DolibarrApiAccess::$user);
1075 throw new RestException(500,
'Error on creating expedition :'.$this->db->lasterror());
1077 foreach ($this->commande->lines as $line) {
1078 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1080 throw new RestException(500,
'Error on creating expedition lines:'.$this->db->lasterror());
1083 return $shipment->id;
1096 $object = parent::_cleanObjectDatas($object);
1098 unset($object->note);
1099 unset($object->address);
1100 unset($object->barcode_type);
1101 unset($object->barcode_type_code);
1102 unset($object->barcode_type_label);
1103 unset($object->barcode_type_coder);
1117 $commande = array();
1118 foreach (Orders::$FIELDS as $field) {
1119 if (!isset($data[$field])) {
1120 throw new RestException(400, $field.
" field missing");
1122 $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.
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.