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->rights->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->rights->fournisseur->commande->lire) {
105 throw new RestException(401);
111 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
115 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
116 $search_sale = DolibarrApiAccess::$user->id;
119 $sql =
"SELECT t.rowid";
120 if ((!DolibarrApiAccess::$user->rights->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";
125 if ((!DolibarrApiAccess::$user->rights->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->rights->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 (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->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 (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->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);
296 public function delete($id)
298 if (!DolibarrApiAccess::$user->rights->fournisseur->commande->supprimer) {
299 throw new RestException(401);
301 $result = $this->order->fetch($id);
303 throw new RestException(404,
'Supplier order not found');
307 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
310 if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
311 throw new RestException(500,
'Error when deleting order');
317 'message' =>
'Supplier order deleted'
341 public function validate($id, $idwarehouse = 0, $notrigger = 0)
343 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
344 throw new RestException(401);
346 $result = $this->order->fetch($id);
348 throw new RestException(404,
'Order not found');
352 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
355 $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
357 throw new RestException(304,
'Error nothing done. May be object is already validated');
360 throw new RestException(500,
'Error when validating Order: '.$this->order->error);
366 'message' =>
'Order validated (Ref='.$this->order->ref.
')'
389 public function approve($id, $idwarehouse = 0, $secondlevel = 0)
391 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
392 throw new RestException(401);
394 $result = $this->order->fetch($id);
396 throw new RestException(404,
'Order not found');
400 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
403 $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
405 throw new RestException(304,
'Error nothing done. May be object is already approved');
408 throw new RestException(500,
'Error when approve Order: '.$this->order->error);
414 'message' =>
'Order approved (Ref='.$this->order->ref.
')'
440 public function makeOrder($id, $date, $method, $comment =
'')
442 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
443 throw new RestException(401);
445 $result = $this->order->fetch($id);
447 throw new RestException(404,
'Order not found');
451 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
454 $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
456 throw new RestException(304,
'Error nothing done. May be object is already sent');
459 throw new RestException(500,
'Error when sending Order: '.$this->order->error);
465 'message' =>
'Order sent (Ref='.$this->order->ref.
')'
505 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
506 throw new RestException(401);
508 $result = $this->order->fetch($id);
510 throw new RestException(404,
'Order not found');
514 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
517 foreach ($lines as $line) {
518 $lineObj =(object) $line;
520 $result=$this->order->dispatchProduct(DolibarrApiAccess::$user,
521 $lineObj->fk_product,
530 $lineObj->notrigger);
533 throw new RestException(500,
'Error dispatch order line '.$line->id.
': '.$this->order->error);
537 $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
540 throw new RestException(304,
'Error nothing done. May be object is already dispatched');
543 throw new RestException(500,
'Error when receivce order: '.$this->order->error);
549 'message' =>
'Order received (Ref='.$this->order->ref.
')'
564 $object = parent::_cleanObjectDatas($object);
566 unset($object->rowid);
567 unset($object->barcode_type);
568 unset($object->barcode_type_code);
569 unset($object->barcode_type_label);
570 unset($object->barcode_type_coder);
586 foreach (SupplierOrders::$FIELDS as $field) {
587 if (!isset($data[$field])) {
588 throw new RestException(400,
"$field field missing");
590 $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.
makeOrder($id, $date, $method, $comment='')
Sends an order to the vendor.
_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, &$error='')
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
$conf db
API class for accounts.