dolibarr  13.0.2
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 $mesg = '';
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', 'string');
46 $label = GETPOST('label', 'alpha');
47 
48 // Security check
49 if ($user->socid > 0) accessforbidden();
50 if (!$user->rights->accounting->chartofaccount) accessforbidden();
51 
52 
53 $object = new AccountingAccount($db);
54 
55 
56 /*
57  * Action
58  */
59 
60 if (GETPOST('cancel', 'alpha'))
61 {
62  $urltogo = $backtopage ? $backtopage : dol_buildpath('/accountancy/admin/account.php', 1);
63  header("Location: ".$urltogo);
64  exit;
65 }
66 
67 if ($action == 'add' && $user->rights->accounting->chartofaccount)
68 {
69  if (!$cancel) {
70  if (!$account_number)
71  {
72  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountNumber")), null, 'errors');
73  $action = 'create';
74  } elseif (!$label)
75  {
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) $conf->global->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 ($conf->global->ACCOUNTING_MANAGE_ZERO == 1) {
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_buildpath('/accountancy/admin/account.php', 1);
126  header("Location: " . $urltogo);
127  exit;
128  }
129  }
130  }
131 } elseif ($action == 'edit' && $user->rights->accounting->chartofaccount) {
132  if (!$cancel) {
133  if (!$account_number)
134  {
135  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("AccountNumber")), null, 'errors');
136  $action = 'update';
137  } elseif (!$label)
138  {
139  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Label")), null, 'errors');
140  $action = 'update';
141  } else {
142  $result = $object->fetch($id);
143 
144  $sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
145 
146  dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
147  $result2 = $db->query($sql);
148  $obj = $db->fetch_object($result2);
149 
150  // Clean code
151 
152  // To manage zero or not at the end of the accounting account
153  if ($conf->global->ACCOUNTING_MANAGE_ZERO == 1) {
154  $account_number = $account_number;
155  } else {
156  $account_number = clean_account($account_number);
157  }
158 
159  if (GETPOST('account_parent', 'int') <= 0) {
160  $account_parent = 0;
161  } else {
162  $account_parent = GETPOST('account_parent', 'int');
163  }
164 
165  $object->fk_pcg_version = $obj->pcg_version;
166  $object->pcg_type = GETPOST('pcg_type', 'alpha');
167  $object->account_number = $account_number;
168  $object->account_parent = $account_parent;
169  $object->account_category = GETPOST('account_category', 'alpha');
170  $object->label = $label;
171  $object->labelshort = GETPOST('labelshort', 'alpha');
172 
173  $result = $object->update($user);
174 
175  if ($result > 0) {
176  $urltogo = $backtopage ? $backtopage : ($_SERVER["PHP_SELF"] . "?id=" . $id);
177  header("Location: " . $urltogo);
178  exit();
179  } else {
180  $mesg = $object->error;
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->rights->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($conf->global->CHARTOFACCOUNTS);
215 
216 $title = $langs->trans('AccountAccounting')." - ".$langs->trans('Card');
217 $helpurl = '';
218 llxheader('', $title, $helpurl);
219 
220 
221 // Create mode
222 if ($action == 'create') {
223  print load_fiche_titre($langs->trans('NewAccountingAccount'));
224 
225  print '<form name="add" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
226  print '<input type="hidden" name="token" value="'.newToken().'">';
227  print '<input type="hidden" name="action" value="add">';
228 
230 
231  print '<table class="border centpercent">';
232 
233  // Chart of account
234  print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("Chartofaccounts").'</span></td>';
235  print '<td>';
236  print $accountsystem->ref;
237  print '</td></tr>';
238 
239  // Account number
240  print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("AccountNumber").'</span></td>';
241  print '<td><input name="account_number" size="30" value="'.$account_number.'"></td></tr>';
242 
243  // Label
244  print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td>';
245  print '<td><input name="label" size="70" value="'.$object->label.'"></td></tr>';
246 
247  // Label short
248  print '<tr><td>'.$langs->trans("LabelToShow").'</td>';
249  print '<td><input name="labelshort" size="70" value="'.$object->labelshort.'"></td></tr>';
250 
251  // Account parent
252  print '<tr><td>'.$langs->trans("Accountparent").'</td>';
253  print '<td>';
254  print $formaccounting->select_account($object->account_parent, 'account_parent', 1, null, 0, 0, 'minwidth200');
255  print '</td></tr>';
256 
257  // Chart of accounts type
258  print '<tr><td>'.$langs->trans("Pcgtype").'</td>';
259  print '<td>';
260  print '<input type="text" name="pcg_type" value="'.dol_escape_htmltag(GETPOSTISSET('pcg_type') ? GETPOST('pcg_type', 'alpha') : $object->pcg_type).'">';
261  print '</td></tr>';
262 
263  // Category
264  print '<tr><td>'.$langs->trans("AccountingCategory").'</td>';
265  print '<td>';
266  $formaccounting->select_accounting_category($object->account_category, 'account_category', 1, 0, 1);
267  print '</td></tr>';
268 
269  print '</table>';
270 
272 
273  print '<div class="center">';
274  print '<input class="button button-save" type="submit" value="'.$langs->trans("Save").'">';
275  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
276  print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
277  print '</div>';
278 
279  print '</form>';
280 } elseif ($id > 0 || $ref) {
281  $result = $object->fetch($id, $ref, 1);
282 
283  if ($result > 0) {
284  dol_htmloutput_mesg($mesg);
285 
286  $head = accounting_prepare_head($object);
287 
288  // Edit mode
289  if ($action == 'update')
290  {
291  print dol_get_fiche_head($head, 'card', $langs->trans('AccountAccounting'), 0, 'billr');
292 
293  print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
294  print '<input type="hidden" name="token" value="'.newToken().'">';
295  print '<input type="hidden" name="action" value="edit">';
296  print '<input type="hidden" name="id" value="'.$id.'">';
297  print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
298 
299  print '<table class="border centpercent">';
300 
301  // Account number
302  print '<tr><td class="titlefieldcreate"><span class="fieldrequired">'.$langs->trans("AccountNumber").'</span></td>';
303  print '<td><input name="account_number" size="30" value="'.$object->account_number.'"</td></tr>';
304 
305  // Label
306  print '<tr><td><span class="fieldrequired">'.$langs->trans("Label").'</span></td>';
307  print '<td><input name="label" size="70" value="'.$object->label.'"</td></tr>';
308 
309  // Label short
310  print '<tr><td>'.$langs->trans("LabelToShow").'</td>';
311  print '<td><input name="labelshort" size="70" value="'.$object->labelshort.'"</td></tr>';
312 
313  // Account parent
314  print '<tr><td>'.$langs->trans("Accountparent").'</td>';
315  print '<td>';
316  print $formaccounting->select_account($object->account_parent, 'account_parent', 1);
317  print '</td></tr>';
318 
319  // Chart of accounts type
320  print '<tr><td>'.$langs->trans("Pcgtype").'</td>';
321  print '<td>';
322  print '<input type="text" name="pcg_type" value="'.dol_escape_htmltag(GETPOSTISSET('pcg_type') ? GETPOST('pcg_type', 'alpha') : $object->pcg_type).'">';
323  print '</td></tr>';
324 
325  // Category
326  print '<tr><td>'.$langs->trans("AccountingCategory").'</td>';
327  print '<td>';
328  $formaccounting->select_accounting_category($object->account_category, 'account_category', 1);
329  print '</td></tr>';
330 
331  print '</table>';
332 
334 
335  print '<div class="center">';
336  print '<input type="submit" class="button button-save" value="'.$langs->trans("Save").'">';
337  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
338  print '<input type="submit" name="cancel" class="button button-cancel" value="'.$langs->trans("Cancel").'">';
339  print '</div>';
340 
341  print '</form>';
342  } else {
343  // View mode
344  $linkback = '<a href="'.DOL_URL_ROOT.'/accountancy/admin/account.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
345 
346  print dol_get_fiche_head($head, 'card', $langs->trans('AccountAccounting'), -1, 'billr');
347 
348  dol_banner_tab($object, 'ref', $linkback, 1, 'account_number', 'ref');
349 
350 
351  print '<div class="fichecenter">';
352  print '<div class="underbanner clearboth"></div>';
353 
354  print '<table class="border centpercent">';
355 
356  // Label
357  print '<tr><td class="titlefield">'.$langs->trans("Label").'</td>';
358  print '<td colspan="2">'.$object->label.'</td></tr>';
359 
360  // Label to show
361  print '<tr><td class="titlefield">'.$langs->trans("LabelToShow").'</td>';
362  print '<td colspan="2">'.$object->labelshort.'</td></tr>';
363 
364  // Account parent
365  $accp = new AccountingAccount($db);
366  if (!empty($object->account_parent)) {
367  $accp->fetch($object->account_parent, '');
368  }
369  print '<tr><td>'.$langs->trans("Accountparent").'</td>';
370  print '<td colspan="2">'.$accp->account_number.' - '.$accp->label.'</td></tr>';
371 
372  // Category
373  print "<tr><td>".$langs->trans("AccountingCategory")."</td><td colspan='2'>".$object->account_category_label."</td>";
374 
375  // Chart of accounts type
376  print '<tr><td>'.$langs->trans("Pcgtype").'</td>';
377  print '<td colspan="2">'.$object->pcg_type.'</td></tr>';
378 
379  print '</table>';
380 
381  print '</div>';
382 
384 
385  /*
386  * Actions buttons
387  */
388  print '<div class="tabsAction">';
389 
390  if (!empty($user->rights->accounting->chartofaccount)) {
391  print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=update&token='.newToken().'&id='.$id.'">'.$langs->trans('Modify').'</a>';
392  } else {
393  print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotAllowed")).'">'.$langs->trans('Modify').'</a>';
394  }
395 
396  if (!empty($user->rights->accounting->chartofaccount)) {
397  print '<a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?action=delete&token='.newToken().'&id='.$id.'">'.$langs->trans('Delete').'</a>';
398  } else {
399  print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotAllowed")).'">'.$langs->trans('Delete').'</a>';
400  }
401 
402  print '</div>';
403  }
404  } else {
405  dol_print_error($db, $object->error, $object->errors);
406  }
407 }
408 
409 // End of page
410 llxFooter();
411 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
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 ...
Class to manage accountancy systems.
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
dol_htmloutput_mesg($mesgstring= '', $mesgarray=array(), $style= 'ok', $keepembedded=0)
Print formated messages to output (Used to show messages on html output).
print
Draft customers invoices.
Definition: index.php:89
Class to manage generation of HTML components for accounting management.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
accounting_prepare_head(AccountingAccount $object)
Prepare array with list of tabs.
newToken()
Return the value of token currently saved into session with name &#39;newtoken&#39;.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Class to manage accounting accounts.
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.
clean_account($account)
Return accounting account without zero on the right.
llxFooter()
Empty footer.
Definition: wrapper.php:59