19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/reception/class/reception.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/reception/class/receptionlinebatch.class.php';
35 public static $FIELDS = array(
53 $this->reception =
new Reception($this->db);
65 public function get(
$id)
67 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
68 throw new RestException(403);
71 $result = $this->reception->fetch(
$id);
73 throw new RestException(404,
'Reception not found');
77 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
80 $this->reception->fetchObjectLinked();
103 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
105 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
106 throw new RestException(403);
112 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
116 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
117 $search_sale = DolibarrApiAccess::$user->id;
120 $sql =
"SELECT t.rowid";
121 $sql .=
" FROM ".MAIN_DB_PREFIX.
"reception AS t";
122 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"reception_extrafields AS ef ON (ef.fk_object = t.rowid)";
123 $sql .=
' WHERE t.entity IN ('.getEntity(
'reception').
')';
125 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
128 if ($search_sale && $search_sale !=
'-1') {
129 if ($search_sale == -2) {
130 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
131 } elseif ($search_sale > 0) {
132 $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).
")";
140 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
145 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
147 $sql .= $this->db->order($sortfield, $sortorder);
152 $offset = $limit * $page;
154 $sql .= $this->db->plimit($limit + 1, $offset);
158 $result = $this->db->query($sql);
161 $num = $this->db->num_rows($result);
162 $min = min($num, ($limit <= 0 ? $num : $limit));
165 $obj = $this->db->fetch_object($result);
166 $reception_static =
new Reception($this->db);
167 if ($reception_static->fetch($obj->rowid)) {
173 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
177 if ($pagination_data) {
178 $totalsResult = $this->db->query($sqlTotals);
179 $total = $this->db->fetch_object($totalsResult)->total;
184 $obj_ret[
'data'] = $tmp;
185 $obj_ret[
'pagination'] = [
186 'total' => (int) $total,
188 'page_count' => ceil((
int) $total / $limit),
202 public function post($request_data =
null)
204 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
205 throw new RestException(403,
"Insuffisant rights");
208 $result = $this->
_validate($request_data);
210 foreach ($request_data as $field => $value) {
211 if ($field ===
'caller') {
213 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
217 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
219 if (isset($request_data[
"lines"])) {
221 foreach ($request_data[
"lines"] as $line) {
224 $receptionline->fk_product = $line[
'fk_product'];
225 $receptionline->fk_entrepot = $line[
'fk_entrepot'];
226 $receptionline->fk_element = $line[
'fk_element'] ?? $line[
'origin_id'];
227 $receptionline->origin_line_id = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
228 $receptionline->fk_elementdet = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
229 $receptionline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
230 $receptionline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
231 $receptionline->qty = $line[
'qty'];
233 $receptionline->array_options = $line[
'array_options'];
234 $receptionline->batch = $line[
'batch'];
235 $receptionline->eatby = $line[
'eatby'];
236 $receptionline->sellby = $line[
'sellby'];
237 $receptionline->cost_price = $line[
'cost_price'];
238 $receptionline->status = $line[
'status'];
240 $lines[] = $receptionline;
242 $this->reception->lines = $lines;
245 if ($this->reception->create(DolibarrApiAccess::$user) < 0) {
246 throw new RestException(500,
"Error creating reception", array_merge(array($this->reception->error), $this->reception->errors));
249 return $this->reception->id;
431 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
432 throw new RestException(403);
435 $result = $this->reception->fetch(
$id);
437 throw new RestException(404,
'Reception not found');
441 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
446 $updateRes = $this->reception->deleteLine(DolibarrApiAccess::$user, $lineid);
447 if ($updateRes < 0) {
448 throw new RestException(405, $this->reception->error);
454 'message' =>
'Line deleted'
466 public function put(
$id, $request_data =
null)
468 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
469 throw new RestException(403);
472 $result = $this->reception->fetch(
$id);
474 throw new RestException(404,
'Reception not found');
478 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
480 foreach ($request_data as $field => $value) {
481 if ($field ==
'id') {
484 if ($field ===
'caller') {
486 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
490 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
493 if ($this->reception->update(DolibarrApiAccess::$user) > 0) {
494 return $this->
get(
$id);
496 throw new RestException(500, $this->reception->error);
506 public function delete(
$id)
508 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'supprimer')) {
509 throw new RestException(403);
511 $result = $this->reception->fetch(
$id);
513 throw new RestException(404,
'Reception not found');
517 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
520 if (!$this->reception->delete(DolibarrApiAccess::$user)) {
521 throw new RestException(500,
'Error when deleting reception : '.$this->reception->error);
527 'message' =>
'Reception deleted'
553 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
554 throw new RestException(403);
556 $result = $this->reception->fetch(
$id);
558 throw new RestException(404,
'Reception not found');
562 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
565 $result = $this->reception->valid(DolibarrApiAccess::$user, $notrigger);
567 throw new RestException(304,
'Error nothing done. May be object is already validated');
570 throw new RestException(500,
'Error when validating Reception: '.$this->reception->error);
574 $result = $this->reception->fetch(
$id);
576 $this->reception->fetchObjectLinked();
675 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
676 throw new RestException(403);
679 $result = $this->reception->fetch(
$id);
681 throw new RestException(404,
'Reception not found');
685 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
688 $result = $this->reception->setClosed();
690 throw new RestException(304,
'Error nothing done. May be object is already closed');
693 throw new RestException(500,
'Error when closing Reception: '.$this->reception->error);
697 $result = $this->reception->fetch(
$id);
699 $this->reception->fetchObjectLinked();
721 unset(
$object->barcode_type_code);
722 unset(
$object->barcode_type_label);
723 unset(
$object->barcode_type_coder);
726 foreach (
$object->lines as $line) {
727 unset($line->canvas);
729 unset($line->tva_tx);
730 unset($line->vat_src_code);
731 unset($line->total_ht);
732 unset($line->total_ttc);
733 unset($line->total_tva);
734 unset($line->total_localtax1);
735 unset($line->total_localtax2);
736 unset($line->remise_percent);
752 $reception = array();
753 foreach (Receptions::$FIELDS as $field) {
754 if (!isset($data[$field])) {
755 throw new RestException(400,
"$field field missing");
757 $reception[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $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.
__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.