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');
115 public function index($sortfield =
"pl.batch", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
121 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'lire')) {
122 throw new RestException(403,
"Insufficient rights to view your products lots");
125 $sortorder = (strtoupper($sortorder) ===
'DESC') ?
'DESC' :
'ASC';
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";
132 $sql .=
" WHERE pl.entity IN (".getEntity(
'productlot').
")";
139 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
144 $sqlTotals = preg_replace(
'/^\s*SELECT\s+pl\.rowid/i',
'SELECT COUNT(pl.rowid) AS total', $sql);
147 $sql .= $this->db->order($sortfield, $sortorder);
151 $page = max(0, (
int) $page);
152 $offset = $limit * $page;
153 $sql .= $this->db->plimit($limit + 1, $offset);
156 $res = $this->db->query($sql);
158 throw new RestException(503,
'Error when retrieving product lot list: '.$this->db->lasterror());
162 $num = $this->db->num_rows($res);
163 $max = ($limit > 0) ? min($num, $limit) : $num;
166 $obj = $this->db->fetch_object($res);
170 if ($pl->fetch((
int) $obj->rowid) > 0) {
177 if ($pagination_data) {
179 $totRes = $this->db->query($sqlTotals);
181 $row = $this->db->fetch_object($totRes);
182 if ($row && isset($row->total)) $total = (int) $row->total;
186 $safeLimit = ($limit > 0) ? (
int) $limit : max(1, count($obj_ret));
187 $pageCount = (int) ceil($total / $safeLimit);
194 'page' => (int) $page,
195 'page_count' => $pageCount,
196 'limit' => $safeLimit
218 public function post($request_data =
null)
220 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer')) {
221 throw new RestException(403,
"Insufficient rights to create your product lot");
225 $result = $this->
_validate($request_data);
227 foreach ($request_data as $field => $value) {
228 if ($field ===
'caller') {
230 $this->productlot->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
234 $this->productlot->$field = $this->
_checkValForAPI($field, $value, $this->productlot);
244 if ($this->productlot->create(DolibarrApiAccess::$user) < 0) {
245 throw new RestException(500,
"Error creating event", array_merge(array($this->productlot->error), $this->productlot->errors));
248 return $this->productlot->id;
265 public function put(
$id, $request_data =
null)
267 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer')) {
268 throw new RestException(403,
"Insufficient rights to create your Product lot");
271 $result = $this->productlot->fetch(
$id);
273 $this->productlot->fetch_optionals();
274 $this->productlot->oldcopy = clone $this->productlot;
277 throw new RestException(404,
'productlot not found');
281 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
283 foreach ($request_data as $field => $value) {
284 if ($field ==
'id') {
287 if ($field ===
'caller') {
289 $this->productlot->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
293 if ($field ==
'array_options' && is_array($value)) {
294 foreach ($value as $index => $val) {
295 $this->productlot->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->productlot);
299 $this->productlot->$field = $this->
_checkValForAPI($field, $value, $this->productlot);
302 if ($this->productlot->update(DolibarrApiAccess::$user) > 0) {
303 return $this->
get(
$id);
322 public function delete(
$id)
324 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'supprimer')) {
325 throw new RestException(403,
"Insufficient rights to delete your product lot");
328 $result = $this->productlot->fetch(
$id);
330 $this->productlot->fetch_optionals();
331 $this->productlot->oldcopy = clone $this->productlot;
335 throw new RestException(404,
'Product lot not found');
339 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
342 if ($this->productlot->delete(DolibarrApiAccess::$user) < 0) {
343 throw new RestException(500,
'Error when delete Product lot : '.implode(
',', $this->productlot->errors));
349 'message' =>
'Product lot deleted'
363 if ($data ===
null) {
369 foreach (self::$FIELDS as $field) {
370 if (!isset($data[$field]) || $data[$field] ===
'') {
371 throw new RestException(400,
"$field field missing");
373 $lot[$field] = $data[$field];
417 unset(
$object->barcode_type_code);
418 unset(
$object->barcode_type_label);
419 unset(
$object->barcode_type_coder);
420 unset(
$object->mode_reglement_id);
421 unset(
$object->cond_reglement_id);
422 unset(
$object->cond_reglement);
423 unset(
$object->fk_delivery_address);
424 unset(
$object->shipping_method_id);
428 unset(
$object->total_localtax1);
429 unset(
$object->total_localtax2);
432 unset(
$object->label_incoterms);
433 unset(
$object->location_incoterms);
440 unset(
$object->demand_reason_id);
441 unset(
$object->transport_mode_id);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.