19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/don/class/don.class.php';
34 public static $FIELDS = array(
50 $this->don =
new Don($this->db);
63 public function get(
$id)
65 if (!DolibarrApiAccess::$user->hasRight(
'don',
'lire')) {
66 throw new RestException(403);
69 $result = $this->don->fetch(
$id);
71 throw new RestException(404,
'Donation not found');
75 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
101 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
103 if (!DolibarrApiAccess::$user->hasRight(
'don',
'lire')) {
104 throw new RestException(403);
110 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
112 $sql =
"SELECT t.rowid";
113 if ((!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids)) {
114 $sql .=
", sc.fk_soc, sc.fk_user";
116 $sql .=
" FROM ".MAIN_DB_PREFIX.
"don AS t LEFT JOIN ".MAIN_DB_PREFIX.
"don_extrafields AS ef ON (ef.fk_object = t.rowid)";
118 $sql .=
' WHERE t.entity IN ('.getEntity(
'don').
')';
119 if ((!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids)) {
120 $sql .=
" AND t.fk_soc = sc.fk_soc";
122 if ($thirdparty_ids) {
123 $sql .=
" AND t.fk_soc = ".((int) $thirdparty_ids).
" ";
131 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
136 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
138 $sql .= $this->db->order($sortfield, $sortorder);
143 $offset = $limit * $page;
145 $sql .= $this->db->plimit($limit + 1, $offset);
149 $result = $this->db->query($sql);
152 $num = $this->db->num_rows($result);
153 $min = min($num, ($limit <= 0 ? $num : $limit));
156 $obj = $this->db->fetch_object($result);
157 $don_static =
new Don($this->db);
158 if ($don_static->fetch($obj->rowid)) {
166 throw new RestException(503,
'Error when retrieve donation list : '.$this->db->lasterror());
170 if ($pagination_data) {
171 $totalsResult = $this->db->query($sqlTotals);
172 $total = $this->db->fetch_object($totalsResult)->total;
177 $obj_ret[
'data'] = $tmp;
178 $obj_ret[
'pagination'] = [
179 'total' => (int) $total,
181 'page_count' => ceil((
int) $total / $limit),
195 public function post($request_data =
null)
197 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
198 throw new RestException(403,
"Insuffisant rights");
202 $result = $this->
_validate($request_data);
204 foreach ($request_data as $field => $value) {
205 if ($field ===
'caller') {
207 $this->don->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
211 $this->don->$field = $this->
_checkValForAPI($field, $value, $this->don);
221 if ($this->don->create(DolibarrApiAccess::$user) < 0) {
222 throw new RestException(500,
"Error creating donation", array_merge(array($this->don->error), $this->don->errors));
225 return $this->don->id;
235 public function put(
$id, $request_data =
null)
237 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
238 throw new RestException(403);
241 $result = $this->don->fetch(
$id);
243 throw new RestException(404,
'Donation not found');
247 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
249 foreach ($request_data as $field => $value) {
250 if ($field ==
'id') {
253 if ($field ===
'caller') {
255 $this->don->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
259 $this->don->$field = $this->
_checkValForAPI($field, $value, $this->don);
262 if ($this->don->update(DolibarrApiAccess::$user) > 0) {
263 return $this->
get(
$id);
265 throw new RestException(500, $this->don->error);
275 public function delete(
$id)
277 if (!DolibarrApiAccess::$user->hasRight(
'don',
'supprimer')) {
278 throw new RestException(403);
281 $result = $this->don->fetch(
$id);
283 throw new RestException(404,
'Donation not found');
287 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
290 if (!$this->don->delete(DolibarrApiAccess::$user)) {
291 throw new RestException(500,
'Error when delete donation : '.$this->don->error);
297 'message' =>
'Donation deleted'
326 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
327 throw new RestException(403);
330 $result = $this->don->fetch(
$id);
332 throw new RestException(404,
'Donation not found');
336 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
340 $result = $this->don->valid_promesse(
$id, DolibarrApiAccess::$user->
id, $notrigger);
342 throw new RestException(304,
'Error nothing done. May be object is already validated');
345 throw new RestException(500,
'Error when validating Order: '.$this->don->error);
347 $result = $this->don->fetch(
$id);
349 throw new RestException(404,
'Order not found');
353 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
356 $this->don->fetchObjectLinked();
376 unset(
$object->barcode_type_code);
377 unset(
$object->barcode_type_label);
378 unset(
$object->barcode_type_coder);
393 foreach (Donations::$FIELDS as $field) {
394 if (!isset($data[$field])) {
395 throw new RestException(400, $field.
" field missing");
397 $don[$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 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.