20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/reception/class/reception.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/reception/class/receptionlinebatch.class.php';
36 public static $FIELDS = array(
54 $this->reception =
new Reception($this->db);
66 public function get(
$id)
68 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
69 throw new RestException(403);
72 $result = $this->reception->fetch(
$id);
74 throw new RestException(404,
'Reception not found');
78 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
81 $this->reception->fetchObjectLinked();
106 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
108 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'lire')) {
109 throw new RestException(403);
115 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
119 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
120 $search_sale = DolibarrApiAccess::$user->id;
123 $sql =
"SELECT t.rowid";
124 $sql .=
" FROM ".MAIN_DB_PREFIX.
"reception AS t";
125 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"reception_extrafields AS ef ON (ef.fk_object = t.rowid)";
126 $sql .=
' WHERE t.entity IN ('.getEntity(
'reception').
')';
128 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
131 if ($search_sale && $search_sale !=
'-1') {
132 if ($search_sale == -2) {
133 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
134 } elseif ($search_sale > 0) {
135 $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).
")";
143 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
148 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
150 $sql .= $this->db->order($sortfield, $sortorder);
155 $offset = $limit * $page;
157 $sql .= $this->db->plimit($limit + 1, $offset);
161 $result = $this->db->query($sql);
164 $num = $this->db->num_rows($result);
165 $min = min($num, ($limit <= 0 ? $num : $limit));
168 $obj = $this->db->fetch_object($result);
169 $reception_static =
new Reception($this->db);
170 if ($reception_static->fetch($obj->rowid)) {
176 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
180 if ($pagination_data) {
181 $totalsResult = $this->db->query($sqlTotals);
182 $total = $this->db->fetch_object($totalsResult)->total;
187 $obj_ret[
'data'] = $tmp;
188 $obj_ret[
'pagination'] = [
189 'total' => (int) $total,
191 'page_count' => ceil((
int) $total / $limit),
207 public function post($request_data =
null)
209 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
210 throw new RestException(403,
"Insuffisant rights");
213 $result = $this->
_validate($request_data);
215 foreach ($request_data as $field => $value) {
216 if ($field ===
'caller') {
218 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
222 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
224 if (isset($request_data[
"lines"]) && is_array($request_data[
'lines'])) {
226 foreach ($request_data[
"lines"] as $line) {
229 $receptionline->fk_product = $line[
'fk_product'];
230 $receptionline->fk_entrepot = $line[
'fk_entrepot'];
231 $receptionline->fk_element = $line[
'fk_element'] ?? $line[
'origin_id'];
232 $receptionline->origin_line_id = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
233 $receptionline->fk_elementdet = $line[
'fk_elementdet'] ?? $line[
'origin_line_id'];
234 $receptionline->origin_type = $line[
'element_type'] ?? $line[
'origin_type'];
235 $receptionline->element_type = $line[
'element_type'] ?? $line[
'origin_type'];
236 $receptionline->qty = $line[
'qty'];
238 $receptionline->array_options = $line[
'array_options'];
239 $receptionline->batch = $line[
'batch'];
240 $receptionline->eatby = $line[
'eatby'];
241 $receptionline->sellby = $line[
'sellby'];
242 $receptionline->cost_price = $line[
'cost_price'];
243 $receptionline->status = $line[
'status'];
245 $lines[] = $receptionline;
247 $this->reception->lines = $lines;
250 if ($this->reception->create(DolibarrApiAccess::$user) < 0) {
251 throw new RestException(500,
"Error creating reception", array_merge(array($this->reception->error), $this->reception->errors));
254 return $this->reception->id;
438 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
439 throw new RestException(403);
442 $result = $this->reception->fetch(
$id);
444 throw new RestException(404,
'Reception not found');
448 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
453 $updateRes = $this->reception->deleteLine(DolibarrApiAccess::$user, $lineid);
454 if ($updateRes < 0) {
455 throw new RestException(405, $this->reception->error);
461 'message' =>
'Line deleted'
475 public function put(
$id, $request_data =
null)
477 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
478 throw new RestException(403);
481 $result = $this->reception->fetch(
$id);
483 throw new RestException(404,
'Reception not found');
487 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
489 foreach ($request_data as $field => $value) {
490 if ($field ==
'id') {
493 if ($field ===
'caller') {
495 $this->reception->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
499 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
502 if ($this->reception->update(DolibarrApiAccess::$user) > 0) {
503 return $this->
get(
$id);
505 throw new RestException(500, $this->reception->error);
517 public function delete(
$id)
519 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'supprimer')) {
520 throw new RestException(403);
522 $result = $this->reception->fetch(
$id);
524 throw new RestException(404,
'Reception not found');
528 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
531 if (!$this->reception->delete(DolibarrApiAccess::$user)) {
532 throw new RestException(500,
'Error when deleting reception : '.$this->reception->error);
538 'message' =>
'Reception deleted'
564 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
565 throw new RestException(403);
567 $result = $this->reception->fetch(
$id);
569 throw new RestException(404,
'Reception not found');
573 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
576 $result = $this->reception->valid(DolibarrApiAccess::$user, $notrigger);
578 throw new RestException(304,
'Error nothing done. May be object is already validated');
581 throw new RestException(500,
'Error when validating Reception: '.$this->reception->error);
585 $result = $this->reception->fetch(
$id);
587 $this->reception->fetchObjectLinked();
686 if (!DolibarrApiAccess::$user->hasRight(
'reception',
'creer')) {
687 throw new RestException(403);
690 $result = $this->reception->fetch(
$id);
692 throw new RestException(404,
'Reception not found');
696 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
699 $result = $this->reception->setClosed();
701 throw new RestException(304,
'Error nothing done. May be object is already closed');
704 throw new RestException(500,
'Error when closing Reception: '.$this->reception->error);
708 $result = $this->reception->fetch(
$id);
710 $this->reception->fetchObjectLinked();
732 unset(
$object->barcode_type_code);
733 unset(
$object->barcode_type_label);
734 unset(
$object->barcode_type_coder);
737 foreach (
$object->lines as $line) {
738 unset($line->canvas);
740 unset($line->tva_tx);
741 unset($line->vat_src_code);
742 unset($line->total_ht);
743 unset($line->total_ttc);
744 unset($line->total_tva);
745 unset($line->total_localtax1);
746 unset($line->total_localtax2);
747 unset($line->remise_percent);
763 $reception = array();
764 foreach (Receptions::$FIELDS as $field) {
765 if (!isset($data[$field])) {
766 throw new RestException(400,
"$field field missing");
768 $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.