20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/don/class/don.class.php';
35 public static $FIELDS = array(
51 $this->don =
new Don($this->db);
64 public function get(
$id)
66 if (!DolibarrApiAccess::$user->hasRight(
'don',
'lire')) {
67 throw new RestException(403);
70 $result = $this->don->fetch(
$id);
72 throw new RestException(404,
'Donation not found');
76 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
104 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
106 if (!DolibarrApiAccess::$user->hasRight(
'don',
'lire')) {
107 throw new RestException(403);
113 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
115 $sql =
"SELECT t.rowid";
116 if ((!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids)) {
117 $sql .=
", sc.fk_soc, sc.fk_user";
119 $sql .=
" FROM ".MAIN_DB_PREFIX.
"don AS t LEFT JOIN ".MAIN_DB_PREFIX.
"don_extrafields AS ef ON (ef.fk_object = t.rowid)";
121 $sql .=
' WHERE t.entity IN ('.getEntity(
'don').
')';
122 if ((!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids)) {
123 $sql .=
" AND t.fk_soc = sc.fk_soc";
125 if ($thirdparty_ids) {
126 $sql .=
" AND t.fk_soc = ".((int) $thirdparty_ids).
" ";
134 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
139 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
141 $sql .= $this->db->order($sortfield, $sortorder);
146 $offset = $limit * $page;
148 $sql .= $this->db->plimit($limit + 1, $offset);
152 $result = $this->db->query($sql);
155 $num = $this->db->num_rows($result);
156 $min = min($num, ($limit <= 0 ? $num : $limit));
159 $obj = $this->db->fetch_object($result);
160 $don_static =
new Don($this->db);
161 if ($don_static->fetch($obj->rowid)) {
169 throw new RestException(503,
'Error when retrieve donation list : '.$this->db->lasterror());
173 if ($pagination_data) {
174 $totalsResult = $this->db->query($sqlTotals);
175 $total = $this->db->fetch_object($totalsResult)->total;
180 $obj_ret[
'data'] = $tmp;
181 $obj_ret[
'pagination'] = [
182 'total' => (int) $total,
184 'page_count' => ceil((
int) $total / $limit),
200 public function post($request_data =
null)
202 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
203 throw new RestException(403,
"Insuffisant rights");
207 $result = $this->
_validate($request_data);
209 foreach ($request_data as $field => $value) {
210 if ($field ===
'caller') {
212 $this->don->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
216 $this->don->$field = $this->
_checkValForAPI($field, $value, $this->don);
226 if ($this->don->create(DolibarrApiAccess::$user) < 0) {
227 throw new RestException(500,
"Error creating donation", array_merge(array($this->don->error), $this->don->errors));
230 return $this->don->id;
242 public function put(
$id, $request_data =
null)
244 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
245 throw new RestException(403);
248 $result = $this->don->fetch(
$id);
250 throw new RestException(404,
'Donation not found');
254 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
256 foreach ($request_data as $field => $value) {
257 if ($field ==
'id') {
260 if ($field ===
'caller') {
262 $this->don->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
266 if ($field ==
'array_options' && is_array($value)) {
267 foreach ($value as $index => $val) {
268 $this->don->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->don);
273 $this->don->$field = $this->
_checkValForAPI($field, $value, $this->don);
276 if ($this->don->update(DolibarrApiAccess::$user) > 0) {
277 return $this->
get(
$id);
279 throw new RestException(500, $this->don->error);
291 public function delete(
$id)
293 if (!DolibarrApiAccess::$user->hasRight(
'don',
'supprimer')) {
294 throw new RestException(403);
297 $result = $this->don->fetch(
$id);
299 throw new RestException(404,
'Donation not found');
303 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
306 if (!$this->don->delete(DolibarrApiAccess::$user)) {
307 throw new RestException(500,
'Error when delete donation : '.$this->don->error);
313 'message' =>
'Donation deleted'
342 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
343 throw new RestException(403);
346 $result = $this->don->fetch(
$id);
348 throw new RestException(404,
'Donation not found');
352 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
356 $result = $this->don->valid_promesse(
$id, DolibarrApiAccess::$user->
id, $notrigger);
358 throw new RestException(304,
'Error nothing done. May be object is already validated');
361 throw new RestException(500,
'Error when validating Order: '.$this->don->error);
363 $result = $this->don->fetch(
$id);
365 throw new RestException(404,
'Order not found');
369 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
372 $this->don->fetchObjectLinked();
392 unset(
$object->barcode_type_code);
393 unset(
$object->barcode_type_label);
394 unset(
$object->barcode_type_coder);
408 if ($data ===
null) {
412 foreach (Donations::$FIELDS as $field) {
413 if (!isset($data[$field])) {
414 throw new RestException(400, $field.
" field missing");
416 $don[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
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 donations.
validate($id, $idwarehouse=0, $notrigger=0)
Validate an donation.
post($request_data=null)
Create donation object.
_cleanObjectDatas($object)
Clean sensible object datas.
put($id, $request_data=null)
Update order general fields (won't touch lines of order)
__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='', $pagination_data=false)
List donations.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...