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";
130 $sql .=
" WHERE 1=1";
137 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
142 $sqlTotals = preg_replace(
'/^\s*SELECT\s+pl\.rowid/i',
'SELECT COUNT(pl.rowid) AS total', $sql);
145 $sql .= $this->db->order($sortfield, $sortorder);
149 $page = max(0, (
int) $page);
150 $offset = $limit * $page;
151 $sql .= $this->db->plimit($limit + 1, $offset);
154 $res = $this->db->query($sql);
156 throw new RestException(503,
'Error when retrieving product lot list: '.$this->db->lasterror());
160 $num = $this->db->num_rows($res);
161 $max = ($limit > 0) ? min($num, $limit) : $num;
164 $obj = $this->db->fetch_object($res);
168 if ($pl->fetch((
int) $obj->rowid) > 0) {
175 if ($pagination_data) {
177 $totRes = $this->db->query($sqlTotals);
179 $row = $this->db->fetch_object($totRes);
180 if ($row && isset($row->total)) $total = (int) $row->total;
184 $safeLimit = ($limit > 0) ? (
int) $limit : max(1, count($obj_ret));
185 $pageCount = (int) ceil($total / $safeLimit);
192 'page' => (int) $page,
193 'page_count' => $pageCount,
194 'limit' => $safeLimit
216 public function post($request_data =
null)
218 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer')) {
219 throw new RestException(403,
"Insufficient rights to create your product lot");
223 $result = $this->
_validate($request_data);
225 foreach ($request_data as $field => $value) {
226 if ($field ===
'caller') {
228 $this->productlot->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
232 $this->productlot->$field = $this->
_checkValForAPI($field, $value, $this->productlot);
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));
246 return $this->productlot->id;
263 public function put(
$id, $request_data =
null)
265 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'creer')) {
266 throw new RestException(403,
"Insufficient rights to create your Product lot");
269 $result = $this->productlot->fetch(
$id);
271 $this->productlot->fetch_optionals();
272 $this->productlot->oldcopy = clone $this->productlot;
275 throw new RestException(404,
'productlot not found');
279 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
281 foreach ($request_data as $field => $value) {
282 if ($field ==
'id') {
285 if ($field ===
'caller') {
287 $this->productlot->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
291 if ($field ==
'array_options' && is_array($value)) {
292 foreach ($value as $index => $val) {
293 $this->productlot->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->productlot);
297 $this->productlot->$field = $this->
_checkValForAPI($field, $value, $this->productlot);
300 if ($this->productlot->update(DolibarrApiAccess::$user) > 0) {
301 return $this->
get(
$id);
320 public function delete(
$id)
322 if (!DolibarrApiAccess::$user->hasRight(
'produit',
'supprimer')) {
323 throw new RestException(403,
"Insufficient rights to delete your product lot");
326 $result = $this->productlot->fetch(
$id);
328 $this->productlot->fetch_optionals();
329 $this->productlot->oldcopy = clone $this->productlot;
333 throw new RestException(404,
'Product lot not found');
337 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
340 if ($this->productlot->delete(DolibarrApiAccess::$user) < 0) {
341 throw new RestException(500,
'Error when delete Product lot : '.implode(
',', $this->productlot->errors));
347 'message' =>
'Product lot deleted'
361 if ($data ===
null) {
367 foreach (self::$FIELDS as $field) {
368 if (!isset($data[$field]) || $data[$field] ===
'') {
369 throw new RestException(400,
"$field field missing");
371 $lot[$field] = $data[$field];
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);
426 unset(
$object->total_localtax1);
427 unset(
$object->total_localtax2);
430 unset(
$object->label_incoterms);
431 unset(
$object->location_incoterms);
438 unset(
$object->demand_reason_id);
439 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.