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);
211 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
213 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
214 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
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);
402 public function putLine($id, $lineid, $request_data =
null)
404 if (!DolibarrApiAccess::$user->rights->commande->creer) {
405 throw new RestException(401);
408 $result = $this->commande->fetch($id);
410 throw new RestException(404,
'Order not found');
414 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
417 $request_data = (object) $request_data;
419 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
420 $request_data->label =
sanitizeVal($request_data->label);
422 $updateRes = $this->commande->updateline(
425 $request_data->subprice,
427 $request_data->remise_percent,
428 $request_data->tva_tx,
429 $request_data->localtax1_tx,
430 $request_data->localtax2_tx,
431 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
432 $request_data->info_bits,
433 $request_data->date_start,
434 $request_data->date_end,
435 $request_data->product_type,
436 $request_data->fk_parent_line,
438 $request_data->fk_fournprice,
439 $request_data->pa_ht,
440 $request_data->label,
441 $request_data->special_code,
442 $request_data->array_options,
443 $request_data->fk_unit,
444 $request_data->multicurrency_subprice,
446 $request_data->ref_ext,
450 if ($updateRes > 0) {
451 $result = $this->
get($id);
452 unset($result->line);
472 if (!DolibarrApiAccess::$user->rights->commande->creer) {
473 throw new RestException(401);
476 $result = $this->commande->fetch($id);
478 throw new RestException(404,
'Order not found');
482 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
485 $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid, $id);
486 if ($updateRes > 0) {
487 return $this->
get($id);
489 throw new RestException(405, $this->commande->error);
508 if (!DolibarrApiAccess::$user->rights->commande->lire) {
509 throw new RestException(401);
512 $result = $this->commande->fetch($id);
514 throw new RestException(404,
'Order not found');
518 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
521 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
541 if (!DolibarrApiAccess::$user->rights->commande->creer) {
542 throw new RestException(401);
545 $result = $this->commande->fetch($id);
547 throw new RestException(404,
'Order not found');
551 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
554 $result = $this->commande->add_contact($contactid, $type,
'external');
557 throw new RestException(500,
'Error when added the contact');
561 throw new RestException(304,
'contact already added');
567 'message' =>
'Contact linked to the order'
589 if (!DolibarrApiAccess::$user->rights->commande->creer) {
590 throw new RestException(401);
593 $result = $this->commande->fetch($id);
595 throw new RestException(404,
'Order not found');
599 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
602 $contacts = $this->commande->liste_contact();
604 foreach ($contacts as $contact) {
605 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
606 $result = $this->commande->delete_contact($contact[
'rowid']);
609 throw new RestException(500,
'Error when deleted the contact');
617 'message' =>
'Contact unlinked from order'
629 public function put($id, $request_data =
null)
631 if (!DolibarrApiAccess::$user->rights->commande->creer) {
632 throw new RestException(401);
635 $result = $this->commande->fetch($id);
637 throw new RestException(404,
'Order not found');
641 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
643 foreach ($request_data as $field => $value) {
644 if ($field ==
'id') {
647 $this->commande->$field = $value;
651 if (!empty($this->commande->availability_id)) {
652 if ($this->commande->availability($this->commande->availability_id) < 0) {
653 throw new RestException(400,
'Error while updating availability');
657 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
658 return $this->
get($id);
660 throw new RestException(500, $this->commande->error);
670 public function delete($id)
672 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
673 throw new RestException(401);
675 $result = $this->commande->fetch($id);
677 throw new RestException(404,
'Order not found');
681 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
684 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
685 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
691 'message' =>
'Order deleted'
718 public function validate($id, $idwarehouse = 0, $notrigger = 0)
720 if (!DolibarrApiAccess::$user->rights->commande->creer) {
721 throw new RestException(401);
723 $result = $this->commande->fetch($id);
725 throw new RestException(404,
'Order not found');
728 $result = $this->commande->fetch_thirdparty();
731 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
734 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
736 throw new RestException(304,
'Error nothing done. May be object is already validated');
739 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
741 $result = $this->commande->fetch($id);
743 $this->commande->fetchObjectLinked();
746 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
747 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
771 if (!DolibarrApiAccess::$user->rights->commande->creer) {
772 throw new RestException(401);
775 throw new RestException(400,
'Order ID is mandatory');
777 $result = $this->commande->fetch($id);
779 throw new RestException(404,
'Order not found');
782 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
784 throw new RestException(405, $this->commande->error);
785 } elseif ($result == 0) {
786 throw new RestException(304);
808 if (!DolibarrApiAccess::$user->rights->commande->creer) {
809 throw new RestException(401);
812 throw new RestException(400,
'Order ID is mandatory');
814 $result = $this->commande->fetch($id);
816 throw new RestException(404,
'Order not found');
819 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
821 throw new RestException(400, $this->commande->error);
824 $result = $this->commande->fetch($id);
826 throw new RestException(404,
'Order not found');
830 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
833 $this->commande->fetchObjectLinked();
847 public function close($id, $notrigger = 0)
849 if (!DolibarrApiAccess::$user->rights->commande->creer) {
850 throw new RestException(401);
852 $result = $this->commande->fetch($id);
854 throw new RestException(404,
'Order not found');
858 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
861 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
863 throw new RestException(304,
'Error nothing done. May be object is already closed');
866 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
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 $this->commande->fetchObjectLinked();
894 if (!DolibarrApiAccess::$user->rights->commande->creer) {
895 throw new RestException(401);
897 $result = $this->commande->fetch($id);
899 throw new RestException(404,
'Order not found');
903 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
906 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
908 throw new RestException(304,
'Nothing done. May be object is already closed');
911 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
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 $this->commande->fetchObjectLinked();
945 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
947 if (!DolibarrApiAccess::$user->rights->propal->lire) {
948 throw new RestException(401);
950 if (!DolibarrApiAccess::$user->rights->commande->creer) {
951 throw new RestException(401);
953 if (empty($proposalid)) {
954 throw new RestException(400,
'Proposal ID is mandatory');
958 $result = $propal->fetch($proposalid);
960 throw new RestException(404,
'Proposal not found');
963 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
965 throw new RestException(405, $this->commande->error);
967 $this->commande->fetchObjectLinked();
987 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
988 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
989 throw new RestException(401);
992 $sql =
"SELECT e.rowid";
993 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
994 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
995 $sql .=
" ON e.rowid = edet.fk_expedition";
996 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
997 $sql .=
" ON edet.fk_origin_line = cdet.rowid";
998 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
999 $sql .=
" ON cdet.fk_commande = c.rowid";
1000 $sql .=
" WHERE c.rowid = ".((int) $id);
1001 $sql .=
" GROUP BY e.rowid";
1002 $sql .= $this->
db->order(
"e.rowid",
"ASC");
1005 $result = $this->
db->query($sql);
1008 $num = $this->
db->num_rows($result);
1010 throw new RestException(404,
'Shipments not found ');
1014 $obj = $this->
db->fetch_object($result);
1016 if ($shipment_static->fetch($obj->rowid)) {
1022 throw new RestException(500,
'Error when retrieve shipment list : '.$this->
db->lasterror());
1043 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1044 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1045 throw new RestException(401);
1047 if ($warehouse_id <= 0) {
1048 throw new RestException(404,
'Warehouse not found');
1050 $result = $this->commande->fetch($id);
1052 throw new RestException(404,
'Order not found');
1055 $shipment->socid = $this->commande->socid;
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];
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.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
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.
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.
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.