19 use Luracast\Restler\RestException;
21 require_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();
100 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $product_ids =
'', $status =
'', $sqlfilters =
'')
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") && !$socids) {
116 $search_sale = DolibarrApiAccess::$user->id;
119 $sql =
"SELECT t.rowid";
120 if ((!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir") && !$socids) || $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") && !$socids) || $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") && !$socids) || $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).
")";
143 if ($search_sale > 0) {
144 $sql .=
" AND t.rowid = sc.fk_soc";
148 if ($status ==
'draft') {
149 $sql .=
" AND t.fk_statut IN (0)";
151 if ($status ==
'validated') {
152 $sql .=
" AND t.fk_statut IN (1)";
154 if ($status ==
'approved') {
155 $sql .=
" AND t.fk_statut IN (2)";
157 if ($status ==
'running') {
158 $sql .=
" AND t.fk_statut IN (3)";
160 if ($status ==
'received_start') {
161 $sql .=
" AND t.fk_statut IN (4)";
163 if ($status ==
'received_end') {
164 $sql .=
" AND t.fk_statut IN (5)";
166 if ($status ==
'cancelled') {
167 $sql .=
" AND t.fk_statut IN (6,7)";
169 if ($status ==
'refused') {
170 $sql .=
" AND t.fk_statut IN (9)";
173 if ($search_sale > 0) {
174 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
181 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
185 $sql .= $this->db->order($sortfield, $sortorder);
190 $offset = $limit * $page;
192 $sql .= $this->db->plimit($limit + 1, $offset);
195 $result = $this->db->query(
$sql);
198 $num = $this->db->num_rows($result);
199 $min = min($num, ($limit <= 0 ? $num : $limit));
201 $obj = $this->db->fetch_object($result);
203 if ($order_static->fetch($obj->rowid)) {
209 throw new RestException(503,
'Error when retrieve supplier order list : '.$this->db->lasterror());
211 if (!count($obj_ret)) {
212 throw new RestException(404,
'No supplier order found');
225 public function post($request_data =
null)
227 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
228 throw new RestException(401,
"Insuffisant rights");
231 $result = $this->
_validate($request_data);
233 foreach ($request_data as $field => $value) {
234 $this->order->$field = $value;
236 if (!array_keys($request_data,
'date')) {
237 $this->order->date =
dol_now();
248 if ($this->order->create(DolibarrApiAccess::$user) < 0) {
249 throw new RestException(500,
"Error creating order", array_merge(array($this->order->error), $this->order->errors));
251 return $this->order->id;
261 public function put($id, $request_data =
null)
263 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
264 throw new RestException(401);
267 $result = $this->order->fetch($id);
269 throw new RestException(404,
'Supplier order not found');
273 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
276 foreach ($request_data as $field => $value) {
277 if ($field ==
'id') {
280 $this->order->$field = $value;
283 if ($this->order->update(DolibarrApiAccess::$user)) {
284 return $this->
get($id);
306 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
307 throw new RestException(401);
310 $result = $this->order->fetch($id);
312 throw new RestException(404,
'Supplier order not found');
316 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
320 if ($source ==
'all' || $source ==
'external') {
321 $tmpContacts = $this->order->liste_contact(-1,
'external', 0, $type);
322 $contacts = array_merge($contacts, $tmpContacts);
325 if ($source ==
'all' || $source ==
'internal') {
326 $tmpContacts = $this->order->liste_contact(-1,
'internal', 0, $type);
327 $contacts = array_merge($contacts, $tmpContacts);
349 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
350 throw new RestException(401);
353 $result = $this->order->fetch($id);
355 throw new RestException(404,
'Supplier order not found');
359 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
362 $result = $this->order->add_contact($contactid, $type, $source);
365 throw new RestException(500,
'Error when added the contact');
369 throw new RestException(304,
'contact already added');
375 'message' =>
'Contact linked to the order'
398 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
399 throw new RestException(401);
402 $result = $this->order->fetch($id);
404 throw new RestException(404,
'Supplier order not found');
408 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
411 $contacts = $this->order->liste_contact(-1, $source, 0, $type);
413 $contactToUnlink = 0;
414 foreach ($contacts as $contact) {
415 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
416 $contactToUnlink = $contact[
'rowid'];
421 if ($contactToUnlink == 0) {
422 throw new RestException(404,
'Linked contact not found');
425 $result = $this->order->delete_contact($contact[
'rowid']);
428 throw new RestException(500,
'Error when deleted the contact');
434 'message' =>
'Contact unlinked from supplier order'
445 public function delete($id)
447 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"supprimer")) {
448 throw new RestException(401);
450 $result = $this->order->fetch($id);
452 throw new RestException(404,
'Supplier order not found');
456 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
459 if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
460 throw new RestException(500,
'Error when deleting order');
466 'message' =>
'Supplier order deleted'
490 public function validate($id, $idwarehouse = 0, $notrigger = 0)
492 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
493 throw new RestException(401);
495 $result = $this->order->fetch($id);
497 throw new RestException(404,
'Order not found');
501 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
504 $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
506 throw new RestException(304,
'Error nothing done. May be object is already validated');
509 throw new RestException(500,
'Error when validating Order: '.$this->order->error);
515 'message' =>
'Order validated (Ref='.$this->order->ref.
')'
538 public function approve($id, $idwarehouse = 0, $secondlevel = 0)
540 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
541 throw new RestException(401);
543 $result = $this->order->fetch($id);
545 throw new RestException(404,
'Order not found');
549 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
552 $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
554 throw new RestException(304,
'Error nothing done. May be object is already approved');
557 throw new RestException(500,
'Error when approve Order: '.$this->order->error);
563 'message' =>
'Order approved (Ref='.$this->order->ref.
')'
589 public function makeOrder($id, $date, $method, $comment =
'')
591 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
592 throw new RestException(401);
594 $result = $this->order->fetch($id);
596 throw new RestException(404,
'Order not found');
600 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
603 $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
605 throw new RestException(304,
'Error nothing done. May be object is already sent');
608 throw new RestException(500,
'Error when sending Order: '.$this->order->error);
614 'message' =>
'Order sent (Ref='.$this->order->ref.
')'
654 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
655 throw new RestException(401);
657 $result = $this->order->fetch($id);
659 throw new RestException(404,
'Order not found');
663 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
666 foreach ($lines as $line) {
667 $lineObj =(object) $line;
669 $result=$this->order->dispatchProduct(DolibarrApiAccess::$user,
670 $lineObj->fk_product,
679 $lineObj->notrigger);
682 throw new RestException(500,
'Error dispatch order line '.$line->id.
': '.$this->order->error);
686 $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
689 throw new RestException(304,
'Error nothing done. May be object is already dispatched');
692 throw new RestException(500,
'Error when receivce order: '.$this->order->error);
698 'message' =>
'Order received (Ref='.$this->order->ref.
')'
713 $object = parent::_cleanObjectDatas($object);
715 unset($object->rowid);
716 unset($object->barcode_type);
717 unset($object->barcode_type_code);
718 unset($object->barcode_type_label);
719 unset($object->barcode_type_coder);
735 foreach (SupplierOrders::$FIELDS as $field) {
736 if (!isset($data[$field])) {
737 throw new RestException(400,
"$field field missing");
739 $order[$field] = $data[$field];
Class to manage predefined suppliers products.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
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='')
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.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.