dolibarr  7.0.0-beta
account.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
3  * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@zendsi.com>
4  * Copyright (C) 2016-2017 Laurent Destailleur <eldy@users.sourceforge.net>
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/admin.lib.php';
28 require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
29 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
30 
31 // Langs
32 $langs->load("compta");
33 $langs->load("bills");
34 $langs->load("admin");
35 $langs->load("accountancy");
36 $langs->load("salaries");
37 
38 $mesg = '';
39 $action = GETPOST('action','aZ09');
40 $cancel = GETPOST('cancel','alpha');
41 $id = GETPOST('id', 'int');
42 $rowid = GETPOST('rowid', 'int');
43 
44 $search_account = GETPOST("search_account");
45 $search_label = GETPOST("search_label");
46 $search_accountparent = GETPOST("search_accountparent");
47 $search_pcgtype = GETPOST("search_pcgtype");
48 $search_pcgsubtype = GETPOST("search_pcgsubtype");
49 
50 // Security check
51 if ($user->societe_id > 0) accessforbidden();
52 if (! $user->rights->accounting->chartofaccount) accessforbidden();
53 
54 // Load variable for pagination
55 $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
56 $sortfield = GETPOST('sortfield','alpha');
57 $sortorder = GETPOST('sortorder','alpha');
58 $page = GETPOST('page','int');
59 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
60 $offset = $limit * $page;
61 $pageprev = $page - 1;
62 $pagenext = $page + 1;
63 if (! $sortfield) $sortfield = "aa.account_number";
64 if (! $sortorder) $sortorder = "ASC";
65 
66 $arrayfields=array(
67  'aa.account_number'=>array('label'=>$langs->trans("AccountNumber"), 'checked'=>1),
68  'aa.label'=>array('label'=>$langs->trans("Label"), 'checked'=>1),
69  'aa.account_parent'=>array('label'=>$langs->trans("Accountparent"), 'checked'=>0),
70  'aa.pcg_type'=>array('label'=>$langs->trans("Pcgtype"), 'checked'=>1, 'help'=>'PcgtypeDesc'),
71  'aa.pcg_subtype'=>array('label'=>$langs->trans("Pcgsubtype"), 'checked'=>1, 'help'=>'PcgtypeDesc'),
72  'aa.active'=>array('label'=>$langs->trans("Activated"), 'checked'=>1)
73 );
74 
75 $accounting = new AccountingAccount($db);
76 
77 // Initialize technical object to manage context to save list fields
78 $contextpage=GETPOST('contextpage','aZ')?GETPOST('contextpage','aZ'):'accountingaccountlist';
79 
80 
81 /*
82  * Actions
83  */
84 
85 if (GETPOST('cancel','alpha')) { $action='list'; $massaction=''; }
86 if (! GETPOST('confirmmassaction','alpha')) { $massaction=''; }
87 
88 $parameters=array();
89 $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
90 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
91 
92 if (empty($reshook))
93 {
94  if (! empty($cancel)) $action = '';
95 
96  include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
97 
98  if (GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter.x','alpha') ||GETPOST('button_removefilter','alpha')) // All test are required to be compatible with all browsers
99  {
100  $search_account = "";
101  $search_label = "";
102  $search_accountparent = "";
103  $search_pcgtype = "";
104  $search_pcgsubtype = "";
105  $search_array_options=array();
106  }
107 
108  if (GETPOST('change_chart','alpha'))
109  {
110  $chartofaccounts = GETPOST('chartofaccounts', 'int');
111 
112  if ($chartofaccounts > 0)
113  {
114  // Get language code for this $chartofaccounts
115  $sql ='SELECT code FROM '.MAIN_DB_PREFIX.'c_country as c, '.MAIN_DB_PREFIX.'accounting_system as a';
116  $sql.=' WHERE c.rowid = a.fk_country AND a.rowid = '.(int) $chartofaccounts;
117  $resql = $db->query($sql);
118  if ($resql)
119  {
120  $obj = $db->fetch_object($resql);
121  $country_code = $obj->code;
122  }
123  else dol_print_error($db);
124 
125  // Try to load sql file
126  if ($country_code)
127  {
128  $sqlfile = DOL_DOCUMENT_ROOT.'/install/mysql/data/llx_accounting_account_'.strtolower($country_code).'.sql';
129  $result = run_sql($sqlfile, 1, 0, 1);
130  }
131 
132  if (! dolibarr_set_const($db, 'CHARTOFACCOUNTS', $chartofaccounts, 'chaine', 0, '', $conf->entity)) {
133  $error++;
134  }
135  } else {
136  $error ++;
137  }
138  }
139 
140  if ($action == 'disable') {
141  if ($accounting->fetch($id)) {
142  $result = $accounting->account_desactivate($id);
143  }
144 
145  $action = 'update';
146  if ($result < 0) {
147  setEventMessages($accounting->error, $accounting->errors, 'errors');
148  }
149  } else if ($action == 'enable') {
150  if ($accounting->fetch($id)) {
151  $result = $accounting->account_activate($id);
152  }
153  $action = 'update';
154  if ($result < 0) {
155  setEventMessages($accounting->error, $accounting->errors, 'errors');
156  }
157  }
158 }
159 
160 
161 /*
162  * View
163  */
164 
165 $form=new Form($db);
166 
167 llxHeader('', $langs->trans("ListAccounts"));
168 
169 if ($action == 'delete') {
170  $formconfirm = $html->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $id, $langs->trans('DeleteAccount'), $langs->trans('ConfirmDeleteAccount'), 'confirm_delete', '', 0, 1);
171  print $formconfirm;
172 }
173 
174 $pcgver = $conf->global->CHARTOFACCOUNTS;
175 
176 $sql = "SELECT aa.rowid, aa.fk_pcg_version, aa.pcg_type, aa.pcg_subtype, aa.account_number, aa.account_parent , aa.label, aa.active, ";
177 $sql .= " a2.rowid as rowid2, a2.label as label2, a2.account_number as account_number2";
178 $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_account as aa";
179 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_system as asy ON aa.fk_pcg_version = asy.pcg_version";
180 // Dirty hack wainting that foreign key account_parent is an integer to be compared correctly with rowid
181 if ($db->type == 'pgsql') $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as a2 ON a2.rowid = CAST(aa.account_parent AS INTEGER)";
182 else $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as a2 ON a2.rowid = CAST(aa.account_parent AS UNSIGNED)";
183 $sql .= " WHERE asy.rowid = " . $pcgver;
184 
185 if (strlen(trim($search_account))) $sql .= natural_search("aa.account_number", $search_account);
186 if (strlen(trim($search_label))) $sql .= natural_search("aa.label", $search_label);
187 if (strlen(trim($search_accountparent))) $sql .= natural_search("aa.account_parent", $search_accountparent);
188 if (strlen(trim($search_pcgtype))) $sql .= natural_search("aa.pcg_type", $search_pcgtype);
189 if (strlen(trim($search_pcgsubtype))) $sql .= natural_search("aa.pcg_subtype", $search_pcgsubtype);
190 $sql .= $db->order($sortfield, $sortorder);
191 
192 // Count total nb of records
193 $nbtotalofrecords = '';
194 if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
195 {
196  $resql = $db->query($sql);
197  $nbtotalofrecords = $db->num_rows($resql);
198 }
199 
200 $sql .= $db->plimit($limit + 1, $offset);
201 
202 dol_syslog('accountancy/admin/account.php:: $sql=' . $sql);
203 $resql = $db->query($sql);
204 
205 if ($resql)
206 {
207  $num = $db->num_rows($resql);
208 
209  $param='';
210  if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
211  if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
212  if ($search_account) $param.= '&search_account='.urlencode($search_account);
213  if ($search_label) $param.= '&search_label='.urlencode($search_label);
214  if ($search_accountparent) $param.= '&search_accountparent='.urlencode($search_accountparent);
215  if ($search_pcgtype) $param.= '&search_pcgtype='.urlencode($search_pcgtype);
216  if ($search_pcgsubtype) $param.= '&search_pcgsubtype='.urlencode($search_pcgsubtype);
217  if ($optioncss != '') $param.='&optioncss='.$optioncss;
218 
219 
220  print '<form method="POST" id="searchFormList" action="' . $_SERVER["PHP_SELF"] . '">';
221  if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
222  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
223  print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
224  print '<input type="hidden" name="action" value="list">';
225  print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
226  print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
227  print '<input type="hidden" name="page" value="'.$page.'">';
228  print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
229 
230  $htmlbuttonadd = '<a class="butAction" href="./card.php?action=create">' . $langs->trans("Addanaccount") . '</a>';
231 
232  print_barre_liste($langs->trans('ListAccounts'), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'title_accountancy', 0, $htmlbuttonadd, '', $limit);
233 
234  // Box to select active chart of account
235  print $langs->trans("Selectchartofaccounts") . " : ";
236  print '<select class="flat" name="chartofaccounts" id="chartofaccounts">';
237  $sql = "SELECT a.rowid, a.pcg_version, a.label, a.active, c.code as country_code";
238  $sql .= " FROM " . MAIN_DB_PREFIX . "accounting_system as a";
239  $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_country as c ON a.fk_country = c.rowid";
240  $sql .= " WHERE a.active = 1";
241  dol_syslog('accountancy/admin/account.php $sql='.$sql);
242  print $sql;
243  $resqlchart = $db->query($sql);
244  if ($resqlchart) {
245  $numbis = $db->num_rows($resqlchart);
246  $i = 0;
247  while ($i < $numbis) {
248  $obj = $db->fetch_object($resqlchart);
249 
250  print '<option value="' . $obj->rowid . '"';
251  print ($pcgver == $obj->rowid) ? ' selected' : '';
252  print '>' . $obj->pcg_version . ' - ' . $obj->label . ' - (' . $obj->country_code . ')</option>';
253 
254  $i++;
255  }
256  }
257  else dol_print_error($db);
258  print "</select>";
259  print ajax_combobox("chartofaccounts");
260  print '<input type="submit" class="button" name="change_chart" value="'.dol_escape_htmltag($langs->trans("ChangeAndLoad")).'">';
261 
262  print '<br>';
263  print '<br>';
264 
265  $varpage=empty($contextpage)?$_SERVER["PHP_SELF"]:$contextpage;
266  $selectedfields=$form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
267 
268  print '<div class="div-table-responsive">';
269  print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
270 
271  // Line for search fields
272  print '<tr class="liste_titre_filter">';
273  if (! empty($arrayfields['aa.account_number']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" size="10" name="search_account" value="' . $search_account . '"></td>';
274  if (! empty($arrayfields['aa.label']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" size="20" name="search_label" value="' . $search_label . '"></td>';
275  if (! empty($arrayfields['aa.account_parent']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" size="10" name="search_accountparent" value="' . $search_accountparent . '"></td>';
276  if (! empty($arrayfields['aa.pcg_type']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" size="6" name="search_pcgtype" value="' . $search_pcgtype . '"></td>';
277  if (! empty($arrayfields['aa.pcg_subtype']['checked'])) print '<td class="liste_titre"><input type="text" class="flat" size="6" name="search_pcgsubtype" value="' . $search_pcgsubtype . '"></td>';
278  if (! empty($arrayfields['aa.active']['checked'])) print '<td class="liste_titre">&nbsp;</td>';
279  print '<td align="right" colspan="2" class="liste_titre">';
280  $searchpicto=$form->showFilterAndCheckAddButtons($massactionbutton?1:0, 'checkforselect', 1);
281  print $searchpicto;
282  print '</td>';
283  print '</tr>';
284 
285  print '<tr class="liste_titre">';
286  if (! empty($arrayfields['aa.account_number']['checked'])) print_liste_field_titre($arrayfields['aa.account_number']['label'], $_SERVER["PHP_SELF"],"aa.account_number","",$param,'',$sortfield,$sortorder);
287  if (! empty($arrayfields['aa.label']['checked'])) print_liste_field_titre($arrayfields['aa.label']['label'], $_SERVER["PHP_SELF"],"aa.label","",$param,'',$sortfield,$sortorder);
288  if (! empty($arrayfields['aa.account_parent']['checked'])) print_liste_field_titre($arrayfields['aa.account_parent']['label'], $_SERVER["PHP_SELF"],"aa.account_parent", "", $param,'align="left"',$sortfield,$sortorder);
289  if (! empty($arrayfields['aa.pcg_type']['checked'])) print_liste_field_titre($arrayfields['aa.pcg_type']['label'],$_SERVER["PHP_SELF"],'aa.pcg_type','',$param,'',$sortfield,$sortorder,'',$arrayfields['aa.pcg_type']['help']);
290  if (! empty($arrayfields['aa.pcg_subtype']['checked'])) print_liste_field_titre($arrayfields['aa.pcg_subtype']['label'],$_SERVER["PHP_SELF"],'aa.pcg_subtype','',$param,'',$sortfield,$sortorder,'',$arrayfields['aa.pcg_subtype']['help']);
291  if (! empty($arrayfields['aa.active']['checked'])) print_liste_field_titre($arrayfields['aa.active']['label'],$_SERVER["PHP_SELF"],'aa.active','',$param,'',$sortfield,$sortorder);
292  print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"],"",'','','align="center"',$sortfield,$sortorder,'maxwidthsearch ');
293  print "</tr>\n";
294 
295  $accountstatic = new AccountingAccount($db);
296  $accountparent = new AccountingAccount($db);
297 
298  $i=0;
299  while ($i < min($num, $limit))
300  {
301  $obj = $db->fetch_object($resql);
302 
303  $accountstatic->id = $obj->rowid;
304  $accountstatic->label = $obj->label;
305  $accountstatic->account_number = $obj->account_number;
306 
307  print '<tr class="oddeven">';
308 
309  // Account number
310  if (! empty($arrayfields['aa.account_number']['checked']))
311  {
312  print "<td>";
313  print $accountstatic->getNomUrl(1);
314  print "</td>\n";
315  if (! $i) $totalarray['nbfield']++;
316  }
317 
318  // Account label
319  if (! empty($arrayfields['aa.label']['checked']))
320  {
321  print "<td>";
322  print $obj->label;
323  print "</td>\n";
324  if (! $i) $totalarray['nbfield']++;
325  }
326 
327  // Account parent
328  if (! empty($arrayfields['aa.account_parent']['checked']))
329  {
330  if (! empty($obj->account_parent))
331  {
332  $accountparent->id = $obj->rowid2;
333  $accountparent->label = $obj->label2;
334  $accountparent->account_number = $obj->account_number2;
335 
336  print "<td>";
337  print $accountparent->getNomUrl(1);
338  print "</td>\n";
339  if (! $i) $totalarray['nbfield']++;
340  }
341  else
342  {
343  print '<td>&nbsp;</td>';
344  if (! $i) $totalarray['nbfield']++;
345  }
346  }
347 
348  // Chart of accounts type
349  if (! empty($arrayfields['aa.pcg_type']['checked']))
350  {
351  print "<td>";
352  print $obj->pcg_type;
353  print "</td>\n";
354  if (! $i) $totalarray['nbfield']++;
355  }
356 
357  // Chart of accounts subtype
358  if (! empty($arrayfields['aa.pcg_subtype']['checked']))
359  {
360  print "<td>";
361  print $obj->pcg_subtype;
362  print "</td>\n";
363  if (! $i) $totalarray['nbfield']++;
364  }
365 
366  // Activated or not
367  if (! empty($arrayfields['aa.active']['checked']))
368  {
369  print '<td>';
370  if (empty($obj->active)) {
371  print '<a href="' . $_SERVER["PHP_SELF"] . '?id=' . $obj->rowid . '&action=enable">';
372  print img_picto($langs->trans("Disabled"), 'switch_off');
373  print '</a>';
374  } else {
375  print '<a href="' . $_SERVER["PHP_SELF"] . '?id=' . $obj->rowid . '&action=disable">';
376  print img_picto($langs->trans("Activated"), 'switch_on');
377  print '</a>';
378  }
379  print '</td>';
380  if (! $i) $totalarray['nbfield']++;
381  }
382 
383  // Action
384  print '<td align="center">';
385  if ($user->rights->accounting->chartofaccount) {
386  print '<a href="./card.php?action=update&id=' . $obj->rowid . '&backtopage='.urlencode($_SERVER["PHP_SELF"].'?chartofaccounts='.$object->id).'">';
387  print img_edit();
388  print '</a>';
389  print '&nbsp;';
390  print '<a href="./card.php?action=delete&id=' . $obj->rowid . '&backtopage='.urlencode($_SERVER["PHP_SELF"].'?chartofaccounts='.$object->id). '">';
391  print img_delete();
392  print '</a>';
393  }
394  print '</td>' . "\n";
395  if (! $i) $totalarray['nbfield']++;
396 
397  print "</tr>\n";
398  $i++;
399  }
400 
401  print "</table>";
402  print "</div>";
403  print '</form>';
404 } else {
405  dol_print_error($db);
406 }
407 
408 llxFooter();
409 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:392
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
Definition: admin.lib.php:485
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
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
print_barre_liste($titre, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='title_generic.png', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0)
Print a title with navigation controls for pagination.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
llxHeader()
Empty header.
Definition: wrapper.php:46
natural_search($fields, $value, $mode=0, $nofirstand=0)
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
img_delete($titlealt= 'default', $other= 'class="pictodelete"')
Show delete logo.
print
Draft customers invoices.
Definition: index.php:91
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="")
Show title line of an array.
run_sql($sqlfile, $silent=1, $entity='', $usesavepoint=1, $handler='', $okerror='default')
Launch a sql file.
Definition: admin.lib.php:126
Class to manage accounting accounts.
img_edit($titlealt= 'default', $float=0, $other= 'class="pictoedit"')
Show logo editer/modifier fiche.