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