dolibarr 19.0.3
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{
34 public static $FIELDS = array(
35 'amount'
36 );
37
41 public $don;
42
46 public function __construct()
47 {
48 global $db, $conf;
49 $this->db = $db;
50 $this->don = new Don($this->db);
51 }
52
63 public function get($id)
64 {
65 if (!DolibarrApiAccess::$user->rights->don->lire) {
66 throw new RestException(401);
67 }
68
69 $result = $this->don->fetch($id);
70 if (!$result) {
71 throw new RestException(404, 'Donation not found');
72 }
73
74 if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
75 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
76 }
77
78 // Add external contacts ids
79 //$this->don->contacts_ids = $this->don->liste_contact(-1,'external',1);
80 //$this->don->fetchObjectLinked();
81 return $this->_cleanObjectDatas($this->don);
82 }
83
84
85
102 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $sqlfilters = '', $properties = '')
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->_filterObjectProperties($this->_cleanObjectDatas($don_static), $properties);
162 }
163 $i++;
164 }
165 } else {
166 throw new RestException(503, 'Error when retrieve donation list : '.$this->db->lasterror());
167 }
168
169 return $obj_ret;
170 }
171
178 public function post($request_data = null)
179 {
180 if (!DolibarrApiAccess::$user->rights->don->creer) {
181 throw new RestException(401, "Insuffisant rights");
182 }
183
184 // Check mandatory fields
185 $result = $this->_validate($request_data);
186
187 foreach ($request_data as $field => $value) {
188 if ($field === 'caller') {
189 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
190 $this->don->context['caller'] = $request_data['caller'];
191 continue;
192 }
193
194 $this->don->$field = $value;
195 }
196 /*if (isset($request_data["lines"])) {
197 $lines = array();
198 foreach ($request_data["lines"] as $line) {
199 array_push($lines, (object) $line);
200 }
201 $this->don->lines = $lines;
202 }*/
203
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));
206 }
207
208 return $this->don->id;
209 }
210
219 public function put($id, $request_data = null)
220 {
221 if (!DolibarrApiAccess::$user->rights->don->creer) {
222 throw new RestException(401);
223 }
224
225 $result = $this->don->fetch($id);
226 if (!$result) {
227 throw new RestException(404, 'Donation not found');
228 }
229
230 if (!DolibarrApi::_checkAccessToResource('donation', $this->don->id)) {
231 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
232 }
233 foreach ($request_data as $field => $value) {
234 if ($field == 'id') {
235 continue;
236 }
237 if ($field === 'caller') {
238 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again whith the caller
239 $this->don->context['caller'] = $request_data['caller'];
240 continue;
241 }
242
243 $this->don->$field = $value;
244 }
245
246 if ($this->don->update(DolibarrApiAccess::$user) > 0) {
247 return $this->get($id);
248 } else {
249 throw new RestException(500, $this->don->error);
250 }
251 }
252
259 public function delete($id)
260 {
261 if (!DolibarrApiAccess::$user->rights->don->supprimer) {
262 throw new RestException(401);
263 }
264
265 $result = $this->don->fetch($id);
266 if (!$result) {
267 throw new RestException(404, 'Donation not found');
268 }
269
270 if (!DolibarrApi::_checkAccessToResource('donation', $this->don->id)) {
271 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
272 }
273
274 if (!$this->don->delete(DolibarrApiAccess::$user)) {
275 throw new RestException(500, 'Error when delete donation : '.$this->don->error);
276 }
277
278 return array(
279 'success' => array(
280 'code' => 200,
281 'message' => 'Donation deleted'
282 )
283 );
284 }
285
308 public function validate($id, $idwarehouse = 0, $notrigger = 0)
309 {
310 if (!DolibarrApiAccess::$user->rights->don->creer) {
311 throw new RestException(401);
312 }
313
314 $result = $this->don->fetch($id);
315 if (!$result) {
316 throw new RestException(404, 'Donation not found');
317 }
318
319 if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
320 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
321 }
322
323 $result = $this->don->valid_promesse($id, DolibarrApiAccess::$user->id, $notrigger);
324 if ($result == 0) {
325 throw new RestException(304, 'Error nothing done. May be object is already validated');
326 }
327 if ($result < 0) {
328 throw new RestException(500, 'Error when validating Order: '.$this->don->error);
329 }
330 $result = $this->don->fetch($id);
331 if (!$result) {
332 throw new RestException(404, 'Order not found');
333 }
334
335 if (!DolibarrApi::_checkAccessToResource('don', $this->don->id)) {
336 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
337 }
338
339 $this->don->fetchObjectLinked();
340
341 return $this->_cleanObjectDatas($this->don);
342 }
343
344 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
351 protected function _cleanObjectDatas($object)
352 {
353 // phpcs:enable
354 $object = parent::_cleanObjectDatas($object);
355
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);
362
363 return $object;
364 }
365
373 private function _validate($data)
374 {
375 $don = array();
376 foreach (Donations::$FIELDS as $field) {
377 if (!isset($data[$field])) {
378 throw new RestException(400, $field." field missing");
379 }
380 $don[$field] = $data[$field];
381 }
382 return $don;
383 }
384}
Class for API REST v1.
Definition api.class.php:31
_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.
Definition don.class.php:40
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.