19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/reception/class/reception.class.php';
34 public static $FIELDS = array(
52 $this->reception =
new Reception($this->db);
64 public function get($id)
66 if (!DolibarrApiAccess::$user->rights->reception->lire) {
67 throw new RestException(401);
70 $result = $this->reception->fetch($id);
72 throw new RestException(404,
'Reception not found');
76 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
79 $this->reception->fetchObjectLinked();
101 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'')
105 if (!DolibarrApiAccess::$user->rights->reception->lire) {
106 throw new RestException(401);
112 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
116 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
117 $search_sale = DolibarrApiAccess::$user->id;
120 $sql =
"SELECT t.rowid";
121 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
122 $sql .=
", sc.fk_soc, sc.fk_user";
124 $sql .=
" FROM ".MAIN_DB_PREFIX.
"reception AS t LEFT JOIN ".MAIN_DB_PREFIX.
"reception_extrafields AS ef ON (ef.fk_object = t.rowid)";
126 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
127 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
130 $sql .=
' WHERE t.entity IN ('.getEntity(
'reception').
')';
131 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
132 $sql .=
" AND t.fk_soc = sc.fk_soc";
135 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
137 if ($search_sale > 0) {
138 $sql .=
" AND t.rowid = sc.fk_soc";
141 if ($search_sale > 0) {
142 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
149 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
153 $sql .= $this->db->order($sortfield, $sortorder);
158 $offset = $limit * $page;
160 $sql .= $this->db->plimit($limit + 1, $offset);
164 $result = $this->db->query($sql);
167 $num = $this->db->num_rows($result);
168 $min = min($num, ($limit <= 0 ? $num : $limit));
171 $obj = $this->db->fetch_object($result);
172 $reception_static =
new Reception($this->db);
173 if ($reception_static->fetch($obj->rowid)) {
179 throw new RestException(503,
'Error when retrieve commande list : '.$this->db->lasterror());
191 public function post($request_data =
null)
193 if (!DolibarrApiAccess::$user->rights->reception->creer) {
194 throw new RestException(401,
"Insuffisant rights");
197 $result = $this->
_validate($request_data);
199 foreach ($request_data as $field => $value) {
200 if ($field ===
'caller') {
202 $this->reception->context[
'caller'] = $request_data[
'caller'];
206 $this->reception->$field = $value;
208 if (isset($request_data[
"lines"])) {
210 foreach ($request_data[
"lines"] as $line) {
211 array_push($lines, (
object) $line);
213 $this->reception->lines = $lines;
216 if ($this->reception->create(DolibarrApiAccess::$user) < 0) {
217 throw new RestException(500,
"Error creating reception", array_merge(array($this->reception->error), $this->reception->errors));
220 return $this->reception->id;
402 if (!DolibarrApiAccess::$user->rights->reception->creer) {
403 throw new RestException(401);
406 $result = $this->reception->fetch($id);
408 throw new RestException(404,
'Reception not found');
412 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
417 $updateRes = $this->reception->deleteline(DolibarrApiAccess::$user, $lineid);
418 if ($updateRes < 0) {
419 throw new RestException(405, $this->reception->error);
425 'message' =>
'Line deleted'
437 public function put($id, $request_data =
null)
439 if (!DolibarrApiAccess::$user->rights->reception->creer) {
440 throw new RestException(401);
443 $result = $this->reception->fetch($id);
445 throw new RestException(404,
'Reception not found');
449 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
451 foreach ($request_data as $field => $value) {
452 if ($field ==
'id') {
455 if ($field ===
'caller') {
457 $this->reception->context[
'caller'] = $request_data[
'caller'];
461 if ($field ==
'array_options' && is_array($value)) {
462 foreach ($value as $index => $val) {
463 $this->reception->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->reception);
467 $this->reception->$field = $this->
_checkValForAPI($field, $value, $this->reception);
470 if ($this->reception->update(DolibarrApiAccess::$user) > 0) {
471 return $this->
get($id);
473 throw new RestException(500, $this->reception->error);
483 public function delete($id)
485 if (!DolibarrApiAccess::$user->rights->reception->supprimer) {
486 throw new RestException(401);
488 $result = $this->reception->fetch($id);
490 throw new RestException(404,
'Reception not found');
494 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
497 if (!$this->reception->delete(DolibarrApiAccess::$user)) {
498 throw new RestException(500,
'Error when deleting reception : '.$this->reception->error);
504 'message' =>
'Reception deleted'
530 if (!DolibarrApiAccess::$user->rights->reception->creer) {
531 throw new RestException(401);
533 $result = $this->reception->fetch($id);
535 throw new RestException(404,
'Reception not found');
539 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
542 $result = $this->reception->valid(DolibarrApiAccess::$user, $notrigger);
544 throw new RestException(304,
'Error nothing done. May be object is already validated');
547 throw new RestException(500,
'Error when validating Reception: '.$this->reception->error);
551 $result = $this->reception->fetch($id);
553 $this->reception->fetchObjectLinked();
650 public function close($id, $notrigger = 0)
652 if (!DolibarrApiAccess::$user->rights->reception->creer) {
653 throw new RestException(401);
656 $result = $this->reception->fetch($id);
658 throw new RestException(404,
'Reception not found');
662 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
665 $result = $this->reception->setClosed();
667 throw new RestException(304,
'Error nothing done. May be object is already closed');
670 throw new RestException(500,
'Error when closing Reception: '.$this->reception->error);
674 $result = $this->reception->fetch($id);
676 $this->reception->fetchObjectLinked();
691 $object = parent::_cleanObjectDatas($object);
693 unset($object->thirdparty);
695 unset($object->note);
696 unset($object->address);
697 unset($object->barcode_type);
698 unset($object->barcode_type_code);
699 unset($object->barcode_type_label);
700 unset($object->barcode_type_coder);
702 if (!empty($object->lines) && is_array($object->lines)) {
703 foreach ($object->lines as $line) {
704 unset($line->tva_tx);
705 unset($line->vat_src_code);
706 unset($line->total_ht);
707 unset($line->total_ttc);
708 unset($line->total_tva);
709 unset($line->total_localtax1);
710 unset($line->total_localtax2);
711 unset($line->remise_percent);
727 $reception = array();
728 foreach (Receptions::$FIELDS as $field) {
729 if (!isset($data[$field])) {
730 throw new RestException(400,
"$field field missing");
732 $reception[$field] = $data[$field];
_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.
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
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.