dolibarr  9.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-2018 Alexandre Spangaro <aspangaro@zendsi.com>
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 <http://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"));
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 $accountingaccount = GETPOST('accountingaccount','alpha');
45 
46 // Security check
47 
48 
49 $object = new AccountingAccount($db);
50 
51 
52 /*
53  * Action
54  */
55 
56 if (GETPOST('cancel','alpha'))
57 {
58  $urltogo=$backtopage?$backtopage:dol_buildpath('/accountancy/admin/account.php',1);
59  header("Location: ".$urltogo);
60  exit;
61 }
62 
63 if ($action == 'add' && $user->rights->accounting->chartofaccount)
64 {
65  if (! $cancel) {
66  $sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
67 
68  dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
69  $result = $db->query($sql);
70  $obj = $db->fetch_object($result);
71 
72  // Clean code
73 
74  // To manage zero or not at the end of the accounting account
75  if($conf->global->ACCOUNTING_MANAGE_ZERO == 1)
76  {
77  $account_number = GETPOST('account_number','string');
78  }
79  else
80  {
81  $account_number = clean_account(GETPOST('account_number','string'));
82  }
83 
84  if (GETPOST('account_parent','int') <= 0)
85  {
86  $account_parent = 0;
87  }
88  else
89  {
90  $account_parent = GETPOST('account_parent','int');
91  }
92 
93  $object->fk_pcg_version = $obj->pcg_version;
94  $object->pcg_type = GETPOST('pcg_type','alpha');
95  $object->pcg_subtype = GETPOST('pcg_subtype','alpha');
96  $object->account_number = $account_number;
97  $object->account_parent = $account_parent;
98  $object->account_category = GETPOST('account_category','alpha');
99  $object->label = GETPOST('label', 'alpha');
100  $object->active = 1;
101 
102  $res = $object->create($user);
103  if ($res == - 3) {
104  $error = 1;
105  $action = "create";
106  setEventMessages($object->error, $object->errors, 'errors');
107  }
108  elseif ($res == - 4) {
109  $error = 2;
110  $action = "create";
111  setEventMessages($object->error, $object->errors, 'errors');
112  }
113  elseif ($res < 0)
114  {
115  $error++;
116  setEventMessages($object->error, $object->errors, 'errors');
117  $action = "create";
118  }
119  if (! $error)
120  {
121  setEventMessages("RecordCreatedSuccessfully",null,'mesgs');
122  $urltogo=$backtopage?$backtopage:dol_buildpath('/accountancy/admin/account.php',1);
123  header("Location: ".$urltogo);
124  exit;
125  }
126  }
127 } else if ($action == 'edit' && $user->rights->accounting->chartofaccount) {
128  if (! $cancel) {
129  $result = $object->fetch($id);
130 
131  $sql = 'SELECT pcg_version FROM ' . MAIN_DB_PREFIX . 'accounting_system WHERE rowid=' . $conf->global->CHARTOFACCOUNTS;
132 
133  dol_syslog('accountancy/admin/card.php:: $sql=' . $sql);
134  $result2 = $db->query($sql);
135  $obj = $db->fetch_object($result2);
136 
137  // Clean code
138 
139  // To manage zero or not at the end of the accounting account
140  if($conf->global->ACCOUNTING_MANAGE_ZERO == 1)
141  {
142  $account_number = GETPOST('account_number','string');
143  }
144  else
145  {
146  $account_number = clean_account(GETPOST('account_number','string'));
147  }
148 
149  if (GETPOST('account_parent','int') <= 0)
150  {
151  $account_parent = 0;
152  }
153  else
154  {
155  $account_parent = GETPOST('account_parent','int');
156  }
157 
158  $object->fk_pcg_version = $obj->pcg_version;
159  $object->pcg_type = GETPOST('pcg_type','alpha');
160  $object->pcg_subtype = GETPOST('pcg_subtype','alpha');
161  $object->account_number = $account_number;
162  $object->account_parent = $account_parent;
163  $object->account_category = GETPOST('account_category','alpha');
164  $object->label = GETPOST('label', 'alpha');
165 
166  $result = $object->update($user);
167 
168  if ($result > 0) {
169  $urltogo=$backtopage?$backtopage:($_SERVER["PHP_SELF"]."?id=".$id);
170  header("Location: " . $urltogo);
171  exit();
172  } else {
173  $mesg = $object->error;
174  }
175  } else {
176  $urltogo=$backtopage?$backtopage:($_SERVER["PHP_SELF"]."?id=".$id);
177  header("Location: " . $urltogo);
178  exit();
179  }
180 } else if ($action == 'delete' && $user->rights->accounting->chartofaccount) {
181  $result = $object->fetch($id);
182 
183  if (! empty($object->id)) {
184  $result = $object->delete($user);
185 
186  if ($result > 0) {
187  header("Location: account.php");
188  exit;
189  }
190  }
191 
192  if ($result < 0) {
193  setEventMessages($object->error, $object->errors, 'errors');
194  }
195 }
196 
197 
198 /*
199  * View
200  */
201 
202 $form = new Form($db);
203 $formaccounting = new FormAccounting($db);
204 
205 $accountsystem = new AccountancySystem($db);
206 $accountsystem->fetch($conf->global->CHARTOFACCOUNTS);
207 
208 $title = $langs->trans('AccountAccounting') ." - ". $langs->trans('Card');
209 $helpurl = '';
210 llxheader('', $title, $helpurl);
211 
212 
213 // Create mode
214 if ($action == 'create') {
215  print load_fiche_titre($langs->trans('NewAccountingAccount'));
216 
217  print '<form name="add" action="' . $_SERVER["PHP_SELF"] . '" method="POST">' . "\n";
218  print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
219  print '<input type="hidden" name="action" value="add">';
220 
221  dol_fiche_head();
222 
223  print '<table class="border" width="100%">';
224 
225  // Chart of account
226  print '<tr><td class="titlefieldcreate"><span class="fieldrequired">' . $langs->trans("Chartofaccounts") . '</span></td>';
227  print '<td>';
228  print $accountsystem->ref;
229  print '</td></tr>';
230 
231  // Account number
232  print '<tr><td class="titlefieldcreate"><span class="fieldrequired">' . $langs->trans("AccountNumber") . '</span></td>';
233  print '<td><input name="account_number" size="30" value="' . $accountingaccount . '"></td></tr>';
234 
235  // Label
236  print '<tr><td><span class="fieldrequired">' . $langs->trans("Label") . '</span></td>';
237  print '<td><input name="label" size="70" value="' . $object->label . '"></td></tr>';
238 
239  // Account parent
240  print '<tr><td>' . $langs->trans("Accountparent") . '</td>';
241  print '<td>';
242  print $formaccounting->select_account($object->account_parent, 'account_parent', 1, null, 0, 0, 'minwidth200');
243  print '</td></tr>';
244 
245  // Category
246  print '<tr><td>' . $langs->trans("AccountingCategory") . '</td>';
247  print '<td>';
248  $formaccounting->select_accounting_category($object->account_category, 'account_category', 1, 0, 1);
249  print '</td></tr>';
250 
251  // Chart of accounts type
252  print '<tr><td>' . $langs->trans("Pcgtype") . '</td>';
253  print '<td>';
254  print '<input type="text" name="pcg_type" value="'.dol_escape_htmltag(isset($_POST['pcg_type'])?GETPOST('pcg_type','alpha'):$object->pcg_type).'">';
255  print '</td></tr>';
256 
257  // Chart of acounts subtype
258  print '<tr><td>' . $langs->trans("Pcgsubtype") . '</td>';
259  print '<td>';
260  print '<input type="text" name="pcg_subtype" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$object->pcg_subtype).'">';
261  print '</td></tr>';
262 
263  print '</table>';
264 
265  dol_fiche_end();
266 
267  print '<div class="center">';
268  print '<input class="button" type="submit" value="' . $langs->trans("Save") . '">';
269  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
270  print '<input class="button" type="submit" name="cancel" value="' . $langs->trans("Cancel") . '">';
271  print '</div>';
272 
273  print '</form>';
274 }
275 else if ($id > 0 || $ref) {
276 
277  $result = $object->fetch($id, $ref, 1);
278 
279  if ($result > 0) {
280  dol_htmloutput_mesg($mesg);
281 
282  $head = accounting_prepare_head($object);
283 
284  // Edit mode
285  if ($action == 'update')
286  {
287  dol_fiche_head($head, 'card', $langs->trans('AccountAccounting'), 0, 'billr');
288 
289  print '<form name="update" action="' . $_SERVER["PHP_SELF"] . '" method="POST">' . "\n";
290  print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
291  print '<input type="hidden" name="action" value="edit">';
292  print '<input type="hidden" name="id" value="' . $id . '">';
293  print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
294 
295  print '<table class="border" width="100%">';
296 
297  // Account number
298  print '<tr><td class="titlefieldcreate"><span class="fieldrequired">' . $langs->trans("AccountNumber") . '</span></td>';
299  print '<td><input name="account_number" size="30" value="' . $object->account_number . '"</td></tr>';
300 
301  // Label
302  print '<tr><td><span class="fieldrequired">' . $langs->trans("Label") . '</span></td>';
303  print '<td><input name="label" size="70" value="' . $object->label . '"</td></tr>';
304 
305  // Account parent
306  print '<tr><td>' . $langs->trans("Accountparent") . '</td>';
307  print '<td>';
308  print $formaccounting->select_account($object->account_parent, 'account_parent', 1);
309  print '</td></tr>';
310 
311  // Category
312  print '<tr><td>'.$langs->trans("AccountingCategory").'</td>';
313  print '<td>';
314  $formaccounting->select_accounting_category($object->account_category, 'account_category', 1);
315  print '</td></tr>';
316 
317  // Chart of accounts type
318  print '<tr><td>' . $langs->trans("Pcgtype") . '</td>';
319  print '<td>';
320  print '<input type="text" name="pcg_type" value="'.dol_escape_htmltag(isset($_POST['pcg_type'])?GETPOST('pcg_type','alpha'):$object->pcg_type).'">';
321  print '</td></tr>';
322 
323  // Chart of accounts subtype
324  print '<tr><td>' . $langs->trans("Pcgsubtype") . '</td>';
325  print '<td>';
326  print '<input type="text" name="pcg_subtype" value="'.dol_escape_htmltag(isset($_POST['pcg_subtype'])?GETPOST('pcg_subtype','alpha'):$object->pcg_subtype).'">';
327  print '</td></tr>';
328 
329  print '</table>';
330 
331  dol_fiche_end();
332 
333  print '<div class="center">';
334  print '<input type="submit" class="button" value="' . $langs->trans("Save") . '">';
335  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
336  print '<input type="submit" name="cancel" class="button" value="' . $langs->trans("Cancel") . '">';
337  print '</div>';
338 
339  print '</form>';
340  } else {
341  // View mode
342  $linkback = '<a href="'.DOL_URL_ROOT.'/accountancy/admin/account.php?restore_lastsearch_values=1">' . $langs->trans("BackToList") . '</a>';
343 
344  dol_fiche_head($head, 'card', $langs->trans('AccountAccounting'), -1, 'billr');
345 
346  dol_banner_tab($object, 'ref', $linkback, 1, 'account_number', 'ref');
347 
348 
349  print '<div class="fichecenter">';
350  print '<div class="underbanner clearboth"></div>';
351 
352  print '<table class="border" width="100%">';
353 
354  // Label
355  print '<tr><td class="titlefield">' . $langs->trans("Label") . '</td>';
356  print '<td colspan="2">' . $object->label . '</td></tr>';
357 
358  // Account parent
359  $accp = new AccountingAccount($db);
360  if (! empty($object->account_parent)) {
361  $accp->fetch($object->account_parent, '');
362  }
363  print '<tr><td>' . $langs->trans("Accountparent") . '</td>';
364  print '<td colspan="2">' . $accp->account_number . ' - ' . $accp->label . '</td></tr>';
365 
366  // Category
367  print "<tr><td>".$langs->trans("AccountingCategory")."</td><td colspan='2'>".$object->account_category_label."</td>";
368 
369  // Chart of accounts type
370  print '<tr><td>' . $langs->trans("Pcgtype") . '</td>';
371  print '<td colspan="2">' . $object->pcg_type . '</td></tr>';
372 
373  // Chart of accounts subtype
374  print '<tr><td>' . $langs->trans("Pcgsubtype") . '</td>';
375  print '<td colspan="2">' . $object->pcg_subtype . '</td></tr>';
376 
377  print '</table>';
378 
379  print '</div>';
380 
381  dol_fiche_end();
382 
383  /*
384  * Actions buttons
385  */
386  print '<div class="tabsAction">';
387 
388  if (! empty($user->rights->accounting->chartofaccount)) {
389  print '<a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?action=update&id=' . $id . '">' . $langs->trans('Modify') . '</a>';
390  } else {
391  print '<a class="butActionRefused" href="#" title="' . dol_escape_htmltag($langs->trans("NotAllowed")) . '">' . $langs->trans('Modify') . '</a>';
392  }
393 
394  if (! empty($user->rights->accounting->chartofaccount)) {
395  print '<a class="butActionDelete" href="' . $_SERVER["PHP_SELF"] . '?action=delete&id=' . $id . '">' . $langs->trans('Delete') . '</a>';
396  } else {
397  print '<a class="butActionRefused" href="#" title="' . dol_escape_htmltag($langs->trans("NotAllowed")) . '">' . $langs->trans('Delete') . '</a>';
398  }
399 
400  print '</div>';
401  }
402  } else {
403  dol_print_error($db, $object->error, $object->errors);
404  }
405 }
406 
407 // End of page
408 llxFooter();
409 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
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.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
dol_fiche_end($notab=0)
Show tab footer of a card.
if($_POST["cancel"]==$langs->trans("Cancel") &&! $id) if($action=='add' && $_POST["cancel"]<> $langs->trans("Cancel")) if($action=='delete') if($id) $form
Actions.
Definition: card.php:153
Class to manage accountancy systems.
Class to manage generation of HTML components for accounting management.
accounting_prepare_head(AccountingAccount $object)
Prepare array with list of tabs.
Class to manage accounting accounts.
clean_account($account)
Return accounting account without zero on the right.
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.
dol_htmloutput_mesg($mesgstring='', $mesgarray=array(), $style='ok', $keepembedded=0)
Print formated messages to output (Used to show messages on html output).