19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
34 public 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(403);
125 $result = $this->commande->fetch($id, $ref, $ref_ext);
127 throw new RestException(404,
'Order not found');
131 throw new RestException(403,
'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);
166 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $sqlfilterlines =
'', $properties =
'')
168 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
169 throw new RestException(403);
175 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
179 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
180 $search_sale = DolibarrApiAccess::$user->id;
183 $sql =
"SELECT t.rowid";
184 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande AS t";
185 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"commande_extrafields AS ef ON (ef.fk_object = t.rowid)";
186 $sql .=
' WHERE t.entity IN ('.getEntity(
'commande').
')';
188 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
191 if ($search_sale && $search_sale !=
'-1') {
192 if ($search_sale == -2) {
193 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
194 } elseif ($search_sale > 0) {
195 $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).
")";
203 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
207 if ($sqlfilterlines) {
209 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commandedet AS tl WHERE tl.fk_commande = t.rowid";
213 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
216 $sql .= $this->db->order($sortfield, $sortorder);
221 $offset = $limit * $page;
223 $sql .= $this->db->plimit($limit + 1, $offset);
227 $result = $this->db->query($sql);
230 $num = $this->db->num_rows($result);
231 $min = min($num, ($limit <= 0 ? $num : $limit));
234 $obj = $this->db->fetch_object($result);
235 $commande_static =
new Commande($this->db);
236 if ($commande_static->fetch($obj->rowid)) {
238 $tmparray = $commande_static->liste_contact(-1,
'external', 1);
239 if (is_array($tmparray)) {
240 $commande_static->contacts_ids = $tmparray;
243 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
244 $commande_static->online_payment_url = getOnlinePaymentUrl(0,
'order', $commande_static->ref);
251 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
265 public function post($request_data =
null)
267 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
268 throw new RestException(403,
"Insuffisant rights");
271 $result = $this->
_validate($request_data);
273 foreach ($request_data as $field => $value) {
274 if ($field ===
'caller') {
276 $this->commande->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
280 $this->commande->$field = $this->
_checkValForAPI($field, $value, $this->commande);
290 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
291 throw new RestException(500,
"Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
294 return ((
int) $this->commande->id);
308 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
309 throw new RestException(403);
312 $result = $this->commande->fetch($id);
314 throw new RestException(404,
'Order not found');
318 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
320 $this->commande->getLinesArray();
322 foreach ($this->commande->lines as $line) {
338 public function postLine($id, $request_data =
null)
340 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
341 throw new RestException(403);
344 $result = $this->commande->fetch($id);
346 throw new RestException(404,
'Order not found');
350 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
353 $request_data = (object) $request_data;
355 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
356 $request_data->label =
sanitizeVal($request_data->label);
358 $updateRes = $this->commande->addline(
360 $request_data->subprice,
362 $request_data->tva_tx,
363 $request_data->localtax1_tx,
364 $request_data->localtax2_tx,
365 $request_data->fk_product,
366 $request_data->remise_percent,
367 $request_data->info_bits,
368 $request_data->fk_remise_except,
369 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
370 $request_data->subprice,
371 $request_data->date_start,
372 $request_data->date_end,
373 $request_data->product_type,
375 $request_data->special_code,
376 $request_data->fk_parent_line,
377 $request_data->fk_fournprice,
378 $request_data->pa_ht,
379 $request_data->label,
380 $request_data->array_options,
381 $request_data->fk_unit,
382 $request_data->origin,
383 $request_data->origin_id,
384 $request_data->multicurrency_subprice,
385 $request_data->ref_ext
388 if ($updateRes > 0) {
391 throw new RestException(400, $this->commande->error);
405 public function putLine($id, $lineid, $request_data =
null)
407 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
408 throw new RestException(403);
411 $result = $this->commande->fetch($id);
413 throw new RestException(404,
'Order not found');
417 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
420 $request_data = (object) $request_data;
422 $request_data->desc =
sanitizeVal($request_data->desc,
'restricthtml');
423 $request_data->label =
sanitizeVal($request_data->label);
425 $updateRes = $this->commande->updateline(
428 $request_data->subprice,
430 $request_data->remise_percent,
431 $request_data->tva_tx,
432 $request_data->localtax1_tx,
433 $request_data->localtax2_tx,
434 $request_data->price_base_type ? $request_data->price_base_type :
'HT',
435 $request_data->info_bits,
436 $request_data->date_start,
437 $request_data->date_end,
438 $request_data->product_type,
439 $request_data->fk_parent_line,
441 $request_data->fk_fournprice,
442 $request_data->pa_ht,
443 $request_data->label,
444 $request_data->special_code,
445 $request_data->array_options,
446 $request_data->fk_unit,
447 $request_data->multicurrency_subprice,
449 $request_data->ref_ext,
453 if ($updateRes > 0) {
454 $result = $this->
get($id);
455 unset($result->line);
475 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
476 throw new RestException(403);
479 $result = $this->commande->fetch($id);
481 throw new RestException(404,
'Order not found');
485 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
488 $updateRes = $this->commande->deleteLine(DolibarrApiAccess::$user, $lineid, $id);
489 if ($updateRes > 0) {
490 return $this->
get($id);
492 throw new RestException(405, $this->commande->error);
511 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'lire')) {
512 throw new RestException(403);
515 $result = $this->commande->fetch($id);
517 throw new RestException(404,
'Order not found');
521 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
524 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
544 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
545 throw new RestException(403);
548 $result = $this->commande->fetch($id);
550 throw new RestException(404,
'Order not found');
554 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
557 $result = $this->commande->add_contact($contactid, $type,
'external');
560 throw new RestException(500,
'Error when added the contact');
564 throw new RestException(304,
'contact already added');
570 'message' =>
'Contact linked to the order'
592 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
593 throw new RestException(403);
596 $result = $this->commande->fetch($id);
598 throw new RestException(404,
'Order not found');
602 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
605 $contacts = $this->commande->liste_contact();
607 foreach ($contacts as $contact) {
608 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
609 $result = $this->commande->delete_contact($contact[
'rowid']);
612 throw new RestException(500,
'Error when deleted the contact');
620 'message' =>
'Contact unlinked from order'
632 public function put($id, $request_data =
null)
634 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
635 throw new RestException(403);
638 $result = $this->commande->fetch($id);
640 throw new RestException(404,
'Order not found');
644 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
646 foreach ($request_data as $field => $value) {
647 if ($field ==
'id') {
650 if ($field ===
'caller') {
652 $this->commande->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
655 if ($field ==
'array_options' && is_array($value)) {
656 foreach ($value as $index => $val) {
657 $this->commande->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->commande);
662 $this->commande->$field = $this->
_checkValForAPI($field, $value, $this->commande);
666 if (!empty($this->commande->availability_id)) {
667 if ($this->commande->availability($this->commande->availability_id) < 0) {
668 throw new RestException(400,
'Error while updating availability');
672 if ($this->commande->update(DolibarrApiAccess::$user) > 0) {
673 return $this->
get($id);
675 throw new RestException(500, $this->commande->error);
685 public function delete($id)
687 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'supprimer')) {
688 throw new RestException(403);
690 $result = $this->commande->fetch($id);
692 throw new RestException(404,
'Order not found');
696 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
699 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
700 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
706 'message' =>
'Order deleted'
733 public function validate($id, $idwarehouse = 0, $notrigger = 0)
735 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
736 throw new RestException(403);
738 $result = $this->commande->fetch($id);
740 throw new RestException(404,
'Order not found');
743 $result = $this->commande->fetch_thirdparty();
746 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
749 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
751 throw new RestException(304,
'Error nothing done. May be object is already validated');
754 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
756 $result = $this->commande->fetch($id);
758 $this->commande->fetchObjectLinked();
761 require_once DOL_DOCUMENT_ROOT.
'/core/lib/payments.lib.php';
762 $this->commande->online_payment_url = getOnlinePaymentUrl(0,
'order', $this->commande->ref);
786 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
787 throw new RestException(403);
790 throw new RestException(400,
'Order ID is mandatory');
792 $result = $this->commande->fetch($id);
794 throw new RestException(404,
'Order not found');
797 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
799 throw new RestException(405, $this->commande->error);
800 } elseif ($result == 0) {
801 throw new RestException(304);
822 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
823 throw new RestException(403);
826 throw new RestException(400,
'Order ID is mandatory');
828 $result = $this->commande->fetch($id);
830 throw new RestException(404,
'Order not found');
833 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
835 throw new RestException(400, $this->commande->error);
838 $result = $this->commande->fetch($id);
840 throw new RestException(404,
'Order not found');
844 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
847 $this->commande->fetchObjectLinked();
861 public function close($id, $notrigger = 0)
863 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
864 throw new RestException(403);
866 $result = $this->commande->fetch($id);
868 throw new RestException(404,
'Order not found');
872 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
875 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
877 throw new RestException(304,
'Error nothing done. May be object is already closed');
880 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
883 $result = $this->commande->fetch($id);
885 throw new RestException(404,
'Order not found');
889 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
892 $this->commande->fetchObjectLinked();
908 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
909 throw new RestException(403);
911 $result = $this->commande->fetch($id);
913 throw new RestException(404,
'Order not found');
917 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
920 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
922 throw new RestException(304,
'Nothing done. May be object is already closed');
925 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
928 $result = $this->commande->fetch($id);
930 throw new RestException(404,
'Order not found');
934 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
937 $this->commande->fetchObjectLinked();
958 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
960 if (!DolibarrApiAccess::$user->hasRight(
'propal',
'lire')) {
961 throw new RestException(403);
963 if (!DolibarrApiAccess::$user->hasRight(
'commande',
'creer')) {
964 throw new RestException(403);
966 if (empty($proposalid)) {
967 throw new RestException(400,
'Proposal ID is mandatory');
970 $propal =
new Propal($this->db);
971 $result = $propal->fetch($proposalid);
973 throw new RestException(404,
'Proposal not found');
976 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
978 throw new RestException(405, $this->commande->error);
980 $this->commande->fetchObjectLinked();
1000 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1001 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'lire')) {
1002 throw new RestException(403);
1005 $sql =
"SELECT e.rowid";
1006 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expedition as e";
1007 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"expeditiondet as edet";
1008 $sql .=
" ON e.rowid = edet.fk_expedition";
1009 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commandedet as cdet";
1010 $sql .=
" ON edet.fk_elementdet = cdet.rowid";
1011 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"commande as c";
1012 $sql .=
" ON cdet.fk_commande = c.rowid";
1013 $sql .=
" WHERE c.rowid = ".((int) $id);
1014 $sql .=
" GROUP BY e.rowid";
1015 $sql .= $this->db->order(
"e.rowid",
"ASC");
1018 $result = $this->db->query($sql);
1021 $num = $this->db->num_rows($result);
1023 throw new RestException(404,
'Shipments not found ');
1027 $obj = $this->db->fetch_object($result);
1028 $shipment_static =
new Expedition($this->db);
1029 if ($shipment_static->fetch($obj->rowid)) {
1035 throw new RestException(500,
'Error when retrieve shipment list : '.$this->db->lasterror());
1056 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
1057 if (!DolibarrApiAccess::$user->hasRight(
'expedition',
'creer')) {
1058 throw new RestException(403);
1060 if ($warehouse_id <= 0) {
1061 throw new RestException(404,
'Warehouse not found');
1063 $result = $this->commande->fetch($id);
1065 throw new RestException(404,
'Order not found');
1068 $shipment->socid = $this->commande->socid;
1069 $shipment->origin_id = $this->commande->id;
1070 $result = $shipment->create(DolibarrApiAccess::$user);
1072 throw new RestException(500,
'Error on creating expedition :'.$this->db->lasterror());
1074 foreach ($this->commande->lines as $line) {
1075 $result = $shipment->create_line($warehouse_id, $line->id, $line->qty);
1077 throw new RestException(500,
'Error on creating expedition lines:'.$this->db->lasterror());
1080 return $shipment->id;
1098 unset(
$object->barcode_type_code);
1099 unset(
$object->barcode_type_label);
1100 unset(
$object->barcode_type_coder);
1114 $commande = array();
1115 foreach (Orders::$FIELDS as $field) {
1116 if (!isset($data[$field])) {
1117 throw new RestException(400, $field.
" field missing");
1119 $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.
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)
setinvoiced($id)
Classify the order as invoiced.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $sqlfilterlines='', $properties='')
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.