19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
35 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 =
'')
107 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
111 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
113 $sql =
"SELECT t.rowid";
114 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
115 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur as t";
117 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
119 if (!empty($product_ids)) $sql .=
", ".MAIN_DB_PREFIX.
"commande_fournisseurdet as cd";
121 $sql .=
' WHERE t.entity IN ('.getEntity(
'supplier_order').
')';
122 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
" AND t.fk_soc = sc.fk_soc";
123 if (!empty($product_ids)) $sql .=
" AND cd.fk_commande = t.rowid AND cd.fk_product IN (".$product_ids.
")";
124 if ($socids) $sql .=
" AND t.fk_soc IN (".$socids.
")";
125 if ($search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
128 if ($status ==
'draft') $sql .=
" AND t.fk_statut IN (0)";
129 if ($status ==
'validated') $sql .=
" AND t.fk_statut IN (1)";
130 if ($status ==
'approved') $sql .=
" AND t.fk_statut IN (2)";
131 if ($status ==
'running') $sql .=
" AND t.fk_statut IN (3)";
132 if ($status ==
'received_start') $sql .=
" AND t.fk_statut IN (4)";
133 if ($status ==
'received_end') $sql .=
" AND t.fk_statut IN (5)";
134 if ($status ==
'cancelled') $sql .=
" AND t.fk_statut IN (6,7)";
135 if ($status ==
'refused') $sql .=
" AND t.fk_statut IN (9)";
137 if ($search_sale > 0)
139 $sql .=
" AND sc.fk_user = ".$search_sale;
146 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
148 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
149 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
152 $sql .= $this->
db->order($sortfield, $sortorder);
158 $offset = $limit * $page;
160 $sql .= $this->
db->plimit($limit + 1, $offset);
163 $result = $this->
db->query($sql);
167 $num = $this->
db->num_rows($result);
168 $min = min($num, ($limit <= 0 ? $num : $limit));
171 $obj = $this->
db->fetch_object($result);
173 if ($order_static->fetch($obj->rowid)) {
179 throw new RestException(503,
'Error when retrieve supplier order list : '.$this->
db->lasterror());
181 if (!count($obj_ret)) {
182 throw new RestException(404,
'No supplier order found');
195 public function post($request_data = null)
197 if (!DolibarrApiAccess::$user->rights->fournisseur->commande->creer) {
198 throw new RestException(401,
"Insuffisant rights");
201 $result = $this->
_validate($request_data);
203 foreach ($request_data as $field => $value) {
204 $this->order->$field = $value;
206 if (!array_keys($request_data,
'date')) {
207 $this->order->date =
dol_now();
218 if ($this->order->create(DolibarrApiAccess::$user) < 0) {
219 throw new RestException(500,
"Error creating order", array_merge(array($this->order->error), $this->order->errors));
221 return $this->order->id;
231 public function put($id, $request_data = null)
233 if (!DolibarrApiAccess::$user->rights->fournisseur->commande->creer) {
234 throw new RestException(401);
237 $result = $this->order->fetch($id);
239 throw new RestException(404,
'Supplier order not found');
243 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
246 foreach ($request_data as $field => $value) {
247 if ($field ==
'id')
continue;
248 $this->order->$field = $value;
251 if ($this->order->update($id, DolibarrApiAccess::$user))
252 return $this->
get($id);
263 public function delete($id)
265 if (!DolibarrApiAccess::$user->rights->fournisseur->commande->supprimer) {
266 throw new RestException(401);
268 $result = $this->order->fetch($id);
270 throw new RestException(404,
'Supplier order not found');
274 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
277 if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
278 throw new RestException(500);
284 'message' =>
'Supplier order deleted'
308 public function validate($id, $idwarehouse = 0, $notrigger = 0)
310 if (!DolibarrApiAccess::$user->rights->fournisseur->commande->creer) {
311 throw new RestException(401);
313 $result = $this->order->fetch($id);
315 throw new RestException(404,
'Order not found');
319 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
322 $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
324 throw new RestException(304,
'Error nothing done. May be object is already validated');
327 throw new RestException(500,
'Error when validating Order: '.$this->order->error);
333 'message' =>
'Order validated (Ref='.$this->order->ref.
')'
348 $object = parent::_cleanObjectDatas($object);
350 unset($object->rowid);
351 unset($object->barcode_type);
352 unset($object->barcode_type_code);
353 unset($object->barcode_type_label);
354 unset($object->barcode_type_coder);
370 foreach (SupplierOrders::$FIELDS as $field) {
371 if (!isset($data[$field]))
372 throw new RestException(400,
"$field field missing");
373 $order[$field] = $data[$field];
__construct()
Constructor.
_validate($data)
Validate fields before create or update object.
dol_now($mode= 'auto')
Return date for now.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
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.
Class to manage predefined suppliers products.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
post($request_data=null)
Create supplier order object.
_cleanObjectDatas($object)
Clean sensible object datas.
put($id, $request_data=null)
Update supplier order.