20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
35 public static $FIELDS = array(
52 $this->commande =
new Commande($this->db);
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(0, $ref,
'', $contact_list);
104 return $this->
_fetch(0,
'', $ref_ext, $contact_list);
120 private function _fetch(
$id, $ref =
'', $ref_ext =
'', $contact_list = 1)
122 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
123 throw new RestException(403);
126 $result = $this->commande->fetch(
$id, $ref, $ref_ext);
128 throw new RestException(404,
'Order not found');
132 throw new RestException(403,
'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);
168 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $sqlfilterlines =
'', $properties =
'', $pagination_data =
false)
170 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
171 throw new RestException(403);
177 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
181 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
182 $search_sale = DolibarrApiAccess::$user->id;
185 $sql =
"SELECT t.rowid";
186 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande AS t";
187 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"commande_extrafields AS ef ON (ef.fk_object = t.rowid)";
188 $sql .=
' WHERE t.entity IN ('.getEntity(
'commande').
')';
190 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
193 if ($search_sale && $search_sale !=
'-1') {
194 if ($search_sale == -2) {
195 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
196 } elseif ($search_sale > 0) {
197 $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).
")";
205 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
209 if ($sqlfilterlines) {
211 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commandedet AS tl WHERE tl.fk_commande = t.rowid";
215 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
220 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
222 $sql .= $this->db->order($sortfield, $sortorder);
227 $offset = $limit * $page;
229 $sql .= $this->db->plimit($limit + 1, $offset);
233 $result = $this->db->query($sql);
236 $num = $this->db->num_rows($result);
237 $min = min($num, ($limit <= 0 ? $num : $limit));
240 $obj = $this->db->fetch_object($result);
241 $commande_static =
new Commande($this->db);
242 if ($commande_static->fetch($obj->rowid)) {
244 $tmparray = $commande_static->liste_contact(-1,
'external', 1);
245 if (is_array($tmparray)) {
246 $commande_static->contacts_ids = $tmparray;
249 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
250 $commande_static->online_payment_url = getOnlinePaymentUrl(0,
'order', $commande_static->ref);
257 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
261 if ($pagination_data) {
262 $totalsResult = $this->db->query($sqlTotals);
263 $total = $this->db->fetch_object($totalsResult)->total;
268 $obj_ret[
'data'] = $tmp;
269 $obj_ret[
'pagination'] = [
270 'total' => (int) $total,
272 'page_count' => ceil((
int) $total / $limit),
288 public function post($request_data =
null)
290 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
291 throw new RestException(403,
"Insuffisant rights");
294 $result = $this->
_validate($request_data);
296 foreach ($request_data as $field => $value) {
297 if ($field ===
'caller') {
299 $this->commande->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
303 $this->commande->$field = $this->
_checkValForAPI($field, $value, $this->commande);
313 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
314 throw new RestException(500,
"Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
317 return ((
int) $this->commande->id);
331 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
332 throw new RestException(403);
335 $result = $this->commande->fetch(
$id);
337 throw new RestException(404,
'Order not found');
341 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
343 $this->commande->getLinesArray();
345 foreach ($this->commande->lines as $line) {
363 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
364 throw new RestException(403);
367 $result = $this->commande->fetch(
$id);
369 throw new RestException(404,
'Order not found');
373 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
376 $request_data = (object) $request_data;
378 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
379 $request_data->label =
sanitizeVal($request_data->label);
381 $updateRes = $this->commande->addline(
383 $request_data->subprice,
385 $request_data->tva_tx,
386 $request_data->localtax1_tx,
387 $request_data->localtax2_tx,
388 $request_data->fk_product,
389 $request_data->remise_percent,
390 $request_data->info_bits,
391 $request_data->fk_remise_except,
392 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
393 $request_data->subprice,
394 $request_data->date_start,
395 $request_data->date_end,
396 $request_data->product_type,
398 $request_data->special_code,
399 $request_data->fk_parent_line,
400 $request_data->fk_fournprice,
401 $request_data->pa_ht,
402 $request_data->label,
403 $request_data->array_options,
404 $request_data->fk_unit,
405 $request_data->origin,
406 $request_data->origin_id,
407 $request_data->multicurrency_subprice,
408 $request_data->ref_ext
411 if ($updateRes > 0) {
414 throw new RestException(400, $this->commande->error);
430 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
431 throw new RestException(403);
434 $result = $this->commande->fetch(
$id);
436 throw new RestException(404,
'Order not found');
440 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
443 $request_data = (object) $request_data;
445 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
446 $request_data->label =
sanitizeVal($request_data->label);
448 $updateRes = $this->commande->updateline(
451 $request_data->subprice,
453 $request_data->remise_percent,
454 $request_data->tva_tx,
455 $request_data->localtax1_tx,
456 $request_data->localtax2_tx,
457 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
458 $request_data->info_bits,
459 $request_data->date_start,
460 $request_data->date_end,
461 $request_data->product_type,
462 $request_data->fk_parent_line,
464 $request_data->fk_fournprice,
465 $request_data->pa_ht,
466 $request_data->label,
467 $request_data->special_code,
468 $request_data->array_options,
469 $request_data->fk_unit,
470 $request_data->multicurrency_subprice,
472 $request_data->ref_ext,
476 if ($updateRes > 0) {
477 $result = $this->
get(
$id);
478 unset($result->line);
498 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
499 throw new RestException(403);
502 $result = $this->commande->fetch(
$id);
504 throw new RestException(404,
'Order not found');
508 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
511 $updateRes = $this->commande->deleteLine(DolibarrApiAccess::$user, $lineid,
$id);
512 if ($updateRes > 0) {
513 return $this->
get(
$id);
515 throw new RestException(405, $this->commande->error);
534 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
535 throw new RestException(403);
538 $result = $this->commande->fetch(
$id);
540 throw new RestException(404,
'Order not found');
544 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
547 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
567 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
568 throw new RestException(403);
571 $result = $this->commande->fetch(
$id);
573 throw new RestException(404,
'Order not found');
577 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
580 $result = $this->commande->add_contact($contactid, $type,
'external');
583 throw new RestException(500,
'Error when added the contact');
587 throw new RestException(304,
'contact already added');
593 'message' =>
'Contact linked to the order'
615 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
616 throw new RestException(403);
619 $result = $this->commande->fetch(
$id);
621 throw new RestException(404,
'Order not found');
625 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
628 $contacts = $this->commande->liste_contact();
630 foreach ($contacts as $contact) {
631 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
632 $result = $this->commande->delete_contact($contact[
'rowid']);
635 throw new RestException(500,
'Error when deleted the contact');
643 'message' =>
'Contact unlinked from order'
655 public function put(
$id, $request_data =
null)
657 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
658 throw new RestException(403);
661 $result = $this->commande->fetch(
$id);
663 throw new RestException(404,
'Order not found');
667 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
669 foreach ($request_data as $field => $value) {
670 if ($field ==
'id') {
673 if ($field ===
'caller') {
675 $this->commande->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
678 if ($field ==
'array_options' && is_array($value)) {
679 foreach ($value as $index => $val) {
680 $this->commande->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->commande);
685 $this->commande->$field = $this->
_checkValForAPI($field, $value, $this->commande);
689 if (!empty($this->commande->availability_id)) {
690 if ($this->commande->availability($this->commande->availability_id) < 0) {
691 throw new RestException(400,
'Error while updating availability');
695 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
696 return $this->
get(
$id);
698 throw new RestException(500, $this->commande->error);
708 public function delete(
$id)
710 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'supprimer')) {
711 throw new RestException(403);
713 $result = $this->commande->fetch(
$id);
715 throw new RestException(404,
'Order not found');
719 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
722 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
723 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
729 'message' =>
'Order deleted'
758 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
759 throw new RestException(403);
761 $result = $this->commande->fetch(
$id);
763 throw new RestException(404,
'Order not found');
766 $result = $this->commande->fetch_thirdparty();
769 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
772 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
774 throw new RestException(304,
'Error nothing done. May be object is already validated');
777 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
779 $result = $this->commande->fetch(
$id);
781 $this->commande->fetchObjectLinked();
784 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
785 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
809 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
810 throw new RestException(403);
813 throw new RestException(400,
'Order ID is mandatory');
815 $result = $this->commande->fetch(
$id);
817 throw new RestException(404,
'Order not found');
820 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
822 throw new RestException(405, $this->commande->error);
823 } elseif ($result == 0) {
824 throw new RestException(304);
845 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
846 throw new RestException(403);
849 throw new RestException(400,
'Order ID is mandatory');
851 $result = $this->commande->fetch(
$id);
853 throw new RestException(404,
'Order not found');
856 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
858 throw new RestException(400, $this->commande->error);
861 $result = $this->commande->fetch(
$id);
863 throw new RestException(404,
'Order not found');
867 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
870 $this->commande->fetchObjectLinked();
886 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
887 throw new RestException(403);
889 $result = $this->commande->fetch(
$id);
891 throw new RestException(404,
'Order not found');
895 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
898 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
900 throw new RestException(304,
'Error nothing done. May be object is already closed');
903 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
906 $result = $this->commande->fetch(
$id);
908 throw new RestException(404,
'Order not found');
912 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
915 $this->commande->fetchObjectLinked();
931 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
932 throw new RestException(403);
934 $result = $this->commande->fetch(
$id);
936 throw new RestException(404,
'Order not found');
940 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
943 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
945 throw new RestException(304,
'Nothing done. May be object is already closed');
948 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
951 $result = $this->commande->fetch(
$id);
953 throw new RestException(404,
'Order not found');
957 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
960 $this->commande->fetchObjectLinked();
981 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
983 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
984 throw new RestException(403);
986 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
987 throw new RestException(403);
989 if (empty($proposalid)) {
990 throw new RestException(400,
'Proposal ID is mandatory');
993 $propal =
new Propal($this->db);
994 $result = $propal->fetch($proposalid);
996 throw new RestException(404,
'Proposal not found');
999 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
1001 throw new RestException(405, $this->commande->error);
1003 $this->commande->fetchObjectLinked();
1023 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1024 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
1025 throw new RestException(403);
1028 $sql =
"SELECT e.rowid";
1029 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
1030 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
1031 $sql .=
" ON e.rowid = edet.fk_expedition";
1032 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
1033 $sql .=
" ON edet.fk_elementdet = cdet.rowid";
1034 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
1035 $sql .=
" ON cdet.fk_commande = c.rowid";
1036 $sql .=
" WHERE c.rowid = ".((int)
$id);
1037 $sql .=
" GROUP BY e.rowid";
1038 $sql .= $this->db->order(
"e.rowid",
"ASC");
1041 $result = $this->db->query($sql);
1044 $num = $this->db->num_rows($result);
1046 throw new RestException(404,
'Shipments not found ');
1050 $obj = $this->db->fetch_object($result);
1051 $shipment_static =
new Expedition($this->db);
1052 if ($shipment_static->fetch($obj->rowid)) {
1058 throw new RestException(500,
'Error when retrieve shipment list : '.$this->db->lasterror());
1079 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1080 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
1081 throw new RestException(403);
1083 if ($warehouse_id <= 0) {
1084 throw new RestException(404,
'Warehouse not found');
1086 $result = $this->commande->fetch(
$id);
1088 throw new RestException(404,
'Order not found');
1091 $shipment->socid = $this->commande->socid;
1092 $shipment->origin_id = $this->commande->id;
1093 $shipment->origin = $this->commande->element;
1094 $result = $shipment->create(DolibarrApiAccess::$user);
1096 throw new RestException(500,
'Error on creating expedition :'.$this->db->lasterror());
1098 foreach ($this->commande->lines as $line) {
1099 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1101 throw new RestException(500,
'Error on creating expedition lines:'.$this->db->lasterror());
1104 return $shipment->id;
1122 unset(
$object->barcode_type_code);
1123 unset(
$object->barcode_type_label);
1124 unset(
$object->barcode_type_coder);
1138 $commande = array();
1139 foreach (Orders::$FIELDS as $field) {
1140 if (!isset($data[$field])) {
1141 throw new RestException(400, $field.
" field missing");
1143 $commande[$field] = $data[$field];
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")
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='', $pagination_data=false)
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...