19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
35 static $FIELDS = array(
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(
'', $ref,
'', $contact_list);
104 return $this->
_fetch(
'',
'', $ref_ext, $contact_list);
120 private function _fetch($id, $ref =
'', $ref_ext =
'', $contact_list = 1)
122 if (!DolibarrApiAccess::$user->rights->commande->lire) {
123 throw new RestException(401);
126 $result = $this->commande->fetch($id, $ref, $ref_ext);
128 throw new RestException(404,
'Order not found');
132 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
136 $this->commande->contacts_ids = $this->commande->liste_contact(-1,
'external', $contact_list);
137 $this->commande->fetchObjectLinked();
157 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'')
164 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
168 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
170 $sql =
"SELECT t.rowid";
171 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
172 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande as t";
174 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
176 $sql .=
' WHERE t.entity IN ('.getEntity(
'commande').
')';
177 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
" AND t.fk_soc = sc.fk_soc";
178 if ($socids) $sql .=
" AND t.fk_soc IN (".$socids.
")";
179 if ($search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
181 if ($search_sale > 0)
183 $sql .=
" AND sc.fk_user = ".$search_sale;
190 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
192 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
193 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
196 $sql .= $this->
db->order($sortfield, $sortorder);
202 $offset = $limit * $page;
204 $sql .= $this->
db->plimit($limit + 1, $offset);
208 $result = $this->
db->query($sql);
212 $num = $this->
db->num_rows($result);
213 $min = min($num, ($limit <= 0 ? $num : $limit));
217 $obj = $this->
db->fetch_object($result);
219 if ($commande_static->fetch($obj->rowid)) {
221 $commande_static->contacts_ids = $commande_static->liste_contact(-1,
'external', 1);
227 throw new RestException(503,
'Error when retrieve commande list : '.$this->
db->lasterror());
229 if (!count($obj_ret)) {
230 throw new RestException(404,
'No order found');
243 public function post($request_data = null)
245 if (!DolibarrApiAccess::$user->rights->commande->creer) {
246 throw new RestException(401,
"Insuffisant rights");
249 $result = $this->
_validate($request_data);
251 foreach ($request_data as $field => $value) {
252 $this->commande->$field = $value;
262 if ($this->commande->create(DolibarrApiAccess::$user) < 0) {
263 throw new RestException(500,
"Error creating order", array_merge(array($this->commande->error), $this->commande->errors));
266 return $this->commande->id;
280 if (!DolibarrApiAccess::$user->rights->commande->lire) {
281 throw new RestException(401);
284 $result = $this->commande->fetch($id);
286 throw new RestException(404,
'Order not found');
290 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
292 $this->commande->getLinesArray();
294 foreach ($this->commande->lines as $line) {
310 public function postLine($id, $request_data = null)
312 if (!DolibarrApiAccess::$user->rights->commande->creer) {
313 throw new RestException(401);
316 $result = $this->commande->fetch($id);
318 throw new RestException(404,
'Order not found');
322 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
324 $request_data = (object) $request_data;
325 $updateRes = $this->commande->addline(
327 $request_data->subprice,
329 $request_data->tva_tx,
330 $request_data->localtax1_tx,
331 $request_data->localtax2_tx,
332 $request_data->fk_product,
333 $request_data->remise_percent,
334 $request_data->info_bits,
335 $request_data->fk_remise_except,
338 $request_data->date_start,
339 $request_data->date_end,
340 $request_data->product_type,
342 $request_data->special_code,
343 $request_data->fk_parent_line,
344 $request_data->fk_fournprice,
345 $request_data->pa_ht,
346 $request_data->label,
347 $request_data->array_options,
348 $request_data->fk_unit,
349 $request_data->origin,
350 $request_data->origin_id,
351 $request_data->multicurrency_subprice,
352 $request_data->ref_ext
355 if ($updateRes > 0) {
358 throw new RestException(400, $this->commande->error);
373 public function putLine($id, $lineid, $request_data = null)
375 if (!DolibarrApiAccess::$user->rights->commande->creer) {
376 throw new RestException(401);
379 $result = $this->commande->fetch($id);
381 throw new RestException(404,
'Order not found');
385 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
387 $request_data = (object) $request_data;
388 $updateRes = $this->commande->updateline(
391 $request_data->subprice,
393 $request_data->remise_percent,
394 $request_data->tva_tx,
395 $request_data->localtax1_tx,
396 $request_data->localtax2_tx,
398 $request_data->info_bits,
399 $request_data->date_start,
400 $request_data->date_end,
401 $request_data->product_type,
402 $request_data->fk_parent_line,
404 $request_data->fk_fournprice,
405 $request_data->pa_ht,
406 $request_data->label,
407 $request_data->special_code,
408 $request_data->array_options,
409 $request_data->fk_unit,
410 $request_data->multicurrency_subprice,
412 $request_data->ref_ext
415 if ($updateRes > 0) {
416 $result = $this->
get($id);
417 unset($result->line);
439 if (!DolibarrApiAccess::$user->rights->commande->creer) {
440 throw new RestException(401);
443 $result = $this->commande->fetch($id);
445 throw new RestException(404,
'Order not found');
449 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
454 $updateRes = $this->commande->deleteline(DolibarrApiAccess::$user, $lineid);
455 if ($updateRes > 0) {
456 return $this->
get($id);
458 throw new RestException(405, $this->commande->error);
478 if (!DolibarrApiAccess::$user->rights->commande->lire) {
479 throw new RestException(401);
482 $result = $this->commande->fetch($id);
484 throw new RestException(404,
'Order not found');
488 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
491 $contacts = $this->commande->liste_contact(-1,
'external', 0, $type);
512 if (!DolibarrApiAccess::$user->rights->commande->creer) {
513 throw new RestException(401);
516 $result = $this->commande->fetch($id);
518 throw new RestException(404,
'Order not found');
522 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
525 $result = $this->commande->add_contact($contactid, $type,
'external');
528 throw new RestException(500,
'Error when added the contact');
532 throw new RestException(304,
'contact already added');
538 'message' =>
'Contact linked to the order'
560 if (!DolibarrApiAccess::$user->rights->commande->creer) {
561 throw new RestException(401);
564 $result = $this->commande->fetch($id);
566 throw new RestException(404,
'Order not found');
570 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
573 $contacts = $this->commande->liste_contact();
575 foreach ($contacts as $contact) {
576 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
577 $result = $this->commande->delete_contact($contact[
'rowid']);
580 throw new RestException(500,
'Error when deleted the contact');
588 'message' =>
'Contact unlinked from order'
601 public function put($id, $request_data = null)
603 if (!DolibarrApiAccess::$user->rights->commande->creer) {
604 throw new RestException(401);
607 $result = $this->commande->fetch($id);
609 throw new RestException(404,
'Order not found');
613 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
615 foreach ($request_data as $field => $value) {
616 if ($field ==
'id')
continue;
617 $this->commande->$field = $value;
621 if (!empty($this->commande->availability_id)) {
622 if ($this->commande->availability($this->commande->availability_id) < 0)
623 throw new RestException(400,
'Error while updating availability');
626 if ($this->commande->update(DolibarrApiAccess::$user) > 0)
628 return $this->
get($id);
630 throw new RestException(500, $this->commande->error);
640 public function delete($id)
642 if (!DolibarrApiAccess::$user->rights->commande->supprimer) {
643 throw new RestException(401);
645 $result = $this->commande->fetch($id);
647 throw new RestException(404,
'Order not found');
651 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
654 if (!$this->commande->delete(DolibarrApiAccess::$user)) {
655 throw new RestException(500,
'Error when deleting order : '.$this->commande->error);
661 'message' =>
'Order deleted'
688 public function validate($id, $idwarehouse = 0, $notrigger = 0)
690 if (!DolibarrApiAccess::$user->rights->commande->creer) {
691 throw new RestException(401);
693 $result = $this->commande->fetch($id);
695 throw new RestException(404,
'Order not found');
699 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
702 $result = $this->commande->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
704 throw new RestException(304,
'Error nothing done. May be object is already validated');
707 throw new RestException(500,
'Error when validating Order: '.$this->commande->error);
709 $result = $this->commande->fetch($id);
711 throw new RestException(404,
'Order not found');
715 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
718 $this->commande->fetchObjectLinked();
743 if (!DolibarrApiAccess::$user->rights->commande->creer) {
744 throw new RestException(401);
747 throw new RestException(400,
'Order ID is mandatory');
749 $result = $this->commande->fetch($id);
751 throw new RestException(404,
'Order not found');
754 $result = $this->commande->set_reopen(DolibarrApiAccess::$user);
756 throw new RestException(405, $this->commande->error);
757 } elseif ($result == 0) {
758 throw new RestException(304);
781 if (!DolibarrApiAccess::$user->rights->commande->creer) {
782 throw new RestException(401);
785 throw new RestException(400,
'Order ID is mandatory');
787 $result = $this->commande->fetch($id);
789 throw new RestException(404,
'Order not found');
792 $result = $this->commande->classifyBilled(DolibarrApiAccess::$user);
794 throw new RestException(400, $this->commande->error);
797 $result = $this->commande->fetch($id);
799 throw new RestException(404,
'Order not found');
803 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
806 $this->commande->fetchObjectLinked();
821 public function close($id, $notrigger = 0)
823 if (!DolibarrApiAccess::$user->rights->commande->creer) {
824 throw new RestException(401);
826 $result = $this->commande->fetch($id);
828 throw new RestException(404,
'Order not found');
832 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
835 $result = $this->commande->cloture(DolibarrApiAccess::$user, $notrigger);
837 throw new RestException(304,
'Error nothing done. May be object is already closed');
840 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
843 $result = $this->commande->fetch($id);
845 throw new RestException(404,
'Order not found');
849 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
852 $this->commande->fetchObjectLinked();
869 if (!DolibarrApiAccess::$user->rights->commande->creer) {
870 throw new RestException(401);
872 $result = $this->commande->fetch($id);
874 throw new RestException(404,
'Order not found');
878 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
881 $result = $this->commande->setDraft(DolibarrApiAccess::$user, $idwarehouse);
883 throw new RestException(304,
'Nothing done. May be object is already closed');
886 throw new RestException(500,
'Error when closing Order: '.$this->commande->error);
889 $result = $this->commande->fetch($id);
891 throw new RestException(404,
'Order not found');
895 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
898 $this->commande->fetchObjectLinked();
921 require_once DOL_DOCUMENT_ROOT.
'/comm/propal/class/propal.class.php';
923 if (!DolibarrApiAccess::$user->rights->propal->lire) {
924 throw new RestException(401);
926 if (!DolibarrApiAccess::$user->rights->commande->creer) {
927 throw new RestException(401);
929 if (empty($proposalid)) {
930 throw new RestException(400,
'Proposal ID is mandatory');
934 $result = $propal->fetch($proposalid);
936 throw new RestException(404,
'Proposal not found');
939 $result = $this->commande->createFromProposal($propal, DolibarrApiAccess::$user);
941 throw new RestException(405, $this->commande->error);
943 $this->commande->fetchObjectLinked();
959 $object = parent::_cleanObjectDatas($object);
961 unset($object->note);
962 unset($object->address);
963 unset($object->barcode_type);
964 unset($object->barcode_type_code);
965 unset($object->barcode_type_label);
966 unset($object->barcode_type_coder);
981 foreach (Orders::$FIELDS as $field) {
982 if (!isset($data[$field]))
983 throw new RestException(400, $field.
" field missing");
984 $commande[$field] = $data[$field];
postContact($id, $contactid, $type)
Add a contact type of given order.
putLine($id, $lineid, $request_data=null)
Update a line to given order.
post($request_data=null)
Create a sale order.
__construct()
Constructor.
setinvoiced($id)
Classify the order as invoiced.
postLine($id, $request_data=null)
Add a line to given order.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $thirdparty_ids= '', $sqlfilters= '')
List orders.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
getByRefExt($ref_ext, $contact_list=1)
Get properties of an order object by ref_ext.
_validate($data)
Validate fields before create or update object.
getByRef($ref, $contact_list=1)
Get properties of an order object by ref.
getContacts($id, $type= '')
Get contacts of given order.
settodraft($id, $idwarehouse=-1)
Set an order to draft.
Class to manage customers orders.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
close($id, $notrigger=0)
Close an order (Classify it as "Delivered")
_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)
_cleanObjectDatas($object)
Clean sensible object datas.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an order.
deleteLine($id, $lineid)
Delete a line to given order.
deleteContact($id, $contactid, $type)
Unlink a contact type of given order.
reopen($id)
Tag the order as validated (opened)
getLines($id)
Get lines of an order.
createOrderFromProposal($proposalid)
Create an order using an existing proposal.
Class to manage proposals.