21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/reception/class/reception.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/reception/class/receptionlinebatch.class.php';
25require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
38 public static $FIELDS = array(
56 $this->reception =
new Reception($this->db);
68 public function get(
$id)
70 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
71 throw new RestException(403);
74 $result = $this->reception->fetch(
$id);
76 throw new RestException(404,
'Reception not found');
80 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
83 $this->reception->fetchObjectLinked();
108 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
110 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
111 throw new RestException(403);
117 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
121 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
122 $search_sale = DolibarrApiAccess::$user->id;
125 $sql =
"SELECT t.rowid";
126 $sql .=
" FROM ".MAIN_DB_PREFIX.
"reception AS t";
127 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"reception_extrafields AS ef ON (ef.fk_object = t.rowid)";
128 if (preg_match(
"/el\.fk_source:=:\d+/", $sqlfilters)) {
129 $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'";
131 $sql .=
' WHERE t.entity IN ('.getEntity(
'reception').
')';
133 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
136 if ($search_sale && $search_sale !=
'-1') {
137 if ($search_sale == -2) {
138 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
139 } elseif ($search_sale > 0) {
140 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
148 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
153 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
155 $sql .= $this->db->order($sortfield, $sortorder);
160 $offset = $limit * $page;
162 $sql .= $this->db->plimit($limit + 1, $offset);
166 $result = $this->db->query($sql);
169 $num = $this->db->num_rows($result);
170 $min = min($num, ($limit <= 0 ? $num : $limit));
173 $obj = $this->db->fetch_object($result);
174 $reception_static =
new Reception($this->db);
175 if ($reception_static->fetch($obj->rowid)) {
181 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
185 if ($pagination_data) {
186 $totalsResult = $this->db->query($sqlTotals);
187 $total = $this->db->fetch_object($totalsResult)->total;
192 $obj_ret[
'data'] = $tmp;
193 $obj_ret[
'pagination'] = [
194 'total' => (int) $total,
196 'page_count' => ceil((
int) $total / $limit),
212 public function post($request_data =
null)
214 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
215 throw new RestException(403,
"Insufficiant rights");
218 $result = $this->
_validate($request_data);
220 foreach ($request_data as $field => $value) {
221 if ($field ===
'caller') {
223 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
227 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
229 if (isset($request_data[
"lines"]) && is_array($request_data[
'lines'])) {
231 foreach ($request_data[
"lines"] as $line) {
234 $receptionline->fk_product = $line[
'fk_product'];
235 $receptionline->fk_entrepot = $line[
'fk_entrepot'];
236 $receptionline->fk_element = $line[
'fk_element'] ?? $line[
'origin_id'];
237 $receptionline->origin_line_id = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
238 $receptionline->fk_elementdet = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
239 $receptionline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
240 $receptionline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
241 $receptionline->qty = $line[
'qty'];
243 $receptionline->array_options = $line[
'array_options'];
244 $receptionline->batch = $line[
'batch'];
245 $receptionline->eatby = $line[
'eatby'];
246 $receptionline->sellby = $line[
'sellby'];
247 $receptionline->cost_price = $line[
'cost_price'];
248 $receptionline->status = $line[
'status'];
250 $lines[] = $receptionline;
252 $this->reception->lines = $lines;
255 if ($this->reception->create(DolibarrApiAccess::$user) < 0) {
256 throw new RestException(500,
"Error creating reception", array_merge(array($this->reception->error), $this->reception->errors));
259 return $this->reception->id;
447 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
448 throw new RestException(403);
451 $result = $this->reception->fetch(
$id);
453 throw new RestException(404,
'Reception not found');
457 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
462 $updateRes = $this->reception->deleteLine(DolibarrApiAccess::$user, $lineid);
463 if ($updateRes < 0) {
464 throw new RestException(405, $this->reception->error);
470 'message' =>
'Line deleted'
484 public function put(
$id, $request_data =
null)
486 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
487 throw new RestException(403);
490 $result = $this->reception->fetch(
$id);
492 throw new RestException(404,
'Reception not found');
496 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
498 foreach ($request_data as $field => $value) {
499 if ($field ==
'id') {
502 if ($field ===
'caller') {
504 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
508 if ($field ==
'array_options' && is_array($value)) {
509 foreach ($value as $index => $val) {
515 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
518 if ($this->reception->update(DolibarrApiAccess::$user) > 0) {
519 return $this->
get(
$id);
521 throw new RestException(500, $this->reception->error);
533 public function delete(
$id)
535 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'supprimer')) {
536 throw new RestException(403);
538 $result = $this->reception->fetch(
$id);
540 throw new RestException(404,
'Reception not found');
544 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
547 if (!$this->reception->delete(DolibarrApiAccess::$user)) {
548 throw new RestException(500,
'Error when deleting reception : '.$this->reception->error);
554 'message' =>
'Reception deleted'
580 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
581 throw new RestException(403);
583 $result = $this->reception->fetch(
$id);
585 throw new RestException(404,
'Reception not found');
589 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
592 $result = $this->reception->valid(DolibarrApiAccess::$user, $notrigger);
594 throw new RestException(304,
'Error nothing done. May be object is already validated');
597 throw new RestException(500,
'Error when validating Reception: '.$this->reception->error);
601 $result = $this->reception->fetch(
$id);
603 $this->reception->fetchObjectLinked();
702 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
703 throw new RestException(403);
706 $result = $this->reception->fetch(
$id);
708 throw new RestException(404,
'Reception not found');
712 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
715 $result = $this->reception->setClosed();
717 throw new RestException(304,
'Error nothing done. May be object is already closed');
720 throw new RestException(500,
'Error when closing Reception: '.$this->reception->error);
724 $result = $this->reception->fetch(
$id);
726 $this->reception->fetchObjectLinked();
751 unset(
$object->barcode_type_code);
752 unset(
$object->barcode_type_label);
753 unset(
$object->barcode_type_coder);
756 foreach (
$object->lines as $line) {
757 unset($line->canvas);
759 unset($line->tva_tx);
760 unset($line->vat_src_code);
761 unset($line->total_ht);
762 unset($line->total_ttc);
763 unset($line->total_tva);
764 unset($line->total_localtax1);
765 unset($line->total_localtax2);
766 unset($line->remise_percent);
782 if ($data ===
null) {
785 $reception = array();
786 foreach (Receptions::$FIELDS as $field) {
787 if (!isset($data[$field])) {
788 throw new RestException(400,
"$field field missing");
790 $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.
_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.
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)
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.