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);
510 $result = $orderline->fetch($lineid);
512 throw new RestException(404,
'Order line not found');
515 if ($orderline->fk_commande !=
$id) {
516 throw new RestException(403,
'Line does not belong to this order');
519 $updateRes = $this->commande->updateline(
522 $request_data->subprice,
524 $request_data->remise_percent,
525 $request_data->tva_tx,
526 $request_data->localtax1_tx,
527 $request_data->localtax2_tx,
528 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
529 $request_data->info_bits,
530 $request_data->date_start,
531 $request_data->date_end,
532 $request_data->product_type,
533 $request_data->fk_parent_line,
535 $request_data->fk_fournprice,
536 $request_data->pa_ht,
537 $request_data->label,
538 $request_data->special_code,
539 $request_data->array_options,
540 $request_data->fk_unit,
541 $request_data->multicurrency_subprice,
543 $request_data->ref_ext,
547 if ($updateRes > 0) {
548 $result = $this->
get(
$id);
549 unset($result->line);
569 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
570 throw new RestException(403);
573 $result = $this->commande->fetch(
$id);
575 throw new RestException(404,
'Order not found');
579 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
582 $updateRes = $this->commande->deleteLine(DolibarrApiAccess::$user, $lineid,
$id);
583 if ($updateRes > 0) {
584 return $this->
get(
$id);
586 throw new RestException(405, $this->commande->error);
605 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
606 throw new RestException(403);
609 $result = $this->commande->fetch(
$id);
611 throw new RestException(404,
'Order not found');
615 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
618 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
640 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
641 throw new RestException(403);
644 $result = $this->commande->fetch(
$id);
646 throw new RestException(404,
'Order not found');
650 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
653 $result = $this->commande->add_contact($contactid, $type,
'external');
656 throw new RestException(500,
'Error when added the contact');
660 throw new RestException(304,
'contact already added');
666 'message' =>
'Contact linked to the order'
690 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
691 throw new RestException(403);
694 $result = $this->commande->fetch(
$id);
696 throw new RestException(404,
'Order not found');
700 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
703 $contacts = $this->commande->liste_contact();
705 foreach ($contacts as $contact) {
706 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
707 $result = $this->commande->delete_contact($contact[
'rowid']);
710 throw new RestException(500,
'Error when deleted the contact');
718 'message' =>
'Contact unlinked from order'
732 public function put(
$id, $request_data =
null)
734 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
735 throw new RestException(403);
738 $result = $this->commande->fetch(
$id);
740 throw new RestException(404,
'Order not found');
744 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
746 foreach ($request_data as $field => $value) {
747 if ($field ==
'id') {
750 if ($field ===
'caller') {
752 $this->commande->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
755 if ($field ==
'array_options' && is_array($value)) {
756 foreach ($value as $index => $val) {
757 $this->commande->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->commande);
762 $this->commande->$field = $this->
_checkValForAPI($field, $value, $this->commande);
766 if (!empty($this->commande->availability_id)) {
767 if ($this->commande->availability($this->commande->availability_id) < 0) {
768 throw new RestException(400,
'Error while updating availability');
772 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
773 return $this->
get(
$id);
775 throw new RestException(500, $this->commande->error);
787 public function delete(
$id)
789 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'supprimer')) {
790 throw new RestException(403);
792 $result = $this->commande->fetch(
$id);
794 throw new RestException(404,
'Order not found');
798 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
801 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
802 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
808 'message' =>
'Order deleted'
836 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
837 throw new RestException(403);
839 $result = $this->commande->fetch(
$id);
841 throw new RestException(404,
'Order not found');
844 $result = $this->commande->fetch_thirdparty();
847 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
850 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
852 throw new RestException(304,
'Error nothing done. May be object is already validated');
855 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
857 $result = $this->commande->fetch(
$id);
859 $this->commande->fetchObjectLinked();
862 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
863 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
887 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
888 throw new RestException(403);
891 throw new RestException(400,
'Order ID is mandatory');
893 $result = $this->commande->fetch(
$id);
895 throw new RestException(404,
'Order not found');
898 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
900 throw new RestException(405, $this->commande->error);
901 } elseif ($result == 0) {
902 throw new RestException(304);
923 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
924 throw new RestException(403);
927 throw new RestException(400,
'Order ID is mandatory');
929 $result = $this->commande->fetch(
$id);
931 throw new RestException(404,
'Order not found');
934 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
936 throw new RestException(400, $this->commande->error);
939 $result = $this->commande->fetch(
$id);
941 throw new RestException(404,
'Order not found');
945 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
948 $this->commande->fetchObjectLinked();
964 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
965 throw new RestException(403);
967 $result = $this->commande->fetch(
$id);
969 throw new RestException(404,
'Order not found');
973 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
976 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
978 throw new RestException(304,
'Error nothing done. May be object is already closed');
981 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
984 $result = $this->commande->fetch(
$id);
986 throw new RestException(404,
'Order not found');
990 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
993 $this->commande->fetchObjectLinked();
1009 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
1010 throw new RestException(403);
1012 $result = $this->commande->fetch(
$id);
1014 throw new RestException(404,
'Order not found');
1018 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1021 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
1023 throw new RestException(304,
'Nothing done. May be object is already closed');
1026 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
1029 $result = $this->commande->fetch(
$id);
1031 throw new RestException(404,
'Order not found');
1035 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1038 $this->commande->fetchObjectLinked();
1059 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
1061 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
1062 throw new RestException(403);
1064 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
1065 throw new RestException(403);
1067 if (empty($proposalid)) {
1068 throw new RestException(400,
'Proposal ID is mandatory');
1071 $propal =
new Propal($this->db);
1072 $result = $propal->fetch($proposalid);
1074 throw new RestException(404,
'Proposal not found');
1077 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
1079 throw new RestException(405, $this->commande->error);
1081 $this->commande->fetchObjectLinked();
1103 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1104 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
1105 throw new RestException(403);
1108 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1111 $sql =
"SELECT e.rowid";
1112 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
1113 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
1114 $sql .=
" ON e.rowid = edet.fk_expedition";
1115 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
1116 $sql .=
" ON edet.fk_elementdet = cdet.rowid";
1117 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
1118 $sql .=
" ON cdet.fk_commande = c.rowid";
1119 $sql .=
" WHERE c.rowid = ".((int)
$id);
1120 $sql .=
" GROUP BY e.rowid";
1121 $sql .= $this->db->order(
"e.rowid",
"ASC");
1124 $result = $this->db->query($sql);
1127 $num = $this->db->num_rows($result);
1129 throw new RestException(404,
'Shipments not found ');
1133 $obj = $this->db->fetch_object($result);
1134 $shipment_static =
new Expedition($this->db);
1135 if ($shipment_static->fetch($obj->rowid)) {
1141 throw new RestException(500,
'Error when retrieve shipment list : '.$this->db->lasterror());
1162 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1163 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
1164 throw new RestException(403);
1166 if ($warehouse_id <= 0) {
1167 throw new RestException(404,
'Warehouse not found');
1169 $result = $this->commande->fetch(
$id);
1171 throw new RestException(404,
'Order not found');
1174 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1177 $shipment->socid = $this->commande->socid;
1178 $shipment->origin_id = $this->commande->id;
1179 $shipment->origin = $this->commande->element;
1180 $result = $shipment->create(DolibarrApiAccess::$user);
1182 throw new RestException(500,
'Error on creating expedition :'.$this->db->lasterror());
1184 foreach ($this->commande->lines as $line) {
1185 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1187 throw new RestException(500,
'Error on creating expedition lines:'.$this->db->lasterror());
1190 return $shipment->id;
1208 unset(
$object->barcode_type_code);
1209 unset(
$object->barcode_type_label);
1210 unset(
$object->barcode_type_coder);
1224 if ($data ===
null) {
1227 $commande = array();
1228 foreach (Orders::$FIELDS as $field) {
1229 if (!isset($data[$field])) {
1230 throw new RestException(400, $field.
" field missing");
1232 $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.
Class to manage order lines.
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.