dolibarr  18.0.0
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-2020 Alexandre Spangaro <aspangaro@open-dsi.fr>
4  * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
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 // Load Dolibarr environment
27 require '../../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancysystem.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
32 
33 $error = 0;
34 
35 // Load translation files required by the page
36 $langs->loadLangs(array('accountancy', 'bills', 'compta'));
37 
38 $action = GETPOST('action', 'aZ09');
39 $backtopage = GETPOST('backtopage', 'alpha');
40 $id = GETPOST('id', 'int');
41 $ref = GETPOST('ref', 'alpha');
42 $rowid = GETPOST('rowid', 'int');
43 $cancel = GETPOST('cancel', 'alpha');
44 
45 $account_number = GETPOST('account_number', 'alphanohtml');
46 $label = GETPOST('label', 'alpha');
47 
48 // Security check
49 if ($user->socid > 0) {
51 }
52 if (!$user->hasRight('accounting', 'chartofaccount')) {
54 }
55 
56 
57 $object = new AccountingAccount($db);
58 
59 
60 /*
61  * Action
62  */
63 
64 if (GETPOST('cancel', 'alpha')) {
65  $urltogo = $backtopage ? $backtopage : DOL_URL_ROOT.'/accountancy/admin/account.php';
66  header("Location: ".$urltogo);
67  exit;
68 }
69 
70 if ($action == 'add' && $user->hasRight('accounting', 'chartofaccount')) {
71  if (!$cancel) {
72  if (!$account_number) {
73  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountNumber")), null, 'errors');
74  $action = 'create';
75  } elseif (!$label) {
76  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")), null, 'errors');
77  $action = 'create';
78  } else {
79  $sql = "SELECT pcg_version FROM " . MAIN_DB_PREFIX . "accounting_system WHERE rowid = ".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
80 
81  dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
82  $result = $db->query($sql);
83  $obj = $db->fetch_object($result);
84 
85  // Clean code
86 
87  // To manage zero or not at the end of the accounting account
88  if (!empty($conf->global->ACCOUNTING_MANAGE_ZERO)) {
89  $account_number = $account_number;
90  } else {
91  $account_number = clean_account($account_number);
92  }
93 
94  if (GETPOST('account_parent', 'int') <= 0) {
95  $account_parent = 0;
96  } else {
97  $account_parent = GETPOST('account_parent', 'int');
98  }
99 
100  $object->fk_pcg_version = $obj->pcg_version;
101  $object->pcg_type = GETPOST('pcg_type', 'alpha');
102  $object->account_number = $account_number;
103  $object->account_parent = $account_parent;
104  $object->account_category = GETPOST('account_category', 'alpha');
105  $object->label = $label;
106  $object->labelshort = GETPOST('labelshort', 'alpha');
107  $object->active = 1;
108 
109  $res = $object->create($user);
110  if ($res == -3) {
111  $error = 1;
112  $action = "create";
113  setEventMessages($object->error, $object->errors, 'errors');
114  } elseif ($res == -4) {
115  $error = 2;
116  $action = "create";
117  setEventMessages($object->error, $object->errors, 'errors');
118  } elseif ($res < 0) {
119  $error++;
120  setEventMessages($object->error, $object->errors, 'errors');
121  $action = "create";
122  }
123  if (!$error) {
124  setEventMessages("RecordCreatedSuccessfully", null, 'mesgs');
125  $urltogo = $backtopage ? $backtopage : DOL_URL_ROOT.'/accountancy/admin/account.php';
126  header("Location: " . $urltogo);
127  exit;
128  }
129  }
130  }
131 } elseif ($action == 'edit' && $user->hasRight('accounting', 'chartofaccount')) {
132  if (!$cancel) {
133  if (!$account_number) {
134  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountNumber")), null, 'errors');
135  $action = 'update';
136  } elseif (!$label) {
137  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")), null, 'errors');
138  $action = 'update';
139  } else {
140  $result = $object->fetch($id);
141 
142  $sql = "SELECT pcg_version FROM ".MAIN_DB_PREFIX."accounting_system WHERE rowid=".((int) getDolGlobalInt('CHARTOFACCOUNTS'));
143 
144  dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
145  $result2 = $db->query($sql);
146  $obj = $db->fetch_object($result2);
147 
148  // Clean code
149 
150  // To manage zero or not at the end of the accounting account
151  if (!empty($conf->global->ACCOUNTING_MANAGE_ZERO)) {
152  $account_number = $account_number;
153  } else {
154  $account_number = clean_account($account_number);
155  }
156 
157  if (GETPOST('account_parent', 'int') <= 0) {
158  $account_parent = 0;
159  } else {
160  $account_parent = GETPOST('account_parent', 'int');
161  }
162 
163  $object->fk_pcg_version = $obj->pcg_version;
164  $object->pcg_type = GETPOST('pcg_type', 'alpha');
165  $object->account_number = $account_number;
166  $object->account_parent = $account_parent;
167  $object->account_category = GETPOST('account_category', 'alpha');
168  $object->label = $label;
169  $object->labelshort = GETPOST('labelshort', 'alpha');
170 
171  $result = $object->update($user);
172 
173  if ($result > 0) {
174  $urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"] . "?id=" . $id);
175  header("Location: " . $urltogo);
176  exit();
177  } elseif ($result == -2) {
178  setEventMessages($langs->trans("ErrorAccountNumberAlreadyExists", $object->account_number), null, 'errors');
179  } else {
180  setEventMessages($object->error, null, 'errors');
181  }
182  }
183  } else {
184  $urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"]."?id=".$id);
185  header("Location: ".$urltogo);
186  exit();
187  }
188 } elseif ($action == 'delete' && $user->hasRight('accounting', 'chartofaccount')) {
189  $result = $object->fetch($id);
190 
191  if (!empty($object->id)) {
192  $result = $object->delete($user);
193 
194  if ($result > 0) {
195  header("Location: account.php");
196  exit;
197  }
198  }
199 
200  if ($result < 0) {
201  setEventMessages($object->error, $object->errors, 'errors');
202  }
203 }
204 
205 
206 /*
207  * View
208  */
209 
210 $form = new Form($db);
211 $formaccounting = new FormAccounting($db);
212 
213 $accountsystem = new AccountancySystem($db);
214 $accountsystem->fetch(getDolGlobalInt('CHARTOFACCOUNTS'));
215 
216 $title = $langs->trans('AccountAccounting')." - ".$langs->trans('Card');
217 
218 $help_url = 'EN:Category:Accounting';
219 
220 llxheader('', $title, $help_url);
221 
222 
223 // Create mode
224 if ($action == 'create') {
225  print load_fiche_titre($langs->trans('NewAccountingAccount'));
226 
227  print '<form name="add" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
228  print '<input type="hidden" name="token" value="'.newToken().'">';
229  print '<input type="hidden" name="action" value="add">';
230 
231  print dol_get_fiche_head();
232 
233  print '<table class="border centpercent">';
234 
235  // Chart of account
236  print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("Chartofaccounts").'</span></td>';
237  print '<td>';
238  print $accountsystem->ref;
239  print '</td></tr>';
240 
241  // Account number
242  print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("AccountNumber").'</span></td>';
243  print '<td><input name="account_number" size="30" value="'.$account_number.'"></td></tr>';
244 
245  // Label
246  print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td>';
247  print '<td><input name="label" size="70" value="'.$object->label.'"></td></tr>';
248 
249  // Label short
250  print '<tr><td>'.$langs->trans("LabelToShow").'</td>';
251  print '<td><input name="labelshort" size="70" value="'.$object->labelshort.'"></td></tr>';
252 
253  // Account parent
254  print '<tr><td>'.$langs->trans("Accountparent").'</td>';
255  print '<td>';
256  print $formaccounting->select_account($object->account_parent, 'account_parent', 1, null, 0, 0, 'minwidth200');
257  print '</td></tr>';
258 
259  // Chart of accounts type
260  print '<tr><td>';
261  print $form->textwithpicto($langs->trans("Pcgtype"), $langs->transnoentitiesnoconv("PcgtypeDesc"));
262  print '</td>';
263  print '<td>';
264  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).'">';
265  // autosuggest from existing account types if found
266  print '<datalist id="pcg_type_datalist">';
267  $sql = "SELECT DISTINCT pcg_type FROM " . MAIN_DB_PREFIX . "accounting_account";
268  $sql .= " WHERE fk_pcg_version = '" . $db->escape($accountsystem->ref) . "'";
269  $sql .= ' AND entity in ('.getEntity('accounting_account', 0).')'; // Always limit to current entity. No sharing in accountancy.
270  $sql .= ' LIMIT 50000'; // just as a sanity check
271  $resql = $db->query($sql);
272  if ($resql) {
273  while ($obj = $db->fetch_object($resql)) {
274  print '<option value="' . dol_escape_htmltag($obj->pcg_type) . '">';
275  }
276  }
277  print '</datalist>';
278  print '</td></tr>';
279 
280  // Category
281  print '<tr><td>';
282  print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc"));
283  print '</td>';
284  print '<td>';
285  print $formaccounting->select_accounting_category($object->account_category, 'account_category', 1, 0, 1);
286  print '</td></tr>';
287 
288  print '</table>';
289 
290  print dol_get_fiche_end();
291 
292  print '<div class="center">';
293  print '<input class="button button-save" type="submit" value="'.$langs->trans("Save").'">';
294  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
295  print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
296  print '</div>';
297 
298  print '</form>';
299 } elseif ($id > 0 || $ref) {
300  $result = $object->fetch($id, $ref, 1);
301 
302  if ($result > 0) {
303  $head = accounting_prepare_head($object);
304 
305  // Edit mode
306  if ($action == 'update') {
307  print dol_get_fiche_head($head, 'card', $langs->trans('AccountAccounting'), 0, 'accounting_account');
308 
309  print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
310  print '<input type="hidden" name="token" value="'.newToken().'">';
311  print '<input type="hidden" name="action" value="edit">';
312  print '<input type="hidden" name="id" value="'.$id.'">';
313  print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
314 
315  print '<table class="border centpercent">';
316 
317  // Account number
318  print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("AccountNumber").'</span></td>';
319  print '<td><input name="account_number" size="30" value="'.$object->account_number.'"</td></tr>';
320 
321  // Label
322  print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td>';
323  print '<td><input name="label" size="70" value="'.$object->label.'"</td></tr>';
324 
325  // Label short
326  print '<tr><td>'.$langs->trans("LabelToShow").'</td>';
327  print '<td><input name="labelshort" size="70" value="'.$object->labelshort.'"</td></tr>';
328 
329  // Account parent
330  print '<tr><td>'.$langs->trans("Accountparent").'</td>';
331  print '<td>';
332  // Note: We accept disabled account as parent account so we can build a hierarchy and use only childs
333  print $formaccounting->select_account($object->account_parent, 'account_parent', 1, array(), 0, 0, 'minwidth100 maxwidth300 maxwidthonsmartphone', 1, '');
334  print '</td></tr>';
335 
336  // Chart of accounts type
337  print '<tr><td>';
338  print $form->textwithpicto($langs->trans("Pcgtype"), $langs->transnoentitiesnoconv("PcgtypeDesc"));
339  print '</td>';
340  print '<td>';
341  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).'">';
342  // autosuggest from existing account types if found
343  print '<datalist id="pcg_type_datalist">';
344  $sql = 'SELECT DISTINCT pcg_type FROM ' . MAIN_DB_PREFIX . 'accounting_account';
345  $sql .= " WHERE fk_pcg_version = '" . $db->escape($accountsystem->ref) . "'";
346  $sql .= ' AND entity in ('.getEntity('accounting_account', 0).')'; // Always limit to current entity. No sharing in accountancy.
347  $sql .= ' LIMIT 50000'; // just as a sanity check
348  $resql = $db->query($sql);
349  if ($resql) {
350  while ($obj = $db->fetch_object($resql)) {
351  print '<option value="' . dol_escape_htmltag($obj->pcg_type) . '">';
352  }
353  }
354  print '</datalist>';
355  print '</td></tr>';
356 
357  // Category
358  print '<tr><td>';
359  print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc"));
360  print '</td>';
361  print '<td>';
362  print $formaccounting->select_accounting_category($object->account_category, 'account_category', 1);
363  print '</td></tr>';
364 
365  print '</table>';
366 
367  print dol_get_fiche_end();
368 
369  print $form->buttonsSaveCancel();
370 
371  print '</form>';
372  } else {
373  // View mode
374  $linkback = '<a href="'.DOL_URL_ROOT.'/accountancy/admin/account.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
375 
376  print dol_get_fiche_head($head, 'card', $langs->trans('AccountAccounting'), -1, 'accounting_account');
377 
378  dol_banner_tab($object, 'ref', $linkback, 1, 'account_number', 'ref');
379 
380 
381  print '<div class="fichecenter">';
382  print '<div class="underbanner clearboth"></div>';
383 
384  print '<table class="border centpercent">';
385 
386  // Label
387  print '<tr><td class="titlefield">'.$langs->trans("Label").'</td>';
388  print '<td colspan="2">'.$object->label.'</td></tr>';
389 
390  // Label to show
391  print '<tr><td class="titlefield">'.$langs->trans("LabelToShow").'</td>';
392  print '<td colspan="2">'.$object->labelshort.'</td></tr>';
393 
394  // Account parent
395  $accp = new AccountingAccount($db);
396  if (!empty($object->account_parent)) {
397  $accp->fetch($object->account_parent, '');
398  }
399  print '<tr><td>'.$langs->trans("Accountparent").'</td>';
400  print '<td colspan="2">'.$accp->account_number.' - '.$accp->label.'</td></tr>';
401 
402  // Group of accounting account
403  print '<tr><td>';
404  print $form->textwithpicto($langs->trans("Pcgtype"), $langs->transnoentitiesnoconv("PcgtypeDesc"));
405  print '</td>';
406  print '<td colspan="2">'.$object->pcg_type.'</td></tr>';
407 
408  // Custom group of accounting account
409  print "<tr><td>";
410  print $form->textwithpicto($langs->trans("AccountingCategory"), $langs->transnoentitiesnoconv("AccountingAccountGroupsDesc"));
411  print "</td><td colspan='2'>".$object->account_category_label."</td>";
412 
413  print '</table>';
414 
415  print '</div>';
416 
417  print dol_get_fiche_end();
418 
419  /*
420  * Actions buttons
421  */
422  print '<div class="tabsAction">';
423 
424  if ($user->hasRight('accounting', 'chartofaccount')) {
425  print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=update&token='.newToken().'&id='.$object->id.'">'.$langs->trans('Modify').'</a>';
426  } else {
427  print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotAllowed")).'">'.$langs->trans('Modify').'</a>';
428  }
429 
430  // Delete
431  $permissiontodelete = $user->hasRight('accounting', 'chartofaccount');
432  print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), 'delete', $permissiontodelete);
433 
434  print '</div>';
435  }
436  } else {
437  dol_print_error($db, $object->error, $object->errors);
438  }
439 }
440 
441 // End of page
442 llxFooter();
443 $db->close();
accounting_prepare_head
accounting_prepare_head(AccountingAccount $object)
Prepare array with list of tabs.
Definition: accounting.lib.php:52
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:70
dol_escape_htmltag
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...
Definition: functions.lib.php:1600
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5477
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:609
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:5107
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
FormAccounting
Class to manage generation of HTML components for accounting management.
Definition: html.formaccounting.class.php:33
dol_banner_tab
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
Definition: functions.lib.php:2205
$help_url
if(GETPOST('button_removefilter_x', 'alpha')||GETPOST('button_removefilter.x', 'alpha')||GETPOST('button_removefilter', 'alpha')) if(GETPOST('button_search_x', 'alpha')||GETPOST('button_search.x', 'alpha')||GETPOST('button_search', 'alpha')) if($action=="save" &&empty($cancel)) $help_url
View.
Definition: agenda.php:118
clean_account
clean_account($account)
Return accounting account without zero on the right.
Definition: accounting.lib.php:81
AccountancySystem
Class to manage accountancy systems.
Definition: accountancysystem.class.php:29
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1741
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
Definition: functions.lib.php:8673
$sql
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:11654
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2177
dol_get_fiche_head
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.
Definition: functions.lib.php:1979
AccountingAccount
Class to manage accounting accounts.
Definition: accountingaccount.class.php:36
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:509
dolGetButtonAction
dolGetButtonAction($label, $text='', $actionType='default', $url='', $id='', $userRight=1, $params=array())
Function dolGetButtonAction.
Definition: functions.lib.php:11080
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:53
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
Definition: security.lib.php:1164
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:156