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();
103 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $product_ids =
'', $status =
'', $sqlfilters =
'', $sqlfilterlines =
'', $properties =
'', $pagination_data =
false)
105 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
106 throw new RestException(403);
112 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
116 if (!DolibarrApiAccess::$user->hasRight(
"societe",
"client",
"voir") && !empty($socids)) {
117 $search_sale = DolibarrApiAccess::$user->id;
120 $sql =
"SELECT t.rowid";
121 $sql .=
" FROM ".MAIN_DB_PREFIX.
"commande_fournisseur AS t";
122 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"commande_fournisseur_extrafields AS ef ON (ef.fk_object = t.rowid)";
123 if (!empty($product_ids)) {
124 $sql .=
", ".MAIN_DB_PREFIX.
"commande_fournisseurdet as cd";
126 $sql .=
' WHERE t.entity IN ('.getEntity(
'supplier_order').
')';
127 if (!empty($product_ids)) {
128 $sql .=
" AND cd.fk_commande = t.rowid AND cd.fk_product IN (".$this->db->sanitize($product_ids).
")";
131 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
134 if ($status ==
'draft') {
135 $sql .=
" AND t.fk_statut IN (0)";
137 if ($status ==
'validated') {
138 $sql .=
" AND t.fk_statut IN (1)";
140 if ($status ==
'approved') {
141 $sql .=
" AND t.fk_statut IN (2)";
143 if ($status ==
'running') {
144 $sql .=
" AND t.fk_statut IN (3)";
146 if ($status ==
'received_start') {
147 $sql .=
" AND t.fk_statut IN (4)";
149 if ($status ==
'received_end') {
150 $sql .=
" AND t.fk_statut IN (5)";
152 if ($status ==
'cancelled') {
153 $sql .=
" AND t.fk_statut IN (6,7)";
155 if ($status ==
'refused') {
156 $sql .=
" AND t.fk_statut IN (9)";
159 if ($search_sale && $search_sale !=
'-1') {
160 if ($search_sale == -2) {
161 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
162 } elseif ($search_sale > 0) {
163 $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).
")";
171 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
175 if ($sqlfilterlines) {
177 $sql .=
" AND EXISTS (SELECT tl.rowid FROM ".MAIN_DB_PREFIX.
"commande_fournisseurdet AS tl WHERE tl.fk_commande = t.rowid";
181 throw new RestException(400,
'Error when validating parameter sqlfilterlines -> '.$errormessage);
186 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
188 $sql .= $this->db->order($sortfield, $sortorder);
193 $offset = $limit * $page;
195 $sql .= $this->db->plimit($limit + 1, $offset);
198 $result = $this->db->query($sql);
201 $num = $this->db->num_rows($result);
202 $min = min($num, ($limit <= 0 ? $num : $limit));
204 $obj = $this->db->fetch_object($result);
206 if ($order_static->fetch($obj->rowid)) {
212 throw new RestException(503,
'Error when retrieve supplier order list : '.$this->db->lasterror());
216 if ($pagination_data) {
217 $totalsResult = $this->db->query($sqlTotals);
218 $total = $this->db->fetch_object($totalsResult)->total;
223 $obj_ret[
'data'] = $tmp;
224 $obj_ret[
'pagination'] = [
225 'total' => (int) $total,
227 'page_count' => ceil((
int) $total / $limit),
243 public function post($request_data =
null)
245 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
246 throw new RestException(403,
"Insuffisant rights");
249 $result = $this->
_validate($request_data);
251 foreach ($request_data as $field => $value) {
252 if ($field ===
'caller') {
254 $this->order->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
258 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
260 if (!array_keys($request_data,
'date')) {
261 $this->order->date =
dol_now();
272 if ($this->order->create(DolibarrApiAccess::$user) < 0) {
273 throw new RestException(500,
"Error creating order", array_merge(array($this->order->error), $this->order->errors));
275 return $this->order->id;
285 public function put(
$id, $request_data =
null)
287 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
288 throw new RestException(403);
291 $result = $this->order->fetch(
$id);
293 throw new RestException(404,
'Supplier order not found');
297 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
300 foreach ($request_data as $field => $value) {
301 if ($field ==
'id') {
304 if ($field ===
'caller') {
306 $this->order->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
309 if ($field ==
'array_options' && is_array($value)) {
310 foreach ($value as $index => $val) {
311 $this->order->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->order);
315 $this->order->$field = $this->
_checkValForAPI($field, $value, $this->order);
318 if ($this->order->update(DolibarrApiAccess::$user)) {
319 return $this->
get(
$id);
341 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"lire")) {
342 throw new RestException(403);
345 $result = $this->order->fetch(
$id);
347 throw new RestException(404,
'Supplier order not found');
351 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
355 if ($source ==
'all' || $source ==
'external') {
356 $tmpContacts = $this->order->liste_contact(-1,
'external', 0, $type);
357 $contacts = array_merge($contacts, $tmpContacts);
360 if ($source ==
'all' || $source ==
'internal') {
361 $tmpContacts = $this->order->liste_contact(-1,
'internal', 0, $type);
362 $contacts = array_merge($contacts, $tmpContacts);
384 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
385 throw new RestException(403);
388 $result = $this->order->fetch(
$id);
390 throw new RestException(404,
'Supplier order not found');
394 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
397 $result = $this->order->add_contact($contactid, $type, $source);
400 throw new RestException(500,
'Error when added the contact');
404 throw new RestException(304,
'contact already added');
410 'message' =>
'Contact linked to the order'
433 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer")) {
434 throw new RestException(403);
437 $result = $this->order->fetch(
$id);
439 throw new RestException(404,
'Supplier order not found');
443 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
446 $contacts = $this->order->liste_contact(-1, $source, 0, $type);
448 $contactToUnlink = 0;
449 foreach ($contacts as $contact) {
450 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
451 $contactToUnlink = $contact[
'rowid'];
456 if ($contactToUnlink == 0) {
457 throw new RestException(404,
'Linked contact not found');
460 $result = $this->order->delete_contact($contact[
'rowid']);
463 throw new RestException(500,
'Error when deleted the contact');
469 'message' =>
'Contact unlinked from supplier order'
480 public function delete(
$id)
482 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"supprimer")) {
483 throw new RestException(403);
485 $result = $this->order->fetch(
$id);
487 throw new RestException(404,
'Supplier order not found');
491 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
494 if ($this->order->delete(DolibarrApiAccess::$user) < 0) {
495 throw new RestException(500,
'Error when deleting order');
501 'message' =>
'Supplier order deleted'
527 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
528 throw new RestException(403);
530 $result = $this->order->fetch(
$id);
532 throw new RestException(404,
'Order not found');
536 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
539 $result = $this->order->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
541 throw new RestException(304,
'Error nothing done. May be object is already validated');
544 throw new RestException(500,
'Error when validating Order: '.$this->order->error);
550 'message' =>
'Order validated (Ref='.$this->order->ref.
')'
573 public function approve(
$id, $idwarehouse = 0, $secondlevel = 0)
575 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
576 throw new RestException(403);
578 $result = $this->order->fetch(
$id);
580 throw new RestException(404,
'Order not found');
584 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
587 $result = $this->order->approve(DolibarrApiAccess::$user, $idwarehouse, $secondlevel);
589 throw new RestException(304,
'Error nothing done. May be object is already approved');
592 throw new RestException(500,
'Error when approve Order: '.$this->order->error);
598 'message' =>
'Order approved (Ref='.$this->order->ref.
')'
626 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
627 throw new RestException(403);
629 $result = $this->order->fetch(
$id);
631 throw new RestException(404,
'Order not found');
635 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
638 $result = $this->order->commande(DolibarrApiAccess::$user, $date, $method, $comment);
640 throw new RestException(304,
'Error nothing done. May be object is already sent');
643 throw new RestException(500,
'Error when sending Order: '.$this->order->error);
649 'message' =>
'Order sent (Ref='.$this->order->ref.
')'
689 if (!DolibarrApiAccess::$user->hasRight(
"fournisseur",
"commande",
"creer") && !DolibarrApiAccess::$user->hasRight(
"supplier_order",
"creer")) {
690 throw new RestException(403);
692 $result = $this->order->fetch(
$id);
694 throw new RestException(404,
'Order not found');
698 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
701 foreach ($lines as $line) {
702 $lineObj =(object) $line;
704 $result=$this->order->dispatchProduct(
705 DolibarrApiAccess::$user,
706 $lineObj->fk_product,
719 throw new RestException(500,
'Error dispatch order line '.$line->id.
': '.$this->order->error);
723 $result = $this->order->calcAndSetStatusDispatch(DolibarrApiAccess::$user, $closeopenorder, $comment);
726 throw new RestException(304,
'Error nothing done. May be object is already dispatched');
729 throw new RestException(500,
'Error when receivce order: '.$this->order->error);
735 'message' =>
'Order received (Ref='.$this->order->ref.
')'
754 unset(
$object->barcode_type_code);
755 unset(
$object->barcode_type_label);
756 unset(
$object->barcode_type_coder);
772 foreach (SupplierOrders::$FIELDS as $field) {
773 if (!isset($data[$field])) {
774 throw new RestException(400,
"$field field missing");
776 $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='', $pagination_data=false)
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...