19use Luracast\Restler\RestException;
21require_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);
165 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $sqlfilterlines =
'')
169 if (!DolibarrApiAccess::$user->hasRight(
'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 LEFT JOIN ".MAIN_DB_PREFIX.
"commande_extrafields AS ef ON (ef.fk_object = t.rowid)";
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 if ($sqlfilterlines) {
219 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commandedet AS tl WHERE tl.fk_commande = t.rowid";
223 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
226 $sql .= $this->db->order($sortfield, $sortorder);
231 $offset = $limit * $page;
233 $sql .= $this->db->plimit($limit + 1, $offset);
237 $result = $this->db->query($sql);
240 $num = $this->db->num_rows($result);
241 $min = min($num, ($limit <= 0 ? $num : $limit));
244 $obj = $this->db->fetch_object($result);
245 $commande_static =
new Commande($this->db);
246 if ($commande_static->fetch($obj->rowid)) {
248 $tmparray = $commande_static->liste_contact(-1,
'external', 1);
249 if (is_array($tmparray)) {
250 $commande_static->contacts_ids = $tmparray;
253 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
254 $commande_static->online_payment_url = getOnlinePaymentUrl(0,
'order', $commande_static->ref);
261 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
263 if (!count($obj_ret)) {
264 throw new RestException(404,
'No order found');
277 public function post($request_data =
null)
279 if (!DolibarrApiAccess::$user->rights->commande->creer) {
280 throw new RestException(401,
"Insuffisant rights");
283 $result = $this->
_validate($request_data);
285 foreach ($request_data as $field => $value) {
286 $this->commande->$field = $value;
296 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
297 throw new RestException(500,
"Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
300 return ((
int) $this->commande->id);
314 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
315 throw new RestException(401);
318 $result = $this->commande->fetch($id);
320 throw new RestException(404,
'Order not found');
324 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
326 $this->commande->getLinesArray();
328 foreach ($this->commande->lines as $line) {
344 public function postLine($id, $request_data =
null)
346 if (!DolibarrApiAccess::$user->rights->commande->creer) {
347 throw new RestException(401);
350 $result = $this->commande->fetch($id);
352 throw new RestException(404,
'Order not found');
356 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
359 $request_data = (object) $request_data;
361 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
362 $request_data->label =
sanitizeVal($request_data->label);
364 $updateRes = $this->commande->addline(
366 $request_data->subprice,
368 $request_data->tva_tx,
369 $request_data->localtax1_tx,
370 $request_data->localtax2_tx,
371 $request_data->fk_product,
372 $request_data->remise_percent,
373 $request_data->info_bits,
374 $request_data->fk_remise_except,
375 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
376 $request_data->subprice,
377 $request_data->date_start,
378 $request_data->date_end,
379 $request_data->product_type,
381 $request_data->special_code,
382 $request_data->fk_parent_line,
383 $request_data->fk_fournprice,
384 $request_data->pa_ht,
385 $request_data->label,
386 $request_data->array_options,
387 $request_data->fk_unit,
388 $request_data->origin,
389 $request_data->origin_id,
390 $request_data->multicurrency_subprice,
391 $request_data->ref_ext
394 if ($updateRes > 0) {
397 throw new RestException(400, $this->commande->error);
411 public function putLine($id, $lineid, $request_data =
null)
413 if (!DolibarrApiAccess::$user->rights->commande->creer) {
414 throw new RestException(401);
417 $result = $this->commande->fetch($id);
419 throw new RestException(404,
'Order not found');
423 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
426 $request_data = (object) $request_data;
428 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
429 $request_data->label =
sanitizeVal($request_data->label);
431 $updateRes = $this->commande->updateline(
434 $request_data->subprice,
436 $request_data->remise_percent,
437 $request_data->tva_tx,
438 $request_data->localtax1_tx,
439 $request_data->localtax2_tx,
440 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
441 $request_data->info_bits,
442 $request_data->date_start,
443 $request_data->date_end,
444 $request_data->product_type,
445 $request_data->fk_parent_line,
447 $request_data->fk_fournprice,
448 $request_data->pa_ht,
449 $request_data->label,
450 $request_data->special_code,
451 $request_data->array_options,
452 $request_data->fk_unit,
453 $request_data->multicurrency_subprice,
455 $request_data->ref_ext,
459 if ($updateRes > 0) {
460 $result = $this->
get($id);
461 unset($result->line);
481 if (!DolibarrApiAccess::$user->rights->commande->creer) {
482 throw new RestException(401);
485 $result = $this->commande->fetch($id);
487 throw new RestException(404,
'Order not found');
491 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
494 $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid, $id);
495 if ($updateRes > 0) {
496 return $this->
get($id);
498 throw new RestException(405, $this->commande->error);
517 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
518 throw new RestException(401);
521 $result = $this->commande->fetch($id);
523 throw new RestException(404,
'Order not found');
527 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
530 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
550 if (!DolibarrApiAccess::$user->rights->commande->creer) {
551 throw new RestException(401);
554 $result = $this->commande->fetch($id);
556 throw new RestException(404,
'Order not found');
560 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
563 $result = $this->commande->add_contact($contactid, $type,
'external');
566 throw new RestException(500,
'Error when added the contact');
570 throw new RestException(304,
'contact already added');
576 'message' =>
'Contact linked to the order'
598 if (!DolibarrApiAccess::$user->rights->commande->creer) {
599 throw new RestException(401);
602 $result = $this->commande->fetch($id);
604 throw new RestException(404,
'Order not found');
608 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
611 $contacts = $this->commande->liste_contact();
613 foreach ($contacts as $contact) {
614 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
615 $result = $this->commande->delete_contact($contact[
'rowid']);
618 throw new RestException(500,
'Error when deleted the contact');
626 'message' =>
'Contact unlinked from order'
638 public function put($id, $request_data =
null)
640 if (!DolibarrApiAccess::$user->rights->commande->creer) {
641 throw new RestException(401);
644 $result = $this->commande->fetch($id);
646 throw new RestException(404,
'Order not found');
650 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
652 foreach ($request_data as $field => $value) {
653 if ($field ==
'id') {
656 if ($field ==
'array_options' && is_array($value)) {
657 foreach ($value as $index => $val) {
658 $this->commande->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->commande);
662 $this->commande->$field = $value;
666 if (!empty($this->commande->availability_id)) {
667 if ($this->commande->availability($this->commande->availability_id) < 0) {
668 throw new RestException(400,
'Error while updating availability');
672 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
673 return $this->
get($id);
675 throw new RestException(500, $this->commande->error);
685 public function delete($id)
687 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
688 throw new RestException(401);
690 $result = $this->commande->fetch($id);
692 throw new RestException(404,
'Order not found');
696 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
699 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
700 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
706 'message' =>
'Order deleted'
733 public function validate($id, $idwarehouse = 0, $notrigger = 0)
735 if (!DolibarrApiAccess::$user->rights->commande->creer) {
736 throw new RestException(401);
738 $result = $this->commande->fetch($id);
740 throw new RestException(404,
'Order not found');
743 $result = $this->commande->fetch_thirdparty();
746 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
749 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
751 throw new RestException(304,
'Error nothing done. May be object is already validated');
754 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
756 $result = $this->commande->fetch($id);
758 $this->commande->fetchObjectLinked();
761 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
762 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
786 if (!DolibarrApiAccess::$user->rights->commande->creer) {
787 throw new RestException(401);
790 throw new RestException(400,
'Order ID is mandatory');
792 $result = $this->commande->fetch($id);
794 throw new RestException(404,
'Order not found');
797 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
799 throw new RestException(405, $this->commande->error);
800 } elseif ($result == 0) {
801 throw new RestException(304);
823 if (!DolibarrApiAccess::$user->rights->commande->creer) {
824 throw new RestException(401);
827 throw new RestException(400,
'Order ID is mandatory');
829 $result = $this->commande->fetch($id);
831 throw new RestException(404,
'Order not found');
834 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
836 throw new RestException(400, $this->commande->error);
839 $result = $this->commande->fetch($id);
841 throw new RestException(404,
'Order not found');
845 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
848 $this->commande->fetchObjectLinked();
862 public function close($id, $notrigger = 0)
864 if (!DolibarrApiAccess::$user->rights->commande->creer) {
865 throw new RestException(401);
867 $result = $this->commande->fetch($id);
869 throw new RestException(404,
'Order not found');
873 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
876 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
878 throw new RestException(304,
'Error nothing done. May be object is already closed');
881 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
884 $result = $this->commande->fetch($id);
886 throw new RestException(404,
'Order not found');
890 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
893 $this->commande->fetchObjectLinked();
909 if (!DolibarrApiAccess::$user->rights->commande->creer) {
910 throw new RestException(401);
912 $result = $this->commande->fetch($id);
914 throw new RestException(404,
'Order not found');
918 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
921 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
923 throw new RestException(304,
'Nothing done. May be object is already closed');
926 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
929 $result = $this->commande->fetch($id);
931 throw new RestException(404,
'Order not found');
935 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
938 $this->commande->fetchObjectLinked();
960 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
962 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
963 throw new RestException(401);
965 if (!DolibarrApiAccess::$user->rights->commande->creer) {
966 throw new RestException(401);
968 if (empty($proposalid)) {
969 throw new RestException(400,
'Proposal ID is mandatory');
972 $propal =
new Propal($this->db);
973 $result = $propal->fetch($proposalid);
975 throw new RestException(404,
'Proposal not found');
978 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
980 throw new RestException(405, $this->commande->error);
982 $this->commande->fetchObjectLinked();
1002 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1003 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
1004 throw new RestException(401);
1007 $sql =
"SELECT e.rowid";
1008 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
1009 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
1010 $sql .=
" ON e.rowid = edet.fk_expedition";
1011 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
1012 $sql .=
" ON edet.fk_origin_line = cdet.rowid";
1013 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
1014 $sql .=
" ON cdet.fk_commande = c.rowid";
1015 $sql .=
" WHERE c.rowid = ".((int) $id);
1016 $sql .=
" GROUP BY e.rowid";
1017 $sql .= $this->db->order(
"e.rowid",
"ASC");
1020 $result = $this->db->query($sql);
1023 $num = $this->db->num_rows($result);
1025 throw new RestException(404,
'Shipments not found ');
1029 $obj = $this->db->fetch_object($result);
1030 $shipment_static =
new Expedition($this->db);
1031 if ($shipment_static->fetch($obj->rowid)) {
1037 throw new RestException(500,
'Error when retrieve shipment list : '.$this->db->lasterror());
1058 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1059 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1060 throw new RestException(401);
1062 if ($warehouse_id <= 0) {
1063 throw new RestException(404,
'Warehouse not found');
1065 $result = $this->commande->fetch($id);
1067 throw new RestException(404,
'Order not found');
1070 $shipment->socid = $this->commande->socid;
1071 $shipment->origin_id = $this->commande->id;
1072 $result = $shipment->create(DolibarrApiAccess::$user);
1074 throw new RestException(500,
'Error on creating expedition :'.$this->db->lasterror());
1076 foreach ($this->commande->lines as $line) {
1077 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1079 throw new RestException(500,
'Error on creating expedition lines:'.$this->db->lasterror());
1082 return $shipment->id;
1095 $object = parent::_cleanObjectDatas($object);
1097 unset($object->note);
1098 unset($object->address);
1099 unset($object->barcode_type);
1100 unset($object->barcode_type_code);
1101 unset($object->barcode_type_label);
1102 unset($object->barcode_type_coder);
1116 $commande = array();
1117 foreach (Orders::$FIELDS as $field) {
1118 if (!isset($data[$field])) {
1119 throw new RestException(400, $field.
" field missing");
1121 $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.
_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)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $sqlfilterlines='')
List orders.
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, &$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.