dolibarr 21.0.0-beta
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
42$error = 0;
43
44// Load translation files required by the page
45$langs->loadLangs(array('accountancy', 'bills', 'compta'));
46
47$action = GETPOST('action', 'aZ09');
48$backtopage = GETPOST('backtopage', 'alpha');
49$id = GETPOSTINT('id');
50$ref = GETPOST('ref', 'alpha');
51$rowid = GETPOSTINT('rowid');
52$cancel = GETPOST('cancel', 'alpha');
53
54$account_number = GETPOST('account_number', 'alphanohtml');
55$label = GETPOST('label', 'alpha');
56
57// Security check
58if ($user->socid > 0) {
60}
61if (!$user->hasRight('accounting', 'chartofaccount')) {
63}
64
65
67
68
69/*
70 * Action
71 */
72
73if (GETPOST('cancel', 'alpha')) {
74 $urltogo = $backtopage ? $backtopage : DOL_URL_ROOT.'/accountancy/admin/account.php';
75 header("Location: ".$urltogo);
76 exit;
77}
78
79if ($action == 'add' && $user->hasRight('accounting', 'chartofaccount')) {
80 if (!$cancel) {
81 if (!$account_number) {
82 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountNumber")), null, 'errors');
83 $action = 'create';
84 } elseif (!$label) {
85 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")), null, 'errors');
86 $action = 'create';
87 } else {
88 $sql = "SELECT pcg_version FROM " . MAIN_DB_PREFIX . "accounting_system WHERE rowid = ".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
89
90 dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
91 $result = $db->query($sql);
92 $obj = $db->fetch_object($result);
93
94 // Clean code
95
96 // To manage zero or not at the end of the accounting account
97 if (!getDolGlobalString('ACCOUNTING_MANAGE_ZERO')) {
98 $account_number = clean_account($account_number);
99 }
100
101 $account_parent = (GETPOSTINT('account_parent') > 0) ? GETPOSTINT('account_parent') : 0;
102
103 $object->fk_pcg_version = $obj->pcg_version;
104 $object->pcg_type = GETPOST('pcg_type', 'alpha');
105 $object->account_number = $account_number;
106 $object->account_parent = $account_parent;
107 $object->account_category = GETPOSTINT('account_category');
108 $object->label = $label;
109 $object->labelshort = GETPOST('labelshort', 'alpha');
110 $object->active = 1;
111
112 $res = $object->create($user);
113 if ($res == -3) {
114 $error = 1;
115 $action = "create";
116 setEventMessages($object->error, $object->errors, 'errors');
117 } elseif ($res == -4) {
118 $error = 2;
119 $action = "create";
120 setEventMessages($object->error, $object->errors, 'errors');
121 } elseif ($res < 0) {
122 $error++;
123 setEventMessages($object->error, $object->errors, 'errors');
124 $action = "create";
125 }
126 if (!$error) {
127 setEventMessages("RecordCreatedSuccessfully", null, 'mesgs');
128 $urltogo = $backtopage ? $backtopage : DOL_URL_ROOT.'/accountancy/admin/account.php';
129 header("Location: " . $urltogo);
130 exit;
131 }
132 }
133 }
134} elseif ($action == 'edit' && $user->hasRight('accounting', 'chartofaccount')) {
135 if (!$cancel) {
136 if (!$account_number) {
137 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountNumber")), null, 'errors');
138 $action = 'update';
139 } elseif (!$label) {
140 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")), null, 'errors');
141 $action = 'update';
142 } else {
143 $result = $object->fetch($id);
144
145 $sql = "SELECT pcg_version FROM ".MAIN_DB_PREFIX."accounting_system WHERE rowid=".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
146
147 dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
148 $result2 = $db->query($sql);
149 $obj = $db->fetch_object($result2);
150
151 // Clean code
152
153 // To manage zero or not at the end of the accounting account
154 if (!getDolGlobalString('ACCOUNTING_MANAGE_ZERO')) {
155 $account_number = clean_account($account_number);
156 }
157
158 $account_parent = (GETPOSTINT('account_parent') > 0) ? GETPOSTINT('account_parent') : 0;
159
160 $object->fk_pcg_version = $obj->pcg_version;
161 $object->pcg_type = GETPOST('pcg_type', 'alpha');
162 $object->account_number = $account_number;
163 $object->account_parent = $account_parent;
164 $object->account_category = GETPOSTINT('account_category');
165 $object->label = $label;
166 $object->labelshort = GETPOST('labelshort', 'alpha');
167
168 $result = $object->update($user);
169
170 if ($result > 0) {
171 $urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"] . "?id=" . $id);
172 header("Location: " . $urltogo);
173 exit();
174 } elseif ($result == -2) {
175 setEventMessages($langs->trans("ErrorAccountNumberAlreadyExists", $object->account_number), null, 'errors');
176 } else {
177 setEventMessages($object->error, null, 'errors');
178 }
179 }
180 } else {
181 $urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"]."?id=".$id);
182 header("Location: ".$urltogo);
183 exit();
184 }
185} elseif ($action == 'delete' && $user->hasRight('accounting', 'chartofaccount')) {
186 $result = $object->fetch($id);
187
188 if (!empty($object->id)) {
189 $result = $object->delete($user);
190
191 if ($result > 0) {
192 header("Location: account.php");
193 exit;
194 }
195 }
196
197 if ($result < 0) {
198 setEventMessages($object->error, $object->errors, 'errors');
199 }
200}
201
202
203/*
204 * View
205 */
206
207$form = new Form($db);
208$formaccounting = new FormAccounting($db);
209
210$accountsystem = new AccountancySystem($db);
211$accountsystem->fetch(getDolGlobalInt('CHARTOFACCOUNTS'));
212
213$title = $langs->trans('AccountAccounting')." - ".$langs->trans('Card');
214
215$help_url = 'EN:Module_Double_Entry_Accounting#Setup|FR:Module_Comptabilit&eacute;_en_Partie_Double#Configuration';
216
217llxHeader('', $title, $help_url, '', 0, 0, '', '', '', 'mod-accountancy page-admin_card');
218
219
220// Create mode
221if ($action == 'create') {
222 print load_fiche_titre($langs->trans('NewAccountingAccount'));
223
224 print '<form name="add" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
225 print '<input type="hidden" name="token" value="'.newToken().'">';
226 print '<input type="hidden" name="action" value="add">';
227
228 print dol_get_fiche_head();
229
230 print '<table class="border centpercent">';
231
232 // Chart of account
233 print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("Chartofaccounts").'</span></td>';
234 print '<td>';
235 print $accountsystem->ref;
236 print '</td></tr>';
237
238 // Account number
239 print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("AccountNumber").'</span></td>';
240 print '<td><input name="account_number" size="30" value="'.$account_number.'"></td></tr>';
241
242 // Label
243 print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td>';
244 print '<td><input name="label" size="70" value="'.$object->label.'"></td></tr>';
245
246 // Label short
247 print '<tr><td>'.$langs->trans("LabelToShow").'</td>';
248 print '<td><input name="labelshort" size="70" value="'.$object->labelshort.'"></td></tr>';
249
250 // Account parent
251 print '<tr><td>'.$langs->trans("Accountparent").'</td>';
252 print '<td>';
253 print $formaccounting->select_account($object->account_parent, 'account_parent', 1, [], 0, 0, 'minwidth200');
254 print '</td></tr>';
255
256 // Chart of accounts type
257 print '<tr><td>';
258 print $form->textwithpicto($langs->trans("Pcgtype"), $langs->transnoentitiesnoconv("PcgtypeDesc"));
259 print '</td>';
260 print '<td>';
261 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).'">';
262 // autosuggest from existing account types if found
263 print '<datalist id="pcg_type_datalist">';
264 $sql = "SELECT DISTINCT pcg_type FROM " . MAIN_DB_PREFIX . "accounting_account";
265 $sql .= " WHERE fk_pcg_version = '" . $db->escape($accountsystem->ref) . "'";
266 $sql .= ' AND entity in ('.getEntity('accounting_account', 0).')'; // Always limit to current entity. No sharing in accountancy.
267 $sql .= ' LIMIT 50000'; // just as a sanity check
268 $resql = $db->query($sql);
269 if ($resql) {
270 while ($obj = $db->fetch_object($resql)) {
271 print '<option value="' . dol_escape_htmltag($obj->pcg_type) . '">';
272 }
273 }
274 print '</datalist>';
275 print '</td></tr>';
276
277 // Category
278 print '<tr><td>';
279 print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc"));
280 print '</td>';
281 print '<td>';
282 print $formaccounting->select_accounting_category($object->account_category, 'account_category', 1, 0, 1);
283 print '</td></tr>';
284
285 print '</table>';
286
287 print dol_get_fiche_end();
288
289 print '<div class="center">';
290 print '<input class="button button-save" type="submit" value="'.$langs->trans("Save").'">';
291 print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
292 print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
293 print '</div>';
294
295 print '</form>';
296} elseif ($id > 0 || $ref) {
297 $result = $object->fetch($id, $ref, 1);
298
299 if ($result > 0) {
301
302 // Edit mode
303 if ($action == 'update') {
304 print dol_get_fiche_head($head, 'card', $langs->trans('AccountAccounting'), 0, 'accounting_account');
305
306 print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
307 print '<input type="hidden" name="token" value="'.newToken().'">';
308 print '<input type="hidden" name="action" value="edit">';
309 print '<input type="hidden" name="id" value="'.$id.'">';
310 print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
311
312 print '<table class="border centpercent">';
313
314 // Account number
315 print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("AccountNumber").'</span></td>';
316 print '<td><input name="account_number" size="30" value="'.$object->account_number.'"></td></tr>';
317
318 // Label
319 print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td>';
320 print '<td><input name="label" size="70" value="'.$object->label.'"></td></tr>';
321
322 // Label short
323 print '<tr><td>'.$langs->trans("LabelToShow").'</td>';
324 print '<td><input name="labelshort" size="70" value="'.$object->labelshort.'"></td></tr>';
325
326 // Account parent
327 print '<tr><td>'.$langs->trans("Accountparent").'</td>';
328 print '<td>';
329 // Note: We accept disabled account as parent account so we can build a hierarchy and use only children
330 print $formaccounting->select_account($object->account_parent, 'account_parent', 1, array(), 0, 0, 'minwidth100 maxwidth300 maxwidthonsmartphone', 1, '');
331 print '</td></tr>';
332
333 // Chart of accounts type
334 print '<tr><td>';
335 print $form->textwithpicto($langs->trans("Pcgtype"), $langs->transnoentitiesnoconv("PcgtypeDesc"));
336 print '</td>';
337 print '<td>';
338 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).'">';
339 // autosuggest from existing account types if found
340 print '<datalist id="pcg_type_datalist">';
341 $sql = 'SELECT DISTINCT pcg_type FROM ' . MAIN_DB_PREFIX . 'accounting_account';
342 $sql .= " WHERE fk_pcg_version = '" . $db->escape($accountsystem->ref) . "'";
343 $sql .= ' AND entity in ('.getEntity('accounting_account', 0).')'; // Always limit to current entity. No sharing in accountancy.
344 $sql .= ' LIMIT 50000'; // just as a sanity check
345 $resql = $db->query($sql);
346 if ($resql) {
347 while ($obj = $db->fetch_object($resql)) {
348 print '<option value="' . dol_escape_htmltag($obj->pcg_type) . '">';
349 }
350 }
351 print '</datalist>';
352 print '</td></tr>';
353
354 // Category
355 print '<tr><td>';
356 print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc"));
357 print '</td>';
358 print '<td>';
359 print $formaccounting->select_accounting_category($object->account_category, 'account_category', 1);
360 print '</td></tr>';
361
362 print '</table>';
363
364 print dol_get_fiche_end();
365
366 print $form->buttonsSaveCancel();
367
368 print '</form>';
369 } else {
370 // View mode
371 $linkback = '<a href="'.DOL_URL_ROOT.'/accountancy/admin/account.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
372
373 print dol_get_fiche_head($head, 'card', $langs->trans('AccountAccounting'), -1, 'accounting_account');
374
375 dol_banner_tab($object, 'ref', $linkback, 1, 'account_number', 'ref');
376
377
378 print '<div class="fichecenter">';
379 print '<div class="underbanner clearboth"></div>';
380
381 print '<table class="border centpercent tableforfield">';
382
383 // Label
384 print '<tr><td class="titlefield">'.$langs->trans("Label").'</td>';
385 print '<td colspan="2">'.$object->label.'</td></tr>';
386
387 // Label to show
388 print '<tr><td class="titlefield">'.$langs->trans("LabelToShow").'</td>';
389 print '<td colspan="2">'.$object->labelshort.'</td></tr>';
390
391 // Account parent
392 $accp = new AccountingAccount($db);
393 if (!empty($object->account_parent)) {
394 $accp->fetch($object->account_parent, '');
395 }
396 print '<tr><td>'.$langs->trans("Accountparent").'</td>';
397 print '<td colspan="2">'.$accp->account_number.' - '.$accp->label.'</td></tr>';
398
399 // Group of accounting account
400 print '<tr><td>';
401 print $form->textwithpicto($langs->trans("Pcgtype"), $langs->transnoentitiesnoconv("PcgtypeDesc"));
402 print '</td>';
403 print '<td colspan="2">'.$object->pcg_type.'</td></tr>';
404
405 // Custom group of accounting account
406 print "<tr><td>";
407 print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc"));
408 print "</td><td colspan='2'>".$object->account_category_label."</td>";
409
410 print '</table>';
411
412 print '</div>';
413
414 print dol_get_fiche_end();
415
416 /*
417 * Actions buttons
418 */
419 print '<div class="tabsAction">';
420
421 if ($user->hasRight('accounting', 'chartofaccount')) {
422 print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=update&token='.newToken().'&id='.$object->id.'">'.$langs->trans('Modify').'</a>';
423 } else {
424 print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotAllowed")).'">'.$langs->trans('Modify').'</a>';
425 }
426
427 // Delete
428 $permissiontodelete = $user->hasRight('accounting', 'chartofaccount');
429 print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), 'delete', $permissiontodelete);
430
431 print '</div>';
432 }
433 } else {
434 dol_print_error($db, $object->error, $object->errors);
435 }
436}
437
438// End of page
439llxFooter();
440$db->close();
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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:71
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
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.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
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.
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 a 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.