22use Luracast\Restler\RestException;
24require_once DOL_DOCUMENT_ROOT.
'/reception/class/reception.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/reception/class/receptionlinebatch.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
27require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
28require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.dispatch.class.php';
41 public static $FIELDS = array(
59 $this->reception =
new Reception($this->db);
71 public function get(
$id)
73 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
74 throw new RestException(403);
77 $result = $this->reception->fetch(
$id);
79 throw new RestException(404,
'Reception not found');
83 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
86 $this->reception->fetchObjectLinked();
111 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
113 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
114 throw new RestException(403);
120 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
124 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
125 $search_sale = DolibarrApiAccess::$user->id;
128 $sql =
"SELECT t.rowid";
129 $sql .=
" FROM ".MAIN_DB_PREFIX.
"reception AS t";
130 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"reception_extrafields AS ef ON (ef.fk_object = t.rowid)";
131 if (preg_match(
"/el\.fk_source:=:\d+/", $sqlfilters)) {
132 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"element_element as el ON el.fk_target = t.rowid AND el.targettype = 'reception' AND el.sourcetype = 'order_supplier'";
134 $sql .=
' WHERE t.entity IN ('.getEntity(
'reception').
')';
136 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
139 if ($search_sale && $search_sale !=
'-1') {
140 if ($search_sale == -2) {
141 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
142 } elseif ($search_sale > 0) {
143 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
151 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
156 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
158 $sql .= $this->db->order($sortfield, $sortorder);
163 $offset = $limit * $page;
165 $sql .= $this->db->plimit($limit + 1, $offset);
169 $result = $this->db->query($sql);
172 $num = $this->db->num_rows($result);
173 $min = min($num, ($limit <= 0 ? $num : $limit));
176 $obj = $this->db->fetch_object($result);
177 $reception_static =
new Reception($this->db);
178 if ($reception_static->fetch($obj->rowid)) {
184 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
188 if ($pagination_data) {
189 $totalsResult = $this->db->query($sqlTotals);
190 $total = $this->db->fetch_object($totalsResult)->total;
195 $obj_ret[
'data'] = $tmp;
196 $obj_ret[
'pagination'] = [
197 'total' => (int) $total,
199 'page_count' => ceil((
int) $total / $limit),
215 public function post($request_data =
null)
217 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
218 throw new RestException(403,
"Insufficiant rights");
221 $result = $this->
_validate($request_data);
223 foreach ($request_data as $field => $value) {
224 if ($field ===
'caller') {
226 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
230 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
232 if (isset($request_data[
"lines"]) && is_array($request_data[
'lines'])) {
234 foreach ($request_data[
"lines"] as $line) {
237 $receptionline->fk_product = $line[
'fk_product'];
238 $receptionline->fk_entrepot = $line[
'fk_entrepot'];
239 $receptionline->fk_element = $line[
'fk_element'] ?? $line[
'origin_id'];
240 $receptionline->origin_line_id = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
241 $receptionline->fk_elementdet = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
242 $receptionline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
243 $receptionline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
244 $receptionline->qty = $line[
'qty'];
246 $receptionline->array_options = $line[
'array_options'];
247 $receptionline->batch = $line[
'batch'];
248 $receptionline->eatby = $line[
'eatby'];
249 $receptionline->sellby = $line[
'sellby'];
250 $receptionline->cost_price = $line[
'cost_price'];
251 $receptionline->status = $line[
'status'];
253 $lines[] = $receptionline;
255 $this->reception->lines = $lines;
258 if ($this->reception->create(DolibarrApiAccess::$user) < 0) {
259 throw new RestException(500,
"Error creating reception", array_merge(array($this->reception->error), $this->reception->errors));
262 return $this->reception->id;
281 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
282 throw new RestException(403);
285 $result = $this->reception->fetch(
$id);
287 throw new RestException(404,
'Reception not found');
291 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
297 foreach ($this->reception->lines as $line) {
330 public function postLine(
$id, $fk_origin_line = 0, $qty = 0, $warehouse_id = 0, $batch =
'', $eatby =
'', $sellby =
'', $cost_price = 0, $comment =
'')
332 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
333 throw new RestException(403);
336 $result = $this->reception->fetch(
$id);
338 throw new RestException(404,
'Reception not found');
342 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
346 if ((
int) $this->reception->status != Reception::STATUS_DRAFT) {
347 throw new RestException(405,
'Lines can only be added to a draft reception');
350 $fk_origin_line = (int) $fk_origin_line;
351 if ($fk_origin_line <= 0) {
352 throw new RestException(400,
'Field fk_origin_line is mandatory');
357 throw new RestException(400,
'Field qty is mandatory and must be greater than 0');
361 $resorderline = $orderline->fetch($fk_origin_line);
362 if ($resorderline <= 0) {
363 throw new RestException(404,
'Origin supplier order line '.$fk_origin_line.
' not found');
368 if ($this->reception->origin_id > 0 && (
int) $orderline->fk_commande != (
int) $this->reception->origin_id) {
369 throw new RestException(400,
'Origin line '.$fk_origin_line.
' does not belong to the supplier order of this reception');
373 if (empty($orderline->fk_product)) {
374 throw new RestException(400,
'Origin line '.$fk_origin_line.
' has no product: only product lines can be received');
382 $addResult = $this->reception->addline((
int) $warehouse_id, $fk_origin_line, $qty, array(), trim((
string) $comment), $eatbydate, $sellbydate, trim((
string) $batch), $cost_price);
383 if ($addResult < 0) {
385 throw new RestException(400, $this->reception->errorsToString() ? $this->reception->errorsToString() :
'Error while adding reception line');
390 $line = end($this->reception->lines);
392 $createRes = $line->create(DolibarrApiAccess::$user);
393 if ($createRes > 0) {
397 throw new RestException(500, $line->errorsToString());
423 public function putLine(
$id, $lineid, $qty = 0, $warehouse_id = 0)
425 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
426 throw new RestException(403);
429 $result = $this->reception->fetch(
$id);
431 throw new RestException(404,
'Reception not found');
435 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
439 if ((
int) $this->reception->status != Reception::STATUS_DRAFT) {
440 throw new RestException(405,
'Only draft reception lines can be updated');
445 throw new RestException(400,
'Field qty is mandatory and must be greater than 0');
451 if ($warehouse_id > 0) {
452 $line->fk_entrepot = (int) $warehouse_id;
455 $updateRes = $line->update(DolibarrApiAccess::$user);
456 if ($updateRes > 0) {
457 return $this->
get(
$id);
460 throw new RestException(500, $line->errorsToString());
483 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
484 throw new RestException(403);
487 $result = $this->reception->fetch(
$id);
489 throw new RestException(404,
'Reception not found');
493 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
497 if ((
int) $this->reception->status != Reception::STATUS_DRAFT) {
498 throw new RestException(405,
'Only draft reception lines can be deleted');
503 $deleteRes = $line->delete(DolibarrApiAccess::$user);
504 if ($deleteRes > 0) {
505 return $this->
get(
$id);
508 throw new RestException(500, $line->errorsToString());
523 $resline = $line->fetch($lineid);
524 if ($resline <= 0 || empty($line->id)) {
525 throw new RestException(404,
'Reception line not found');
527 if ((
int) $line->fk_reception != (
int) $this->reception->id) {
528 throw new RestException(404,
'Line '.((
int) $lineid).
' is not a line of reception '.((
int) $this->reception->id));
543 public function put(
$id, $request_data =
null)
545 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
546 throw new RestException(403);
549 $result = $this->reception->fetch(
$id);
551 throw new RestException(404,
'Reception not found');
555 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
557 foreach ($request_data as $field => $value) {
558 if ($field ==
'id') {
561 if ($field ===
'caller') {
563 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
567 if ($field ==
'array_options' && is_array($value)) {
568 foreach ($value as $index => $val) {
574 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
577 if ($this->reception->update(DolibarrApiAccess::$user) > 0) {
578 return $this->
get(
$id);
580 throw new RestException(500, $this->reception->errorsToString());
592 public function delete(
$id)
594 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'supprimer')) {
595 throw new RestException(403);
597 $result = $this->reception->fetch(
$id);
599 throw new RestException(404,
'Reception not found');
603 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
606 if (!$this->reception->delete(DolibarrApiAccess::$user)) {
607 throw new RestException(500,
'Error when deleting reception : '.$this->reception->errorsToString());
613 'message' =>
'Reception deleted'
633 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
634 throw new RestException(403);
636 $result = $this->reception->fetch(
$id);
638 throw new RestException(404,
'Reception not found');
642 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
645 $result = $this->reception->valid(DolibarrApiAccess::$user, $notrigger);
647 throw new RestException(304,
'Error nothing done. May be object is already validated');
650 throw new RestException(500,
'Error when validating Reception: '.$this->reception->errorsToString());
654 $result = $this->reception->fetch(
$id);
656 $this->reception->fetchObjectLinked();
755 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
756 throw new RestException(403);
759 $result = $this->reception->fetch(
$id);
761 throw new RestException(404,
'Reception not found');
765 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
768 $result = $this->reception->setClosed();
770 throw new RestException(304,
'Error nothing done. May be object is already closed');
773 throw new RestException(500,
'Error when closing Reception: '.$this->reception->errorsToString());
777 $result = $this->reception->fetch(
$id);
779 $this->reception->fetchObjectLinked();
806 if (!((!
getDolGlobalInt(
'MAIN_USE_ADVANCED_PERMS') && DolibarrApiAccess::$user->hasRight(
'reception',
'creer'))
807 || (
getDolGlobalInt(
'MAIN_USE_ADVANCED_PERMS') && DolibarrApiAccess::$user->hasRight(
'reception',
'reception_advance',
'validate')))) {
808 throw new RestException(403);
811 $result = $this->reception->fetch(
$id);
813 throw new RestException(404,
'Reception not found');
817 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
820 $result = $this->reception->setDraft(DolibarrApiAccess::$user);
822 throw new RestException(304,
'Error nothing done. May be object is already draft');
825 throw new RestException(500,
'Error when setting reception back to draft: '.$this->reception->errorsToString());
829 $result = $this->reception->fetch(
$id);
831 $this->reception->fetchObjectLinked();
856 unset(
$object->barcode_type_code);
857 unset(
$object->barcode_type_label);
858 unset(
$object->barcode_type_coder);
861 foreach (
$object->lines as $line) {
862 unset($line->canvas);
864 unset($line->tva_tx);
865 unset($line->vat_src_code);
866 unset($line->total_ht);
867 unset($line->total_ttc);
868 unset($line->total_tva);
869 unset($line->total_localtax1);
870 unset($line->total_localtax2);
871 unset($line->remise_percent);
887 if ($data ===
null) {
890 $reception = array();
894 $mandatory = Receptions::$FIELDS;
895 if (
getDolGlobalString(
'RECEPTION_STANDALONE') && !isset($data[
'origin_id']) && !isset($data[
'origin_type'])) {
896 $mandatory = array_values(array_diff($mandatory, array(
'origin_id',
'origin_type')));
898 foreach ($mandatory as $field) {
899 if (!isset($data[$field])) {
900 throw new RestException(400,
"$field field missing");
902 $reception[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
Class to manage line orders.
_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 to manage receptions.
Class to manage table commandefournisseurdispatch.
close($id, $notrigger=0)
Classify the reception as invoiced.
validate($id, $notrigger=0)
Validate a reception.
post($request_data=null)
Create reception object.
getLines($id)
Get lines of a reception.
deleteLine($id, $lineid)
Delete a line of a given reception.
setToDraft($id)
Set a reception back to draft status.
putLine($id, $lineid, $qty=0, $warehouse_id=0)
Update a line of a given reception.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='', $pagination_data=false)
List receptions.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
__construct()
Constructor.
fetchLineOfReception($lineid)
Load a line and check it belongs to the reception currently loaded.
_validate($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update reception general fields (won't touch lines of reception)
postLine($id, $fk_origin_line=0, $qty=0, $warehouse_id=0, $batch='', $eatby='', $sellby='', $cost_price=0, $comment='')
Add a line to a given reception.
dol_stringtotime($string, $gm=1, $processnotimeasnoon=0)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.