-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathresult.php
More file actions
751 lines (659 loc) · 28 KB
/
result.php
File metadata and controls
751 lines (659 loc) · 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
<?php
/* Copyright (C) 2016-2017 Jamal Elbaz <jamelbaz@gmail.com>
* Copyright (C) 2016-2022 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2018-2020 Laurent Destailleur <eldy@destailleur.fr>
* Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/compta/resultat/result.php
* \ingroup compta, accountancy
* \brief Page for accounting result
*/
// Load Dolibarr environment
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancycategory.class.php';
require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancyreport.class.php';
/**
* @var Conf $conf
* @var DoliDB $db
* @var HookManager $hookmanager
* @var Societe $mysoc
* @var Translate $langs
* @var User $user
*/
// Load translation files required by the page
$langs->loadLangs(array('compta', 'bills', 'donation', 'salaries', 'accountancy'));
$id_report = GETPOSTINT('id_report');
if ($id_report <= 0) {
$id_report = 1;
}
$error = 0;
$mesg = '';
$action = GETPOST('action', 'aZ09');
$cat_id = GETPOST('account_category');
$selectcpt = GETPOST('cpt_bk');
$id = GETPOSTINT('id');
$rowid = GETPOSTINT('rowid');
$cancel = GETPOST('cancel', 'alpha');
$showaccountdetail = GETPOST('showaccountdetail', 'aZ09') ? GETPOST('showaccountdetail', 'aZ09') : 'no';
$date_startmonth = GETPOSTINT('date_startmonth');
$date_startday = GETPOSTINT('date_startday');
$date_startyear = GETPOSTINT('date_startyear');
$date_endmonth = GETPOSTINT('date_endmonth');
$date_endday = GETPOSTINT('date_endday');
$date_endyear = GETPOSTINT('date_endyear');
$nbofyear = 1;
// Change this to test different cases of setup
//$conf->global->SOCIETE_FISCAL_MONTH_START = 7;
// Date range
$year = GETPOSTINT('year'); // year with current month, is the month of the period we must show
if (empty($year)) {
$year_current = (int) dol_print_date(dol_now('gmt'), "%Y", 'gmt');
$month_current = (int) dol_print_date(dol_now(), "%m");
$year_start = $year_current - ($nbofyear - 1);
} else {
$year_current = $year;
$month_current = (int) dol_print_date(dol_now(), "%m");
$year_start = $year - ($nbofyear - 1);
}
$date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
$date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
// We define date_start and date_end
if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
$q = GETPOSTINT("q") ? GETPOSTINT("q") : 0;
if ($q == 0) {
// We define date_start and date_end
$year_end = $year_start + ($nbofyear - 1);
$month_start = GETPOSTINT("month") ? GETPOSTINT("month") : getDolGlobalInt('SOCIETE_FISCAL_MONTH_START', 1);
$date_startmonth = $month_start;
if (!GETPOST('month')) {
if (!$year && $month_start > $month_current) {
$year_start--;
$year_end--;
}
$month_end = $month_start - 1;
if ($month_end < 1) {
$month_end = 12;
} else {
$year_end++;
}
} else {
$month_end = $month_start;
}
$date_start = dol_get_first_day($year_start, $month_start, false);
$date_end = dol_get_last_day($year_end, $month_end, false);
}
if ($q == 1) {
$date_start = dol_get_first_day($year_start, 1, false);
$date_end = dol_get_last_day($year_start, 3, false);
}
if ($q == 2) {
$date_start = dol_get_first_day($year_start, 4, false);
$date_end = dol_get_last_day($year_start, 6, false);
}
if ($q == 3) {
$date_start = dol_get_first_day($year_start, 7, false);
$date_end = dol_get_last_day($year_start, 9, false);
}
if ($q == 4) {
$date_start = dol_get_first_day($year_start, 10, false);
$date_end = dol_get_last_day($year_start, 12, false);
}
}
if (($date_start < dol_time_plus_duree($date_end, -1, 'y')) || ($date_start > $date_end)) {
$date_end = dol_time_plus_duree($date_start - 1, 1, 'y');
}
// $date_start and $date_end are defined. We force $start_year and $nbofyear
$tmps = dol_getdate($date_start);
$start_year = $tmps['year'];
$start_month = $tmps['mon'];
$tmpe = dol_getdate($date_end);
$year_end = $tmpe['year'];
$month_end = $tmpe['mon'];
$nbofyear = ($year_end - $start_year) + 1;
$date_startmonth = $start_month;
$date_endmonth = $month_end;
$date_start_previous = dol_time_plus_duree($date_start, -1, 'y');
$date_end_previous = dol_time_plus_duree($date_end, -1, 'y');
//var_dump($date_start." ".$date_end." ".$date_start_previous." ".$date_end_previous." ".$nbofyear);
if ($cat_id == 0) {
$cat_id = null;
}
// Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES' or 'BOOKKEEPING')
$modecompta = getDolGlobalString('ACCOUNTING_MODE');
if (isModEnabled('accounting')) {
$modecompta = 'BOOKKEEPING';
}
if (GETPOST("modecompta", 'alpha')) {
$modecompta = GETPOST("modecompta", 'alpha');
}
$AccCat = new AccountancyCategory($db);
// Security check
$socid = GETPOSTINT('socid');
if ($user->socid > 0) {
$socid = $user->socid;
}
$hookmanager->initHooks(['resultreportlist']);
if (isModEnabled('comptabilite')) {
$result = restrictedArea($user, 'compta', '', '', 'resultat');
}
if (isModEnabled('accounting')) {
$result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
}
/*
* View
*/
$months = array(
$langs->trans("MonthShort01"),
$langs->trans("MonthShort02"),
$langs->trans("MonthShort03"),
$langs->trans("MonthShort04"),
$langs->trans("MonthShort05"),
$langs->trans("MonthShort06"),
$langs->trans("MonthShort07"),
$langs->trans("MonthShort08"),
$langs->trans("MonthShort09"),
$langs->trans("MonthShort10"),
$langs->trans("MonthShort11"),
$langs->trans("MonthShort12"),
);
llxHeader('', $langs->trans('ReportInOut'));
$builddate = 0;
$name = '';
$period = '';
$calcmode = 0;
$description = '';
$form = new Form($db);
$textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($start_year - 1).'&showaccountdetail='.urlencode($showaccountdetail).'">'.img_previous().'</a>';
$textnextyear = ' <a href="'.$_SERVER["PHP_SELF"].'?year='.($start_year + 1).'&showaccountdetail='.urlencode($showaccountdetail).'">'.img_next().'</a>';
// Affiche en-tete de rapport
if ($modecompta == "CREANCES-DETTES") {
$name = $langs->trans("AnnualByAccountDueDebtMode");
$calcmode = $langs->trans("CalcModeDebt");
$calcmode .= '<br>('.$langs->trans("SeeReportInInputOutputMode", '<a href="'.$_SERVER["PHP_SELF"].'?year='.$start_year.(GETPOST("month") > 0 ? '&month='.GETPOST("month") : '').'&modecompta=RECETTES-DEPENSES">', '</a>').')';
if (isModEnabled('accounting')) {
$calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '<a href="'.$_SERVER["PHP_SELF"].'?year='.$start_year.'&modecompta=BOOKKEEPING">', '</a>').')';
}
$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
//$periodlink='<a href="'.$_SERVER["PHP_SELF"].'?year='.($year-1).'&modecompta='.$modecompta.'">'.img_previous().'</a> <a href="'.$_SERVER["PHP_SELF"].'?year='.($year+1).'&modecompta='.$modecompta.'">'.img_next().'</a>';
$description = $langs->trans("RulesResultDue");
if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
$description .= $langs->trans("DepositsAreNotIncluded");
} else {
$description .= $langs->trans("DepositsAreIncluded");
}
if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
$description .= $langs->trans("SupplierDepositsAreNotIncluded");
}
$builddate = dol_now();
//$exportlink=$langs->trans("NotYetAvailable");
} elseif ($modecompta == "RECETTES-DEPENSES") {
$name = $langs->trans("AnnualByAccountInputOutputMode");
$calcmode = $langs->trans("CalcModePayment");
$calcmode .= '<br>('.$langs->trans("SeeReportInDueDebtMode", '<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.(GETPOST("month") > 0 ? '&month='.GETPOST("month") : '').'&modecompta=CREANCES-DETTES">', '</a>').')';
if (isModEnabled('accounting')) {
$calcmode .= '<br>('.$langs->trans("SeeReportInBookkeepingMode", '<a href="'.$_SERVER["PHP_SELF"].'?year='.$year.'&modecompta=BOOKKEEPING">', '</a>').')';
}
$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
//$periodlink='<a href="'.$_SERVER["PHP_SELF"].'?year='.($year-1).'&modecompta='.$modecompta.'">'.img_previous().'</a> <a href="'.$_SERVER["PHP_SELF"].'?year='.($year+1).'&modecompta='.$modecompta.'">'.img_next().'</a>';
$description = $langs->trans("RulesResultInOut");
$builddate = dol_now();
//$exportlink=$langs->trans("NotYetAvailable");
} elseif ($modecompta == "BOOKKEEPING") {
$accountingreportstatic = new AccountancyReport($db);
$accountingreportstatic->fetch($id_report);
$reportLabel = '';
$reportLabel = $accountingreportstatic->label;
$name = $langs->trans("ReportInOut").', '.$langs->trans("ByPersonalizedAccountGroups").' '.$reportLabel;
$calcmode = $langs->trans("CalcModeBookkeeping");
//$calcmode.='<br>('.$langs->trans("SeeReportInDueDebtMode",'<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=CREANCES-DETTES">','</a>').')';
//$calcmode.='<br>('.$langs->trans("SeeReportInInputOutputMode",'<a href="'.$_SERVER["PHP_SELF"].'?year_start='.$year_start.'&modecompta=RECETTES-DEPENSES">','</a>').')';
$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
$arraylist = array('no' => $langs->trans("None"), 'yes' => $langs->trans("AccountWithNonZeroValues"), 'all' => $langs->trans("All"));
$period .= ' <span class="opacitymedium">'.$langs->trans("DetailBy").'</span> '.$form->selectarray('showaccountdetail', $arraylist, $showaccountdetail, 0);
$periodlink = $textprevyear.$textnextyear;
$exportlink = '';
$description = $langs->trans("RulesResultBookkeepingPersonalized");
$description .= ' ('.$langs->trans("SeePageForSetup", DOL_URL_ROOT.'/accountancy/admin/categories_list.php?search_country_id='.$mysoc->country_id.'&mainmenu=accountancy&leftmenu=accountancy_admin', $langs->transnoentitiesnoconv("Accountancy").' / '.$langs->transnoentitiesnoconv("Setup").' / '.$langs->transnoentitiesnoconv("AccountingCategory")).')';
$builddate = dol_now();
}
report_header($name, '', $period, $periodlink ?? '', $description, $builddate, $exportlink ?? '', array('modecompta' => $modecompta, 'action' => ''), $calcmode);
if (isModEnabled('accounting') && $modecompta != 'BOOKKEEPING') {
print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, '1');
}
$moreforfilter = '';
print '<div class="div-table-responsive">';
print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
print '<tr class="liste_titre">';
print '<th class="liste_titre">'.$langs->trans("AccountingCategory").'</th>';
print '<th class="liste_titre"></th>';
print '<th class="liste_titre right">'.$langs->trans("PreviousPeriod").'</th>';
print '<th class="liste_titre right">'.$langs->trans("SelectedPeriod").'</th>';
foreach ($months as $k => $v) {
if (($k + 1) >= $date_startmonth && (($date_startmonth <= $date_endmonth && ($k + 1) <= $date_endmonth) || ($date_startmonth > $date_endmonth))) {
print '<th class="liste_titre right width50">'.$langs->trans('MonthShort'.sprintf("%02d", ($k + 1))).'</th>';
}
}
if ($date_startmonth > $date_endmonth) {
foreach ($months as $k => $v) {
if (($k + 1) < $date_startmonth && ($k + 1) <= $date_endmonth) {
print '<th class="liste_titre right width50">'.$langs->trans('MonthShort'.sprintf("%02d", ($k + 1))).'</th>';
}
}
}
print '</tr>';
if ($modecompta == 'CREANCES-DETTES') {
//if (!empty($date_start) && !empty($date_end))
// $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
} elseif ($modecompta == "RECETTES-DEPENSES") {
//if (!empty($date_start) && !empty($date_end))
// $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
} elseif ($modecompta == "BOOKKEEPING") {
// Get array of all report groups that are active
$cats = $AccCat->getCats(-1, 1, $id_report); // WARNING: Computed groups must be after group they include
$unactive_cats = $AccCat->getCats(-1, 0, $id_report);
/*
$sql = 'SELECT DISTINCT t.numero_compte as nb FROM '.MAIN_DB_PREFIX.'accounting_bookkeeping as t, '.MAIN_DB_PREFIX.'accounting_account as aa';
$sql.= " WHERE t.numero_compte = aa.account_number AND aa.fk_accounting_category = 0";
if (!empty($date_start) && !empty($date_end))
$sql.= " AND t.doc_date >= '".$db->idate($date_start)."' AND t.doc_date <= '".$db->idate($date_end)."'";
if (!empty($month)) {
$sql .= " AND MONTH(t.doc_date) = " . ((int) $month);
}
$resql = $db->query($sql);
if ($resql)
{
$num_rows = $db->num_rows($resql);
if ($num_rows) {
print '<div class="warning">Warning: There is '.$num_rows.' accounts in your ledger table that are not set into a reporting group</div>';
$i = 0;
//while ($i < $num) {
// $obj = $db->fetch_object($resql);
// $i++;
//}
}
}
else dol_print_error($db);
*/
$j = 1;
$sommes = array();
$totPerAccount = array();
if (!is_array($cats) && $cats < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
} elseif (is_array($cats) && count($cats) == 0) {
print '<tr class="liste_total">';
print '<td colspan="15">';
print '<span class="opacitymedium">';
print $langs->trans("ErrorNoAccountingCategoryForThisCountry", $mysoc->country_code, $langs->transnoentitiesnoconv("Accountancy"), $langs->transnoentitiesnoconv("Setup"), $langs->transnoentitiesnoconv("AccountingCategory"));
print '</span>';
print '</td>';
print '</tr>';
} elseif (is_array($cats) && count($cats) > 0) {
// Loop on each custom group of accounts
foreach ($cats as $cat) {
if (!empty($cat['category_type'])) {
// category calculated
// When we enter here, $sommes was filled by group of accounts
$formula = $cat['formula'];
print '<tr class="liste_total">';
// Code and Label
print '<td class="liste_total tdoverflowmax100" title="'.dol_escape_htmltag($cat['code']).'">';
print dol_escape_htmltag($cat['code']);
print '</td><td class="tdoverflowmax250 borderright" title="'.dol_escape_htmltag($cat['label']).'">';
print dol_escape_htmltag($cat['label']);
print '</td>';
$vars = array();
// Unactive categories have a total of 0 to be used in the formula.
foreach ($unactive_cats as $un_cat) {
$vars[$un_cat['code']] = 0;
}
$code = null;
// Previous Fiscal year (N-1)
foreach ($sommes as $code_idx => $det) {
$code = $code_idx;
$vars[$code_idx] = empty($det['NP']) ? 0 : $det['NP'];
}
$result = strtr($formula, $vars);
$result = str_replace('--', '+', $result);
if (preg_match('/[a-z]/i', $result)) {
$r = 'Error bad formula: '.$result;
$rshort = 'Err';
print '<td class="liste_total right"><span class="amount" title="'.dol_escape_htmltag($r).'">'.$rshort.'</span></td>';
} else {
//var_dump($result);
//$r = $AccCat->calculate($result);
$r = (float) dol_eval((string) $result, 1, 1, '1');
if (getDolGlobalInt('ACCOUNTANCY_TRUNC_DECIMAL_ON_BALANCE_REPORT')) {
print '<td class="liste_total right"><span class="amount">'.price($r, 0, '', 1, 0, 0).'</span></td>';
} else {
print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
}
}
if ($code !== null && !isset($sommes[$code])) {
$sommes[$code] = array();
}
// Year N
$code = $cat['code']; // code of categorie ('VTE', 'MAR', ...)
if (empty($sommes[$code]['NP'])) {
$sommes[$code]['NP'] = $r;
} else {
$sommes[$code]['NP'] += $r;
}
// Current fiscal year (N)
if (!empty($sommes)) {
foreach ($sommes as $code_idx => $det) {
$code = $code_idx;
$vars[$code_idx] = empty($det['N']) ? 0 : $det['N'];
}
}
$result = strtr($formula, $vars);
$result = str_replace('--', '+', $result);
//$r = $AccCat->calculate($result);
$r = (float) dol_eval((string) $result, 1, 1, '1');
if (getDolGlobalInt('ACCOUNTANCY_TRUNC_DECIMAL_ON_BALANCE_REPORT')) {
print '<td class="liste_total right borderright"><span class="amount">'.price($r, 0, '', 1, 0, 0).'</span></td>';
} else {
print '<td class="liste_total right borderright"><span class="amount">'.price($r).'</span></td>';
}
if (empty($sommes[$code]['N'])) {
$sommes[$code]['N'] = $r;
} else {
$sommes[$code]['N'] += $r;
}
// Detail by month
foreach ($months as $k => $v) {
if (($k + 1) >= $date_startmonth && (($date_startmonth <= $date_endmonth && ($k + 1) <= $date_endmonth) || ($date_startmonth > $date_endmonth))) {
foreach ($sommes as $code_idx => $det) {
$code = $code_idx;
$vars[$code_idx] = empty($det['M'][$k]) ? 0 : $det['M'][$k];
}
$result = strtr($formula, $vars);
$result = str_replace('--', '+', $result);
//$r = $AccCat->calculate($result);
$r = (float) dol_eval((string) $result, 1, 1, '1');
if (getDolGlobalInt('ACCOUNTANCY_TRUNC_DECIMAL_ON_BALANCE_REPORT')) {
print '<td class="liste_total right"><span class="amount">'.price($r, 0, '', 1, 0, 0).'</span></td>';
} else {
print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
}
if (empty($sommes[$code]['M'][$k])) {
$sommes[$code]['M'][$k] = $r;
} else {
$sommes[$code]['M'][$k] += $r;
}
}
}
if ($date_startmonth > $date_endmonth) {
foreach ($months as $k => $v) {
if (($k + 1) < $date_startmonth && ($k + 1) <= $date_endmonth) {
foreach ($sommes as $code_idx => $det) {
$code = $code_idx;
$vars[$code_idx] = empty($det['M'][$k]) ? 0 : $det['M'][$k];
}
$result = strtr($formula, $vars);
$result = str_replace('--', '+', $result);
//$r = $AccCat->calculate($result);
$r = (float) dol_eval((string) $result, 1, 1, '1');
if (getDolGlobalInt('ACCOUNTANCY_TRUNC_DECIMAL_ON_BALANCE_REPORT')) {
print '<td class="liste_total right"><span class="amount">'.price($r, 0, '', 1, 0, 0).'</span></td>';
} else {
print '<td class="liste_total right"><span class="amount">'.price($r).'</span></td>';
}
if (empty($sommes[$code]['M'][$k])) {
$sommes[$code]['M'][$k] = $r;
} else {
$sommes[$code]['M'][$k] += $r;
}
}
}
}
print "</tr>\n";
//var_dump($sommes);
} else { // normal category
$code = $cat['code']; // Category code we process
$totCat = array();
$totCat['NP'] = 0;
$totCat['N'] = 0;
$totCat['M'] = array();
foreach ($months as $k => $v) {
$totCat['M'][$k] = 0;
}
if (!isset($sommes[$code])) {
$sommes[$code] = array();
}
// Set $cpts with array of accounts in the category/group
$cpts = $AccCat->getCptsCat((int) $cat['rowid']);
// We should loop over empty $cpts array, else the category _code_ is used in the formula, which leads to wrong result if the code is a number.
if (empty($cpts)) {
$cpts[] = array();
}
$arrayofaccountforfilter = array();
foreach ($cpts as $i => $cpt) { // Loop on each account.
if (isset($cpt['account_number'])) {
$arrayofaccountforfilter[] = $cpt['account_number'];
}
}
// N-1
if (!empty($arrayofaccountforfilter)) {
$return = $AccCat->getSumDebitCredit($arrayofaccountforfilter, $date_start_previous, $date_end_previous, empty($cat['dc']) ? 0 : $cat['dc']);
if ($return < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
$resultNP = 0;
} else {
foreach ($cpts as $i => $cpt) { // Loop on each account found
$resultNP = empty($AccCat->sdcperaccount[$cpt['account_number']]) ? 0 : $AccCat->sdcperaccount[$cpt['account_number']];
if (empty($totCat['NP'])) {
$totCat['NP'] = $resultNP;
} else {
$totCat['NP'] += $resultNP;
}
if (empty($sommes[$code]['NP'])) {
$sommes[$code]['NP'] = $resultNP;
} else {
$sommes[$code]['NP'] += $resultNP;
}
$totPerAccount[$cpt['account_number']]['NP'] = $resultNP;
}
}
}
// Set value into column N and month M ($totCat)
// This make 12 calls for each accountancy account (12 months M)
foreach ($cpts as $i => $cpt) { // Loop on each account.
// We make 1 loop for each account because we may want detail per account.
// @todo Optimize to ask a 'group by' account and a filter with account in (..., ...) in request
// Each month
$resultN = 0;
foreach ($months as $k => $v) {
$monthtoprocess = $k + 1; // ($k+1) is month 1, 2, ..., 12
$yeartoprocess = $start_year;
if (($k + 1) < $start_month) {
$yeartoprocess++;
}
if (($yeartoprocess == $start_year && ($k + 1) >= $date_startmonth && $k < $date_endmonth) ||
($yeartoprocess == $start_year + 1 && ($k + 1) < $date_startmonth)
) {
//var_dump($monthtoprocess.'_'.$yeartoprocess);
if (isset($cpt['account_number'])) {
$return = $AccCat->getSumDebitCredit((int) $cpt['account_number'], $date_start, $date_end, empty($cat['dc']) ? 0 : $cat['dc'], 'nofilter', $monthtoprocess, $yeartoprocess);
if ($return < 0) {
setEventMessages(null, $AccCat->errors, 'errors');
$resultM = 0;
} else {
$resultM = $AccCat->sdc;
}
} else {
$resultM = 0;
}
if (empty($totCat['M'][$k])) {
$totCat['M'][$k] = $resultM;
} else {
$totCat['M'][$k] += $resultM;
}
if (empty($sommes[$code]['M'][$k])) {
$sommes[$code]['M'][$k] = $resultM;
} else {
$sommes[$code]['M'][$k] += $resultM;
}
if (isset($cpt['account_number'])) {
$totPerAccount[$cpt['account_number']]['M'][$k] = $resultM;
}
$resultN += $resultM;
}
}
if (empty($totCat)) {
$totCat['N'] = $resultN;
} else {
$totCat['N'] += $resultN;
}
if (empty($sommes[$code]['N'])) {
$sommes[$code]['N'] = $resultN;
} else {
$sommes[$code]['N'] += $resultN;
}
if (isset($cpt['account_number'])) {
$totPerAccount[$cpt['account_number']]['N'] = $resultN;
}
}
// Now output columns for row $code ('VTE', 'MAR', ...)
print '<tr'.($showaccountdetail != 'no' ? ' class="trforbreak"' : '').'>';
// Column group
print '<td class="tdoverflowmax100" title="'.dol_escape_htmltag($cat['code']).'">';
print dol_escape_htmltag($cat['code']);
print '</td>';
// Label of group
$labeltoshow = dol_escape_htmltag($cat['label']);
if (count($cpts) > 0 && !empty($cpts[0])) { // Show example of 5 first accounting accounts
$i = 0;
foreach ($cpts as $cpt) {
if ($i > 5) {
$labeltoshow .= '...)';
break;
}
if ($i > 0) {
$labeltoshow .= ', ';
} else {
$labeltoshow .= ' (';
}
$labeltoshow .= dol_escape_htmltag($cpt['account_number']);
$i++;
}
if ($i <= 5) {
$labeltoshow .= ')';
}
} else {
$labeltoshow .= ' - <span class="warning">'.$langs->trans("GroupIsEmptyCheckSetup").'</span>';
}
print '<td class="tdoverflowmax250 borderright" title="'.dol_escape_htmltag(dol_string_nohtmltag($labeltoshow)).'">';
print $labeltoshow;
print '</td>';
if (getDolGlobalInt('ACCOUNTANCY_TRUNC_DECIMAL_ON_BALANCE_REPORT')) {
print '<td class="right"><span class="amount">'.price($totCat['NP'], 0, '', 1, 0, 0).'</span></td>';
print '<td class="right borderright"><span class="amount">'.price($totCat['N'], 0, '', 1, 0, 0).'</span></td>';
} else {
print '<td class="right"><span class="amount">'.price($totCat['NP']).'</span></td>';
print '<td class="right borderright"><span class="amount">'.price($totCat['N']).'</span></td>';
}
// Each month
foreach ($totCat['M'] as $k => $v) {
if (($k + 1) >= $date_startmonth && (($date_startmonth <= $date_endmonth && ($k + 1) <= $date_endmonth) || ($date_startmonth > $date_endmonth))) {
if (getDolGlobalInt('ACCOUNTANCY_TRUNC_DECIMAL_ON_BALANCE_REPORT')) {
print '<td class="right nowraponall"><span class="amount">'.price($v, 0, '', 1, 0, 0).'</span></td>';
} else {
print '<td class="right nowraponall"><span class="amount">'.price($v).'</span></td>';
}
}
}
if ($date_startmonth > $date_endmonth) {
foreach ($totCat['M'] as $k => $v) {
if (($k + 1) < $date_startmonth && ($k + 1) <= $date_endmonth) {
if (getDolGlobalInt('ACCOUNTANCY_TRUNC_DECIMAL_ON_BALANCE_REPORT')) {
print '<td class="right nowraponall"><span class="amount">'.price($v, 0, '', 1, 0, 0).'</span></td>';
} else {
print '<td class="right nowraponall"><span class="amount">'.price($v).'</span></td>';
}
}
}
}
print "</tr>\n";
// Loop on detail of all accounts to output the detail
if ($showaccountdetail != 'no') {
foreach ($cpts as $i => $cpt) {
if (isset($cpt['account_number'])) {
$resultNP = $totPerAccount[$cpt['account_number']]['NP'];
$resultN = $totPerAccount[$cpt['account_number']]['N'];
} else {
$resultNP = 0;
$resultN = 0;
}
if ($showaccountdetail == 'all' || $resultN != 0) {
print '<tr>';
print '<td></td>';
if (isset($cpt['account_number'])) {
$labeldetail = ' '.length_accountg($cpt['account_number']).' - '.$cpt['account_label'];
} else {
$labeldetail = '-';
}
print '<td class="tdoverflowmax250 borderright" title="'.dol_escape_htmltag($labeldetail).'">';
print dol_escape_htmltag($labeldetail);
print '</td>';
if (getDolGlobalInt('ACCOUNTANCY_TRUNC_DECIMAL_ON_BALANCE_REPORT')) {
print '<td class="right"><span class="amount">'.price($resultNP, 0, '', 1, 0, 0).'</span></td>';
print '<td class="right borderright"><span class="amount">'.price($resultN, 0, '', 1, 0, 0).'</span></td>';
} else {
print '<td class="right"><span class="amount">'.price($resultNP).'</span></td>';
print '<td class="right borderright"><span class="amount">'.price($resultN).'</span></td>';
}
// Make one call for each month
foreach ($months as $k => $v) {
if (($k + 1) >= $date_startmonth && (($date_startmonth <= $date_endmonth && ($k + 1) <= $date_endmonth) || ($date_startmonth > $date_endmonth))) {
if (isset($cpt['account_number'])) {
$resultM = $totPerAccount[$cpt['account_number']]['M'][$k];
} else {
$resultM = 0;
}
print '<td class="right"><span class="amount">'.price($resultM).'</span></td>';
}
}
if ($date_startmonth > $date_endmonth) {
foreach ($months as $k => $v) {
if (($k + 1) < $date_startmonth && ($k + 1) <= $date_endmonth) {
if (isset($cpt['account_number'])) {
$resultM = empty($totPerAccount[$cpt['account_number']]['M'][$k]) ? 0 : $totPerAccount[$cpt['account_number']]['M'][$k];
} else {
$resultM = 0;
}
print '<td class="right"><span class="amount">'.price($resultM).'</span></td>';
}
}
}
print "</tr>\n";
}
}
}
}
}
}
}
print "</table>";
print '</div>';
// End of page
llxFooter();
$db->close();