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);
102 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $thirdparty_ids =
'', $sqlfilters =
'', $properties =
'')
104 if (!DolibarrApiAccess::$user->hasRight(
'don',
'lire')) {
105 throw new RestException(403);
111 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
113 $sql =
"SELECT t.rowid";
114 if ((!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids)) {
115 $sql .=
", sc.fk_soc, sc.fk_user";
117 $sql .=
" FROM ".MAIN_DB_PREFIX.
"don AS t LEFT JOIN ".MAIN_DB_PREFIX.
"don_extrafields AS ef ON (ef.fk_object = t.rowid)";
119 $sql .=
' WHERE t.entity IN ('.getEntity(
'don').
')';
120 if ((!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids)) {
121 $sql .=
" AND t.fk_soc = sc.fk_soc";
123 if ($thirdparty_ids) {
124 $sql .=
" AND t.fk_soc = ".((int) $thirdparty_ids).
" ";
132 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
136 $sql .= $this->db->order($sortfield, $sortorder);
141 $offset = $limit * $page;
143 $sql .= $this->db->plimit($limit + 1, $offset);
147 $result = $this->db->query($sql);
150 $num = $this->db->num_rows($result);
151 $min = min($num, ($limit <= 0 ? $num : $limit));
154 $obj = $this->db->fetch_object($result);
155 $don_static =
new Don($this->db);
156 if ($don_static->fetch($obj->rowid)) {
164 throw new RestException(503,
'Error when retrieve donation list : '.$this->db->lasterror());
176 public function post($request_data =
null)
178 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
179 throw new RestException(403,
"Insuffisant rights");
183 $result = $this->
_validate($request_data);
185 foreach ($request_data as $field => $value) {
186 if ($field ===
'caller') {
188 $this->don->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
192 $this->don->$field = $this->
_checkValForAPI($field, $value, $this->don);
202 if ($this->don->create(DolibarrApiAccess::$user) < 0) {
203 throw new RestException(500,
"Error creating donation", array_merge(array($this->don->error), $this->don->errors));
206 return $this->don->id;
216 public function put($id, $request_data =
null)
218 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
219 throw new RestException(403);
222 $result = $this->don->fetch($id);
224 throw new RestException(404,
'Donation not found');
228 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
230 foreach ($request_data as $field => $value) {
231 if ($field ==
'id') {
234 if ($field ===
'caller') {
236 $this->don->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
240 $this->don->$field = $this->
_checkValForAPI($field, $value, $this->don);
243 if ($this->don->update(DolibarrApiAccess::$user) > 0) {
244 return $this->
get($id);
246 throw new RestException(500, $this->don->error);
256 public function delete($id)
258 if (!DolibarrApiAccess::$user->hasRight(
'don',
'supprimer')) {
259 throw new RestException(403);
262 $result = $this->don->fetch($id);
264 throw new RestException(404,
'Donation not found');
268 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
271 if (!$this->don->delete(DolibarrApiAccess::$user)) {
272 throw new RestException(500,
'Error when delete donation : '.$this->don->error);
278 'message' =>
'Donation deleted'
305 public function validate($id, $idwarehouse = 0, $notrigger = 0)
307 if (!DolibarrApiAccess::$user->hasRight(
'don',
'creer')) {
308 throw new RestException(403);
311 $result = $this->don->fetch($id);
313 throw new RestException(404,
'Donation not found');
317 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
321 $result = $this->don->valid_promesse($id, DolibarrApiAccess::$user->
id, $notrigger);
323 throw new RestException(304,
'Error nothing done. May be object is already validated');
326 throw new RestException(500,
'Error when validating Order: '.$this->don->error);
328 $result = $this->don->fetch($id);
330 throw new RestException(404,
'Order not found');
334 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
337 $this->don->fetchObjectLinked();
357 unset(
$object->barcode_type_code);
358 unset(
$object->barcode_type_label);
359 unset(
$object->barcode_type_coder);
374 foreach (Donations::$FIELDS as $field) {
375 if (!isset($data[$field])) {
376 throw new RestException(400, $field.
" field missing");
378 $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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='', $properties='')
List donations.
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.
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.