20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
35 public static $FIELDS = array(
53 $this->commande =
new Commande($this->db);
67 public function get(
$id, $contact_list = 1)
69 return $this->
_fetch(
$id,
'',
'', $contact_list);
85 public function getByRef($ref, $contact_list = 1)
87 return $this->
_fetch(0, $ref,
'', $contact_list);
105 return $this->
_fetch(0,
'', $ref_ext, $contact_list);
121 private function _fetch(
$id, $ref =
'', $ref_ext =
'', $contact_list = 1)
123 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
124 throw new RestException(403);
127 $result = $this->commande->fetch(
$id, $ref, $ref_ext);
129 throw new RestException(404,
'Order not found');
133 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
137 $tmparray = $this->commande->liste_contact(-1,
'external', $contact_list);
138 if (is_array($tmparray)) {
139 $this->commande->contacts_ids = $tmparray;
141 $this->commande->fetchObjectLinked();
144 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
145 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
170 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $sqlfilterlines =
'', $properties =
'', $pagination_data =
false, $loadlinkedobjects = 0)
172 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
173 throw new RestException(403);
179 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
183 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
184 $search_sale = DolibarrApiAccess::$user->id;
187 $sql =
"SELECT t.rowid";
188 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande AS t";
189 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"commande_extrafields AS ef ON (ef.fk_object = t.rowid)";
190 $sql .=
' WHERE t.entity IN ('.getEntity(
'commande').
')';
192 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
195 if ($search_sale && $search_sale !=
'-1') {
196 if ($search_sale == -2) {
197 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
198 } elseif ($search_sale > 0) {
199 $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).
")";
207 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
211 if ($sqlfilterlines) {
213 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commandedet AS tl WHERE tl.fk_commande = t.rowid";
217 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
222 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
224 $sql .= $this->db->order($sortfield, $sortorder);
229 $offset = $limit * $page;
231 $sql .= $this->db->plimit($limit + 1, $offset);
235 $result = $this->db->query($sql);
238 $num = $this->db->num_rows($result);
239 $min = min($num, ($limit <= 0 ? $num : $limit));
242 $obj = $this->db->fetch_object($result);
243 $commande_static =
new Commande($this->db);
244 if ($commande_static->fetch($obj->rowid) > 0) {
246 $tmparray = $commande_static->liste_contact(-1,
'external', 1);
247 if (is_array($tmparray)) {
248 $commande_static->contacts_ids = $tmparray;
251 if ($loadlinkedobjects) {
253 $commande_static->fetchObjectLinked();
257 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
258 $commande_static->online_payment_url = getOnlinePaymentUrl(0,
'order', $commande_static->ref);
265 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
269 if ($pagination_data) {
270 $totalsResult = $this->db->query($sqlTotals);
271 $total = $this->db->fetch_object($totalsResult)->total;
276 $obj_ret[
'data'] = $tmp;
277 $obj_ret[
'pagination'] = [
278 'total' => (int) $total,
280 'page_count' => ceil((
int) $total / $limit),
296 public function post($request_data =
null)
298 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
299 throw new RestException(403,
"Insuffisant rights");
302 $result = $this->
_validate($request_data);
304 foreach ($request_data as $field => $value) {
305 if ($field ===
'caller') {
307 $this->commande->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
311 $this->commande->$field = $this->
_checkValForAPI($field, $value, $this->commande);
321 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
322 throw new RestException(500,
"Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
325 return ((
int) $this->commande->id);
339 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
340 throw new RestException(403);
343 $result = $this->commande->fetch(
$id);
345 throw new RestException(404,
'Order not found');
349 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
351 $this->commande->getLinesArray();
353 foreach ($this->commande->lines as $line) {
371 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
372 throw new RestException(403);
375 $result = $this->commande->fetch(
$id);
377 throw new RestException(404,
'Order not found');
381 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
384 $request_data = (object) $request_data;
386 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
387 $request_data->label =
sanitizeVal($request_data->label);
389 $updateRes = $this->commande->addline(
391 $request_data->subprice,
393 $request_data->tva_tx,
394 $request_data->localtax1_tx,
395 $request_data->localtax2_tx,
396 $request_data->fk_product,
397 $request_data->remise_percent,
398 $request_data->info_bits,
399 $request_data->fk_remise_except,
400 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
401 $request_data->subprice,
402 $request_data->date_start,
403 $request_data->date_end,
404 $request_data->product_type,
406 $request_data->special_code,
407 $request_data->fk_parent_line,
408 $request_data->fk_fournprice,
409 $request_data->pa_ht,
410 $request_data->label,
411 $request_data->array_options,
412 $request_data->fk_unit,
413 $request_data->origin,
414 $request_data->origin_id,
415 $request_data->multicurrency_subprice,
416 $request_data->ref_ext
419 if ($updateRes > 0) {
422 throw new RestException(400, $this->commande->error);
438 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
439 throw new RestException(403);
442 $result = $this->commande->fetch(
$id);
444 throw new RestException(404,
'Order not found');
448 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
451 $request_data = (object) $request_data;
453 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
454 $request_data->label =
sanitizeVal($request_data->label);
456 $updateRes = $this->commande->updateline(
459 $request_data->subprice,
461 $request_data->remise_percent,
462 $request_data->tva_tx,
463 $request_data->localtax1_tx,
464 $request_data->localtax2_tx,
465 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
466 $request_data->info_bits,
467 $request_data->date_start,
468 $request_data->date_end,
469 $request_data->product_type,
470 $request_data->fk_parent_line,
472 $request_data->fk_fournprice,
473 $request_data->pa_ht,
474 $request_data->label,
475 $request_data->special_code,
476 $request_data->array_options,
477 $request_data->fk_unit,
478 $request_data->multicurrency_subprice,
480 $request_data->ref_ext,
484 if ($updateRes > 0) {
485 $result = $this->
get(
$id);
486 unset($result->line);
506 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
507 throw new RestException(403);
510 $result = $this->commande->fetch(
$id);
512 throw new RestException(404,
'Order not found');
516 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
519 $updateRes = $this->commande->deleteLine(DolibarrApiAccess::$user, $lineid,
$id);
520 if ($updateRes > 0) {
521 return $this->
get(
$id);
523 throw new RestException(405, $this->commande->error);
542 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
543 throw new RestException(403);
546 $result = $this->commande->fetch(
$id);
548 throw new RestException(404,
'Order not found');
552 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
555 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
575 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
576 throw new RestException(403);
579 $result = $this->commande->fetch(
$id);
581 throw new RestException(404,
'Order not found');
585 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
588 $result = $this->commande->add_contact($contactid, $type,
'external');
591 throw new RestException(500,
'Error when added the contact');
595 throw new RestException(304,
'contact already added');
601 'message' =>
'Contact linked to the order'
623 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
624 throw new RestException(403);
627 $result = $this->commande->fetch(
$id);
629 throw new RestException(404,
'Order not found');
633 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
636 $contacts = $this->commande->liste_contact();
638 foreach ($contacts as $contact) {
639 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
640 $result = $this->commande->delete_contact($contact[
'rowid']);
643 throw new RestException(500,
'Error when deleted the contact');
651 'message' =>
'Contact unlinked from order'
663 public function put(
$id, $request_data =
null)
665 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
666 throw new RestException(403);
669 $result = $this->commande->fetch(
$id);
671 throw new RestException(404,
'Order not found');
675 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
677 foreach ($request_data as $field => $value) {
678 if ($field ==
'id') {
681 if ($field ===
'caller') {
683 $this->commande->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
686 if ($field ==
'array_options' && is_array($value)) {
687 foreach ($value as $index => $val) {
688 $this->commande->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->commande);
693 $this->commande->$field = $this->
_checkValForAPI($field, $value, $this->commande);
697 if (!empty($this->commande->availability_id)) {
698 if ($this->commande->availability($this->commande->availability_id) < 0) {
699 throw new RestException(400,
'Error while updating availability');
703 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
704 return $this->
get(
$id);
706 throw new RestException(500, $this->commande->error);
716 public function delete(
$id)
718 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'supprimer')) {
719 throw new RestException(403);
721 $result = $this->commande->fetch(
$id);
723 throw new RestException(404,
'Order not found');
727 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
730 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
731 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
737 'message' =>
'Order deleted'
766 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
767 throw new RestException(403);
769 $result = $this->commande->fetch(
$id);
771 throw new RestException(404,
'Order not found');
774 $result = $this->commande->fetch_thirdparty();
777 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
780 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
782 throw new RestException(304,
'Error nothing done. May be object is already validated');
785 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
787 $result = $this->commande->fetch(
$id);
789 $this->commande->fetchObjectLinked();
792 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
793 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
817 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
818 throw new RestException(403);
821 throw new RestException(400,
'Order ID is mandatory');
823 $result = $this->commande->fetch(
$id);
825 throw new RestException(404,
'Order not found');
828 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
830 throw new RestException(405, $this->commande->error);
831 } elseif ($result == 0) {
832 throw new RestException(304);
853 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
854 throw new RestException(403);
857 throw new RestException(400,
'Order ID is mandatory');
859 $result = $this->commande->fetch(
$id);
861 throw new RestException(404,
'Order not found');
864 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
866 throw new RestException(400, $this->commande->error);
869 $result = $this->commande->fetch(
$id);
871 throw new RestException(404,
'Order not found');
875 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
878 $this->commande->fetchObjectLinked();
894 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
895 throw new RestException(403);
897 $result = $this->commande->fetch(
$id);
899 throw new RestException(404,
'Order not found');
903 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
906 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
908 throw new RestException(304,
'Error 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(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
923 $this->commande->fetchObjectLinked();
939 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
940 throw new RestException(403);
942 $result = $this->commande->fetch(
$id);
944 throw new RestException(404,
'Order not found');
948 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
951 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
953 throw new RestException(304,
'Nothing done. May be object is already closed');
956 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
959 $result = $this->commande->fetch(
$id);
961 throw new RestException(404,
'Order not found');
965 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
968 $this->commande->fetchObjectLinked();
989 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
991 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
992 throw new RestException(403);
994 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
995 throw new RestException(403);
997 if (empty($proposalid)) {
998 throw new RestException(400,
'Proposal ID is mandatory');
1001 $propal =
new Propal($this->db);
1002 $result = $propal->fetch($proposalid);
1004 throw new RestException(404,
'Proposal not found');
1007 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
1009 throw new RestException(405, $this->commande->error);
1011 $this->commande->fetchObjectLinked();
1031 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1032 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
1033 throw new RestException(403);
1036 $sql =
"SELECT e.rowid";
1037 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
1038 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
1039 $sql .=
" ON e.rowid = edet.fk_expedition";
1040 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
1041 $sql .=
" ON edet.fk_elementdet = cdet.rowid";
1042 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
1043 $sql .=
" ON cdet.fk_commande = c.rowid";
1044 $sql .=
" WHERE c.rowid = ".((int)
$id);
1045 $sql .=
" GROUP BY e.rowid";
1046 $sql .= $this->db->order(
"e.rowid",
"ASC");
1049 $result = $this->db->query($sql);
1052 $num = $this->db->num_rows($result);
1054 throw new RestException(404,
'Shipments not found ');
1058 $obj = $this->db->fetch_object($result);
1059 $shipment_static =
new Expedition($this->db);
1060 if ($shipment_static->fetch($obj->rowid)) {
1066 throw new RestException(500,
'Error when retrieve shipment list : '.$this->db->lasterror());
1087 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1088 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
1089 throw new RestException(403);
1091 if ($warehouse_id <= 0) {
1092 throw new RestException(404,
'Warehouse not found');
1094 $result = $this->commande->fetch(
$id);
1096 throw new RestException(404,
'Order not found');
1099 $shipment->socid = $this->commande->socid;
1100 $shipment->origin_id = $this->commande->id;
1101 $shipment->origin = $this->commande->element;
1102 $result = $shipment->create(DolibarrApiAccess::$user);
1104 throw new RestException(500,
'Error on creating expedition :'.$this->db->lasterror());
1106 foreach ($this->commande->lines as $line) {
1107 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1109 throw new RestException(500,
'Error on creating expedition lines:'.$this->db->lasterror());
1112 return $shipment->id;
1130 unset(
$object->barcode_type_code);
1131 unset(
$object->barcode_type_label);
1132 unset(
$object->barcode_type_coder);
1146 $commande = array();
1147 foreach (Orders::$FIELDS as $field) {
1148 if (!isset($data[$field])) {
1149 throw new RestException(400, $field.
" field missing");
1151 $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.
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.
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.
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.