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(403);
70 $result = $this->order->fetch($id);
72 throw new RestException(404,
'Supplier order not found');
76 throw new RestException(403,
'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(403);
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 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur AS t";
121 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"commande_fournisseur_extrafields AS ef ON (ef.fk_object = t.rowid)";
122 if (!empty($product_ids)) {
123 $sql .=
", ".MAIN_DB_PREFIX.
"commande_fournisseurdet as cd";
125 $sql .=
' WHERE t.entity IN ('.getEntity(
'supplier_order').
')';
126 if (!empty($product_ids)) {
127 $sql .=
" AND cd.fk_commande = t.rowid AND cd.fk_product IN (".$this->db->sanitize($product_ids).
")";
130 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
133 if ($status ==
'draft') {
134 $sql .=
" AND t.fk_statut IN (0)";
136 if ($status ==
'validated') {
137 $sql .=
" AND t.fk_statut IN (1)";
139 if ($status ==
'approved') {
140 $sql .=
" AND t.fk_statut IN (2)";
142 if ($status ==
'running') {
143 $sql .=
" AND t.fk_statut IN (3)";
145 if ($status ==
'received_start') {
146 $sql .=
" AND t.fk_statut IN (4)";
148 if ($status ==
'received_end') {
149 $sql .=
" AND t.fk_statut IN (5)";
151 if ($status ==
'cancelled') {
152 $sql .=
" AND t.fk_statut IN (6,7)";
154 if ($status ==
'refused') {
155 $sql .=
" AND t.fk_statut IN (9)";
158 if ($search_sale && $search_sale !=
'-1') {
159 if ($search_sale == -2) {
160 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
161 } elseif ($search_sale > 0) {
162 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
170 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
174 if ($sqlfilterlines) {
176 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet AS tl WHERE tl.fk_commande = t.rowid";
180 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
184 $sql .= $this->db->order($sortfield, $sortorder);
189 $offset = $limit * $page;
191 $sql .= $this->db->plimit($limit + 1, $offset);
194 $result = $this->db->query($sql);
197 $num = $this->db->num_rows($result);
198 $min = min($num, ($limit <= 0 ? $num : $limit));
200 $obj = $this->db->fetch_object($result);
202 if ($order_static->fetch($obj->rowid)) {
208 throw new RestException(503,
'Error when retrieve supplier order list : '.$this->db->lasterror());
222 public function post($request_data =
null)
224 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
225 throw new RestException(403,
"Insuffisant rights");
228 $result = $this->
_validate($request_data);
230 foreach ($request_data as $field => $value) {
231 if ($field ===
'caller') {
233 $this->order->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
237 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
239 if (!array_keys($request_data,
'date')) {
240 $this->order->date =
dol_now();
251 if ($this->order->create(DolibarrApiAccess::$user) < 0) {
252 throw new RestException(500,
"Error creating order", array_merge(array($this->order->error), $this->order->errors));
254 return $this->order->id;
264 public function put($id, $request_data =
null)
266 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
267 throw new RestException(403);
270 $result = $this->order->fetch($id);
272 throw new RestException(404,
'Supplier order not found');
276 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
279 foreach ($request_data as $field => $value) {
280 if ($field ==
'id') {
283 if ($field ===
'caller') {
285 $this->order->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
288 if ($field ==
'array_options' && is_array($value)) {
289 foreach ($value as $index => $val) {
290 $this->order->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->order);
294 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
297 if ($this->order->update(DolibarrApiAccess::$user)) {
298 return $this->
get($id);
320 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
321 throw new RestException(403);
324 $result = $this->order->fetch($id);
326 throw new RestException(404,
'Supplier order not found');
330 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
334 if ($source ==
'all' || $source ==
'external') {
335 $tmpContacts = $this->order->liste_contact(-1,
'external', 0, $type);
336 $contacts = array_merge($contacts, $tmpContacts);
339 if ($source ==
'all' || $source ==
'internal') {
340 $tmpContacts = $this->order->liste_contact(-1,
'internal', 0, $type);
341 $contacts = array_merge($contacts, $tmpContacts);
363 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
364 throw new RestException(403);
367 $result = $this->order->fetch($id);
369 throw new RestException(404,
'Supplier order not found');
373 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
376 $result = $this->order->add_contact($contactid, $type, $source);
379 throw new RestException(500,
'Error when added the contact');
383 throw new RestException(304,
'contact already added');
389 'message' =>
'Contact linked to the order'
412 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
413 throw new RestException(403);
416 $result = $this->order->fetch($id);
418 throw new RestException(404,
'Supplier order not found');
422 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
425 $contacts = $this->order->liste_contact(-1, $source, 0, $type);
427 $contactToUnlink = 0;
428 foreach ($contacts as $contact) {
429 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
430 $contactToUnlink = $contact[
'rowid'];
435 if ($contactToUnlink == 0) {
436 throw new RestException(404,
'Linked contact not found');
439 $result = $this->order->delete_contact($contact[
'rowid']);
442 throw new RestException(500,
'Error when deleted the contact');
448 'message' =>
'Contact unlinked from supplier order'
459 public function delete($id)
461 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"supprimer")) {
462 throw new RestException(403);
464 $result = $this->order->fetch($id);
466 throw new RestException(404,
'Supplier order not found');
470 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
473 if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
474 throw new RestException(500,
'Error when deleting order');
480 'message' =>
'Supplier order deleted'
504 public function validate($id, $idwarehouse = 0, $notrigger = 0)
506 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
507 throw new RestException(403);
509 $result = $this->order->fetch($id);
511 throw new RestException(404,
'Order not found');
515 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
518 $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
520 throw new RestException(304,
'Error nothing done. May be object is already validated');
523 throw new RestException(500,
'Error when validating Order: '.$this->order->error);
529 'message' =>
'Order validated (Ref='.$this->order->ref.
')'
552 public function approve($id, $idwarehouse = 0, $secondlevel = 0)
554 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
555 throw new RestException(403);
557 $result = $this->order->fetch($id);
559 throw new RestException(404,
'Order not found');
563 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
566 $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
568 throw new RestException(304,
'Error nothing done. May be object is already approved');
571 throw new RestException(500,
'Error when approve Order: '.$this->order->error);
577 'message' =>
'Order approved (Ref='.$this->order->ref.
')'
603 public function makeOrder($id, $date, $method, $comment =
'')
605 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
606 throw new RestException(403);
608 $result = $this->order->fetch($id);
610 throw new RestException(404,
'Order not found');
614 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
617 $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
619 throw new RestException(304,
'Error nothing done. May be object is already sent');
622 throw new RestException(500,
'Error when sending Order: '.$this->order->error);
628 'message' =>
'Order sent (Ref='.$this->order->ref.
')'
668 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
669 throw new RestException(403);
671 $result = $this->order->fetch($id);
673 throw new RestException(404,
'Order not found');
677 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
680 foreach ($lines as $line) {
681 $lineObj =(object) $line;
683 $result=$this->order->dispatchProduct(
684 DolibarrApiAccess::$user,
685 $lineObj->fk_product,
698 throw new RestException(500,
'Error dispatch order line '.$line->id.
': '.$this->order->error);
702 $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
705 throw new RestException(304,
'Error nothing done. May be object is already dispatched');
708 throw new RestException(500,
'Error when receivce order: '.$this->order->error);
714 'message' =>
'Order received (Ref='.$this->order->ref.
')'
733 unset(
$object->barcode_type_code);
734 unset(
$object->barcode_type_label);
735 unset(
$object->barcode_type_coder);
751 foreach (SupplierOrders::$FIELDS as $field) {
752 if (!isset($data[$field])) {
753 throw new RestException(400,
"$field field missing");
755 $order[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
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.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.