dolibarr 23.0.3
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
86 return $this->_cleanObjectDatas($this->paiement);
87 }
88
89
109 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
110 {
111 $obj_ret = array();
112 $tmpobject = new Paiement($this->db);
113
114 if (!DolibarrApiAccess::$user->hasRight('facture', 'lire')) {
115 throw new RestException(403);
116 }
117
118 $sql = "SELECT t.rowid";
119 $sql .= " FROM ".$this->db->prefix().$tmpobject->table_element." AS t";
120 $sql .= " WHERE 1 = 1";
121 if ($tmpobject->ismultientitymanaged) {
122 $sql .= ' AND t.entity IN ('.getEntity($tmpobject->element).')';
123 }
124 if ($sqlfilters) {
125 $errormessage = '';
126 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
127 if ($errormessage) {
128 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
129 }
130 }
131
132 $sql .= $this->db->order($sortfield, $sortorder);
133 if ($limit) {
134 if ($page < 0) {
135 $page = 0;
136 }
137 $offset = $limit * $page;
138
139 $sql .= $this->db->plimit($limit + 1, $offset);
140 }
141
142 $result = $this->db->query($sql);
143 $i = 0;
144 if ($result) {
145 $num = $this->db->num_rows($result);
146 $min = min($num, ($limit <= 0 ? $num : $limit));
147 while ($i < $min) {
148 $obj = $this->db->fetch_object($result);
149 $tmp_object = new Paiement($this->db);
150 if ($tmp_object->fetch($obj->rowid)) {
151 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($tmp_object), $properties);
152 }
153 $i++;
154 }
155 } else {
156 throw new RestException(503, 'Error when retrieving Paiement list: '.$this->db->lasterror());
157 }
158
159 return $obj_ret;
160 }
161
162
179 public function put($id, $request_data = null)
180 {
181 if (!DolibarrApiAccess::$user->hasRight('facture', 'creer')) {
182 throw new RestException(403);
183 }
184
185 $result = $this->paiement->fetch($id);
186 if (!$result) {
187 throw new RestException(404, 'paiement not found');
188 }
189
190 foreach ($request_data as $field => $value) {
191 if ($field == 'id') {
192 continue;
193 }
194 if ($field === 'caller') {
195 // 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
196 $this->paiement->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
197 continue;
198 }
199
200 $this->paiement->$field = $this->_checkValForAPI($field, $value, $this->paiement);
201 }
202
203 // Clean data
204 // $this->paiement->abc = sanitizeVal($this->paiement->abc, 'alphanohtml');
205
206 if ($this->paiement->update(DolibarrApiAccess::$user, 0) > 0) {
207 return $this->get($id);
208 } else {
209 throw new RestException(500, $this->paiement->error);
210 }
211 }
212
227 public function delete($id)
228 {
229 if (!DolibarrApiAccess::$user->hasRight('facture', 'supprimer')) {
230 throw new RestException(403);
231 }
232
233 $result = $this->paiement->fetch($id);
234 if (!$result) {
235 throw new RestException(404, 'Paiement not found');
236 }
237
238 if ($this->paiement->delete(DolibarrApiAccess::$user) == 0) {
239 throw new RestException(409, 'Error when deleting Paiement : '.$this->paiement->error);
240 } elseif ($this->paiement->delete(DolibarrApiAccess::$user) < 0) {
241 throw new RestException(500, 'Error when deleting Paiement : '.$this->paiement->error);
242 }
243
244 return array(
245 'success' => array(
246 'code' => 200,
247 'message' => 'Paiement deleted'
248 )
249 );
250 }
251
252
253 /* END MODULEBUILDER API PAIEMENT */
254
255
256
257 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
268 protected function _cleanObjectDatas($object)
269 {
270 // phpcs:enable
271 $object = parent::_cleanObjectDatas($object);
272
273 unset($object->rowid);
274 unset($object->canvas);
275
276 return $object;
277 }
278}
$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:33
_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.
Definition api.class.php:98
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.
_cleanObjectDatas($object)
Clean sensitive object data fields @phpstan-template T.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.