dolibarr 21.0.0-alpha
card.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
3 * Copyright (C) 2013-2024 Alexandre Spangaro <aspangaro@easya.solutions>
4 * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Load Dolibarr environment
28require '../../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
31require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancysystem.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
33
34$error = 0;
35
36// Load translation files required by the page
37$langs->loadLangs(array('accountancy', 'bills', 'compta'));
38
39$action = GETPOST('action', 'aZ09');
40$backtopage = GETPOST('backtopage', 'alpha');
41$id = GETPOSTINT('id');
42$ref = GETPOST('ref', 'alpha');
43$rowid = GETPOSTINT('rowid');
44$cancel = GETPOST('cancel', 'alpha');
45
46$account_number = GETPOST('account_number', 'alphanohtml');
47$label = GETPOST('label', 'alpha');
48
49// Security check
50if ($user->socid > 0) {
52}
53if (!$user->hasRight('accounting', 'chartofaccount')) {
55}
56
57
59
60
61/*
62 * Action
63 */
64
65if (GETPOST('cancel', 'alpha')) {
66 $urltogo = $backtopage ? $backtopage : DOL_URL_ROOT.'/accountancy/admin/account.php';
67 header("Location: ".$urltogo);
68 exit;
69}
70
71if ($action == 'add' && $user->hasRight('accounting', 'chartofaccount')) {
72 if (!$cancel) {
73 if (!$account_number) {
74 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountNumber")), null, 'errors');
75 $action = 'create';
76 } elseif (!$label) {
77 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")), null, 'errors');
78 $action = 'create';
79 } else {
80 $sql = "SELECT pcg_version FROM " . MAIN_DB_PREFIX . "accounting_system WHERE rowid = ".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
81
82 dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
83 $result = $db->query($sql);
84 $obj = $db->fetch_object($result);
85
86 // Clean code
87
88 // To manage zero or not at the end of the accounting account
89 if (!getDolGlobalString('ACCOUNTING_MANAGE_ZERO')) {
90 $account_number = clean_account($account_number);
91 }
92
93 $account_parent = (GETPOSTINT('account_parent') > 0) ? GETPOSTINT('account_parent') : 0;
94
95 $object->fk_pcg_version = $obj->pcg_version;
96 $object->pcg_type = GETPOST('pcg_type', 'alpha');
97 $object->account_number = $account_number;
98 $object->account_parent = $account_parent;
99 $object->account_category = GETPOSTINT('account_category');
100 $object->label = $label;
101 $object->labelshort = GETPOST('labelshort', 'alpha');
102 $object->active = 1;
103
104 $res = $object->create($user);
105 if ($res == -3) {
106 $error = 1;
107 $action = "create";
108 setEventMessages($object->error, $object->errors, 'errors');
109 } elseif ($res == -4) {
110 $error = 2;
111 $action = "create";
112 setEventMessages($object->error, $object->errors, 'errors');
113 } elseif ($res < 0) {
114 $error++;
115 setEventMessages($object->error, $object->errors, 'errors');
116 $action = "create";
117 }
118 if (!$error) {
119 setEventMessages("RecordCreatedSuccessfully", null, 'mesgs');
120 $urltogo = $backtopage ? $backtopage : DOL_URL_ROOT.'/accountancy/admin/account.php';
121 header("Location: " . $urltogo);
122 exit;
123 }
124 }
125 }
126} elseif ($action == 'edit' && $user->hasRight('accounting', 'chartofaccount')) {
127 if (!$cancel) {
128 if (!$account_number) {
129 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountNumber")), null, 'errors');
130 $action = 'update';
131 } elseif (!$label) {
132 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")), null, 'errors');
133 $action = 'update';
134 } else {
135 $result = $object->fetch($id);
136
137 $sql = "SELECT pcg_version FROM ".MAIN_DB_PREFIX."accounting_system WHERE rowid=".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
138
139 dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
140 $result2 = $db->query($sql);
141 $obj = $db->fetch_object($result2);
142
143 // Clean code
144
145 // To manage zero or not at the end of the accounting account
146 if (!getDolGlobalString('ACCOUNTING_MANAGE_ZERO')) {
147 $account_number = clean_account($account_number);
148 }
149
150 $account_parent = (GETPOSTINT('account_parent') > 0) ? GETPOSTINT('account_parent') : 0;
151
152 $object->fk_pcg_version = $obj->pcg_version;
153 $object->pcg_type = GETPOST('pcg_type', 'alpha');
154 $object->account_number = $account_number;
155 $object->account_parent = $account_parent;
156 $object->account_category = GETPOSTINT('account_category');
157 $object->label = $label;
158 $object->labelshort = GETPOST('labelshort', 'alpha');
159
160 $result = $object->update($user);
161
162 if ($result > 0) {
163 $urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"] . "?id=" . $id);
164 header("Location: " . $urltogo);
165 exit();
166 } elseif ($result == -2) {
167 setEventMessages($langs->trans("ErrorAccountNumberAlreadyExists", $object->account_number), null, 'errors');
168 } else {
169 setEventMessages($object->error, null, 'errors');
170 }
171 }
172 } else {
173 $urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"]."?id=".$id);
174 header("Location: ".$urltogo);
175 exit();
176 }
177} elseif ($action == 'delete' && $user->hasRight('accounting', 'chartofaccount')) {
178 $result = $object->fetch($id);
179
180 if (!empty($object->id)) {
181 $result = $object->delete($user);
182
183 if ($result > 0) {
184 header("Location: account.php");
185 exit;
186 }
187 }
188
189 if ($result < 0) {
190 setEventMessages($object->error, $object->errors, 'errors');
191 }
192}
193
194
195/*
196 * View
197 */
198
199$form = new Form($db);
200$formaccounting = new FormAccounting($db);
201
202$accountsystem = new AccountancySystem($db);
203$accountsystem->fetch(getDolGlobalInt('CHARTOFACCOUNTS'));
204
205$title = $langs->trans('AccountAccounting')." - ".$langs->trans('Card');
206
207$help_url = 'EN:Module_Double_Entry_Accounting#Setup|FR:Module_Comptabilit&eacute;_en_Partie_Double#Configuration';
208
209llxHeader('', $title, $help_url);
210
211
212// Create mode
213if ($action == 'create') {
214 print load_fiche_titre($langs->trans('NewAccountingAccount'));
215
216 print '<form name="add" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
217 print '<input type="hidden" name="token" value="'.newToken().'">';
218 print '<input type="hidden" name="action" value="add">';
219
220 print dol_get_fiche_head();
221
222 print '<table class="border centpercent">';
223
224 // Chart of account
225 print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("Chartofaccounts").'</span></td>';
226 print '<td>';
227 print $accountsystem->ref;
228 print '</td></tr>';
229
230 // Account number
231 print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("AccountNumber").'</span></td>';
232 print '<td><input name="account_number" size="30" value="'.$account_number.'"></td></tr>';
233
234 // Label
235 print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td>';
236 print '<td><input name="label" size="70" value="'.$object->label.'"></td></tr>';
237
238 // Label short
239 print '<tr><td>'.$langs->trans("LabelToShow").'</td>';
240 print '<td><input name="labelshort" size="70" value="'.$object->labelshort.'"></td></tr>';
241
242 // Account parent
243 print '<tr><td>'.$langs->trans("Accountparent").'</td>';
244 print '<td>';
245 print $formaccounting->select_account($object->account_parent, 'account_parent', 1, [], 0, 0, 'minwidth200');
246 print '</td></tr>';
247
248 // Chart of accounts type
249 print '<tr><td>';
250 print $form->textwithpicto($langs->trans("Pcgtype"), $langs->transnoentitiesnoconv("PcgtypeDesc"));
251 print '</td>';
252 print '<td>';
253 print '<input type="text" name="pcg_type" list="pcg_type_datalist" value="'.dol_escape_htmltag(GETPOSTISSET('pcg_type') ? GETPOST('pcg_type', 'alpha') : $object->pcg_type).'">';
254 // autosuggest from existing account types if found
255 print '<datalist id="pcg_type_datalist">';
256 $sql = "SELECT DISTINCT pcg_type FROM " . MAIN_DB_PREFIX . "accounting_account";
257 $sql .= " WHERE fk_pcg_version = '" . $db->escape($accountsystem->ref) . "'";
258 $sql .= ' AND entity in ('.getEntity('accounting_account', 0).')'; // Always limit to current entity. No sharing in accountancy.
259 $sql .= ' LIMIT 50000'; // just as a sanity check
260 $resql = $db->query($sql);
261 if ($resql) {
262 while ($obj = $db->fetch_object($resql)) {
263 print '<option value="' . dol_escape_htmltag($obj->pcg_type) . '">';
264 }
265 }
266 print '</datalist>';
267 print '</td></tr>';
268
269 // Category
270 print '<tr><td>';
271 print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc"));
272 print '</td>';
273 print '<td>';
274 print $formaccounting->select_accounting_category($object->account_category, 'account_category', 1, 0, 1);
275 print '</td></tr>';
276
277 print '</table>';
278
279 print dol_get_fiche_end();
280
281 print '<div class="center">';
282 print '<input class="button button-save" type="submit" value="'.$langs->trans("Save").'">';
283 print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
284 print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
285 print '</div>';
286
287 print '</form>';
288} elseif ($id > 0 || $ref) {
289 $result = $object->fetch($id, $ref, 1);
290
291 if ($result > 0) {
293
294 // Edit mode
295 if ($action == 'update') {
296 print dol_get_fiche_head($head, 'card', $langs->trans('AccountAccounting'), 0, 'accounting_account');
297
298 print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
299 print '<input type="hidden" name="token" value="'.newToken().'">';
300 print '<input type="hidden" name="action" value="edit">';
301 print '<input type="hidden" name="id" value="'.$id.'">';
302 print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
303
304 print '<table class="border centpercent">';
305
306 // Account number
307 print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("AccountNumber").'</span></td>';
308 print '<td><input name="account_number" size="30" value="'.$object->account_number.'"</td></tr>';
309
310 // Label
311 print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td>';
312 print '<td><input name="label" size="70" value="'.$object->label.'"</td></tr>';
313
314 // Label short
315 print '<tr><td>'.$langs->trans("LabelToShow").'</td>';
316 print '<td><input name="labelshort" size="70" value="'.$object->labelshort.'"</td></tr>';
317
318 // Account parent
319 print '<tr><td>'.$langs->trans("Accountparent").'</td>';
320 print '<td>';
321 // Note: We accept disabled account as parent account so we can build a hierarchy and use only children
322 print $formaccounting->select_account($object->account_parent, 'account_parent', 1, array(), 0, 0, 'minwidth100 maxwidth300 maxwidthonsmartphone', 1, '');
323 print '</td></tr>';
324
325 // Chart of accounts type
326 print '<tr><td>';
327 print $form->textwithpicto($langs->trans("Pcgtype"), $langs->transnoentitiesnoconv("PcgtypeDesc"));
328 print '</td>';
329 print '<td>';
330 print '<input type="text" name="pcg_type" list="pcg_type_datalist" value="'.dol_escape_htmltag(GETPOSTISSET('pcg_type') ? GETPOST('pcg_type', 'alpha') : $object->pcg_type).'">';
331 // autosuggest from existing account types if found
332 print '<datalist id="pcg_type_datalist">';
333 $sql = 'SELECT DISTINCT pcg_type FROM ' . MAIN_DB_PREFIX . 'accounting_account';
334 $sql .= " WHERE fk_pcg_version = '" . $db->escape($accountsystem->ref) . "'";
335 $sql .= ' AND entity in ('.getEntity('accounting_account', 0).')'; // Always limit to current entity. No sharing in accountancy.
336 $sql .= ' LIMIT 50000'; // just as a sanity check
337 $resql = $db->query($sql);
338 if ($resql) {
339 while ($obj = $db->fetch_object($resql)) {
340 print '<option value="' . dol_escape_htmltag($obj->pcg_type) . '">';
341 }
342 }
343 print '</datalist>';
344 print '</td></tr>';
345
346 // Category
347 print '<tr><td>';
348 print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc"));
349 print '</td>';
350 print '<td>';
351 print $formaccounting->select_accounting_category($object->account_category, 'account_category', 1);
352 print '</td></tr>';
353
354 print '</table>';
355
356 print dol_get_fiche_end();
357
358 print $form->buttonsSaveCancel();
359
360 print '</form>';
361 } else {
362 // View mode
363 $linkback = '<a href="'.DOL_URL_ROOT.'/accountancy/admin/account.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
364
365 print dol_get_fiche_head($head, 'card', $langs->trans('AccountAccounting'), -1, 'accounting_account');
366
367 dol_banner_tab($object, 'ref', $linkback, 1, 'account_number', 'ref');
368
369
370 print '<div class="fichecenter">';
371 print '<div class="underbanner clearboth"></div>';
372
373 print '<table class="border centpercent tableforfield">';
374
375 // Label
376 print '<tr><td class="titlefield">'.$langs->trans("Label").'</td>';
377 print '<td colspan="2">'.$object->label.'</td></tr>';
378
379 // Label to show
380 print '<tr><td class="titlefield">'.$langs->trans("LabelToShow").'</td>';
381 print '<td colspan="2">'.$object->labelshort.'</td></tr>';
382
383 // Account parent
384 $accp = new AccountingAccount($db);
385 if (!empty($object->account_parent)) {
386 $accp->fetch($object->account_parent, '');
387 }
388 print '<tr><td>'.$langs->trans("Accountparent").'</td>';
389 print '<td colspan="2">'.$accp->account_number.' - '.$accp->label.'</td></tr>';
390
391 // Group of accounting account
392 print '<tr><td>';
393 print $form->textwithpicto($langs->trans("Pcgtype"), $langs->transnoentitiesnoconv("PcgtypeDesc"));
394 print '</td>';
395 print '<td colspan="2">'.$object->pcg_type.'</td></tr>';
396
397 // Custom group of accounting account
398 print "<tr><td>";
399 print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc"));
400 print "</td><td colspan='2'>".$object->account_category_label."</td>";
401
402 print '</table>';
403
404 print '</div>';
405
406 print dol_get_fiche_end();
407
408 /*
409 * Actions buttons
410 */
411 print '<div class="tabsAction">';
412
413 if ($user->hasRight('accounting', 'chartofaccount')) {
414 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=update&token='.newToken().'&id='.$object->id.'">'.$langs->trans('Modify').'</a>';
415 } else {
416 print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotAllowed")).'">'.$langs->trans('Modify').'</a>';
417 }
418
419 // Delete
420 $permissiontodelete = $user->hasRight('accounting', 'chartofaccount');
421 print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), 'delete', $permissiontodelete);
422
423 print '</div>';
424 }
425 } else {
426 dol_print_error($db, $object->error, $object->errors);
427 }
428}
429
430// End of page
431llxFooter();
432$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
accounting_prepare_head(AccountingAccount $object)
Prepare array with list of tabs.
clean_account($account)
Return accounting account without zero on the right.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:70
Class to manage accountancy systems.
Class to manage accounting accounts.
Class to manage generation of HTML components for accounting management.
Class to manage generation of HTML components Only common components must be here.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dolGetButtonAction($label, $text='', $actionType='default', $url='', $id='', $userRight=1, $params=array())
Function dolGetButtonAction.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
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...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.