dolibarr  9.0.0
listbyaccount.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2016 Neil Orley <neil.orley@oeris.fr>
3  * Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
4  * Copyright (C) 2013-2016 Florian Henry <florian.henry@open-concept.pro>
5  * Copyright (C) 2013-2018 Alexandre Spangaro <aspangaro@zendsi.com>
6  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
28 require '../../main.inc.php';
29 
30 require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
31 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php';
32 require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingjournal.class.php';
33 require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
34 require_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php';
35 require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
36 
37 // Load translation files required by the page
38 $langs->loadLangs(array("accountancy"));
39 
40 $page = GETPOST("page");
41 $sortorder = GETPOST("sortorder");
42 $sortfield = GETPOST("sortfield");
43 $action = GETPOST('action', 'alpha');
44 $search_date_start = dol_mktime(0, 0, 0, GETPOST('search_date_startmonth', 'int'), GETPOST('search_date_startday', 'int'), GETPOST('search_date_startyear', 'int'));
45 $search_date_end = dol_mktime(0, 0, 0, GETPOST('search_date_endmonth', 'int'), GETPOST('search_date_endday', 'int'), GETPOST('search_date_endyear', 'int'));
46 $search_doc_date = dol_mktime(0, 0, 0, GETPOST('doc_datemonth', 'int'), GETPOST('doc_dateday', 'int'), GETPOST('doc_dateyear', 'int'));
47 
48 $search_accountancy_code = GETPOST("search_accountancy_code");
49 $search_accountancy_code_start = GETPOST('search_accountancy_code_start', 'alpha');
50 if ($search_accountancy_code_start == - 1) {
51  $search_accountancy_code_start = '';
52 }
53 $search_accountancy_code_end = GETPOST('search_accountancy_code_end', 'alpha');
54 if ($search_accountancy_code_end == - 1) {
55  $search_accountancy_code_end = '';
56 }
57 $search_doc_ref = GETPOST('search_doc_ref', 'alpha');
58 $search_label_operation = GETPOST('search_label_operation', 'alpha');
59 $search_direction = GETPOST('search_direction', 'alpha');
60 $search_ledger_code = GETPOST('search_ledger_code', 'alpha');
61 $search_debit = GETPOST('search_debit', 'alpha');
62 $search_credit = GETPOST('search_credit', 'alpha');
63 
64 // Load variable for pagination
65 $limit = GETPOST('limit','int')?GETPOST('limit', 'int'):(empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)?$conf->liste_limit:$conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
66 $sortfield = GETPOST('sortfield', 'alpha');
67 $sortorder = GETPOST('sortorder', 'alpha');
68 $page = GETPOST('page','int');
69 if (empty($page) || $page < 0) { $page = 0; }
70 $offset = $limit * $page;
71 $pageprev = $page - 1;
72 $pagenext = $page + 1;
73 if ($sortorder == "") $sortorder = "ASC";
74 if ($sortfield == "") $sortfield = "t.rowid";
75 
76 if (empty($search_date_start) && empty($search_date_end)) {
77  $sql = "SELECT date_start, date_end from ".MAIN_DB_PREFIX."accounting_fiscalyear ";
78  $sql.= " where date_start < '".$db->idate(dol_now())."' and date_end > '".$db->idate(dol_now())."'";
79  $sql.= $db->plimit(1);
80  $res = $db->query($sql);
81 
82  if ($res->num_rows > 0) {
83  $fiscalYear = $db->fetch_object($res);
84  $search_date_start = strtotime($fiscalYear->date_start);
85  $search_date_end = strtotime($fiscalYear->date_end);
86  } else {
87  $month_start= ($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1);
88  $year_start = dol_print_date(dol_now(), '%Y');
89  if (dol_print_date(dol_now(), '%m') < $month_start) $year_start--; // If current month is lower that starting fiscal month, we start last year
90  $year_end = $year_start + 1;
91  $month_end = $month_start - 1;
92  if ($month_end < 1)
93  {
94  $month_end = 12;
95  $year_end--;
96  }
97  $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
98  $search_date_end = dol_get_last_day($year_end, $month_end);
99  }
100 }
101 
102 $object = new BookKeeping($db);
103 
104 /*
105  * Action
106  */
107 if (GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter.x','alpha') || GETPOST('button_removefilter','alpha')) // All tests are required to be compatible with all browsers
108 {
109  $search_doc_date = '';
110  $search_accountancy_code = '';
111  $search_accountancy_code_start = '';
112  $search_accountancy_code_end = '';
113  $search_label_account = '';
114  $search_doc_ref = '';
115  $search_label_operation = '';
116  $search_direction = '';
117  $search_ledger_code = '';
118  $search_date_start='';
119  $search_date_end='';
120  $search_date_startyear='';
121  $search_date_startmonth='';
122  $search_date_startday='';
123  $search_date_endyear='';
124  $search_date_endmonth='';
125  $search_date_endday='';
126  $search_debit = '';
127  $search_credit = '';
128 }
129 
130 // Must be after the remove filter action, before the export.
131 $param = '';
132 $filter = array ();
133 
134 if (! empty($search_date_start)) {
135  $filter['t.doc_date>='] = $search_date_start;
136  $param .= '&search_date_startmonth=' . GETPOST('search_date_startmonth', 'int') . '&search_date_startday=' . GETPOST('search_date_startday', 'int') . '&search_date_startyear=' . GETPOST('search_date_startyear', 'int');
137 }
138 if (! empty($search_date_end)) {
139  $filter['t.doc_date<='] = $search_date_end;
140  $param .= '&search_date_endmonth=' . GETPOST('search_date_endmonth', 'int') . '&search_date_endday=' . GETPOST('search_date_endday', 'int') . '&search_date_endyear=' . GETPOST('search_date_endyear', 'int');
141 }
142 if (! empty($search_doc_date)) {
143  $filter['t.doc_date'] = $search_doc_date;
144  $param .= '&doc_datemonth=' . GETPOST('doc_datemonth', 'int') . '&doc_dateday=' . GETPOST('doc_dateday', 'int') . '&doc_dateyear=' . GETPOST('doc_dateyear', 'int');
145 }
146 if (! empty($search_accountancy_code_start)) {
147  $filter['t.numero_compte>='] = $search_accountancy_code_start;
148  $param .= '&search_accountancy_code_start=' . urlencode($search_accountancy_code_start);
149 }
150 if (! empty($search_accountancy_code_end)) {
151  $filter['t.numero_compte<='] = $search_accountancy_code_end;
152  $param .= '&search_accountancy_code_end=' . urlencode($search_accountancy_code_end);
153 }
154 if (! empty($search_label_account)) {
155  $filter['t.label_compte'] = $search_label_account;
156  $param .= '&search_label_compte=' . urlencode($search_label_account);
157 }
158 if (! empty($search_doc_ref)) {
159  $filter['t.doc_ref'] = $search_doc_ref;
160  $param .= '&search_doc_ref=' . urlencode($search_doc_ref);
161 }
162 if (! empty($search_label_operation)) {
163  $filter['t.label_operation'] = $search_label_operation;
164  $param .= '&search_label_operation=' . urlencode($search_label_operation);
165 }
166 if (! empty($search_direction)) {
167  $filter['t.sens'] = $search_direction;
168  $param .= '&search_direction=' . urlencode($search_direction);
169 }
170 if (! empty($search_ledger_code)) {
171  $filter['t.code_journal'] = $search_ledger_code;
172  $param .= '&search_ledger_code=' . urlencode($search_ledger_code);
173 }
174 if (! empty($search_debit)) {
175  $filter['t.debit'] = $search_debit;
176  $param .= '&search_debit=' . urlencode($search_debit);
177 }
178 if (! empty($search_credit)) {
179  $filter['t.credit'] = $search_credit;
180  $param .= '&search_credit=' . urlencode($search_credit);
181 }
182 
183 
184 if ($action == 'delmouvconfirm') {
185 
186  $mvt_num = GETPOST('mvt_num', 'int');
187 
188  if (! empty($mvt_num)) {
189  $result = $object->deleteMvtNum($mvt_num);
190  if ($result < 0) {
191  setEventMessages($object->error, $object->errors, 'errors');
192  }
193  Header("Location: listbyaccount.php");
194  exit();
195  }
196 }
197 
198 
199 /*
200  * View
201  */
202 
203 $formaccounting = new FormAccounting($db);
204 $formother = new FormOther($db);
205 $form = new Form($db);
206 
207 $title_page = $langs->trans("Bookkeeping") . ' ' . strtolower($langs->trans("By")) . ' ' . strtolower($langs->trans("AccountAccounting"));
208 
209 llxHeader('', $title_page);
210 
211 
212 // List
213 $nbtotalofrecords = '';
214 if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
215  $nbtotalofrecords = $object->fetchAllByAccount($sortorder, $sortfield, 0, 0, $filter);
216  if ($nbtotalofrecords < 0) {
217  setEventMessages($object->error, $object->errors, 'errors');
218  }
219 }
220 
221 $result = $object->fetchAllByAccount($sortorder, $sortfield, $limit, $offset, $filter);
222 
223 if ($result < 0) {
224  setEventMessages($object->error, $object->errors, 'errors');
225 }
226 
227 $num=count($object->lines);
228 
229 
230 if ($action == 'delmouv') {
231  $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?mvt_num=' . GETPOST('mvt_num'), $langs->trans('DeleteMvt'), $langs->trans('ConfirmDeleteMvtPartial'), 'delmouvconfirm', '', 0, 1);
232  print $formconfirm;
233 }
234 if ($action == 'delbookkeepingyear') {
235 
236  $form_question = array ();
237  $delyear = GETPOST('delyear');
238 
239  if (empty($delyear)) {
240  $delyear = dol_print_date(dol_now(), '%Y');
241  }
242  $year_array = $formaccounting->selectyear_accountancy_bookkepping($delyear, 'delyear', 0, 'array');
243 
244  $form_question['delyear'] = array (
245  'name' => 'delyear',
246  'type' => 'select',
247  'label' => $langs->trans('DelYear'),
248  'values' => $year_array,
249  'default' => $delyear
250  );
251 
252  $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans('DeleteMvt'), $langs->trans('ConfirmDeleteMvt'), 'delbookkeepingyearconfirm', $form_question, 0, 1, 250);
253  print $formconfirm;
254 }
255 
256 
257 print '<form method="POST" id="searchFormList" action="' . $_SERVER["PHP_SELF"] . '">';
258 
259 $viewflat = ' <a class="nohover marginrightonly" href="'.DOL_URL_ROOT.'/accountancy/bookkeeping/list.php?'.$param.'">' . $langs->trans("ViewFlatList") . '</a>';
260 $newcardbutton = '<a class="butActionNew" href="./card.php?action=create"><span class="valignmiddle">' . $langs->trans("NewAccountingMvt").'</span>';
261 $newcardbutton.= '<span class="fa fa-plus-circle valignmiddle"></span>';
262 $newcardbutton.= '</a>';
263 
264 if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.urlencode($contextpage);
265 if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.urlencode($limit);
266 
267 print_barre_liste($title_page, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $result, $nbtotalofrecords, 'title_accountancy', 0, $viewflat.$newcardbutton, '', $limit);
268 
269 // Reverse sort order
270 if ( preg_match('/^asc/i', $sortorder) )
271  $sortorder = "asc";
272 else
273  $sortorder = "desc";
274 
275 print '<table class="noborder" width="100%">';
276 
277 print '<tr class="liste_titre">';
278 print '<td class="liste_titre">';
279 print '<div class="nowrap">';
280 print $langs->trans('From').' ';
281 print $formaccounting->select_account($search_accountancy_code_start, 'search_accountancy_code_start', 1, array (), 1, 1, 'maxwidth200');
282 print '</div>';
283 print '<div class="nowrap">';
284 print $langs->trans('to').' ';
285 print $formaccounting->select_account($search_accountancy_code_end, 'search_accountancy_code_end', 1, array (), 1, 1, 'maxwidth200');
286 print '</div>';
287 print '</td>';
288 print '<td class="liste_titre"></td>';
289 print '<td class="liste_titre" align="center">';
290 print $langs->trans('From') . ': ';
291 print $form->selectDate($search_date_start, 'search_date_start', 0, 0, 1);
292 print '<br>';
293 print $langs->trans('to') . ': ';
294 print $form->selectDate($search_date_end, 'search_date_end', 0, 0, 1);
295 print '</td>';
296 print '<td class="liste_titre"><input type="text" size="7" class="flat" name="search_doc_ref" value="' . dol_escape_htmltag($search_doc_ref) . '"/></td>';
297 print '<td class="liste_titre"><input type="text" size="7" class="flat" name="search_label_operation" value="' . dol_escape_htmltag($search_label_operation) . '"/></td>';
298 print '<td class="liste_titre" align="right"><input type="text" class="flat" name="search_debit" size="4" value="'.dol_escape_htmltag($search_debit).'"></td>';
299 print '<td class="liste_titre" align="right"><input type="text" class="flat" name="search_credit" size="4" value="'.dol_escape_htmltag($search_credit).'"></td>';
300 print '<td class="liste_titre" align="center"><input type="text" name="search_ledger_code" size="3" value="' . dol_escape_htmltag($search_ledger_code) . '"></td>';
301 print '<td class="liste_titre" align="right" colspan="2">';
302 $searchpicto=$form->showFilterAndCheckAddButtons(0);
303 print $searchpicto;
304 print '</td>';
305 
306 print '<tr class="liste_titre">';
307 print_liste_field_titre("AccountAccountingShort", $_SERVER['PHP_SELF']);
308 print_liste_field_titre("TransactionNumShort", $_SERVER['PHP_SELF'], "t.piece_num", "", $param, 'align="right"', $sortfield, $sortorder);
309 print_liste_field_titre("Docdate", $_SERVER['PHP_SELF'], "t.doc_date", "", $param, 'align="center"', $sortfield, $sortorder);
310 print_liste_field_titre("Piece", $_SERVER['PHP_SELF'], "t.doc_ref", "", $param, "", $sortfield, $sortorder);
311 print_liste_field_titre("Label");
312 print_liste_field_titre("Debit", $_SERVER['PHP_SELF'], "t.debit", "", $param, 'align="right"', $sortfield, $sortorder);
313 print_liste_field_titre("Credit", $_SERVER['PHP_SELF'], "t.credit", "", $param, 'align="right"', $sortfield, $sortorder);
314 print_liste_field_titre("Codejournal", $_SERVER['PHP_SELF'], "t.code_journal", "", $param, 'align="center"', $sortfield, $sortorder);
315 print_liste_field_titre('', $_SERVER["PHP_SELF"], "", $param, "", 'width="60" align="center"', $sortfield, $sortorder);
316 print "</tr>\n";
317 
318 print '</tr>';
319 
320 
321 $total_debit = 0;
322 $total_credit = 0;
323 $sous_total_debit = 0;
324 $sous_total_credit = 0;
325 $displayed_account_number = null; // Start with undefined to be able to distinguish with empty
326 
327 $i=0;
328 while ($i < min($num, $limit))
329 {
330  $line = $object->lines[$i];
331 
332  $total_debit += $line->debit;
333  $total_credit += $line->credit;
334 
335  $accountg = length_accountg($line->numero_compte);
336  //if (empty($accountg)) $accountg = '-';
337 
338  // Is it a break ?
339  if ($accountg != $displayed_account_number || ! isset($displayed_account_number)) {
340 
341  // Affiche un Sous-Total par compte comptable
342  if (isset($displayed_account_number)) {
343  print '<tr class="liste_total"><td align="right" colspan="5">'.$langs->trans("SubTotal").':</td><td class="nowrap" align="right">'.price($sous_total_debit).'</td><td class="nowrap" align="right">'.price($sous_total_credit).'</td>';
344  print "<td>&nbsp;</td>\n";
345  print "<td>&nbsp;</td>\n";
346  print '</tr>';
347  }
348 
349  // Show the break account
350  $colspan = 9;
351  print "<tr>";
352  print '<td colspan="'.$colspan.'" style="font-weight:bold; border-bottom: 1pt solid black;">';
353  if (! empty($line->numero_compte) && $line->numero_compte != '-1') print length_accountg($line->numero_compte) . ' : ' . $object->get_compte_desc($line->numero_compte);
354  else print '<span class="error">'.$langs->trans("Unknown").'</span>';
355  print '</td>';
356  print '</tr>';
357 
358  $displayed_account_number = $accountg;
359  //if (empty($displayed_account_number)) $displayed_account_number='-';
360  $sous_total_debit = 0;
361  $sous_total_credit = 0;
362  }
363 
364  print '<tr class="oddeven">';
365  print '<td>&nbsp;</td>';
366  print '<td align="right"><a href="./card.php?piece_num=' . $line->piece_num . '">'.$line->piece_num.'</a></td>';
367  print '<td align="center">' . dol_print_date($line->doc_date, 'day') . '</td>';
368 
369  // TODO Add a link according to doc_type and fk_doc
370  print '<td class="nowrap">';
371  //if ($line->doc_type == 'supplier_invoice')
372  //if ($line->doc_type == 'customer_invoice')
373  print $line->doc_ref;
374  print '</td>';
375 
376  // Affiche un lien vers la facture client/fournisseur
377  $doc_ref = preg_replace('/\(.*\)/', '', $line->doc_ref);
378  print strlen(length_accounta($line->subledger_account)) == 0 ? '<td>' . $line->label_operation . '</td>' : '<td>' . $line->label_operation . '<br><span style="font-size:0.8em">(' . length_accounta($line->subledger_account) . ')</span></td>';
379 
380 
381  print '<td align="right">' . ($line->debit ? price($line->debit) :''). '</td>';
382  print '<td align="right">' . ($line->credit ? price($line->credit) : '') . '</td>';
383 
384  $accountingjournal = new AccountingJournal($db);
385  $result = $accountingjournal->fetch('',$line->code_journal);
386  $journaltoshow = (($result > 0)?$accountingjournal->getNomUrl(0,0,0,'',0) : $line->code_journal);
387  print '<td align="center">' . $journaltoshow . '</td>';
388 
389  print '<td align="center">';
390  print '<a href="'.DOL_URL_ROOT.'/accountancy/bookkeeping/card.php?piece_num=' . $line->piece_num . '">' . img_edit() . '</a>&nbsp;';
391  print '<a href="' . $_SERVER['PHP_SELF'] . '?action=delmouv&mvt_num=' . $line->piece_num . $param . '&page=' . $page . '">' . img_delete() . '</a>';
392  print '</td>';
393  print "</tr>\n";
394 
395  // Comptabilise le sous-total
396  $sous_total_debit += $line->debit;
397  $sous_total_credit += $line->credit;
398 
399  $i++;
400 }
401 
402 // Affiche un Sous-Total du dernier compte comptable affiché
403 print '<tr class="liste_total">';
404 print '<td align="right" colspan="5">'.$langs->trans("SubTotal").':</td><td class="nowrap" align="right">'.price($sous_total_debit).'</td><td class="nowrap" align="right">'.price($sous_total_credit).'</td>';
405 print "<td>&nbsp;</td>\n";
406 print "<td>&nbsp;</td>\n";
407 print '</tr>';
408 
409 
410 // Affiche le Total
411 print '<tr class="liste_total">';
412 print '<td align="right" colspan="5">'.$langs->trans("Total").':</td>';
413 print '<td align="right">';
414 print price($total_debit);
415 print '</td>';
416 print '<td align="right">';
417 print price($total_credit);
418 print '</td>';
419 print '<td colspan="2"></td>';
420 print '</tr>';
421 
422 print "</table>";
423 print '</form>';
424 
425 // End of page
426 llxFooter();
427 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
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_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
img_edit($titlealt='default', $float=0, $other='class="pictoedit"')
Show logo editer/modifier fiche.
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...
img_delete($titlealt='default', $other='class="pictodelete"')
Show delete logo.
Class to manage Ledger (General Ledger and Subledger)
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.
Classe permettant la generation de composants html autre Only common components are here...
llxHeader()
Empty header.
Definition: wrapper.php:44
length_accounta($accounta)
Return Auxiliary accounting account of thirdparties with defined length.
dol_now($mode='gmt')
Return date for now.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:467
Class to manage generation of HTML components for accounting management.
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="")
Show title line of an array.
Class to manage accounting accounts.
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous) ...