21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/reception/class/reception.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/reception/class/receptionlinebatch.class.php';
37 public static $FIELDS = array(
55 $this->reception =
new Reception($this->db);
67 public function get(
$id)
69 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
70 throw new RestException(403);
73 $result = $this->reception->fetch(
$id);
75 throw new RestException(404,
'Reception not found');
79 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
82 $this->reception->fetchObjectLinked();
107 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
109 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
110 throw new RestException(403);
116 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
120 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
121 $search_sale = DolibarrApiAccess::$user->id;
124 $sql =
"SELECT t.rowid";
125 $sql .=
" FROM ".MAIN_DB_PREFIX.
"reception AS t";
126 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"reception_extrafields AS ef ON (ef.fk_object = t.rowid)";
127 if (preg_match(
"/el\.fk_source:=:\d+/", $sqlfilters)) {
128 $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'";
130 $sql .=
' WHERE t.entity IN ('.getEntity(
'reception').
')';
132 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
135 if ($search_sale && $search_sale !=
'-1') {
136 if ($search_sale == -2) {
137 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
138 } elseif ($search_sale > 0) {
139 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
145 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
147 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
152 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
154 $sql .= $this->db->order($sortfield, $sortorder);
159 $offset = $limit * $page;
161 $sql .= $this->db->plimit($limit + 1, $offset);
165 $result = $this->db->query($sql);
168 $num = $this->db->num_rows($result);
169 $min = min($num, ($limit <= 0 ? $num : $limit));
172 $obj = $this->db->fetch_object($result);
173 $reception_static =
new Reception($this->db);
174 if ($reception_static->fetch($obj->rowid)) {
180 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
184 if ($pagination_data) {
185 $totalsResult = $this->db->query($sqlTotals);
186 $total = $this->db->fetch_object($totalsResult)->total;
191 $obj_ret[
'data'] = $tmp;
192 $obj_ret[
'pagination'] = [
193 'total' => (int) $total,
195 'page_count' => ceil((
int) $total / $limit),
211 public function post($request_data =
null)
213 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
214 throw new RestException(403,
"Insufficiant rights");
217 $result = $this->
_validate($request_data);
219 foreach ($request_data as $field => $value) {
220 if ($field ===
'caller') {
222 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
226 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
228 if (isset($request_data[
"lines"]) && is_array($request_data[
'lines'])) {
230 foreach ($request_data[
"lines"] as $line) {
233 $receptionline->fk_product = $line[
'fk_product'];
234 $receptionline->fk_entrepot = $line[
'fk_entrepot'];
235 $receptionline->fk_element = $line[
'fk_element'] ?? $line[
'origin_id'];
236 $receptionline->origin_line_id = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
237 $receptionline->fk_elementdet = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
238 $receptionline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
239 $receptionline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
240 $receptionline->qty = $line[
'qty'];
242 $receptionline->array_options = $line[
'array_options'];
243 $receptionline->batch = $line[
'batch'];
244 $receptionline->eatby = $line[
'eatby'];
245 $receptionline->sellby = $line[
'sellby'];
246 $receptionline->cost_price = $line[
'cost_price'];
247 $receptionline->status = $line[
'status'];
249 $lines[] = $receptionline;
251 $this->reception->lines = $lines;
254 if ($this->reception->create(DolibarrApiAccess::$user) < 0) {
255 throw new RestException(500,
"Error creating reception", array_merge(array($this->reception->error), $this->reception->errors));
258 return $this->reception->id;
446 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
447 throw new RestException(403);
450 $result = $this->reception->fetch(
$id);
452 throw new RestException(404,
'Reception not found');
456 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
461 $updateRes = $this->reception->deleteLine(DolibarrApiAccess::$user, $lineid);
462 if ($updateRes < 0) {
463 throw new RestException(405, $this->reception->error);
469 'message' =>
'Line deleted'
483 public function put(
$id, $request_data =
null)
485 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
486 throw new RestException(403);
489 $result = $this->reception->fetch(
$id);
491 throw new RestException(404,
'Reception not found');
495 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
497 foreach ($request_data as $field => $value) {
498 if ($field ==
'id') {
501 if ($field ===
'caller') {
503 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
507 if ($field ==
'array_options' && is_array($value)) {
508 foreach ($value as $index => $val) {
514 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
517 if ($this->reception->update(DolibarrApiAccess::$user) > 0) {
518 return $this->
get(
$id);
520 throw new RestException(500, $this->reception->error);
532 public function delete(
$id)
534 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'supprimer')) {
535 throw new RestException(403);
537 $result = $this->reception->fetch(
$id);
539 throw new RestException(404,
'Reception not found');
543 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
546 if (!$this->reception->delete(DolibarrApiAccess::$user)) {
547 throw new RestException(500,
'Error when deleting reception : '.$this->reception->error);
553 'message' =>
'Reception deleted'
579 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
580 throw new RestException(403);
582 $result = $this->reception->fetch(
$id);
584 throw new RestException(404,
'Reception not found');
588 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
591 $result = $this->reception->valid(DolibarrApiAccess::$user, $notrigger);
593 throw new RestException(304,
'Error nothing done. May be object is already validated');
596 throw new RestException(500,
'Error when validating Reception: '.$this->reception->error);
600 $result = $this->reception->fetch(
$id);
602 $this->reception->fetchObjectLinked();
701 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
702 throw new RestException(403);
705 $result = $this->reception->fetch(
$id);
707 throw new RestException(404,
'Reception not found');
711 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
714 $result = $this->reception->setClosed();
716 throw new RestException(304,
'Error nothing done. May be object is already closed');
719 throw new RestException(500,
'Error when closing Reception: '.$this->reception->error);
723 $result = $this->reception->fetch(
$id);
725 $this->reception->fetchObjectLinked();
750 unset(
$object->barcode_type_code);
751 unset(
$object->barcode_type_label);
752 unset(
$object->barcode_type_coder);
755 foreach (
$object->lines as $line) {
756 unset($line->canvas);
758 unset($line->tva_tx);
759 unset($line->vat_src_code);
760 unset($line->total_ht);
761 unset($line->total_ttc);
762 unset($line->total_tva);
763 unset($line->total_localtax1);
764 unset($line->total_localtax2);
765 unset($line->remise_percent);
781 if ($data ===
null) {
784 $reception = array();
785 foreach (Receptions::$FIELDS as $field) {
786 if (!isset($data[$field])) {
787 throw new RestException(400,
"$field field missing");
789 $reception[$field] = $data[$field];
$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.
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 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.
deleteLine($id, $lineid)
Get lines of an 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.
_validate($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update reception general fields (won't touch lines of reception)
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.