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();
102 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'')
104 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
105 throw new RestException(403);
111 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
115 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
116 $search_sale = DolibarrApiAccess::$user->id;
119 $sql =
"SELECT t.rowid";
120 $sql .=
" FROM ".MAIN_DB_PREFIX.
"reception AS t";
121 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"reception_extrafields AS ef ON (ef.fk_object = t.rowid)";
122 $sql .=
' WHERE t.entity IN ('.getEntity(
'reception').
')';
124 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
127 if ($search_sale && $search_sale !=
'-1') {
128 if ($search_sale == -2) {
129 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
130 } elseif ($search_sale > 0) {
131 $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).
")";
139 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
143 $sql .= $this->db->order($sortfield, $sortorder);
148 $offset = $limit * $page;
150 $sql .= $this->db->plimit($limit + 1, $offset);
154 $result = $this->db->query($sql);
157 $num = $this->db->num_rows($result);
158 $min = min($num, ($limit <= 0 ? $num : $limit));
161 $obj = $this->db->fetch_object($result);
162 $reception_static =
new Reception($this->db);
163 if ($reception_static->fetch($obj->rowid)) {
169 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
181 public function post($request_data =
null)
183 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
184 throw new RestException(403,
"Insuffisant rights");
187 $result = $this->
_validate($request_data);
189 foreach ($request_data as $field => $value) {
190 if ($field ===
'caller') {
192 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
196 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
198 if (isset($request_data[
"lines"])) {
200 foreach ($request_data[
"lines"] as $line) {
203 $receptionline->fk_product = $line[
'fk_product'];
204 $receptionline->fk_entrepot = $line[
'fk_entrepot'];
205 $receptionline->fk_element = $line[
'fk_element'] ?? $line[
'origin_id'];
206 $receptionline->origin_line_id = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
207 $receptionline->fk_elementdet = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
208 $receptionline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
209 $receptionline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
210 $receptionline->qty = $line[
'qty'];
212 $receptionline->array_options = $line[
'array_options'];
213 $receptionline->batch = $line[
'batch'];
214 $receptionline->eatby = $line[
'eatby'];
215 $receptionline->sellby = $line[
'sellby'];
216 $receptionline->cost_price = $line[
'cost_price'];
217 $receptionline->status = $line[
'status'];
219 $lines[] = $receptionline;
221 $this->reception->lines = $lines;
224 if ($this->reception->create(DolibarrApiAccess::$user) < 0) {
225 throw new RestException(500,
"Error creating reception", array_merge(array($this->reception->error), $this->reception->errors));
228 return $this->reception->id;
410 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
411 throw new RestException(403);
414 $result = $this->reception->fetch($id);
416 throw new RestException(404,
'Reception not found');
420 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
425 $updateRes = $this->reception->deleteLine(DolibarrApiAccess::$user, $lineid);
426 if ($updateRes < 0) {
427 throw new RestException(405, $this->reception->error);
433 'message' =>
'Line deleted'
445 public function put($id, $request_data =
null)
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);
459 foreach ($request_data as $field => $value) {
460 if ($field ==
'id') {
463 if ($field ===
'caller') {
465 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
469 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
472 if ($this->reception->update(DolibarrApiAccess::$user) > 0) {
473 return $this->
get($id);
475 throw new RestException(500, $this->reception->error);
485 public function delete($id)
487 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'supprimer')) {
488 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);
499 if (!$this->reception->delete(DolibarrApiAccess::$user)) {
500 throw new RestException(500,
'Error when deleting reception : '.$this->reception->error);
506 'message' =>
'Reception deleted'
532 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
533 throw new RestException(403);
535 $result = $this->reception->fetch($id);
537 throw new RestException(404,
'Reception not found');
541 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
544 $result = $this->reception->valid(DolibarrApiAccess::$user, $notrigger);
546 throw new RestException(304,
'Error nothing done. May be object is already validated');
549 throw new RestException(500,
'Error when validating Reception: '.$this->reception->error);
553 $result = $this->reception->fetch($id);
555 $this->reception->fetchObjectLinked();
652 public function close($id, $notrigger = 0)
654 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
655 throw new RestException(403);
658 $result = $this->reception->fetch($id);
660 throw new RestException(404,
'Reception not found');
664 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
667 $result = $this->reception->setClosed();
669 throw new RestException(304,
'Error nothing done. May be object is already closed');
672 throw new RestException(500,
'Error when closing Reception: '.$this->reception->error);
676 $result = $this->reception->fetch($id);
678 $this->reception->fetchObjectLinked();
700 unset(
$object->barcode_type_code);
701 unset(
$object->barcode_type_label);
702 unset(
$object->barcode_type_coder);
705 foreach (
$object->lines as $line) {
706 unset($line->canvas);
708 unset($line->tva_tx);
709 unset($line->vat_src_code);
710 unset($line->total_ht);
711 unset($line->total_ttc);
712 unset($line->total_tva);
713 unset($line->total_localtax1);
714 unset($line->total_localtax2);
715 unset($line->remise_percent);
731 $reception = array();
732 foreach (Receptions::$FIELDS as $field) {
733 if (!isset($data[$field])) {
734 throw new RestException(400,
"$field field missing");
736 $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.
_cleanObjectDatas($object)
Clean sensible object datas.
__construct()
Constructor.
_validate($data)
Validate fields before create or update object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List receptions.
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.