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