dolibarr 25.0.0-alpha
api_paiements.class.php
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20use Luracast\Restler\RestException;
21
22dol_include_once('/compta/paiement/class/paiement.class.php');
23
24
25
39{
43 public $paiement;
44
50 public function __construct()
51 {
52 global $db;
53 $this->db = $db;
54 $this->paiement = new Paiement($this->db);
55 }
56
57 /* BEGIN MODULEBUILDER API PAIEMENT */
58
75 public function get($id)
76 {
77 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
78 throw new RestException(403);
79 }
80
81 $result = $this->paiement->fetch($id);
82 if (!$result) {
83 throw new RestException(404, 'Paiement not found');
84 }
85 if (!$this->_checkAccessToPaymentForExternalUser((int) $this->paiement->id)) {
86 throw new RestException(403, 'Access to this payment not allowed for this user');
87 }
88
89 return $this->_cleanObjectDatas($this->paiement);
90 }
91
92
112 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
113 {
114 $obj_ret = array();
115 $tmpobject = new Paiement($this->db);
116
117 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
118 throw new RestException(403);
119 }
120
121 $sql = "SELECT t.rowid";
122 $sql .= " FROM ".$this->db->prefix().$tmpobject->table_element." AS t";
123 $sql .= " WHERE 1 = 1";
124 if ($tmpobject->ismultientitymanaged) {
125 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
126 }
127 // Case of external user: restrict to payments linked only to invoices of the user's thirdparty
128 $socid = (int) DolibarrApiAccess::$user->socid;
129 if ($socid > 0) {
130 $sql .= " AND EXISTS (SELECT pf.rowid FROM ".$this->db->prefix()."paiement_facture as pf, ".$this->db->prefix()."facture as f";
131 $sql .= " WHERE pf.fk_paiement = t.rowid AND pf.fk_facture = f.rowid AND f.fk_soc = ".((int) $socid).")";
132 $sql .= " AND NOT EXISTS (SELECT pf2.rowid FROM ".$this->db->prefix()."paiement_facture as pf2, ".$this->db->prefix()."facture as f2";
133 $sql .= " WHERE pf2.fk_paiement = t.rowid AND pf2.fk_facture = f2.rowid AND f2.fk_soc <> ".((int) $socid).")";
134 }
135 if ($sqlfilters) {
136 $errormessage = '';
137 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
138 if ($errormessage) {
139 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
140 }
141 }
142
143 $sql .= $this->db->order($sortfield, $sortorder);
144 if ($limit) {
145 if ($page < 0) {
146 $page = 0;
147 }
148 $offset = $limit * $page;
149
150 $sql .= $this->db->plimit($limit + 1, $offset);
151 }
152
153 $result = $this->db->query($sql);
154 $i = 0;
155 if ($result) {
156 $num = $this->db->num_rows($result);
157 $min = min($num, ($limit <= 0 ? $num : $limit));
158 while ($i < $min) {
159 $obj = $this->db->fetch_object($result);
160 $tmp_object = new Paiement($this->db);
161 if ($tmp_object->fetch($obj->rowid)) {
162 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
163 }
164 $i++;
165 }
166 } else {
167 throw new RestException(503, 'Error when retrieving Paiement list: '.$this->db->lasterror());
168 }
169
170 return $obj_ret;
171 }
172
173
190 public function put($id, $request_data = null)
191 {
192 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
193 throw new RestException(403);
194 }
195
196 $result = $this->paiement->fetch($id);
197 if (!$result) {
198 throw new RestException(404, 'paiement not found');
199 }
200 if (!$this->_checkAccessToPaymentForExternalUser((int) $this->paiement->id)) {
201 throw new RestException(403, 'Access to this payment not allowed for this user');
202 }
203
204 foreach ($request_data as $field => $value) {
205 if ($field == 'id') {
206 continue;
207 }
208 if ($field === 'caller') {
209 // 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
210 $this->paiement->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
211 continue;
212 }
213
214 $this->paiement->$field = $this->_checkValForAPI($field, $value, $this->paiement);
215 }
216
217 // Clean data
218 // $this->paiement->abc = sanitizeVal($this->paiement->abc, 'alphanohtml');
219
220 if ($this->paiement->update(DolibarrApiAccess::$user, 0) > 0) {
221 return $this->get($id);
222 } else {
223 throw new RestException(500, $this->paiement->errorsToString());
224 }
225 }
226
241 public function delete($id)
242 {
243 if (!DolibarrApiAccess::$user->hasRight('facture', 'paiement')) {
244 throw new RestException(403);
245 }
246
247 $result = $this->paiement->fetch($id);
248 if (!$result) {
249 throw new RestException(404, 'Paiement not found');
250 }
251 if (!$this->_checkAccessToPaymentForExternalUser((int) $this->paiement->id)) {
252 throw new RestException(403, 'Access to this payment not allowed for this user');
253 }
254
255 if ($this->paiement->delete(DolibarrApiAccess::$user) == 0) {
256 throw new RestException(409, 'Error when deleting Paiement : '.$this->paiement->errorsToString());
257 } elseif ($this->paiement->delete(DolibarrApiAccess::$user) < 0) {
258 throw new RestException(500, 'Error when deleting Paiement : '.$this->paiement->errorsToString());
259 }
260
261 return array(
262 'success' => array(
263 'code' => 200,
264 'message' => 'Paiement deleted'
265 )
266 );
267 }
268
269
270 /* END MODULEBUILDER API PAIEMENT */
271
272
273 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
280 protected function _checkAccessToPaymentForExternalUser($paymentid)
281 {
282 // phpcs:enable
283 $socid = (int) DolibarrApiAccess::$user->socid;
284 if ($socid <= 0) {
285 return true;
286 }
287
288 $sql = "SELECT SUM(CASE WHEN f.fk_soc = ".((int) $socid)." THEN 1 ELSE 0 END) as nbok, SUM(CASE WHEN f.fk_soc = ".((int) $socid)." THEN 0 ELSE 1 END) as nbko";
289 $sql .= " FROM ".$this->db->prefix()."paiement_facture as pf, ".$this->db->prefix()."facture as f";
290 $sql .= " WHERE pf.fk_facture = f.rowid AND pf.fk_paiement = ".((int) $paymentid);
291
292 $resql = $this->db->query($sql);
293 if (!$resql) {
294 throw new RestException(503, 'Error when checking access to payment: '.$this->db->lasterror());
295 }
296 $obj = $this->db->fetch_object($resql);
297 $this->db->free($resql);
298
299 return ($obj && (int) $obj->nbok > 0 && (int) $obj->nbko == 0);
300 }
301
302
303
304 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
315 protected function _cleanObjectDatas($object)
316 {
317 // phpcs:enable
318 $object = parent::_cleanObjectDatas($object);
319
320 unset($object->rowid);
321 unset($object->canvas);
322
323 return $object;
324 }
325}
$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
_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 payments of customer invoices.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List paiements.
put($id, $request_data=null)
Update paiement.
__construct()
Constructor.
_checkAccessToPaymentForExternalUser($paymentid)
Check that an external user can access a payment: all invoices of the payment must belong to the user...
_cleanObjectDatas($object)
Clean sensitive object data fields @phpstan-template T.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
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.