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->rights->don->lire) {
66 throw new RestException(401);
69 $result = $this->don->fetch($id);
71 throw new RestException(404,
'Donation not found');
75 throw new RestException(401,
'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 =
'')
106 if (!DolibarrApiAccess::$user->rights->don->lire) {
107 throw new RestException(401);
113 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
115 $sql =
"SELECT t.rowid";
116 if ((!DolibarrApiAccess::$user->rights->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->rights->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);
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());
178 public function post($request_data =
null)
180 if (!DolibarrApiAccess::$user->rights->don->creer) {
181 throw new RestException(401,
"Insuffisant rights");
185 $result = $this->
_validate($request_data);
187 foreach ($request_data as $field => $value) {
188 if ($field ===
'caller') {
190 $this->don->context[
'caller'] = $request_data[
'caller'];
194 $this->don->$field = $value;
204 if ($this->don->create(DolibarrApiAccess::$user) < 0) {
205 throw new RestException(500,
"Error creating donation", array_merge(array($this->don->error), $this->don->errors));
208 return $this->don->id;
219 public function put($id, $request_data =
null)
221 if (!DolibarrApiAccess::$user->rights->don->creer) {
222 throw new RestException(401);
225 $result = $this->don->fetch($id);
227 throw new RestException(404,
'Donation not found');
231 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
233 foreach ($request_data as $field => $value) {
234 if ($field ==
'id') {
237 if ($field ===
'caller') {
239 $this->don->context[
'caller'] = $request_data[
'caller'];
243 $this->don->$field = $value;
246 if ($this->don->update(DolibarrApiAccess::$user) > 0) {
247 return $this->
get($id);
249 throw new RestException(500, $this->don->error);
259 public function delete($id)
261 if (!DolibarrApiAccess::$user->rights->don->supprimer) {
262 throw new RestException(401);
265 $result = $this->don->fetch($id);
267 throw new RestException(404,
'Donation not found');
271 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
274 if (!$this->don->delete(DolibarrApiAccess::$user)) {
275 throw new RestException(500,
'Error when delete donation : '.$this->don->error);
281 'message' =>
'Donation deleted'
308 public function validate($id, $idwarehouse = 0, $notrigger = 0)
310 if (!DolibarrApiAccess::$user->rights->don->creer) {
311 throw new RestException(401);
314 $result = $this->don->fetch($id);
316 throw new RestException(404,
'Donation not found');
320 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
323 $result = $this->don->valid_promesse($id, DolibarrApiAccess::$user->
id, $notrigger);
325 throw new RestException(304,
'Error nothing done. May be object is already validated');
328 throw new RestException(500,
'Error when validating Order: '.$this->don->error);
330 $result = $this->don->fetch($id);
332 throw new RestException(404,
'Order not found');
336 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
339 $this->don->fetchObjectLinked();
354 $object = parent::_cleanObjectDatas($object);
356 unset($object->note);
357 unset($object->address);
358 unset($object->barcode_type);
359 unset($object->barcode_type_code);
360 unset($object->barcode_type_label);
361 unset($object->barcode_type_coder);
376 foreach (Donations::$FIELDS as $field) {
377 if (!isset($data[$field])) {
378 throw new RestException(400, $field.
" field missing");
380 $don[$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.
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
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.