19use Luracast\Restler\RestException;
21require_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->rights->don->lire) {
67 throw new RestException(401);
70 $result = $this->don->fetch($id);
72 throw new RestException(404,
'Donation not found');
76 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 =
'')
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());
168 if (!count($obj_ret)) {
169 throw new RestException(404,
'No donation found');
181 public function post($request_data =
null)
183 if (!DolibarrApiAccess::$user->rights->don->creer) {
184 throw new RestException(401,
"Insuffisant rights");
188 $result = $this->
_validate($request_data);
190 foreach ($request_data as $field => $value) {
191 $this->don->$field = $value;
201 if ($this->don->create(DolibarrApiAccess::$user) < 0) {
202 throw new RestException(500,
"Error creating donation", array_merge(array($this->don->error), $this->don->errors));
205 return $this->don->id;
216 public function put($id, $request_data =
null)
218 if (!DolibarrApiAccess::$user->rights->don->creer) {
219 throw new RestException(401);
222 $result = $this->don->fetch($id);
224 throw new RestException(404,
'Donation not found');
228 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
230 foreach ($request_data as $field => $value) {
231 if ($field ==
'id') {
234 $this->don->$field = $value;
237 if ($this->don->update(DolibarrApiAccess::$user) > 0) {
238 return $this->
get($id);
240 throw new RestException(500, $this->don->error);
250 public function delete($id)
252 if (!DolibarrApiAccess::$user->rights->don->supprimer) {
253 throw new RestException(401);
256 $result = $this->don->fetch($id);
258 throw new RestException(404,
'Donation not found');
262 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
265 if (!$this->don->delete(DolibarrApiAccess::$user)) {
266 throw new RestException(500,
'Error when delete donation : '.$this->don->error);
272 'message' =>
'Donation deleted'
299 public function validate($id, $idwarehouse = 0, $notrigger = 0)
301 if (!DolibarrApiAccess::$user->rights->don->creer) {
302 throw new RestException(401);
305 $result = $this->don->fetch($id);
307 throw new RestException(404,
'Donation not found');
311 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
314 $result = $this->don->valid_promesse($id, DolibarrApiAccess::$user->
id, $notrigger);
316 throw new RestException(304,
'Error nothing done. May be object is already validated');
319 throw new RestException(500,
'Error when validating Order: '.$this->don->error);
321 $result = $this->don->fetch($id);
323 throw new RestException(404,
'Order not found');
327 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
330 $this->don->fetchObjectLinked();
345 $object = parent::_cleanObjectDatas($object);
347 unset($object->note);
348 unset($object->address);
349 unset($object->barcode_type);
350 unset($object->barcode_type_code);
351 unset($object->barcode_type_label);
352 unset($object->barcode_type_coder);
367 foreach (Donations::$FIELDS as $field) {
368 if (!isset($data[$field])) {
369 throw new RestException(400, $field.
" field missing");
371 $don[$field] = $data[$field];
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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $sqlfilters='')
List donations.
_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.
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.