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