19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/don/class/don.class.php';
35 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 =
'')
109 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
111 $sql =
"SELECT t.rowid";
112 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids)) $sql .=
", sc.fk_soc, sc.fk_user";
113 $sql .=
" FROM ".MAIN_DB_PREFIX.
"don as t";
115 $sql .=
' WHERE t.entity IN ('.getEntity(
'don').
')';
116 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids)) $sql .=
" AND t.fk_soc = sc.fk_soc";
117 if ($thirdparty_ids) $sql .=
" AND t.fk_soc = ".$thirdparty_ids.
" ";
124 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
126 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
127 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
130 $sql .= $this->
db->order($sortfield, $sortorder);
136 $offset = $limit * $page;
138 $sql .= $this->
db->plimit($limit + 1, $offset);
142 $result = $this->
db->query($sql);
146 $num = $this->
db->num_rows($result);
147 $min = min($num, ($limit <= 0 ? $num : $limit));
151 $obj = $this->
db->fetch_object($result);
152 $don_static =
new Don($this->
db);
153 if ($don_static->fetch($obj->rowid)) {
161 throw new RestException(503,
'Error when retrieve donation list : '.$this->
db->lasterror());
163 if (!count($obj_ret)) {
164 throw new RestException(404,
'No donation found');
176 public function post($request_data = null)
178 if (!DolibarrApiAccess::$user->rights->don->creer) {
179 throw new RestException(401,
"Insuffisant rights");
182 $result = $this->
_validate($request_data);
184 foreach ($request_data as $field => $value) {
185 $this->don->$field = $value;
195 if ($this->don->create(DolibarrApiAccess::$user) < 0) {
196 throw new RestException(500,
"Error creating order", array_merge(array($this->don->error), $this->don->errors));
199 return $this->don->id;
210 public function put($id, $request_data = null)
212 if (!DolibarrApiAccess::$user->rights->don->creer) {
213 throw new RestException(401);
216 $result = $this->don->fetch($id);
218 throw new RestException(404,
'Donation not found');
222 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
224 foreach ($request_data as $field => $value) {
225 if ($field ==
'id')
continue;
226 $this->don->$field = $value;
229 if ($this->don->update(DolibarrApiAccess::$user) > 0)
231 return $this->
get($id);
233 throw new RestException(500, $this->don->error);
243 public function delete($id)
245 if (!DolibarrApiAccess::$user->rights->don->supprimer) {
246 throw new RestException(401);
248 $result = $this->don->fetch($id);
250 throw new RestException(404,
'Donation not found');
254 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
257 if (!$this->don->delete(DolibarrApiAccess::$user)) {
258 throw new RestException(500,
'Error when delete donation : '.$this->don->error);
264 'message' =>
'Donation deleted'
291 public function validate($id, $idwarehouse = 0, $notrigger = 0)
293 if (!DolibarrApiAccess::$user->rights->don->creer) {
294 throw new RestException(401);
296 $result = $this->don->fetch($id);
298 throw new RestException(404,
'Donation not found');
302 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
305 $result = $this->don->valid(DolibarrApiAccess::$user, $idwarehouse, $notrigger);
307 throw new RestException(304,
'Error nothing done. May be object is already validated');
310 throw new RestException(500,
'Error when validating Order: '.$this->don->error);
312 $result = $this->don->fetch($id);
314 throw new RestException(404,
'Order not found');
318 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
321 $this->don->fetchObjectLinked();
336 $object = parent::_cleanObjectDatas($object);
338 unset($object->note);
339 unset($object->address);
340 unset($object->barcode_type);
341 unset($object->barcode_type_code);
342 unset($object->barcode_type_label);
343 unset($object->barcode_type_coder);
358 foreach (Orders::$FIELDS as $field) {
359 if (!isset($data[$field]))
360 throw new RestException(400, $field.
" field missing");
361 $don[$field] = $data[$field];
put($id, $request_data=null)
Update order general fields (won't touch lines of order)
validate($id, $idwarehouse=0, $notrigger=0)
Validate an donation.
_validate($data)
Validate fields before create or update object.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
_cleanObjectDatas($object)
Clean sensible object datas.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
post($request_data=null)
Create donation object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
__construct()
Constructor.
Class to manage donations.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $thirdparty_ids= '', $sqlfilters= '')
List donations.