dolibarr 21.0.0-beta
accountancyreport.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
24// Class
25require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
26
30class AccountancyReport // extends CommonObject
31{
35 public $db;
36
40 public $error;
41
45 public $errors = array();
46
50 public $element = 'c_accounting_report';
51
55 public $table_element = 'c_accounting_report';
56
61 public $rowid;
62
66 public $id;
67
71 public $code;
72
76 public $label;
77
81 public $fk_country;
82
86 public $active;
87
93 public function __construct($db)
94 {
95 $this->db = $db;
96 }
97
98
106 public function create($user, $notrigger = 0)
107 {
108 global $conf, $langs;
109 $error = 0;
110
111 // Clean parameters
112 if (isset($this->code)) {
113 $this->code = trim($this->code);
114 }
115 if (isset($this->label)) {
116 $this->label = trim($this->label);
117 }
118 if (isset($this->fk_country)) {
119 $this->fk_country = (int) $this->fk_country;
120 }
121 if (isset($this->active)) {
122 $this->active = (int) $this->active;
123 }
124
125 // Check parameters
126 // Put here code to add control on parameters values
127
128 // Insert request
129 $sql = "INSERT INTO " . $this->db->prefix() . $this->table_element . " (";
130 if ($this->rowid > 0) {
131 $sql .= "rowid, ";
132 }
133 $sql .= "code, ";
134 $sql .= "label, ";
135 $sql .= "fk_country, ";
136 $sql .= "active, ";
137 $sql .= "entity";
138 $sql .= ") VALUES (";
139 if ($this->rowid > 0) {
140 $sql .= " " . ((int) $this->rowid) . ",";
141 }
142 $sql .= " " . (!isset($this->code) ? "NULL" : "'" . $this->db->escape($this->code) . "'") . ",";
143 $sql .= " " . (!isset($this->label) ? 'NULL' : "'" . $this->db->escape($this->label) . "'") . ",";
144 $sql .= " " . (!isset($this->fk_country) ? 'NULL' : ((int) $this->fk_country)) . ",";
145 $sql .= " " . (!isset($this->active) ? 'NULL' : ((int) $this->active));
146 $sql .= ", " . ((int) $conf->entity);
147 $sql .= ")";
148
149 $this->db->begin();
150
151 dol_syslog(get_class($this) . "::create", LOG_DEBUG);
152 $resql = $this->db->query($sql);
153 if (!$resql) {
154 $error++;
155 $this->errors[] = "Error " . $this->db->lasterror();
156 }
157
158 // Commit or rollback
159 if ($error) {
160 foreach ($this->errors as $errmsg) {
161 dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
162 $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
163 }
164 $this->db->rollback();
165 return -1 * $error;
166 } else {
167 $this->db->commit();
168 return $this->id;
169 }
170 }
171
172
181 public function fetch($id, $code = '', $label = '')
182 {
183 $sql = "SELECT";
184 $sql .= " t.rowid,";
185 $sql .= " t.code,";
186 $sql .= " t.label,";
187 $sql .= " t.fk_country,";
188 $sql .= " t.active";
189 $sql .= " FROM " . $this->db->prefix() . $this->table_element . " as t";
190 if ($id) {
191 $sql .= " WHERE t.rowid = " . ((int) $id);
192 } else {
193 $sql .= " WHERE t.entity IN (" . getEntity('c_accounting_report') . ")"; // Don't use entity if you use rowid
194 if ($code) {
195 $sql .= " AND t.code = '" . $this->db->escape($code) . "'";
196 } elseif ($label) {
197 $sql .= " AND t.label = '" . $this->db->escape($label) . "'";
198 }
199 }
200
201 dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
202 $resql = $this->db->query($sql);
203 if ($resql) {
204 if ($this->db->num_rows($resql)) {
205 $obj = $this->db->fetch_object($resql);
206
207 $this->id = $obj->rowid;
208 $this->code = $obj->code;
209 $this->label = $obj->label;
210 $this->fk_country = $obj->fk_country;
211 $this->active = $obj->active;
212 }
213 $this->db->free($resql);
214
215 return 1;
216 } else {
217 $this->error = "Error " . $this->db->lasterror();
218 return -1;
219 }
220 }
221
222
230 public function update($user = null, $notrigger = 0)
231 {
232 global $conf, $langs;
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79