21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/don/class/don.class.php';
36 public static $FIELDS = array(
52 $this->don =
new Don($this->db);
65 public function get(
$id)
67 if (!DolibarrApiAccess::$user->hasRight(
'don',
'lire')) {
68 throw new RestException(403);
71 $result = $this->don->fetch(
$id);
73 throw new RestException(404,
'Donation not found');
77 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
101 if (!DolibarrApiAccess::$user->hasRight(
'don',
'lire')) {
102 throw new RestException(403);
105 throw new RestException(400,
'Donation ID is mandatory');
108 $result = $this->don->fetch(
$id);
110 throw new RestException(503,
'Error when retrieving donation: '.$this->don->error);
112 if (empty($this->don->id)) {
113 throw new RestException(404,
'Donation not found');
117 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
120 $sql =
"SELECT pd.rowid, pd.datep, pd.amount, pd.fk_bank, pd.num_payment";
121 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_donation AS pd";
122 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"don AS d ON d.rowid = pd.fk_donation";
123 $sql .=
" WHERE pd.fk_donation = ".((int)
$id);
124 $sql .=
" AND d.entity IN (".getEntity(
'donation').
")";
125 $sql .=
" ORDER BY pd.datep ASC, pd.rowid ASC";
127 $resql = $this->db->query($sql);
129 throw new RestException(503,
'Error when retrieving donation payments: '.$this->db->lasterror());
133 while ($obj = $this->db->fetch_object($resql)) {
135 'rowid' => (
int) $obj->rowid,
136 'datep' => $this->db->jdate($obj->datep),
137 'amount' => (
float) $obj->amount,
138 'fk_bank' => (
int) $obj->fk_bank,
139 'num_payment' => $obj->num_payment
142 $this->db->free($resql);
166 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'', $pagination_data =
false)
168 if (!DolibarrApiAccess::$user->hasRight(
'don',
'lire')) {
169 throw new RestException(403);
175 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
177 $sql =
"SELECT t.rowid";
178 if ((!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids)) {
179 $sql .=
", sc.fk_soc, sc.fk_user";
181 $sql .=
" FROM ".MAIN_DB_PREFIX.
"don AS t LEFT JOIN ".MAIN_DB_PREFIX.
"don_extrafields AS ef ON (ef.fk_object = t.rowid)";
183 $sql .=
' WHERE t.entity IN ('.getEntity(
'don').
')';
184 if ((!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids)) {
185 $sql .=
" AND t.fk_soc = sc.fk_soc";
187 if ($thirdparty_ids) {
188 $sql .=
" AND t.fk_soc = ".((int) $thirdparty_ids).
" ";
196 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
201 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
203 $sql .= $this->db->order($sortfield, $sortorder);
208 $offset = $limit * $page;
210 $sql .= $this->db->plimit($limit + 1, $offset);
214 $result = $this->db->query($sql);
217 $num = $this->db->num_rows($result);
218 $min = min($num, ($limit <= 0 ? $num : $limit));
221 $obj = $this->db->fetch_object($result);
222 $don_static =
new Don($this->db);
223 if ($don_static->fetch($obj->rowid)) {
231 throw new RestException(503,
'Error when retrieve donation list : '.$this->db->lasterror());
235 if ($pagination_data) {
236 $totalsResult = $this->db->query($sqlTotals);
237 $total = $this->db->fetch_object($totalsResult)->total;
242 $obj_ret[
'data'] = $tmp;
243 $obj_ret[
'pagination'] = [
244 'total' => (int) $total,
246 'page_count' => ceil((
int) $total / $limit),
262 public function post($request_data =
null)
264 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
265 throw new RestException(403,
"Insufficiant rights");
269 $result = $this->
_validate($request_data);
271 foreach ($request_data as $field => $value) {
272 if ($field ===
'caller') {
274 $this->don->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
278 $this->don->$field = $this->
_checkValForAPI($field, $value, $this->don);
288 if ($this->don->create(DolibarrApiAccess::$user) < 0) {
289 throw new RestException(500,
"Error creating donation", array_merge(array($this->don->error), $this->don->errors));
292 return $this->don->id;
304 public function put(
$id, $request_data =
null)
306 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
307 throw new RestException(403);
310 $result = $this->don->fetch(
$id);
312 throw new RestException(404,
'Donation not found');
316 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
318 foreach ($request_data as $field => $value) {
319 if ($field ==
'id') {
322 if ($field ===
'caller') {
324 $this->don->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
328 if ($field ==
'array_options' && is_array($value)) {
329 foreach ($value as $index => $val) {
335 $this->don->$field = $this->
_checkValForAPI($field, $value, $this->don);
338 if ($this->don->update(DolibarrApiAccess::$user) > 0) {
339 return $this->
get(
$id);
341 throw new RestException(500, $this->don->errorsToString());
353 public function delete(
$id)
355 if (!DolibarrApiAccess::$user->hasRight(
'don',
'supprimer')) {
356 throw new RestException(403);
359 $result = $this->don->fetch(
$id);
361 throw new RestException(404,
'Donation not found');
365 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
368 if (!$this->don->delete(DolibarrApiAccess::$user)) {
369 throw new RestException(500,
'Error when delete donation : '.$this->don->errorsToString());
375 'message' =>
'Donation deleted'
404 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
405 throw new RestException(403);
408 $result = $this->don->fetch(
$id);
410 throw new RestException(404,
'Donation not found');
414 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
418 $result = $this->don->valid_promesse(
$id, DolibarrApiAccess::$user->
id, $notrigger);
420 throw new RestException(304,
'Error nothing done. May be object is already validated');
423 throw new RestException(500,
'Error when validating Order: '.$this->don->errorsToString());
425 $result = $this->don->fetch(
$id);
427 throw new RestException(404,
'Order not found');
435 $this->don->fetchObjectLinked();
458 unset(
$object->barcode_type_code);
459 unset(
$object->barcode_type_label);
460 unset(
$object->barcode_type_coder);
474 if ($data ===
null) {
478 foreach (Donations::$FIELDS as $field) {
479 if (!isset($data[$field])) {
480 throw new RestException(400, $field.
" field missing");
482 $don[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
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 @phpstan-template T.
put($id, $request_data=null)
Update order general fields (won't touch lines of order)
getPayments($id)
Get list of payments of a given donation.
__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, $forbiddenfields=array())
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.