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);
180 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
182 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
183 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
186 $sql .= $this->
db->order($sortfield, $sortorder);
191 $offset = $limit * $page;
193 $sql .= $this->
db->plimit($limit + 1, $offset);
196 $result = $this->
db->query($sql);
199 $num = $this->
db->num_rows($result);
200 $min = min($num, ($limit <= 0 ? $num : $limit));
202 $obj = $this->
db->fetch_object($result);
204 if ($order_static->fetch($obj->rowid)) {
210 throw new RestException(503,
'Error when retrieve supplier order list : '.$this->
db->lasterror());
212 if (!count($obj_ret)) {
213 throw new RestException(404,
'No supplier order found');
226 public function post($request_data =
null)
228 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
229 throw new RestException(401,
"Insuffisant rights");
232 $result = $this->
_validate($request_data);
234 foreach ($request_data as $field => $value) {
235 $this->order->$field = $value;
237 if (!array_keys($request_data,
'date')) {
238 $this->order->date =
dol_now();
249 if ($this->order->create(DolibarrApiAccess::$user) < 0) {
250 throw new RestException(500,
"Error creating order", array_merge(array($this->order->error), $this->order->errors));
252 return $this->order->id;
262 public function put($id, $request_data =
null)
264 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
265 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 foreach ($request_data as $field => $value) {
278 if ($field ==
'id') {
281 $this->order->$field = $value;
284 if ($this->order->update(DolibarrApiAccess::$user)) {
285 return $this->
get($id);
297 public function delete($id)
299 if (!DolibarrApiAccess::$user->rights->fournisseur->commande->supprimer) {
300 throw new RestException(401);
302 $result = $this->order->fetch($id);
304 throw new RestException(404,
'Supplier order not found');
308 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
311 if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
312 throw new RestException(500,
'Error when deleting order');
318 'message' =>
'Supplier order deleted'
342 public function validate($id, $idwarehouse = 0, $notrigger = 0)
344 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
345 throw new RestException(401);
347 $result = $this->order->fetch($id);
349 throw new RestException(404,
'Order not found');
353 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
356 $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
358 throw new RestException(304,
'Error nothing done. May be object is already validated');
361 throw new RestException(500,
'Error when validating Order: '.$this->order->error);
367 'message' =>
'Order validated (Ref='.$this->order->ref.
')'
390 public function approve($id, $idwarehouse = 0, $secondlevel = 0)
392 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
393 throw new RestException(401);
395 $result = $this->order->fetch($id);
397 throw new RestException(404,
'Order not found');
401 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
404 $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
406 throw new RestException(304,
'Error nothing done. May be object is already approved');
409 throw new RestException(500,
'Error when approve Order: '.$this->order->error);
415 'message' =>
'Order approved (Ref='.$this->order->ref.
')'
441 public function makeOrder($id, $date, $method, $comment =
'')
443 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
444 throw new RestException(401);
446 $result = $this->order->fetch($id);
448 throw new RestException(404,
'Order not found');
452 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
455 $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
457 throw new RestException(304,
'Error nothing done. May be object is already sent');
460 throw new RestException(500,
'Error when sending Order: '.$this->order->error);
466 'message' =>
'Order sent (Ref='.$this->order->ref.
')'
506 if (empty(DolibarrApiAccess::$user->rights->fournisseur->commande->creer) && empty(DolibarrApiAccess::$user->rights->supplier_order->creer)) {
507 throw new RestException(401);
509 $result = $this->order->fetch($id);
511 throw new RestException(404,
'Order not found');
515 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
518 foreach ($lines as $line) {
519 $lineObj =(object) $line;
521 $result=$this->order->dispatchProduct(DolibarrApiAccess::$user,
522 $lineObj->fk_product,
531 $lineObj->notrigger);
534 throw new RestException(500,
'Error dispatch order line '.$line->id.
': '.$this->order->error);
538 $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
541 throw new RestException(304,
'Error nothing done. May be object is already dispatched');
544 throw new RestException(500,
'Error when receivce order: '.$this->order->error);
550 'message' =>
'Order received (Ref='.$this->order->ref.
')'
565 $object = parent::_cleanObjectDatas($object);
567 unset($object->rowid);
568 unset($object->barcode_type);
569 unset($object->barcode_type_code);
570 unset($object->barcode_type_label);
571 unset($object->barcode_type_coder);
587 foreach (SupplierOrders::$FIELDS as $field) {
588 if (!isset($data[$field])) {
589 throw new RestException(400,
"$field field missing");
591 $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.
_checkFilters($sqlfilters, &$error='')
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.
__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.
dol_now($mode='auto')
Return date for now.
$conf db
API class for accounts.