dolibarr 21.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 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
108 public function index($sortfield = "t.ref", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
109 {
110 global $db, $conf;
111
112 if (!DolibarrApiAccess::$user->rights->workstation->workstation->read) {
113 throw new RestException(403);
114 }
115
116 $obj_ret = array();
117
118 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
119
120 $sql = "SELECT t.rowid, t.ref";
121 $sql .= " FROM ".$this->db->prefix()."workstation_workstation as t";
122
123 // Add sql filters
124 if ($sqlfilters) {
125 $errormessage = '';
126 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
127 if ($errormessage) {
128 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
129 }
130 }
131
132 // this query will return total products with the filters given
133 $sqlTotals = str_replace('SELECT t.rowid, t.ref', 'SELECT count(t.rowid) as total', $sql);
134
135 $sql .= $this->db->order($sortfield, $sortorder);
136 if ($limit) {
137 if ($page < 0) {
138 $page = 0;
139 }
140 $offset = $limit * $page;
141
142 $sql .= $this->db->plimit($limit + 1, $offset);
143 }
144
145 $result = $this->db->query($sql);
146 if ($result) {
147 $num = $this->db->num_rows($result);
148 $min = min($num, ($limit <= 0 ? $num : $limit));
149 $i = 0;
150 while ($i < $min) {
151 $obj = $this->db->fetch_object($result);
152 $workstation_static = new Workstation($this->db);
153 if ($workstation_static->fetch($obj->rowid)) {
154 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($workstation_static), $properties);
155 }
156 $i++;
157 }
158 } else {
159 throw new RestException(503, 'Error when retrieve workstation list : '.$this->db->lasterror());
160 }
161
162 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
163 if ($page > 0) {
164 $totalsResult = $this->db->query($sqlTotals);
165 $total = $this->db->fetch_object($totalsResult)->total;
166
167 $tmp = $obj_ret;
168 $obj_ret = array();
169
170 $obj_ret['data'] = $tmp;
171 $obj_ret['pagination'] = array(
172 'total' => (int) $total,
173 'page' => $page, //count starts from 0
174 'page_count' => ceil((int) $total / $limit),
175 'limit' => $limit
176 );
177 }
178
179 return $obj_ret;
180 }
181
182 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
189 protected function _cleanObjectDatas($object)
190 {
191 // phpcs:enable
192 $object = parent::_cleanObjectDatas($object);
193
194 unset($object->statut);
195
196 unset($object->regeximgext);
197 unset($object->price_by_qty);
198 unset($object->prices_by_qty_id);
199 unset($object->libelle);
200 unset($object->product_id_already_linked);
201 unset($object->reputations);
202 unset($object->db);
203 unset($object->name);
204 unset($object->firstname);
205 unset($object->lastname);
206 unset($object->civility_id);
207 unset($object->contact);
208 unset($object->contact_id);
209 unset($object->contacts_ids);
210 unset($object->thirdparty);
211 unset($object->user);
212 unset($object->origin);
213 unset($object->origin_id);
214 unset($object->fourn_pu);
215 unset($object->fourn_price_base_type);
216 unset($object->fourn_socid);
217 unset($object->ref_fourn);
218 unset($object->ref_supplier);
219 unset($object->product_fourn_id);
220 unset($object->fk_project);
221
222 unset($object->linked_objects);
223 unset($object->linkedObjectsIds);
224 unset($object->oldref);
225 unset($object->actionmsg);
226 unset($object->actionmsg2);
227 unset($object->canvas);
228 unset($object->origin_object);
229 unset($object->expedition);
230 unset($object->livraison);
231 unset($object->commandeFournisseur);
232 unset($object->country_id);
233 unset($object->country_code);
234 unset($object->state_id);
235 unset($object->region_id);
236 unset($object->barcode_type);
237 unset($object->barcode_type_coder);
238 unset($object->shipping_method);
239 unset($object->fk_multicurrency);
240 unset($object->multicurrency_code);
241 unset($object->multicurrency_tx);
242 unset($object->multicurrency_total_ht);
243 unset($object->multicurrency_total_tva);
244 unset($object->multicurrency_total_ttc);
245 unset($object->multicurrency_total_localtax1);
246 unset($object->multicurrency_total_localtax2);
247 unset($object->last_main_doc);
248 unset($object->total_ht);
249 unset($object->total_tva);
250 unset($object->total_localtax1);
251 unset($object->total_localtax2);
252 unset($object->total_ttc);
253 unset($object->totalpaid);
254 unset($object->labelStatus);
255 unset($object->labelStatusShort);
256 unset($object->tpl);
257 unset($object->showphoto_on_popup);
258 unset($object->nb);
259 unset($object->output);
260 unset($object->extraparams);
261 unset($object->product);
262 unset($object->cond_reglement_supplier_id);
263 unset($object->deposit_percent);
264 unset($object->retained_warranty_fk_cond_reglement);
265 unset($object->warehouse_id);
266 unset($object->rowid);
267
268 unset($object->mode_reglement_id);
269 unset($object->cond_reglement_id);
270 unset($object->demand_reason_id);
271 unset($object->transport_mode_id);
272 unset($object->cond_reglement);
273 unset($object->shipping_method_id);
274 unset($object->model_pdf);
275 unset($object->note);
276
277 unset($object->nbphoto);
278 unset($object->recuperableonly);
279 unset($object->multiprices_recuperableonly);
280 unset($object->tva_npr);
281 unset($object->lines);
282 unset($object->fk_bank);
283 unset($object->fk_account);
284
285 unset($object->supplierprices);
286
287 unset($object->stock_reel);
288 unset($object->stock_theorique);
289 unset($object->stock_warehouse);
290
291 return $object;
292 }
293
306 private function _fetch($id, $ref = '')
307 {
308 if (empty($id) && empty($ref)) {
309 throw new RestException(400, 'bad value for parameter id or ref');
310 }
311
312 $id = (empty($id) ? 0 : $id);
313
314 if (!DolibarrApiAccess::$user->rights->workstation->workstation->read) {
315 throw new RestException(403);
316 }
317
318 $result = $this->workstation->fetch($id, $ref);
319 if (!$result) {
320 throw new RestException(404, 'Workstation not found');
321 }
322
323 if (!DolibarrApi::_checkAccessToResource('workstation', $this->workstation->id)) {
324 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
325 }
326
327 return $this->_cleanObjectDatas($this->workstation);
328 }
329}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class for API REST v1.
Definition api.class.php:30
_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