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).
")";
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);
460 if ($receptionline->fetch($lineid) <= 0) {
461 throw new RestException(404,
'Reception line not found');
463 if ($receptionline->fk_reception != $this->reception->id) {
464 throw new RestException(403,
'Line does not belong to this reception');
467 $updateRes = $this->reception->deleteLine(DolibarrApiAccess::$user, $lineid);
468 if ($updateRes < 0) {
469 throw new RestException(405, $this->reception->error);
475 'message' =>
'Line deleted'
489 public function put(
$id, $request_data =
null)
491 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
492 throw new RestException(403);
495 $result = $this->reception->fetch(
$id);
497 throw new RestException(404,
'Reception not found');
501 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
503 foreach ($request_data as $field => $value) {
504 if ($field ==
'id') {
507 if ($field ===
'caller') {
509 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
513 if ($field ==
'array_options' && is_array($value)) {
514 foreach ($value as $index => $val) {
515 $this->reception->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->reception);
520 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
523 if ($this->reception->update(DolibarrApiAccess::$user) > 0) {
524 return $this->
get(
$id);
526 throw new RestException(500, $this->reception->error);
538 public function delete(
$id)
540 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'supprimer')) {
541 throw new RestException(403);
543 $result = $this->reception->fetch(
$id);
545 throw new RestException(404,
'Reception not found');
549 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
552 if (!$this->reception->delete(DolibarrApiAccess::$user)) {
553 throw new RestException(500,
'Error when deleting reception : '.$this->reception->error);
559 'message' =>
'Reception deleted'
585 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
586 throw new RestException(403);
588 $result = $this->reception->fetch(
$id);
590 throw new RestException(404,
'Reception not found');
594 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
597 $result = $this->reception->valid(DolibarrApiAccess::$user, $notrigger);
599 throw new RestException(304,
'Error nothing done. May be object is already validated');
602 throw new RestException(500,
'Error when validating Reception: '.$this->reception->error);
606 $result = $this->reception->fetch(
$id);
608 $this->reception->fetchObjectLinked();
707 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
708 throw new RestException(403);
711 $result = $this->reception->fetch(
$id);
713 throw new RestException(404,
'Reception not found');
717 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
720 $result = $this->reception->setClosed();
722 throw new RestException(304,
'Error nothing done. May be object is already closed');
725 throw new RestException(500,
'Error when closing Reception: '.$this->reception->error);
729 $result = $this->reception->fetch(
$id);
731 $this->reception->fetchObjectLinked();
756 unset(
$object->barcode_type_code);
757 unset(
$object->barcode_type_label);
758 unset(
$object->barcode_type_coder);
761 foreach (
$object->lines as $line) {
762 unset($line->canvas);
764 unset($line->tva_tx);
765 unset($line->vat_src_code);
766 unset($line->total_ht);
767 unset($line->total_ttc);
768 unset($line->total_tva);
769 unset($line->total_localtax1);
770 unset($line->total_localtax2);
771 unset($line->remise_percent);
787 if ($data ===
null) {
790 $reception = array();
791 foreach (Receptions::$FIELDS as $field) {
792 if (!isset($data[$field])) {
793 throw new RestException(400,
"$field field missing");
795 $reception[$field] = $data[$field];
$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 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)
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.