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 $this->commande->$field = $value;
660 if (!empty($this->commande->availability_id)) {
661 if ($this->commande->availability($this->commande->availability_id) < 0) {
662 throw new RestException(400,
'Error while updating availability');
666 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
667 return $this->
get($id);
669 throw new RestException(500, $this->commande->error);
679 public function delete($id)
681 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
682 throw new RestException(401);
684 $result = $this->commande->fetch($id);
686 throw new RestException(404,
'Order not found');
690 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
693 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
694 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
700 'message' =>
'Order deleted'
727 public function validate($id, $idwarehouse = 0, $notrigger = 0)
729 if (!DolibarrApiAccess::$user->rights->commande->creer) {
730 throw new RestException(401);
732 $result = $this->commande->fetch($id);
734 throw new RestException(404,
'Order not found');
737 $result = $this->commande->fetch_thirdparty();
740 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
743 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
745 throw new RestException(304,
'Error nothing done. May be object is already validated');
748 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
750 $result = $this->commande->fetch($id);
752 $this->commande->fetchObjectLinked();
755 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
756 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
780 if (!DolibarrApiAccess::$user->rights->commande->creer) {
781 throw new RestException(401);
784 throw new RestException(400,
'Order ID is mandatory');
786 $result = $this->commande->fetch($id);
788 throw new RestException(404,
'Order not found');
791 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
793 throw new RestException(405, $this->commande->error);
794 } elseif ($result == 0) {
795 throw new RestException(304);
817 if (!DolibarrApiAccess::$user->rights->commande->creer) {
818 throw new RestException(401);
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->classifyBilled(DolibarrApiAccess::$user);
830 throw new RestException(400, $this->commande->error);
833 $result = $this->commande->fetch($id);
835 throw new RestException(404,
'Order not found');
839 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
842 $this->commande->fetchObjectLinked();
856 public function close($id, $notrigger = 0)
858 if (!DolibarrApiAccess::$user->rights->commande->creer) {
859 throw new RestException(401);
861 $result = $this->commande->fetch($id);
863 throw new RestException(404,
'Order not found');
867 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
870 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
872 throw new RestException(304,
'Error nothing done. May be object is already closed');
875 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
878 $result = $this->commande->fetch($id);
880 throw new RestException(404,
'Order not found');
884 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
887 $this->commande->fetchObjectLinked();
903 if (!DolibarrApiAccess::$user->rights->commande->creer) {
904 throw new RestException(401);
906 $result = $this->commande->fetch($id);
908 throw new RestException(404,
'Order not found');
912 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
915 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
917 throw new RestException(304,
'Nothing done. May be object is already closed');
920 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
923 $result = $this->commande->fetch($id);
925 throw new RestException(404,
'Order not found');
929 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
932 $this->commande->fetchObjectLinked();
954 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
956 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
957 throw new RestException(401);
959 if (!DolibarrApiAccess::$user->rights->commande->creer) {
960 throw new RestException(401);
962 if (empty($proposalid)) {
963 throw new RestException(400,
'Proposal ID is mandatory');
966 $propal =
new Propal($this->db);
967 $result = $propal->fetch($proposalid);
969 throw new RestException(404,
'Proposal not found');
972 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
974 throw new RestException(405, $this->commande->error);
976 $this->commande->fetchObjectLinked();
996 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
997 if (!DolibarrApiAccess::$user->rights->expedition->lire) {
998 throw new RestException(401);
1001 $sql =
"SELECT e.rowid";
1002 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
1003 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
1004 $sql .=
" ON e.rowid = edet.fk_expedition";
1005 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
1006 $sql .=
" ON edet.fk_origin_line = cdet.rowid";
1007 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
1008 $sql .=
" ON cdet.fk_commande = c.rowid";
1009 $sql .=
" WHERE c.rowid = ".((int) $id);
1010 $sql .=
" GROUP BY e.rowid";
1011 $sql .= $this->db->order(
"e.rowid",
"ASC");
1014 $result = $this->db->query($sql);
1017 $num = $this->db->num_rows($result);
1019 throw new RestException(404,
'Shipments not found ');
1023 $obj = $this->db->fetch_object($result);
1024 $shipment_static =
new Expedition($this->db);
1025 if ($shipment_static->fetch($obj->rowid)) {
1031 throw new RestException(500,
'Error when retrieve shipment list : '.$this->db->lasterror());
1052 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1053 if (!DolibarrApiAccess::$user->rights->expedition->creer) {
1054 throw new RestException(401);
1056 if ($warehouse_id <= 0) {
1057 throw new RestException(404,
'Warehouse not found');
1059 $result = $this->commande->fetch($id);
1061 throw new RestException(404,
'Order not found');
1064 $shipment->socid = $this->commande->socid;
1065 $shipment->origin_id = $this->commande->id;
1066 $result = $shipment->create(DolibarrApiAccess::$user);
1068 throw new RestException(500,
'Error on creating expedition :'.$this->db->lasterror());
1070 foreach ($this->commande->lines as $line) {
1071 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1073 throw new RestException(500,
'Error on creating expedition lines:'.$this->db->lasterror());
1076 return $shipment->id;
1089 $object = parent::_cleanObjectDatas($object);
1091 unset($object->note);
1092 unset($object->address);
1093 unset($object->barcode_type);
1094 unset($object->barcode_type_code);
1095 unset($object->barcode_type_label);
1096 unset($object->barcode_type_coder);
1110 $commande = array();
1111 foreach (Orders::$FIELDS as $field) {
1112 if (!isset($data[$field])) {
1113 throw new RestException(400, $field.
" field missing");
1115 $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.
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.