19 use Luracast\Restler\RestException;
21 require_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";
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).
" ";
133 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
135 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
136 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
139 $sql .= $this->
db->order($sortfield, $sortorder);
144 $offset = $limit * $page;
146 $sql .= $this->
db->plimit($limit + 1, $offset);
150 $result = $this->
db->query($sql);
153 $num = $this->
db->num_rows($result);
154 $min = min($num, ($limit <= 0 ? $num : $limit));
157 $obj = $this->
db->fetch_object($result);
158 $don_static =
new Don($this->
db);
159 if ($don_static->fetch($obj->rowid)) {
167 throw new RestException(503,
'Error when retrieve donation list : '.$this->
db->lasterror());
169 if (!count($obj_ret)) {
170 throw new RestException(404,
'No donation found');
182 public function post($request_data =
null)
184 if (!DolibarrApiAccess::$user->rights->don->creer) {
185 throw new RestException(401,
"Insuffisant rights");
189 $result = $this->
_validate($request_data);
191 foreach ($request_data as $field => $value) {
192 $this->don->$field = $value;
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;
217 public function put($id, $request_data =
null)
219 if (!DolibarrApiAccess::$user->rights->don->creer) {
220 throw new RestException(401);
223 $result = $this->don->fetch($id);
225 throw new RestException(404,
'Donation not found');
229 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
231 foreach ($request_data as $field => $value) {
232 if ($field ==
'id') {
235 $this->don->$field = $value;
238 if ($this->don->update(DolibarrApiAccess::$user) > 0) {
239 return $this->
get($id);
241 throw new RestException(500, $this->don->error);
251 public function delete($id)
253 if (!DolibarrApiAccess::$user->rights->don->supprimer) {
254 throw new RestException(401);
257 $result = $this->don->fetch($id);
259 throw new RestException(404,
'Donation not found');
263 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
266 if (!$this->don->delete(DolibarrApiAccess::$user)) {
267 throw new RestException(500,
'Error when delete donation : '.$this->don->error);
273 'message' =>
'Donation deleted'
300 public function validate($id, $idwarehouse = 0, $notrigger = 0)
302 if (!DolibarrApiAccess::$user->rights->don->creer) {
303 throw new RestException(401);
306 $result = $this->don->fetch($id);
308 throw new RestException(404,
'Donation not found');
312 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
315 $result = $this->don->valid_promesse($id, DolibarrApiAccess::$user->
id, $notrigger);
317 throw new RestException(304,
'Error nothing done. May be object is already validated');
320 throw new RestException(500,
'Error when validating Order: '.$this->don->error);
322 $result = $this->don->fetch($id);
324 throw new RestException(404,
'Order not found');
328 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
331 $this->don->fetchObjectLinked();
346 $object = parent::_cleanObjectDatas($object);
348 unset($object->note);
349 unset($object->address);
350 unset($object->barcode_type);
351 unset($object->barcode_type_code);
352 unset($object->barcode_type_label);
353 unset($object->barcode_type_coder);
368 foreach (Donations::$FIELDS as $field) {
369 if (!isset($data[$field])) {
370 throw new RestException(400, $field.
" field missing");
372 $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.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.