dolibarr 23.0.3
accountancyreport.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
3 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
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
25// Class
26require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
27
31class AccountancyReport // extends CommonObject
32{
36 public $db;
37
41 public $error;
42
46 public $errors = array();
47
51 public $element = 'c_accounting_report';
52
56 public $table_element = 'c_accounting_report';
57
62 public $rowid;
63
67 public $id;
68
72 public $code;
73
77 public $label;
78
82 public $fk_country;
83
87 public $active;
88
94 public function __construct($db)
95 {
96 $this->db = $db;
97 }
98
99
107 public function create($user, $notrigger = 0)
108 {
109 global $conf, $langs;
110 $error = 0;
111
112 // Clean parameters
113 if (isset($this->code)) {
114 $this->code = trim($this->code);
115 }
116 if (isset($this->label)) {
117 $this->label = trim($this->label);
118 }
119 if (isset($this->fk_country)) {
120 $this->fk_country = (int) $this->fk_country;
121 }
122 if (isset($this->active)) {
123 $this->active = (int) $this->active;
124 }
125
126 // Check parameters
127 // Put here code to add control on parameters values
128
129 // Insert request
130 $sql = "INSERT INTO " . $this->db->prefix() . $this->table_element . " (";
131 if ($this->rowid > 0) {
132 $sql .= "rowid, ";
133 }
134 $sql .= "code, ";
135 $sql .= "label, ";
136 $sql .= "fk_country, ";
137 $sql .= "active, ";
138 $sql .= "entity";
139 $sql .= ") VALUES (";
140 if ($this->rowid > 0) {
141 $sql .= " " . ((int) $this->rowid) . ",";
142 }
143 $sql .= " " . (!isset($this->code) ? "NULL" : "'" . $this->db->escape($this->code) . "'") . ",";
144 $sql .= " " . (!isset($this->label) ? 'NULL' : "'" . $this->db->escape($this->label) . "'") . ",";
145 $sql .= " " . (!isset($this->fk_country) ? 'NULL' : ((int) $this->fk_country)) . ",";
146 $sql .= " " . (!isset($this->active) ? 'NULL' : ((int) $this->active));
147 $sql .= ", " . ((int) $conf->entity);
148 $sql .= ")";
149
150 $this->db->begin();
151
152 dol_syslog(get_class($this) . "::create", LOG_DEBUG);
153 $resql = $this->db->query($sql);
154 if (!$resql) {
155 $error++;
156 $this->errors[] = "Error " . $this->db->lasterror();
157 }
158
159 // Commit or rollback
160 if ($error) {
161 foreach ($this->errors as $errmsg) {
162 dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
163 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
164 }
165 $this->db->rollback();
166 return -1 * $error;
167 } else {
168 $this->db->commit();
169 return $this->id;
170 }
171 }
172
173
182 public function fetch($id, $code = '', $label = '')
183 {
184 $sql = "SELECT";
185 $sql .= " t.rowid,";
186 $sql .= " t.code,";
187 $sql .= " t.label,";
188 $sql .= " t.fk_country,";
189 $sql .= " t.active";
190 $sql .= " FROM " . $this->db->prefix() . $this->table_element . " as t";
191 if ($id) {
192 $sql .= " WHERE t.rowid = " . ((int) $id);
193 } else {
194 $sql .= " WHERE t.entity IN (" . getEntity('c_accounting_report') . ")"; // Don't use entity if you use rowid
195 if ($code) {
196 $sql .= " AND t.code = '" . $this->db->escape($code) . "'";
197 } elseif ($label) {
198 $sql .= " AND t.label = '" . $this->db->escape($label) . "'";
199 }
200 }
201
202 dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
203 $resql = $this->db->query($sql);
204 if ($resql) {
205 if ($this->db->num_rows($resql)) {
206 $obj = $this->db->fetch_object($resql);
207
208 $this->id = $obj->rowid;
209 $this->code = $obj->code;
210 $this->label = $obj->label;
211 $this->fk_country = $obj->fk_country;
212 $this->active = $obj->active;
213 }
214 $this->db->free($resql);
215
216 return 1;
217 } else {
218 $this->error = "Error " . $this->db->lasterror();
219 return -1;
220 }
221 }
222
223
231 public function update($user = null, $notrigger = 0)
232 {
233 $error = 0;
234
235 // Clean parameters
236 if (isset($this->code)) {
237 $this->code = trim($this->code);
238 }
239 if (isset($this->label)) {
240 $this->label = trim($this->label);
241 }
242 if (isset($this->fk_country)) {
243 $this->fk_country = (int) $this->fk_country;
244 }
245 if (isset($this->active)) {
246 $this->active = (int) $this->active;
247 }
248
249
250 // Check parameters
251 // Put here code to add control on parameters values
252
253 // Update request
254 $sql = "UPDATE " . $this->db->prefix() . $this->table_element . " SET";
255 $sql .= " code=" . (isset($this->code) ? "'" . $this->db->escape($this->code) . "'" : "null") . ",";
256 $sql .= " label=" . (isset($this->label) ? "'" . $this->db->escape($this->label) . "'" : "null") . ",";
257 $sql .= " fk_country=" . (isset($this->fk_country) ? ((int) $this->fk_country) : "null") . ",";
258 $sql .= " active=" . (isset($this->active) ? ((int) $this->active) : "null");
259 $sql .= " WHERE rowid=" . ((int) $this->id);
260
261 $this->db->begin();
262
263 dol_syslog(get_class($this) . "::update", LOG_DEBUG);
264 $resql = $this->db->query($sql);
265 if (!$resql) {
266 $error++;
267 $this->errors[] = "Error " . $this->db->lasterror();
268 }
269
270 // Commit or rollback
271 if ($error) {
272 foreach ($this->errors as $errmsg) {
273 dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
274 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
275 }
276 $this->db->rollback();
277 return -1 * $error;
278 } else {
279 $this->db->commit();
280 return 1;
281 }
282 }
283
284
292 public function delete($user, $notrigger = 0)
293 {
294 global $conf, $langs;
295 $error = 0;
296
297 $sql = "DELETE FROM " . $this->db->prefix() . $this->table_element;
298 $sql .= " WHERE rowid=" . ((int) $this->id);
299
300 $this->db->begin();
301
302 dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
303 $resql = $this->db->query($sql);
304 if (!$resql) {
305 $error++;
306 $this->errors[] = "Error " . $this->db->lasterror();
307 }
308
309 // Commit or rollback
310 if ($error) {
311 foreach ($this->errors as $errmsg) {
312 dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
313 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
314 }
315 $this->db->rollback();
316 return -1 * $error;
317 } else {
318 $this->db->commit();
319 return 1;
320 }
321 }
322}
Class to manage reports for accounting categories.
update($user=null, $notrigger=0)
Update object into database.
create($user, $notrigger=0)
Create object into database.
fetch($id, $code='', $label='')
Load object in memory from database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
editval_textarea active