dolibarr 24.0.0-beta
api_workstations.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2019 Cedric Ancelin <icedo.anc@gmail.com>
4 * Copyright (C) 2024 Christian Humpel <christian.humpel@gmail.com>
5 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22use Luracast\Restler\RestException;
23
24require_once DOL_DOCUMENT_ROOT.'/workstation/class/workstation.class.php';
25
40{
44 public $workstation;
45
49 public function __construct()
50 {
51 global $db;
52
53 $this->db = $db;
54 $this->workstation = new Workstation($this->db);
55 }
56
71 public function get($id)
72 {
73 return $this->_fetch($id);
74 }
75
91 public function getByRef($ref)
92 {
93 return $this->_fetch(0, $ref);
94 }
95
111 public function index($sortfield = "t.ref", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
112 {
113 global $db, $conf;
114
115 if (!DolibarrApiAccess::$user->rights->workstation->workstation->read) {
116 throw new RestException(403);
117 }
118
119 $obj_ret = array();
120
121 $socid = DolibarrApiAccess::$user->socid ?: '';
122
123 $sql = "SELECT t.rowid, t.ref";
124 $sql .= " FROM ".$this->db->prefix()."workstation_workstation as t";
125
126 // Add sql filters
127 if ($sqlfilters) {
128 $errormessage = '';
129 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
130 if ($errormessage) {
131 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
132 }
133 }
134
135 // this query will return total products with the filters given
136 $sqlTotals = str_replace('SELECT t.rowid, t.ref', 'SELECT count(t.rowid) as total', $sql);
137
138 $sql .= $this->db->order($sortfield, $sortorder);
139 if ($limit) {
140 if ($page < 0) {
141 $page = 0;
142 }
143 $offset = $limit * $page;
144
145 $sql .= $this->db->plimit($limit + 1, $offset);
146 }
147
148 $result = $this->db->query($sql);
149 if ($result) {
150 $num = $this->db->num_rows($result);
151 $min = min($num, ($limit <= 0 ? $num : $limit));
152 $i = 0;
153 while ($i < $min) {
154 $obj = $this->db->fetch_object($result);
155 $workstation_static = new Workstation($this->db);
156 if ($workstation_static->fetch($obj->rowid)) {
157 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($workstation_static), $properties);
158 }
159 $i++;
160 }
161 } else {
162 throw new RestException(503, 'Error when retrieve workstation list : '.$this->db->lasterror());
163 }
164
165 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
166 if ($page > 0) {
167 $totalsResult = $this->db->query($sqlTotals);
168 $total = $this->db->fetch_object($totalsResult)->total;
169
170 $tmp = $obj_ret;
171 $obj_ret = array();
172
173 $obj_ret['data'] = $tmp;
174 $obj_ret['pagination'] = array(
175 'total' => (int) $total,
176 'page' => $page, //count starts from 0
177 'page_count' => (int) ceil((int) $total / $limit),
178 'limit' => $limit
179 );
180 }
181
182 return $obj_ret;
183 }
184
185 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
196 protected function _cleanObjectDatas($object)
197 {
198 // phpcs:enable
199 $object = parent::_cleanObjectDatas($object);
202 unset($object->statut);
203
204 unset($object->regeximgext);
205 unset($object->price_by_qty);
206 unset($object->prices_by_qty_id);
207 unset($object->product_id_already_linked);
208 unset($object->reputations);
209 unset($object->db);
210 unset($object->name);
211 unset($object->firstname);
212 unset($object->lastname);
213 unset($object->civility_id);
214 unset($object->contact);
215 unset($object->contact_id);
216 unset($object->contacts_ids);
217 unset($object->thirdparty);
218 unset($object->user);
219 unset($object->origin);
220 unset($object->origin_id);
221 unset($object->fourn_pu);
222 unset($object->fourn_price_base_type);
223 unset($object->fourn_socid);
224 unset($object->ref_fourn);
225 unset($object->ref_supplier);
226 unset($object->product_fourn_id);
227 unset($object->fk_project);
228
229 unset($object->linked_objects);
230 unset($object->linkedObjectsIds);
231 unset($object->oldref);
232 unset($object->actionmsg);
233 unset($object->actionmsg2);
234 unset($object->canvas);
235 unset($object->origin_object);
236 unset($object->expedition);
237 unset($object->livraison);
238 unset($object->commandeFournisseur);
239 unset($object->country_id);
240 unset($object->country_code);
241 unset($object->state_id);
242 unset($object->region_id);
243 unset($object->barcode_type);
244 unset($object->barcode_type_coder);
245 unset($object->shipping_method);
246 unset($object->fk_multicurrency);
247 unset($object->multicurrency_code);
248 unset($object->multicurrency_tx);
249 unset($object->multicurrency_total_ht);
250 unset($object->multicurrency_total_tva);
251 unset($object->multicurrency_total_ttc);
252 unset($object->multicurrency_total_localtax1);
253 unset($object->multicurrency_total_localtax2);
254 unset($object->last_main_doc);
255 unset($object->total_ht);
256 unset($object->total_tva);
257 unset($object->total_localtax1);
258 unset($object->total_localtax2);
259 unset($object->total_ttc);
260 unset($object->totalpaid);
261 unset($object->labelStatus);
262 unset($object->labelStatusShort);
263 unset($object->tpl);
264 unset($object->showphoto_on_popup);
265 unset($object->nb);
266 unset($object->output);
267 unset($object->extraparams);
268 unset($object->product);
269 unset($object->cond_reglement_supplier_id);
270 unset($object->deposit_percent);
271 unset($object->retained_warranty_fk_cond_reglement);
272 unset($object->warehouse_id);
273 unset($object->rowid);
274
275 unset($object->mode_reglement_id);
276 unset($object->cond_reglement_id);
277 unset($object->demand_reason_id);
278 unset($object->transport_mode_id);
279 unset($object->cond_reglement);
280 unset($object->shipping_method_id);
281 unset($object->model_pdf);
282 unset($object->note);
283
284 unset($object->nbphoto);
285 unset($object->recuperableonly);
286 unset($object->multiprices_recuperableonly);
287 unset($object->tva_npr);
288 unset($object->lines);
289 unset($object->fk_bank);
290 unset($object->fk_account);
291
292 unset($object->stock_reel);
293 unset($object->stock_theorique);
294 unset($object->stock_warehouse);
295
296 return $object;
297 }
298
311 private function _fetch($id, $ref = '')
312 {
313 if (empty($id) && empty($ref)) {
314 throw new RestException(400, 'bad value for parameter id or ref');
315 }
316
317 $id = (empty($id) ? 0 : $id);
318
319 if (!DolibarrApiAccess::$user->rights->workstation->workstation->read) {
320 throw new RestException(403);
321 }
322
323 $result = $this->workstation->fetch($id, $ref);
324 if (!$result) {
325 throw new RestException(404, 'Workstation not found');
326 }
327
328 if (!DolibarrApi::_checkAccessToResource('workstation', $this->workstation->id)) {
329 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
330 }
331
332 return $this->_cleanObjectDatas($this->workstation);
333 }
334}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:35
_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.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T.
Class for Workstation.
_fetch($id, $ref='')
Get properties of 1 workstation object.
getByRef($ref)
Get properties of a workstation object by ref.
index($sortfield="t.ref", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List workstations.
__construct()
Constructor.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.