dolibarr 18.0.6
api_donations.class.php
1<?php
2/* Copyright (C) 2019 Thibault FOUCART <support@ptibogxiv.net>
3 * Copyright (C) 2019 Laurent Destailleur <eldy@users.sourceforge.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use Luracast\Restler\RestException;
20
21require_once DOL_DOCUMENT_ROOT.'/don/class/don.class.php';
22
30{
31
35 public static $FIELDS = array(
36 'amount'
37 );
38
42 public $don;
43
47 public function __construct()
48 {
49 global $db, $conf;
50 $this->db = $db;
51 $this->don = new Don($this->db);
52 }
53
64 public function get($id)
65 {
66 if (!DolibarrApiAccess::$user->rights->don->lire) {
67 throw new RestException(401);
68 }
69
70 $result = $this->don->fetch($id);
71 if (!$result) {
72 throw new RestException(404, 'Donation not found');
73 }
74
75 if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
76 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
77 }
78
79 // Add external contacts ids
80 //$this->don->contacts_ids = $this->don->liste_contact(-1,'external',1);
81 //$this->don->fetchObjectLinked();
82 return $this->_cleanObjectDatas($this->don);
83 }
84
85
86
102 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '')
103 {
104 global $db, $conf;
105
106 if (!DolibarrApiAccess::$user->rights->don->lire) {
107 throw new RestException(401);
108 }
109
110 $obj_ret = array();
111
112 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
113 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
114
115 $sql = "SELECT t.rowid";
116 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids)) {
117 $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
118 }
119 $sql .= " FROM ".MAIN_DB_PREFIX."don AS t LEFT JOIN ".MAIN_DB_PREFIX."don_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
120
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";
124 }
125 if ($thirdparty_ids) {
126 $sql .= " AND t.fk_soc = ".((int) $thirdparty_ids)." ";
127 }
128
129 // Add sql filters
130 if ($sqlfilters) {
131 $errormessage = '';
132 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
133 if ($errormessage) {
134 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
135 }
136 }
137
138 $sql .= $this->db->order($sortfield, $sortorder);
139 if ($limit) {
140 if ($page < 0) {
141 $page = 0;
142 }
143 $offset = $limit * $page;
144
145 $sql .= $this->db->plimit($limit + 1, $offset);
146 }
147
148 dol_syslog("API Rest request");
149 $result = $this->db->query($sql);
150
151 if ($result) {
152 $num = $this->db->num_rows($result);
153 $min = min($num, ($limit <= 0 ? $num : $limit));
154 $i = 0;
155 while ($i < $min) {
156 $obj = $this->db->fetch_object($result);
157 $don_static = new Don($this->db);
158 if ($don_static->fetch($obj->rowid)) {
159 // Add external contacts ids
160 //$don_static->contacts_ids = $don_static->liste_contact(-1, 'external', 1);
161 $obj_ret[] = $this->_cleanObjectDatas($don_static);
162 }
163 $i++;
164 }
165 } else {
166 throw new RestException(503, 'Error when retrieve donation list : '.$this->db->lasterror());
167 }
168 if (!count($obj_ret)) {
169 throw new RestException(404, 'No donation found');
170 }
171
172 return $obj_ret;
173 }
174
181 public function post($request_data = null)
182 {
183 if (!DolibarrApiAccess::$user->rights->don->creer) {
184 throw new RestException(401, "Insuffisant rights");
185 }
186
187 // Check mandatory fields
188 $result = $this->_validate($request_data);
189
190 foreach ($request_data as $field => $value) {
191 $this->don->$field = $value;
192 }
193 /*if (isset($request_data["lines"])) {
194 $lines = array();
195 foreach ($request_data["lines"] as $line) {
196 array_push($lines, (object) $line);
197 }
198 $this->don->lines = $lines;
199 }*/
200
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));
203 }
204
205 return $this->don->id;
206 }
207
216 public function put($id, $request_data = null)
217 {
218 if (!DolibarrApiAccess::$user->rights->don->creer) {
219 throw new RestException(401);
220 }
221
222 $result = $this->don->fetch($id);
223 if (!$result) {
224 throw new RestException(404, 'Donation not found');
225 }
226
227 if (!DolibarrApi::_checkAccessToResource('donation', $this->don->id)) {
228 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
229 }
230 foreach ($request_data as $field => $value) {
231 if ($field == 'id') {
232 continue;
233 }
234 $this->don->$field = $value;
235 }
236
237 if ($this->don->update(DolibarrApiAccess::$user) > 0) {
238 return $this->get($id);
239 } else {
240 throw new RestException(500, $this->don->error);
241 }
242 }
243
250 public function delete($id)
251 {
252 if (!DolibarrApiAccess::$user->rights->don->supprimer) {
253 throw new RestException(401);
254 }
255
256 $result = $this->don->fetch($id);
257 if (!$result) {
258 throw new RestException(404, 'Donation not found');
259 }
260
261 if (!DolibarrApi::_checkAccessToResource('donation', $this->don->id)) {
262 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
263 }
264
265 if (!$this->don->delete(DolibarrApiAccess::$user)) {
266 throw new RestException(500, 'Error when delete donation : '.$this->don->error);
267 }
268
269 return array(
270 'success' => array(
271 'code' => 200,
272 'message' => 'Donation deleted'
273 )
274 );
275 }
276
299 public function validate($id, $idwarehouse = 0, $notrigger = 0)
300 {
301 if (!DolibarrApiAccess::$user->rights->don->creer) {
302 throw new RestException(401);
303 }
304
305 $result = $this->don->fetch($id);
306 if (!$result) {
307 throw new RestException(404, 'Donation not found');
308 }
309
310 if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
311 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
312 }
313
314 $result = $this->don->valid_promesse($id, DolibarrApiAccess::$user->id, $notrigger);
315 if ($result == 0) {
316 throw new RestException(304, 'Error nothing done. May be object is already validated');
317 }
318 if ($result < 0) {
319 throw new RestException(500, 'Error when validating Order: '.$this->don->error);
320 }
321 $result = $this->don->fetch($id);
322 if (!$result) {
323 throw new RestException(404, 'Order not found');
324 }
325
326 if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
327 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
328 }
329
330 $this->don->fetchObjectLinked();
331
332 return $this->_cleanObjectDatas($this->don);
333 }
334
335 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
342 protected function _cleanObjectDatas($object)
343 {
344 // phpcs:enable
345 $object = parent::_cleanObjectDatas($object);
346
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);
353
354 return $object;
355 }
356
364 private function _validate($data)
365 {
366 $don = array();
367 foreach (Donations::$FIELDS as $field) {
368 if (!isset($data[$field])) {
369 throw new RestException(400, $field." field missing");
370 }
371 $don[$field] = $data[$field];
372 }
373 return $don;
374 }
375}
Class for API REST v1.
Definition api.class.php:31
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.
Definition don.class.php:40
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.