dolibarr 25.0.0-alpha
api_variouspayments.class.php
1<?php
2/*
3 * Copyright (C) 2026 Xabier Trigo Losada <xabi@galicloud.com>
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.'/compta/bank/class/paymentvarious.class.php';
22
31{
35 public static $FIELDS = array(
36 'datep',
37 'amount',
38 'label',
39 );
40
44 public function __construct()
45 {
46 global $db;
47 $this->db = $db;
48 }
49
61 public function get($id)
62 {
63 if (!DolibarrApiAccess::$user->hasRight('banque', 'lire')) {
64 throw new RestException(403);
65 }
66
67 $payment = new PaymentVarious($this->db);
68 $result = $payment->fetch($id);
69 // PaymentVarious::fetch() returns 1 even when the record does not exist,
70 // so also check that the id was actually loaded.
71 if ($result <= 0 || empty($payment->id)) {
72 throw new RestException(404, 'Various payment not found');
73 }
74
75 return $this->_cleanObjectDatas($payment);
76 }
77
98 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
99 {
100 if (!DolibarrApiAccess::$user->hasRight('banque', 'lire')) {
101 throw new RestException(403);
102 }
103
104 $obj_ret = array();
105
106 $sql = "SELECT t.rowid";
107 $sql .= " FROM ".MAIN_DB_PREFIX."payment_various AS t";
108 $sql .= " WHERE t.entity IN (".getEntity('payment_various').")";
109
110 // Add sql filters
111 if ($sqlfilters) {
112 $errormessage = '';
113 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
114 if ($errormessage) {
115 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
116 }
117 }
118
119 // This query will count total records matching the filters (before pagination)
120 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
121
122 $sql .= $this->db->order($sortfield, $sortorder);
123 if ($limit) {
124 if ($page < 0) {
125 $page = 0;
126 }
127 $offset = $limit * $page;
128
129 $sql .= $this->db->plimit($limit + 1, $offset);
130 }
131
132 dol_syslog("API Rest request");
133 $result = $this->db->query($sql);
134
135 if (!$result) {
136 throw new RestException(503, 'Error when retrieving list of various payments: '.$this->db->lasterror());
137 }
138
139 $num = $this->db->num_rows($result);
140 $min = min($num, ($limit <= 0 ? $num : $limit));
141 for ($i = 0; $i < $min; $i++) {
142 $obj = $this->db->fetch_object($result);
143 $payment = new PaymentVarious($this->db);
144 if ($payment->fetch($obj->rowid) > 0) {
145 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($payment), $properties);
146 }
147 }
148
149 // If $pagination_data is true, the response will contain the element data with all values
150 // and the element pagination with pagination data (total, page, page_count, limit)
151 if ($pagination_data) {
152 $totalsResult = $this->db->query($sqlTotals);
153 $total = $this->db->fetch_object($totalsResult)->total;
154
155 $tmp = $obj_ret;
156 $obj_ret = array();
157
158 $obj_ret['data'] = $tmp;
159 $obj_ret['pagination'] = array(
160 'total' => (int) $total,
161 'page' => $page, // count starts from 0
162 'page_count' => ($limit > 0 ? (int) ceil((int) $total / $limit) : 1),
163 'limit' => $limit
164 );
165 }
166
167 return $obj_ret;
168 }
169
182 public function post($request_data = null)
183 {
184 if (!DolibarrApiAccess::$user->hasRight('banque', 'modifier')) {
185 throw new RestException(403);
186 }
187
188 // Check mandatory fields
189 $this->_validate($request_data);
190
191 $payment = new PaymentVarious($this->db);
192 foreach ($request_data as $field => $value) {
193 if ($field === 'caller') {
194 // 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 with the caller
195 $payment->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
196 continue;
197 }
198
199 $payment->$field = $this->_checkValForAPI($field, $value, $payment);
200 }
201
202 if ($payment->create(DolibarrApiAccess::$user) < 0) {
203 throw new RestException(500, 'Error when creating various payment: '.$payment->errorsToString());
204 }
205
206 return $payment->id;
207 }
208
222 public function put($id, $request_data = null)
223 {
224 if (!DolibarrApiAccess::$user->hasRight('banque', 'modifier')) {
225 throw new RestException(403);
226 }
227
228 $payment = new PaymentVarious($this->db);
229 $result = $payment->fetch($id);
230 // PaymentVarious::fetch() returns 1 even when the record does not exist,
231 // so also check that the id was actually loaded.
232 if ($result <= 0 || empty($payment->id)) {
233 throw new RestException(404, 'Various payment not found');
234 }
235
236 foreach ($request_data as $field => $value) {
237 if ($field == 'id') {
238 continue;
239 }
240 if ($field === 'caller') {
241 // 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 with the caller
242 $payment->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
243 continue;
244 }
245 if ($field == 'array_options' && is_array($value)) {
246 foreach ($value as $index => $val) {
247 $payment->array_options[$index] = $this->_checkValExtrafieldsForAPI($index, $val, $payment);
248 }
249 continue;
250 }
251
252 $payment->$field = $this->_checkValForAPI($field, $value, $payment);
253 }
254
255 if ($payment->update(DolibarrApiAccess::$user) > 0) {
256 return $this->get($id);
257 } else {
258 throw new RestException(500, 'Error when updating various payment: '.$payment->errorsToString());
259 }
260 }
261
274 public function delete($id)
275 {
276 if (!DolibarrApiAccess::$user->hasRight('banque', 'modifier')) {
277 throw new RestException(403);
278 }
279
280 $payment = new PaymentVarious($this->db);
281 $result = $payment->fetch($id);
282 // PaymentVarious::fetch() returns 1 even when the record does not exist,
283 // so also check that the id was actually loaded.
284 if ($result <= 0 || empty($payment->id)) {
285 throw new RestException(404, 'Various payment not found');
286 }
287
288 if ($payment->delete(DolibarrApiAccess::$user) < 0) {
289 throw new RestException(500, 'Error when deleting various payment: '.$payment->errorsToString());
290 }
291
292 return array(
293 'success' => array(
294 'code' => 200,
295 'message' => 'Various payment deleted'
296 )
297 );
298 }
299
308 private function _validate($data)
309 {
310 if ($data === null) {
311 $data = array();
312 }
313 $payment = array();
314 foreach (VariousPayments::$FIELDS as $field) {
315 if (!isset($data[$field])) {
316 throw new RestException(400, "$field field missing");
317 }
318 $payment[$field] = $data[$field];
319 }
320 return $payment;
321 }
322
323 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
330 protected function _cleanObjectDatas($object)
331 {
332 // phpcs:enable
333 $object = parent::_cleanObjectDatas($object);
334
335 unset($object->statut); // Not used for various payments
336 unset($object->labelStatus);
337 unset($object->labelStatusShort);
338 unset($object->accountid); // Deprecated, use fk_account
339
340 return $object;
341 }
342}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:35
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Class to manage various payments.
_validate($data)
Validate fields before creating an object.
post($request_data=null)
Create a various payment (manual journal entry).
_cleanObjectDatas($object)
Clean sensitive object data.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List various payments (manual journal entries).
put($id, $request_data=null)
Update a various payment (manual journal entry).
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.