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