19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
35 public static $FIELDS = array(
64 public function get($id)
66 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
67 throw new RestException(401);
70 $result = $this->order->fetch($id);
72 throw new RestException(404,
'Supplier order not found');
76 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
79 $this->order->fetchObjectLinked();
102 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $product_ids =
'', $status =
'', $sqlfilters =
'', $sqlfilterlines =
'', $properties =
'')
104 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
105 throw new RestException(401);
111 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
115 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir") && !empty($socids)) {
116 $search_sale = DolibarrApiAccess::$user->id;
119 $sql =
"SELECT t.rowid";
120 if ((!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir")) || $search_sale > 0) {
121 $sql .=
", sc.fk_soc, sc.fk_user";
123 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur AS t LEFT JOIN ".MAIN_DB_PREFIX.
"commande_fournisseur_extrafields AS ef ON (ef.fk_object = t.rowid)";
125 if ((!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir")) || $search_sale > 0) {
126 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
129 if (!empty($product_ids)) {
130 $sql .=
", ".MAIN_DB_PREFIX.
"commande_fournisseurdet as cd";
133 $sql .=
' WHERE t.entity IN ('.getEntity(
'supplier_order').
')';
134 if ((!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir")) || $search_sale > 0) {
135 $sql .=
" AND t.fk_soc = sc.fk_soc";
137 if (!empty($product_ids)) {
138 $sql .=
" AND cd.fk_commande = t.rowid AND cd.fk_product IN (".$this->db->sanitize($product_ids).
")";
141 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
145 if ($status ==
'draft') {
146 $sql .=
" AND t.fk_statut IN (0)";
148 if ($status ==
'validated') {
149 $sql .=
" AND t.fk_statut IN (1)";
151 if ($status ==
'approved') {
152 $sql .=
" AND t.fk_statut IN (2)";
154 if ($status ==
'running') {
155 $sql .=
" AND t.fk_statut IN (3)";
157 if ($status ==
'received_start') {
158 $sql .=
" AND t.fk_statut IN (4)";
160 if ($status ==
'received_end') {
161 $sql .=
" AND t.fk_statut IN (5)";
163 if ($status ==
'cancelled') {
164 $sql .=
" AND t.fk_statut IN (6,7)";
166 if ($status ==
'refused') {
167 $sql .=
" AND t.fk_statut IN (9)";
170 if ($search_sale > 0) {
171 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
178 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
182 if ($sqlfilterlines) {
184 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet AS tl WHERE tl.fk_commande = t.rowid";
188 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
192 $sql .= $this->db->order($sortfield, $sortorder);
197 $offset = $limit * $page;
199 $sql .= $this->db->plimit($limit + 1, $offset);
202 $result = $this->db->query($sql);
205 $num = $this->db->num_rows($result);
206 $min = min($num, ($limit <= 0 ? $num : $limit));
208 $obj = $this->db->fetch_object($result);
210 if ($order_static->fetch($obj->rowid)) {
216 throw new RestException(503,
'Error when retrieve supplier order list : '.$this->db->lasterror());
230 public function post($request_data =
null)
232 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
233 throw new RestException(401,
"Insuffisant rights");
236 $result = $this->
_validate($request_data);
238 foreach ($request_data as $field => $value) {
239 if ($field ===
'caller') {
241 $this->order->context[
'caller'] = $request_data[
'caller'];
245 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
247 if (!array_keys($request_data,
'date')) {
248 $this->order->date =
dol_now();
259 if ($this->order->create(DolibarrApiAccess::$user) < 0) {
260 throw new RestException(500,
"Error creating order", array_merge(array($this->order->error), $this->order->errors));
262 return $this->order->id;
272 public function put($id, $request_data =
null)
274 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
275 throw new RestException(401);
278 $result = $this->order->fetch($id);
280 throw new RestException(404,
'Supplier order not found');
284 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
287 foreach ($request_data as $field => $value) {
288 if ($field ==
'id') {
291 if ($field ===
'caller') {
293 $this->order->context[
'caller'] = $request_data[
'caller'];
297 if ($field ==
'array_options' && is_array($value)) {
298 foreach ($value as $index => $val) {
299 $this->order->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->order);
304 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
307 if ($this->order->update(DolibarrApiAccess::$user)) {
308 return $this->
get($id);
330 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
331 throw new RestException(401);
334 $result = $this->order->fetch($id);
336 throw new RestException(404,
'Supplier order not found');
340 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
344 if ($source ==
'all' || $source ==
'external') {
345 $tmpContacts = $this->order->liste_contact(-1,
'external', 0, $type);
346 $contacts = array_merge($contacts, $tmpContacts);
349 if ($source ==
'all' || $source ==
'internal') {
350 $tmpContacts = $this->order->liste_contact(-1,
'internal', 0, $type);
351 $contacts = array_merge($contacts, $tmpContacts);
373 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
374 throw new RestException(401);
377 $result = $this->order->fetch($id);
379 throw new RestException(404,
'Supplier order not found');
383 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
386 $result = $this->order->add_contact($contactid, $type, $source);
389 throw new RestException(500,
'Error when added the contact');
393 throw new RestException(304,
'contact already added');
399 'message' =>
'Contact linked to the order'
422 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
423 throw new RestException(401);
426 $result = $this->order->fetch($id);
428 throw new RestException(404,
'Supplier order not found');
432 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
435 $contacts = $this->order->liste_contact(-1, $source, 0, $type);
437 $contactToUnlink = 0;
438 foreach ($contacts as $contact) {
439 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
440 $contactToUnlink = $contact[
'rowid'];
445 if ($contactToUnlink == 0) {
446 throw new RestException(404,
'Linked contact not found');
449 $result = $this->order->delete_contact($contact[
'rowid']);
452 throw new RestException(500,
'Error when deleted the contact');
458 'message' =>
'Contact unlinked from supplier order'
469 public function delete($id)
471 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"supprimer")) {
472 throw new RestException(401);
474 $result = $this->order->fetch($id);
476 throw new RestException(404,
'Supplier order not found');
480 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
483 if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
484 throw new RestException(500,
'Error when deleting order');
490 'message' =>
'Supplier order deleted'
514 public function validate($id, $idwarehouse = 0, $notrigger = 0)
516 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
517 throw new RestException(401);
519 $result = $this->order->fetch($id);
521 throw new RestException(404,
'Order not found');
525 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
528 $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
530 throw new RestException(304,
'Error nothing done. May be object is already validated');
533 throw new RestException(500,
'Error when validating Order: '.$this->order->error);
539 'message' =>
'Order validated (Ref='.$this->order->ref.
')'
562 public function approve($id, $idwarehouse = 0, $secondlevel = 0)
564 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
565 throw new RestException(401);
567 $result = $this->order->fetch($id);
569 throw new RestException(404,
'Order not found');
573 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
576 $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
578 throw new RestException(304,
'Error nothing done. May be object is already approved');
581 throw new RestException(500,
'Error when approve Order: '.$this->order->error);
587 'message' =>
'Order approved (Ref='.$this->order->ref.
')'
613 public function makeOrder($id, $date, $method, $comment =
'')
615 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
616 throw new RestException(401);
618 $result = $this->order->fetch($id);
620 throw new RestException(404,
'Order not found');
624 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
627 $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
629 throw new RestException(304,
'Error nothing done. May be object is already sent');
632 throw new RestException(500,
'Error when sending Order: '.$this->order->error);
638 'message' =>
'Order sent (Ref='.$this->order->ref.
')'
678 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
679 throw new RestException(401);
681 $result = $this->order->fetch($id);
683 throw new RestException(404,
'Order not found');
687 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
690 foreach ($lines as $line) {
691 $lineObj =(object) $line;
693 $result=$this->order->dispatchProduct(
694 DolibarrApiAccess::$user,
695 $lineObj->fk_product,
708 throw new RestException(500,
'Error dispatch order line '.$line->id.
': '.$this->order->error);
712 $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
715 throw new RestException(304,
'Error nothing done. May be object is already dispatched');
718 throw new RestException(500,
'Error when receivce order: '.$this->order->error);
724 'message' =>
'Order received (Ref='.$this->order->ref.
')'
739 $object = parent::_cleanObjectDatas($object);
741 unset($object->rowid);
742 unset($object->barcode_type);
743 unset($object->barcode_type_code);
744 unset($object->barcode_type_label);
745 unset($object->barcode_type_coder);
761 foreach (SupplierOrders::$FIELDS as $field) {
762 if (!isset($data[$field])) {
763 throw new RestException(400,
"$field field missing");
765 $order[$field] = $data[$field];
Class to manage predefined suppliers products.
_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.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an order.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $product_ids='', $status='', $sqlfilters='', $sqlfilterlines='', $properties='')
List orders.
__construct()
Constructor.
put($id, $request_data=null)
Update supplier order.
post($request_data=null)
Create supplier order object.
_cleanObjectDatas($object)
Clean sensible object datas.
deleteContact($id, $contactid, $type, $source)
Unlink a contact type of given supplier order.
makeOrder($id, $date, $method, $comment='')
Sends an order to the vendor.
postContact($id, $contactid, $type, $source)
Add a contact type of given supplier order.
getContacts($id, $source, $type='')
Get contacts of given supplier order.
_validate($data)
Validate fields before create or update object.
approve($id, $idwarehouse=0, $secondlevel=0)
Approve an order.
receiveOrder($id, $closeopenorder, $comment, $lines)
Receives the order, dispatches products.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.