22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT .
'/product/stock/class/productlot.class.php';
39 public static $FIELDS = array(
71 public function get(
$id)
73 if (!DolibarrApiAccess::$user->hasRight(
'product',
'lire')) {
74 throw new RestException(403,
"Insufficient rights to read an event");
77 $result = $this->productlot->initAsSpecimen();
79 $result = $this->productlot->fetch(
$id);
81 $this->productlot->fetch_optionals();
82 $this->productlot->fetchObjectLinked();
86 throw new RestException(404,
'Product lots not found');
90 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
119 public function index($sortfield =
"pl.batch", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
125 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'lire')) {
126 throw new RestException(403,
"Insufficient rights to view your products lots");
129 $sortorder = (strtoupper($sortorder) ===
'DESC') ?
'DESC' :
'ASC';
131 $sql =
"SELECT pl.rowid";
132 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product_lot AS pl";
133 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product_lot_extrafields AS ple ON ple.fk_object = pl.rowid";
136 $sql .=
" WHERE pl.entity IN (".getEntity(
'productlot').
")";
143 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
148 $sqlTotals = preg_replace(
'/^\s*SELECT\s+pl\.rowid/i',
'SELECT COUNT(pl.rowid) AS total', $sql);
151 $sql .= $this->db->order($sortfield, $sortorder);
155 $page = max(0, (
int) $page);
156 $offset = $limit * $page;
157 $sql .= $this->db->plimit($limit + 1, $offset);
160 $res = $this->db->query($sql);
162 throw new RestException(503,
'Error when retrieving product lot list: '.$this->db->lasterror());
166 $num = $this->db->num_rows($res);
167 $max = ($limit > 0) ? min($num, $limit) : $num;
170 $obj = $this->db->fetch_object($res);
174 if ($pl->fetch((
int) $obj->rowid) > 0) {
181 if ($pagination_data) {
183 $totRes = $this->db->query($sqlTotals);
185 $row = $this->db->fetch_object($totRes);
186 if ($row && isset($row->total)) $total = (int) $row->total;
190 $safeLimit = ($limit > 0) ? (
int) $limit : max(1, count($obj_ret));
191 $pageCount = (int) ceil($total / $safeLimit);
198 'page' => (int) $page,
199 'page_count' => $pageCount,
200 'limit' => $safeLimit
222 public function post($request_data =
null)
224 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer')) {
225 throw new RestException(403,
"Insufficient rights to create your product lot");
229 $result = $this->
_validate($request_data);
231 foreach ($request_data as $field => $value) {
232 if ($field ===
'caller') {
234 $this->productlot->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
238 $this->productlot->$field = $this->
_checkValForAPI($field, $value, $this->productlot);
248 if ($this->productlot->create(DolibarrApiAccess::$user) < 0) {
249 throw new RestException(500,
"Error creating event", array_merge(array($this->productlot->error), $this->productlot->errors));
252 return $this->productlot->id;
269 public function put(
$id, $request_data =
null)
271 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer')) {
272 throw new RestException(403,
"Insufficient rights to create your Product lot");
275 $result = $this->productlot->fetch(
$id);
277 $this->productlot->fetch_optionals();
278 $this->productlot->oldcopy = clone $this->productlot;
281 throw new RestException(404,
'productlot not found');
285 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
287 foreach ($request_data as $field => $value) {
288 if ($field ==
'id') {
291 if ($field ===
'caller') {
293 $this->productlot->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
297 if ($field ==
'array_options' && is_array($value)) {
298 foreach ($value as $index => $val) {
303 $this->productlot->$field = $this->
_checkValForAPI($field, $value, $this->productlot);
306 if ($this->productlot->update(DolibarrApiAccess::$user) > 0) {
307 return $this->
get(
$id);
326 public function delete(
$id)
328 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'supprimer')) {
329 throw new RestException(403,
"Insufficient rights to delete your product lot");
332 $result = $this->productlot->fetch(
$id);
334 $this->productlot->fetch_optionals();
335 $this->productlot->oldcopy = clone $this->productlot;
339 throw new RestException(404,
'Product lot not found');
343 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
346 if ($this->productlot->delete(DolibarrApiAccess::$user) < 0) {
347 throw new RestException(500,
'Error when delete Product lot : '.implode(
',', $this->productlot->errors));
353 'message' =>
'Product lot deleted'
367 if ($data ===
null) {
373 foreach (self::$FIELDS as $field) {
374 if (!isset($data[$field]) || $data[$field] ===
'') {
375 throw new RestException(400,
"$field field missing");
377 $lot[$field] = $data[$field];
421 unset(
$object->barcode_type_code);
422 unset(
$object->barcode_type_label);
423 unset(
$object->barcode_type_coder);
424 unset(
$object->mode_reglement_id);
425 unset(
$object->cond_reglement_id);
426 unset(
$object->cond_reglement);
427 unset(
$object->fk_delivery_address);
428 unset(
$object->shipping_method_id);
432 unset(
$object->total_localtax1);
433 unset(
$object->total_localtax2);
436 unset(
$object->label_incoterms);
437 unset(
$object->location_incoterms);
444 unset(
$object->demand_reason_id);
445 unset(
$object->transport_mode_id);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.