dolibarr 24.0.0-beta
bookkeepingtemplate.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 AWeerWolf
3 * Copyright (C) 2026 Alexandre Spangaro <alexandre@inovea-conseil.com>
4 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.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
26// Put here all includes required by your class file
27require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
28require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeepingtemplateline.class.php';
29
34{
38 public $module = 'accountancy';
39
43 public $element = 'accounting_transaction_template';
44
48 public $table_element = 'accounting_transaction_template';
49
53 public $ismultientitymanaged = 1;
54
58 public $isextrafieldmanaged = 1;
59
63 public $picto = 'fa-list';
64
90 // BEGIN MODULEBUILDER PROPERTIES
94 public $fields = array(
95 "rowid" => array("type" => "integer", "label" => "TechnicalID", "enabled" => 1, 'position' => 1, 'notnull' => 1, "visible" => 0, "noteditable" => 1, "index" => 1, "css" => "left", "comment" => "Id"),
96 "entity" => array("type" => "integer", "label" => "Entity", "enabled" => 1, 'position' => 5, 'notnull' => 1, "visible" => 0, "index" => 1, "default" => '1'),
97 "code" => array("type" => "varchar(128)", "label" => "Code", "enabled" => 1, 'position' => 10, 'notnull' => 1, "visible" => 1, "index" => 1, "searchall" => 1, "css" => "minwidth300", "help" => "UniqueCodeForTemplate"),
98 "label" => array("type" => "varchar(255)", "label" => "Label", "enabled" => 1, 'position' => 20, 'notnull' => 0, "visible" => 1, "css" => "minwidth300"),
99 "date_creation" => array("type" => "datetime", "label" => "DateCreation", "enabled" => 1, 'position' => 500, 'notnull' => 1, "visible" => -2),
100 "tms" => array("type" => "timestamp", "label" => "DateModification", "enabled" => 1, 'position' => 501, 'notnull' => 0, "visible" => -2),
101 "fk_user_creat" => array("type" => "integer:User:user/class/user.class.php", "label" => "UserAuthor", "picto" => "user", "enabled" => 1, 'position' => 510, 'notnull' => 1, "visible" => "-2", "csslist" => "tdoverflowmax150"),
102 "fk_user_modif" => array("type" => "integer:User:user/class/user.class.php", "label" => "UserModif", "picto" => "user", "enabled" => 1, 'position' => 511, 'notnull' => -1, "visible" => "-2", "csslist" => "tdoverflowmax150"),
103 "import_key" => array("type" => "varchar(14)", "label" => "ImportId", "enabled" => 1, 'position' => 1000, 'notnull' => -1, "visible" => -2),
104 );
105 // END MODULEBUILDER PROPERTIES
106
110 public $rowid;
111
115 public $entity;
116
120 public $code;
121
125 public $label;
126
130 public $date_creation;
131
135 public $tms;
136
140 public $fk_user_creat;
141
145 public $fk_user_modif;
146
150 public $import_key;
151
155 public $table_element_line = 'accounting_transaction_template_det';
156
160 public $lines = array();
161
162
168 public function __construct(DoliDB $db)
169 {
170 global $conf, $langs;
171
172 $this->db = $db;
173
174 if (!getDolGlobalInt('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid'], $this->fields['code']) && is_array($this->fields['rowid'])) {
175 $this->fields['rowid']['visible'] = 0;
176 }
177 if (!isModEnabled('multicompany') && isset($this->fields['entity'])) {
178 $this->fields['entity']['enabled'] = 0;
179 }
180
181 // Unset fields that are disabled
182 foreach ($this->fields as $key => $val) {
183 if (isset($val['enabled']) && empty($val['enabled'])) {
184 unset($this->fields[$key]);
185 }
186 }
187
188 // Translate some data of arrayofkeyval
189 if (is_object($langs)) {
190 foreach ($this->fields as $key => $val) {
191 if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
192 foreach ($val['arrayofkeyval'] as $key2 => $val2) {
193 $this->fields[$key]['arrayofkeyval'][$key2] = (string) $langs->trans($val2);
194 }
195 }
196 }
197 }
198 }
199
207 public function create(User $user, $notrigger = 0)
208 {
209 $resultcreate = $this->createCommon($user, $notrigger);
210 return $resultcreate;
211 }
212
220 public function createFromClone(User $user, $fromid)
221 {
222 global $langs, $extrafields;
223 $error = 0;
224
225 dol_syslog(__METHOD__, LOG_DEBUG);
226
227 $object = new self($this->db);
228
229 $this->db->begin();
230
231 // Load source object
232 $result = $object->fetchCommon($fromid);
233 if ($result > 0 && !empty($object->table_element_line)) {
234 $object->fetchLines();
235 }
236
237 // Reset some properties
238 unset($object->id);
239 unset($object->fk_user_creat);
240
241 // Clear fields
242 $object->code = "COPY_" . $object->code;
243 $object->label = $langs->trans("CopyOf") . " " . $object->label;
244 $object->date_creation = dol_now();
245
246 // Create clone
247 $object->context['createfromclone'] = 'createfromclone';
248 $result = $object->createCommon($user);
249
250 if ($result < 0) {
251 $error++;
253 }
254
255 if (!$error) {
256 // Copy Attached Lines
257 $this->getLinesArray();
258 foreach ($this->lines as $linetoclone) {
259 // Clear some info
260 unset($linetoclone->id);
261 unset($linetoclone->fk_user_creat);
262 $linetoclone->fk_transaction_template = $object->id;
263
264 // Save in DB
265 $linetoclone->createCommon($user);
266 }
267 }
268
269 unset($object->context['createfromclone']);
270
271 // End
272 if (!$error) {
273 $this->db->commit();
274 return $object;
275 } else {
276 $this->db->rollback();
277 return -1;
278 }
279 }
280
290 public function fetch($id, $code = null, $noextrafields = 0, $nolines = 0)
291 {
292 $result = $this->fetchCommon($id, $code, '', $noextrafields);
293 if ($result > 0 && !empty($this->table_element_line) && empty($nolines)) {
294 $this->fetchLines($noextrafields);
295 }
296 return $result;
297 }
298
305 public function fetchLines($noextrafields = 0)
306 {
307 $this->lines = array();
308
309 $objectline = new BookkeepingTemplateLine($this->db);
310 $result = $objectline->fetchAll('ASC', 'rowid', 0, 0, array('customsql' => 'fk_transaction_template = ' . ((int) $this->id)));
311
312 if (is_numeric($result)) {
313 $this->setErrorsFromObject($objectline);
314 return $result;
315 } else {
316 $this->lines = $result;
317 return $this->lines;
318 }
319 }
320
328 public function update(User $user, $notrigger = 0)
329 {
330 return $this->updateCommon($user, $notrigger);
331 }
332
340 public function delete(User $user, $notrigger = 0)
341 {
342 global $conf, $langs;
343
344 $error = 0;
345
346 $this->db->begin();
347
348 // Delete the Lines first
349 $obj_line = new BookkeepingTemplateLine($this->db);
350
351 $sql = "DELETE FROM ".$this->db->prefix().$obj_line->table_element;
352 $sql .= " WHERE fk_transaction_template = ".((int) $this->id);
353
354 dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
355
356 $resql = $this->db->query($sql);
357 if (!$resql) {
358 $this->errors[] = $this->db->lasterror();
359 $error++;
360 dol_syslog(get_class($this)."::delete error deleting lines: ".$this->db->lasterror(), LOG_ERR);
361 }
362
363 if (empty($error)) {
364 // Call trigger
365 if (!$notrigger) {
366 $result = $this->call_trigger(strtoupper(get_class($this)).'_DELETE', $user);
367 if ($result < 0) {
368 $error++;
369 }
370 }
371 }
372
373 if (empty($error)) {
374 // Delete main object
375 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
376 $sql .= " WHERE rowid = ".((int) $this->id);
377
378 dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
379
380 $resql = $this->db->query($sql);
381 if (!$resql) {
382 $this->errors[] = $this->db->lasterror();
383 $this->error = $this->db->lasterror();
384 $error++;
385 dol_syslog(get_class($this)."::delete error deleting main object: ".$this->db->lasterror(), LOG_ERR);
386 }
387 }
388
389 // Commit or rollback
390 if (empty($error)) {
391 $this->db->commit();
392 return 1;
393 } else {
394 $this->error = implode(', ', $this->errors);
395 $this->db->rollback();
396 return -1;
397 }
398 }
399
408 public function deleteLine(User $user, $idline, $notrigger = 0)
409 {
410 $tmpline = new BookkeepingTemplateLine($this->db);
411 if ($tmpline->fetch($idline) > 0) {
412 return $tmpline->delete($user);
413 }
414
415 return -1;
416 }
417
423 public function getLinesArray()
424 {
425 $this->lines = array();
426
427 $objectline = new BookkeepingTemplateLine($this->db);
428 $result = $objectline->fetchAll('ASC', 'rowid', 0, 0, array('customsql' => 'fk_transaction_template = ' . ((int) $this->id)));
429
430 if (is_numeric($result)) {
431 $this->setErrorsFromObject($objectline);
432 return $result;
433 } else {
434 $this->lines = $result;
435 return $this->lines;
436 }
437 }
438
444 public function getNextNumRef()
445 {
446 global $langs, $conf;
447 $langs->load("accountancy");
448
449 // Standard numbering - Generate a code based on timestamp
450 $code = 'TPL' . dol_print_date(dol_now(), '%Y%m%d%H%M%S');
451
452 return $code;
453 }
454
465 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
466 {
467 global $conf, $langs, $hookmanager;
468
469 if (!empty($conf->dol_no_mouse_hover)) {
470 $notooltip = 1;
471 }
472
473 $result = '';
474
475 $label = img_picto('', $this->picto) . ' <u>' . $langs->trans("BookkeepingTemplate") . '</u>';
476 if (isset($this->code)) {
477 $label .= '<br><b>' . $langs->trans('Code') . ':</b> ' . $this->code;
478 }
479 if (isset($this->label)) {
480 $label .= '<br><b>' . $langs->trans('Label') . ':</b> ' . $this->label;
481 }
482
483 $url = DOL_URL_ROOT . '/accountancy/admin/template/card.php?id=' . $this->id;
484
485 if ($option != 'nolink') {
486 // Add param to save lastsearch_values or not
487 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
488 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
489 $add_save_lastsearch_values = 1;
490 }
491 if ($add_save_lastsearch_values) {
492 $url .= '&save_lastsearch_values=1';
493 }
494 }
495
496 $linkclose = '';
497 if (empty($notooltip)) {
498 if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER')) {
499 $label = $langs->trans("ShowBookkeepingTemplate");
500 $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
501 }
502 $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
503 $linkclose .= ' class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"';
504 } else {
505 $linkclose = ($morecss ? ' class="' . $morecss . '"' : '');
506 }
507
508 if ($option == 'nolink') {
509 $linkstart = '<span';
510 } else {
511 $linkstart = '<a href="' . $url . '"';
512 }
513 $linkstart .= $linkclose . '>';
514 if ($option == 'nolink') {
515 $linkend = '</span>';
516 } else {
517 $linkend = '</a>';
518 }
519
520 $result .= $linkstart;
521
522 if ($withpicto) {
523 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="' . (($withpicto != 2) ? 'paddingright ' : '') . 'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
524 }
525
526 if ($withpicto != 2) {
527 $result .= $this->code;
528 }
529
530 $result .= $linkend;
531
532 return $result;
533 }
534
541 public function getLibStatut($mode = 0)
542 {
543 return $this->LibStatut(1, $mode);
544 }
545
546 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
547
555 public function LibStatut($status, $mode = 0)
556 {
557 // phpcs:enable
558 global $langs;
559
560 return '';
561 }
562}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for BookkeepingTemplate.
getLinesArray()
Create an array of lines.
getNextNumRef()
Returns the reference to the following non used object depending on the active numbering module.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionally the picto)
fetchLines($noextrafields=0)
Load object lines in memory from the database.
LibStatut($status, $mode=0)
Return label of a given status.
__construct(DoliDB $db)
Constructor.
getLibStatut($mode=0)
Return label of status.
create(User $user, $notrigger=0)
Create object into database.
createFromClone(User $user, $fromid)
Clone an object into another one.
deleteLine(User $user, $idline, $notrigger=0)
Delete a line of object in database.
update(User $user, $notrigger=0)
Update object into database.
fetch($id, $code=null, $noextrafields=0, $nolines=0)
Load object in memory from the database.
Class for BookkeepingTemplateLine.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
setErrorsFromObject($object)
setErrorsFromObject
createCommon(User $user, $notrigger=0)
Create object in the database.
updateCommon(User $user, $notrigger=0)
Update object into database.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php