dolibarr 24.0.0-beta
api_productlots.class.php
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2025 William Mead <william@m34d.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 . '/product/stock/class/productlot.class.php';
25
35{
39 public static $FIELDS = array(
40 'fk_product',
41 'batch'
42 );
43
47 public $productlot;
48
52 public function __construct()
53 {
54 global $db, $conf;
55 $this->db = $db;
56 $this->productlot = new Productlot($this->db);
57 }
58
71 public function get($id)
72 {
73 if (!DolibarrApiAccess::$user->hasRight('product', 'lire')) {
74 throw new RestException(403, "Insufficient rights to read an event");
75 }
76 if ($id === 0) {
77 $result = $this->productlot->initAsSpecimen();
78 } else {
79 $result = $this->productlot->fetch($id);
80 if ($result) {
81 $this->productlot->fetch_optionals();
82 $this->productlot->fetchObjectLinked();
83 }
84 }
85 if (!$result) {
86 throw new RestException(404, 'Product lots not found');
87 }
88
89 return $this->_cleanObjectDatas($this->productlot);
90 }
91
115 public function index($sortfield = "pl.batch", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = '', $sqlfilters = '', $properties = '', $pagination_data = false)
116 {
117 global $conf;
118
119 $obj_ret = [];
120
121 if (!DolibarrApiAccess::$user->hasRight('produit', 'lire')) {
122 throw new RestException(403, "Insufficient rights to view your products lots");
123 }
124
125 $sortorder = (strtoupper($sortorder) === 'DESC') ? 'DESC' : 'ASC';
126
127 $sql = "SELECT pl.rowid";
128 $sql .= " FROM ".MAIN_DB_PREFIX."product_lot AS pl";
129 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot_extrafields AS ple ON ple.fk_object = pl.rowid";
130 $sql .= " WHERE 1=1";
131
132 // Filtres universels
133 if ($sqlfilters) {
134 $errormessage = '';
135 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
136 if ($errormessage) {
137 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
138 }
139 }
140
141 // Clone pour total (avant ORDER/LIMIT)
142 $sqlTotals = preg_replace('/^\s*SELECT\s+pl\.rowid/i', 'SELECT COUNT(pl.rowid) AS total', $sql);
143
144 // ORDER BY
145 $sql .= $this->db->order($sortfield, $sortorder);
146
147 // LIMIT/OFFSET
148 if ($limit) {
149 $page = max(0, (int) $page);
150 $offset = $limit * $page;
151 $sql .= $this->db->plimit($limit + 1, $offset);
152 }
153
154 $res = $this->db->query($sql);
155 if (!$res) {
156 throw new RestException(503, 'Error when retrieving product lot list: '.$this->db->lasterror());
157 }
158
159 $i = 0;
160 $num = $this->db->num_rows($res);
161 $max = ($limit > 0) ? min($num, $limit) : $num;
162
163 while ($i < $max) {
164 $obj = $this->db->fetch_object($res);
165 if (!$obj) break;
166
167 $pl = new Productlot($this->db);
168 if ($pl->fetch((int) $obj->rowid) > 0) {
169 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($pl), $properties);
170 }
171 $i++;
172 }
173
174 // Pagination enrichie
175 if ($pagination_data) {
176 $total = 0;
177 $totRes = $this->db->query($sqlTotals);
178 if ($totRes) {
179 $row = $this->db->fetch_object($totRes);
180 if ($row && isset($row->total)) $total = (int) $row->total;
181 }
182
183 // Evite division par zéro
184 $safeLimit = ($limit > 0) ? (int) $limit : max(1, count($obj_ret));
185 $pageCount = (int) ceil($total / $safeLimit);
186
187 $data = $obj_ret;
188 $obj_ret = [
189 'data' => $data,
190 'pagination' => [
191 'total' => $total,
192 'page' => (int) $page,
193 'page_count' => $pageCount,
194 'limit' => $safeLimit
195 ]
196 ];
197 }
198
199 return $obj_ret;
200 }
201
202
216 public function post($request_data = null)
217 {
218 if (!DolibarrApiAccess::$user->hasRight('produit', 'creer')) {
219 throw new RestException(403, "Insufficient rights to create your product lot");
220 }
221
222 // Check mandatory fields
223 $result = $this->_validate($request_data);
224
225 foreach ($request_data as $field => $value) {
226 if ($field === 'caller') {
227 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
228 $this->productlot->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
229 continue;
230 }
231
232 $this->productlot->$field = $this->_checkValForAPI($field, $value, $this->productlot);
233 }
234 /*if (isset($request_data["lines"])) {
235 $lines = array();
236 foreach ($request_data["lines"] as $line) {
237 array_push($lines, (object) $line);
238 }
239 $this->expensereport->lines = $lines;
240 }*/
241
242 if ($this->productlot->create(DolibarrApiAccess::$user) < 0) {
243 throw new RestException(500, "Error creating event", array_merge(array($this->productlot->error), $this->productlot->errors));
244 }
245
246 return $this->productlot->id;
247 }
248
249
263 public function put($id, $request_data = null)
264 {
265 if (!DolibarrApiAccess::$user->hasRight('produit', 'creer')) {
266 throw new RestException(403, "Insufficient rights to create your Product lot");
267 }
268
269 $result = $this->productlot->fetch($id);
270 if ($result) {
271 $this->productlot->fetch_optionals();
272 $this->productlot->oldcopy = clone $this->productlot; // @phan-suppress-current-line PhanTypeMismatchProperty
273 }
274 if (!$result) {
275 throw new RestException(404, 'productlot not found');
276 }
277
278 if (!DolibarrApi::_checkAccessToResource('productlot', $this->productlot->id, 'product_lot', '', 'fk_soc', 'rowid')) {
279 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
280 }
281 foreach ($request_data as $field => $value) {
282 if ($field == 'id') {
283 continue;
284 }
285 if ($field === 'caller') {
286 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
287 $this->productlot->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
288 continue;
289 }
290
291 if ($field == 'array_options' && is_array($value)) {
292 foreach ($value as $index => $val) {
293 $this->productlot->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $this->productlot);
294 }
295 continue;
296 }
297 $this->productlot->$field = $this->_checkValForAPI($field, $value, $this->productlot);
298 }
299
300 if ($this->productlot->update(DolibarrApiAccess::$user) > 0) {
301 return $this->get($id);
302 }
303
304 return false;
305 }
306
320 public function delete($id)
321 {
322 if (!DolibarrApiAccess::$user->hasRight('produit', 'supprimer')) {
323 throw new RestException(403, "Insufficient rights to delete your product lot");
324 }
325
326 $result = $this->productlot->fetch($id);
327 if ($result) {
328 $this->productlot->fetch_optionals();
329 $this->productlot->oldcopy = clone $this->productlot; // @phan-suppress-current-line PhanTypeMismatchProperty
330 }
331
332 if (!$result) {
333 throw new RestException(404, 'Product lot not found');
334 }
335
336 if (!DolibarrApi::_checkAccessToResource('productlot', $this->productlot->id, 'product_lot', '', 'fk_soc', 'rowid')) {
337 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
338 }
339
340 if ($this->productlot->delete(DolibarrApiAccess::$user) < 0) {
341 throw new RestException(500, 'Error when delete Product lot : '.implode(',', $this->productlot->errors));
342 }
343
344 return array(
345 'success' => array(
346 'code' => 200,
347 'message' => 'Product lot deleted'
348 )
349 );
350 }
351
359 private function _validate($data)
360 {
361 if ($data === null) {
362 $data = array();
363 }
364
365 $lot = array();
366
367 foreach (self::$FIELDS as $field) {
368 if (!isset($data[$field]) || $data[$field] === '') {
369 throw new RestException(400, "$field field missing");
370 }
371 $lot[$field] = $data[$field];
372 }
373
374 return $lot;
375 }
376
377
378 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
388 protected function _cleanObjectDatas($object)
389 {
390 // phpcs:enable
391 $object = parent::_cleanObjectDatas($object);
392
393 unset($object->note); // already in note_private or note_public
394 unset($object->usermod);
395 unset($object->libelle);
396 unset($object->context);
397 unset($object->canvas);
398 unset($object->contact);
399 unset($object->contact_id);
400 unset($object->thirdparty);
401 unset($object->user);
402 unset($object->origin);
403 unset($object->origin_id);
404 unset($object->ref_ext);
405 unset($object->statut);
406 unset($object->state_code);
407 unset($object->state_id);
408 unset($object->state);
409 unset($object->region);
410 unset($object->region_code);
411 unset($object->country);
412 unset($object->country_id);
413 unset($object->country_code);
414 unset($object->barcode_type);
415 unset($object->barcode_type_code);
416 unset($object->barcode_type_label);
417 unset($object->barcode_type_coder);
418 unset($object->mode_reglement_id);
419 unset($object->cond_reglement_id);
420 unset($object->cond_reglement);
421 unset($object->fk_delivery_address);
422 unset($object->shipping_method_id);
423 unset($object->fk_account);
424 unset($object->total_ht);
425 unset($object->total_tva);
426 unset($object->total_localtax1);
427 unset($object->total_localtax2);
428 unset($object->total_ttc);
429 unset($object->fk_incoterms);
430 unset($object->label_incoterms);
431 unset($object->location_incoterms);
432 unset($object->name);
433 unset($object->lastname);
434 unset($object->firstname);
435 unset($object->civility_id);
436 unset($object->contact);
437 unset($object->societe);
438 unset($object->demand_reason_id);
439 unset($object->transport_mode_id);
440 unset($object->region_id);
441 unset($object->actions);
442 unset($object->lines);
443
444 return $object;
445 }
446}
$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
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_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.
Class with list of lots and properties.
index($sortfield="pl.batch", $sortorder='ASC', $limit=100, $page=0, $user_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List of product lot.
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create an product lot.
put($id, $request_data=null)
Update an Product lot.
__construct()
Constructor.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
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.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.