21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
36 public static $FIELDS = array(
54 $this->commande =
new Commande($this->db);
68 public function get(
$id, $contact_list = -1)
70 return $this->
_fetch(
$id,
'',
'', $contact_list);
86 public function getByRef($ref, $contact_list = -1)
88 return $this->
_fetch(0, $ref,
'', $contact_list);
106 return $this->
_fetch(0,
'', $ref_ext, $contact_list);
122 private function _fetch(
$id, $ref =
'', $ref_ext =
'', $contact_list = -1)
124 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
125 throw new RestException(403);
128 $result = $this->commande->fetch(
$id, $ref, $ref_ext);
130 throw new RestException(404,
'Order not found');
134 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
137 if ($contact_list > -1) {
139 $tmparray = $this->commande->liste_contact(-1,
'external', $contact_list);
140 if (is_array($tmparray)) {
141 $this->commande->contacts_ids = $tmparray;
143 $tmparray = $this->commande->liste_contact(-1,
'internal', $contact_list);
144 if (is_array($tmparray)) {
145 $this->commande->contacts_ids_internal = $tmparray;
149 $this->commande->fetchObjectLinked();
152 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
153 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
180 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $sqlfilterlines =
'', $properties =
'', $pagination_data =
false, $loadlinkedobjects = 0)
182 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
183 throw new RestException(403);
189 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
193 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
194 $search_sale = DolibarrApiAccess::$user->id;
197 $sql =
"SELECT t.rowid";
198 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande AS t";
199 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"commande_extrafields AS ef ON (ef.fk_object = t.rowid)";
200 $sql .=
' WHERE t.entity IN ('.getEntity(
'commande').
')';
202 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
205 if ($search_sale && $search_sale !=
'-1') {
206 if ($search_sale == -2) {
207 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
208 } elseif ($search_sale > 0) {
209 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
217 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
221 if ($sqlfilterlines) {
223 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commandedet AS tl WHERE tl.fk_commande = t.rowid";
227 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
232 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
234 $sql .= $this->db->order($sortfield, $sortorder);
239 $offset = $limit * $page;
241 $sql .= $this->db->plimit($limit + 1, $offset);
245 $result = $this->db->query($sql);
248 $num = $this->db->num_rows($result);
249 $min = min($num, ($limit <= 0 ? $num : $limit));
252 $obj = $this->db->fetch_object($result);
253 $commande_static =
new Commande($this->db);
254 if ($commande_static->fetch($obj->rowid) > 0) {
256 $tmparray = $commande_static->liste_contact(-1,
'external', 1);
257 if (is_array($tmparray)) {
258 $commande_static->contacts_ids = $tmparray;
261 if ($loadlinkedobjects) {
263 $commande_static->fetchObjectLinked();
267 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
268 $commande_static->online_payment_url = getOnlinePaymentUrl(0,
'order', $commande_static->ref);
275 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
279 if ($pagination_data) {
280 $totalsResult = $this->db->query($sqlTotals);
281 $total = $this->db->fetch_object($totalsResult)->total;
286 $obj_ret[
'data'] = $tmp;
287 $obj_ret[
'pagination'] = [
288 'total' => (int) $total,
290 'page_count' => ceil((
int) $total / $limit),
308 public function post($request_data =
null)
310 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
311 throw new RestException(403,
"Insuffisant rights");
314 $result = $this->
_validate($request_data);
316 foreach ($request_data as $field => $value) {
317 if ($field ===
'caller') {
319 $this->commande->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
323 $this->commande->$field = $this->
_checkValForAPI($field, $value, $this->commande);
333 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
334 throw new RestException(500,
"Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
337 return ((
int) $this->commande->id);
353 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
354 throw new RestException(403);
357 $result = $this->commande->fetch(
$id);
359 throw new RestException(404,
'Order not found');
363 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
365 $this->commande->getLinesArray();
367 foreach ($this->commande->lines as $line) {
386 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
387 throw new RestException(403);
390 $result = $this->commande->fetch(
$id);
392 throw new RestException(404,
'Order not found');
396 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
399 $this->commande->fetch_lines();
400 foreach ($this->commande->lines as $line) {
401 if ($line->id == $lineid) {
405 throw new RestException(404,
'Line not found');
422 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
423 throw new RestException(403);
426 $result = $this->commande->fetch(
$id);
428 throw new RestException(404,
'Order not found');
432 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
435 $request_data = (object) $request_data;
437 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
438 $request_data->label =
sanitizeVal($request_data->label);
440 $updateRes = $this->commande->addline(
442 $request_data->subprice,
444 $request_data->tva_tx,
445 $request_data->localtax1_tx,
446 $request_data->localtax2_tx,
447 $request_data->fk_product,
448 $request_data->remise_percent,
449 $request_data->info_bits,
450 $request_data->fk_remise_except,
451 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
452 $request_data->subprice,
453 $request_data->date_start,
454 $request_data->date_end,
455 $request_data->product_type,
457 $request_data->special_code,
458 $request_data->fk_parent_line,
459 $request_data->fk_fournprice,
460 $request_data->pa_ht,
461 $request_data->label,
462 $request_data->array_options,
463 $request_data->fk_unit,
464 $request_data->origin,
465 $request_data->origin_id,
466 $request_data->multicurrency_subprice,
467 $request_data->ref_ext
470 if ($updateRes > 0) {
473 throw new RestException(400, $this->commande->error);
491 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
492 throw new RestException(403);
495 $result = $this->commande->fetch(
$id);
497 throw new RestException(404,
'Order not found');
501 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
504 $request_data = (object) $request_data;
506 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
507 $request_data->label =
sanitizeVal($request_data->label);
509 $updateRes = $this->commande->updateline(
512 $request_data->subprice,
514 $request_data->remise_percent,
515 $request_data->tva_tx,
516 $request_data->localtax1_tx,
517 $request_data->localtax2_tx,
518 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
519 $request_data->info_bits,
520 $request_data->date_start,
521 $request_data->date_end,
522 $request_data->product_type,
523 $request_data->fk_parent_line,
525 $request_data->fk_fournprice,
526 $request_data->pa_ht,
527 $request_data->label,
528 $request_data->special_code,
529 $request_data->array_options,
530 $request_data->fk_unit,
531 $request_data->multicurrency_subprice,
533 $request_data->ref_ext,
537 if ($updateRes > 0) {
538 $result = $this->
get(
$id);
539 unset($result->line);
559 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
560 throw new RestException(403);
563 $result = $this->commande->fetch(
$id);
565 throw new RestException(404,
'Order not found');
569 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
572 $updateRes = $this->commande->deleteLine(DolibarrApiAccess::$user, $lineid,
$id);
573 if ($updateRes > 0) {
574 return $this->
get(
$id);
576 throw new RestException(405, $this->commande->error);
595 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
596 throw new RestException(403);
599 $result = $this->commande->fetch(
$id);
601 throw new RestException(404,
'Order not found');
605 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
608 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
630 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
631 throw new RestException(403);
634 $result = $this->commande->fetch(
$id);
636 throw new RestException(404,
'Order not found');
640 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
643 $result = $this->commande->add_contact($contactid, $type,
'external');
646 throw new RestException(500,
'Error when added the contact');
650 throw new RestException(304,
'contact already added');
656 'message' =>
'Contact linked to the order'
680 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
681 throw new RestException(403);
684 $result = $this->commande->fetch(
$id);
686 throw new RestException(404,
'Order not found');
690 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
693 $contacts = $this->commande->liste_contact();
695 foreach ($contacts as $contact) {
696 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
697 $result = $this->commande->delete_contact($contact[
'rowid']);
700 throw new RestException(500,
'Error when deleted the contact');
708 'message' =>
'Contact unlinked from order'
722 public function put(
$id, $request_data =
null)
724 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
725 throw new RestException(403);
728 $result = $this->commande->fetch(
$id);
730 throw new RestException(404,
'Order not found');
734 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
736 foreach ($request_data as $field => $value) {
737 if ($field ==
'id') {
740 if ($field ===
'caller') {
742 $this->commande->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
745 if ($field ==
'array_options' && is_array($value)) {
746 foreach ($value as $index => $val) {
747 $this->commande->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->commande);
752 $this->commande->$field = $this->
_checkValForAPI($field, $value, $this->commande);
756 if (!empty($this->commande->availability_id)) {
757 if ($this->commande->availability($this->commande->availability_id) < 0) {
758 throw new RestException(400,
'Error while updating availability');
762 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
763 return $this->
get(
$id);
765 throw new RestException(500, $this->commande->error);
777 public function delete(
$id)
779 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'supprimer')) {
780 throw new RestException(403);
782 $result = $this->commande->fetch(
$id);
784 throw new RestException(404,
'Order not found');
788 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
791 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
792 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
798 'message' =>
'Order deleted'
826 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
827 throw new RestException(403);
829 $result = $this->commande->fetch(
$id);
831 throw new RestException(404,
'Order not found');
834 $result = $this->commande->fetch_thirdparty();
837 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
840 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
842 throw new RestException(304,
'Error nothing done. May be object is already validated');
845 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
847 $result = $this->commande->fetch(
$id);
849 $this->commande->fetchObjectLinked();
852 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
853 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
877 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
878 throw new RestException(403);
881 throw new RestException(400,
'Order ID is mandatory');
883 $result = $this->commande->fetch(
$id);
885 throw new RestException(404,
'Order not found');
888 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
890 throw new RestException(405, $this->commande->error);
891 } elseif ($result == 0) {
892 throw new RestException(304);
913 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
914 throw new RestException(403);
917 throw new RestException(400,
'Order ID is mandatory');
919 $result = $this->commande->fetch(
$id);
921 throw new RestException(404,
'Order not found');
924 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
926 throw new RestException(400, $this->commande->error);
929 $result = $this->commande->fetch(
$id);
931 throw new RestException(404,
'Order not found');
935 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
938 $this->commande->fetchObjectLinked();
954 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
955 throw new RestException(403);
957 $result = $this->commande->fetch(
$id);
959 throw new RestException(404,
'Order not found');
963 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
966 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
968 throw new RestException(304,
'Error nothing done. May be object is already closed');
971 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
974 $result = $this->commande->fetch(
$id);
976 throw new RestException(404,
'Order not found');
980 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
983 $this->commande->fetchObjectLinked();
999 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
1000 throw new RestException(403);
1002 $result = $this->commande->fetch(
$id);
1004 throw new RestException(404,
'Order not found');
1008 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1011 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
1013 throw new RestException(304,
'Nothing done. May be object is already closed');
1016 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
1019 $result = $this->commande->fetch(
$id);
1021 throw new RestException(404,
'Order not found');
1025 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1028 $this->commande->fetchObjectLinked();
1049 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
1051 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
1052 throw new RestException(403);
1054 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
1055 throw new RestException(403);
1057 if (empty($proposalid)) {
1058 throw new RestException(400,
'Proposal ID is mandatory');
1061 $propal =
new Propal($this->db);
1062 $result = $propal->fetch($proposalid);
1064 throw new RestException(404,
'Proposal not found');
1067 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
1069 throw new RestException(405, $this->commande->error);
1071 $this->commande->fetchObjectLinked();
1093 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1094 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
1095 throw new RestException(403);
1098 $sql =
"SELECT e.rowid";
1099 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
1100 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
1101 $sql .=
" ON e.rowid = edet.fk_expedition";
1102 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
1103 $sql .=
" ON edet.fk_elementdet = cdet.rowid";
1104 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
1105 $sql .=
" ON cdet.fk_commande = c.rowid";
1106 $sql .=
" WHERE c.rowid = ".((int)
$id);
1107 $sql .=
" GROUP BY e.rowid";
1108 $sql .= $this->db->order(
"e.rowid",
"ASC");
1111 $result = $this->db->query($sql);
1114 $num = $this->db->num_rows($result);
1116 throw new RestException(404,
'Shipments not found ');
1120 $obj = $this->db->fetch_object($result);
1121 $shipment_static =
new Expedition($this->db);
1122 if ($shipment_static->fetch($obj->rowid)) {
1128 throw new RestException(500,
'Error when retrieve shipment list : '.$this->db->lasterror());
1149 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1150 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
1151 throw new RestException(403);
1153 if ($warehouse_id <= 0) {
1154 throw new RestException(404,
'Warehouse not found');
1156 $result = $this->commande->fetch(
$id);
1158 throw new RestException(404,
'Order not found');
1161 $shipment->socid = $this->commande->socid;
1162 $shipment->origin_id = $this->commande->id;
1163 $shipment->origin = $this->commande->element;
1164 $result = $shipment->create(DolibarrApiAccess::$user);
1166 throw new RestException(500,
'Error on creating expedition :'.$this->db->lasterror());
1168 foreach ($this->commande->lines as $line) {
1169 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1171 throw new RestException(500,
'Error on creating expedition lines:'.$this->db->lasterror());
1174 return $shipment->id;
1192 unset(
$object->barcode_type_code);
1193 unset(
$object->barcode_type_label);
1194 unset(
$object->barcode_type_coder);
1208 if ($data ===
null) {
1211 $commande = array();
1212 foreach (Orders::$FIELDS as $field) {
1213 if (!isset($data[$field])) {
1214 throw new RestException(400, $field.
" field missing");
1216 $commande[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
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.
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")
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $sqlfilterlines='', $properties='', $pagination_data=false, $loadlinkedobjects=0)
List orders.
_cleanObjectDatas($object)
Clean sensible object datas.
_fetch($id, $ref='', $ref_ext='', $contact_list=-1)
Get properties of an order object.
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.
getLine($id, $lineid, $properties='')
Get properties of a line of an order object by id.
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.
getByRefExt($ref_ext, $contact_list=-1)
Get properties of an order object by ref_ext.
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.