1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
15 # Contributors: Antonio Gallardo <agssa@ibw.com.ni>
16 # Benjamin Lee <benjaminlee@consultant.com>
17 # Philip Reetz <p.reetz@linet-services.de>
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 2 of the License, or
23 # (at your option) any later version.
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 # GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #======================================================================
34 # module for preparing Income Statement and Balance Sheet
36 #======================================================================
38 use POSIX qw(strftime);
44 use SL::ReportGenerator;
46 use List::MoreUtils qw(any);
48 require "bin/mozilla/arap.pl";
49 require "bin/mozilla/common.pl";
50 require "bin/mozilla/reportgenerator.pl";
52 # note: this file was particularly hard to strictify.
53 # alot of the vars are passed carelessly between invocations
54 # should there be any missing vars, declare them globally
57 # this is for our long dates
58 # $locale->text('January')
59 # $locale->text('February')
60 # $locale->text('March')
61 # $locale->text('April')
62 # $locale->text('May ')
63 # $locale->text('June')
64 # $locale->text('July')
65 # $locale->text('August')
66 # $locale->text('September')
67 # $locale->text('October')
68 # $locale->text('November')
69 # $locale->text('December')
71 # this is for our short month
72 # $locale->text('Jan')
73 # $locale->text('Feb')
74 # $locale->text('Mar')
75 # $locale->text('Apr')
76 # $locale->text('May')
77 # $locale->text('Jun')
78 # $locale->text('Jul')
79 # $locale->text('Aug')
80 # $locale->text('Sep')
81 # $locale->text('Oct')
82 # $locale->text('Nov')
83 # $locale->text('Dec')
85 # $locale->text('Balance Sheet')
86 # $locale->text('Income Statement')
87 # $locale->text('Trial Balance')
88 # $locale->text('AR Aging')
89 # $locale->text('AP Aging')
90 # $locale->text('Search AR Aging')
91 # $locale->text('Search AP Aging')
92 # $locale->text('Tax collected')
93 # $locale->text('Tax paid')
94 # $locale->text('Receipts')
95 # $locale->text('Payments')
96 # $locale->text('Project Transactions')
97 # $locale->text('Non-taxable Sales')
98 # $locale->text('Non-taxable Purchases')
99 # $locale->text('Business evaluation')
101 # $form->parse_html_template('rp/html_report_susa')
103 my $rp_access_map = {
104 'projects' => 'report',
105 'ar_aging' => 'general_ledger',
106 'ap_aging' => 'general_ledger',
107 'receipts' => 'cash',
108 'payments' => 'cash',
109 'trial_balance' => 'report',
110 'income_statement' => 'report',
112 'balance_sheet' => 'report',
115 sub check_rp_access {
116 my $form = $main::form;
118 my $right = $rp_access_map->{$form->{report}};
119 $right ||= 'DOES_NOT_EXIST';
121 $main::auth->assert($right);
125 $::lxdebug->enter_sub;
130 balance_sheet => $::locale->text('Balance Sheet'),
131 income_statement => $::locale->text('Income Statement'),
132 trial_balance => $::locale->text('Trial Balance'),
133 ar_aging => $::locale->text('Search AR Aging'),
134 ap_aging => $::locale->text('Search AP Aging'),
135 tax_collected => $::locale->text('Tax collected'),
136 tax_paid => $::locale->text('Tax paid'),
137 nontaxable_sales => $::locale->text('Non-taxable Sales'),
138 nontaxable_purchases => $::locale->text('Non-taxable Purchases'),
139 receipts => $::locale->text('Receipts'),
140 payments => $::locale->text('Payments'),
141 projects => $::locale->text('Project Transactions'),
142 bwa => $::locale->text('Business evaluation'),
145 $::form->{title} = $title{$::form->{report}};
148 $::form->all_departments(\%::myconfig);
149 if (@{ $::form->{all_departments} || [] }) {
150 $::form->{selectdepartment} = "<option>\n";
151 map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
154 $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
156 my $is_projects = $::form->{report} eq "projects";
157 my $is_income_statement = $::form->{report} eq "income_statement";
158 my $is_bwa = $::form->{report} eq "bwa";
159 my $is_balance_sheet = $::form->{report} eq "balance_sheet";
160 my $is_trial_balance = $::form->{report} eq "trial_balance";
161 my $is_aging = $::form->{report} =~ /^a[rp]_aging$/;
162 my $is_payments = $::form->{report} =~ /(receipts|payments)$/;
164 my ($label, $nextsub, $vc);
166 my $is_sales = $::form->{report} eq 'ar_aging';
167 $label = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
168 $::form->{vc} = $is_sales ? 'customer' : 'vendor';
170 $nextsub = "generate_$::form->{report}";
173 $::form->all_vc(\%::myconfig, $::form->{vc}, $is_sales ? "AR" : "AP");
174 $vc .= "<option>$_->{name}--$_->{id}\n" for @{ $::form->{"all_$::form->{vc}"} };
176 ? qq|<select name=$::form->{vc}><option>\n$vc</select>|
177 : qq|<input name=$::form->{vc} size=35>|;
180 my ($selection, $paymentaccounts);
182 $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
184 RP->paymentaccounts(\%::myconfig, $::form);
186 $selection = "<option>\n";
187 for my $ref (@{ $::form->{PR} }) {
188 $paymentaccounts .= "$ref->{accno} ";
189 $selection .= "<option>$ref->{accno}--$ref->{description}\n";
194 print $::form->parse_html_template('rp/report', {
195 paymentaccounts => $paymentaccounts,
196 selection => $selection,
197 is_aging => $is_aging,
200 year => DateTime->today->year,
201 today => DateTime->today,
203 accrual => $::instance_conf->get_accounting_method ne 'cash',
204 cash => $::instance_conf->get_accounting_method eq 'cash',
205 is_payments => $is_payments,
206 is_trial_balance => $is_trial_balance,
207 is_balance_sheet => $is_balance_sheet,
209 is_income_statement => $is_income_statement,
210 is_projects => $is_projects,
213 $::lxdebug->leave_sub;
216 sub continue { call_sub($main::form->{"nextsub"}); }
218 sub generate_income_statement {
219 $main::lxdebug->enter_sub();
221 $main::auth->assert('report');
223 my $form = $main::form;
224 my %myconfig = %main::myconfig;
225 my $locale = $main::locale;
227 $form->{padding} = " ";
228 $form->{bold} = "<b>";
229 $form->{endbold} = "</b>";
230 $form->{br} = "<br>";
232 if ($form->{reporttype} eq "custom") {
234 #forgotten the year --> thisyear
235 if ($form->{year} !~ m/^\d\d\d\d$/) {
236 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
242 if ($form->{duetyp} eq "13") {
243 $form->{fromdate} = "1.1.$form->{year}";
244 $form->{todate} = "31.12.$form->{year}";
248 if ($form->{duetyp} eq "A") {
249 $form->{fromdate} = "1.1.$form->{year}";
250 $form->{todate} = "31.3.$form->{year}";
252 if ($form->{duetyp} eq "B") {
253 $form->{fromdate} = "1.4.$form->{year}";
254 $form->{todate} = "30.6.$form->{year}";
256 if ($form->{duetyp} eq "C") {
257 $form->{fromdate} = "1.7.$form->{year}";
258 $form->{todate} = "30.9.$form->{year}";
260 if ($form->{duetyp} eq "D") {
261 $form->{fromdate} = "1.10.$form->{year}";
262 $form->{todate} = "31.12.$form->{year}";
267 $form->{duetyp} eq "1" && do {
268 $form->{fromdate} = "1.1.$form->{year}";
269 $form->{todate} = "31.1.$form->{year}";
272 $form->{duetyp} eq "2" && do {
273 $form->{fromdate} = "1.2.$form->{year}";
275 #this works from 1901 to 2099, 1900 and 2100 fail.
276 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
277 $form->{todate} = "$leap.2.$form->{year}";
280 $form->{duetyp} eq "3" && do {
281 $form->{fromdate} = "1.3.$form->{year}";
282 $form->{todate} = "31.3.$form->{year}";
285 $form->{duetyp} eq "4" && do {
286 $form->{fromdate} = "1.4.$form->{year}";
287 $form->{todate} = "30.4.$form->{year}";
290 $form->{duetyp} eq "5" && do {
291 $form->{fromdate} = "1.5.$form->{year}";
292 $form->{todate} = "31.5.$form->{year}";
295 $form->{duetyp} eq "6" && do {
296 $form->{fromdate} = "1.6.$form->{year}";
297 $form->{todate} = "30.6.$form->{year}";
300 $form->{duetyp} eq "7" && do {
301 $form->{fromdate} = "1.7.$form->{year}";
302 $form->{todate} = "31.7.$form->{year}";
305 $form->{duetyp} eq "8" && do {
306 $form->{fromdate} = "1.8.$form->{year}";
307 $form->{todate} = "31.8.$form->{year}";
310 $form->{duetyp} eq "9" && do {
311 $form->{fromdate} = "1.9.$form->{year}";
312 $form->{todate} = "30.9.$form->{year}";
315 $form->{duetyp} eq "10" && do {
316 $form->{fromdate} = "1.10.$form->{year}";
317 $form->{todate} = "31.10.$form->{year}";
320 $form->{duetyp} eq "11" && do {
321 $form->{fromdate} = "1.11.$form->{year}";
322 $form->{todate} = "30.11.$form->{year}";
325 $form->{duetyp} eq "12" && do {
326 $form->{fromdate} = "1.12.$form->{year}";
327 $form->{todate} = "31.12.$form->{year}";
331 hotfix_reformat_date();
332 } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
334 RP->income_statement(\%myconfig, \%$form);
336 ($form->{department}) = split /--/, $form->{department};
339 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
340 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
342 # if there are any dates construct a where
343 if ($form->{fromdate} || $form->{todate}) {
345 unless ($form->{todate}) {
346 $form->{todate} = $form->current_date(\%myconfig);
349 my $longtodate = $locale->date(\%myconfig, $form->{todate}, 1);
350 my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
352 my $longfromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
353 my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
355 $form->{this_period} = "$shortfromdate\n$shorttodate";
357 $locale->text('for Period')
358 . qq|\n$longfromdate |
359 . $locale->text('Bis')
363 if ($form->{comparefromdate} || $form->{comparetodate}) {
364 my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
365 my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
367 my $longcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 1);
368 my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
370 $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
372 "\n$longcomparefromdate "
373 . $locale->text('Bis')
374 . qq| $longcomparetodate|;
377 # setup variables for the form
378 my @a = qw(company address businessnumber);
379 map { $form->{$_} = $myconfig{$_} } @a;
381 $form->{templates} = $myconfig{templates};
383 $form->{IN} = "income_statement.html";
385 $form->parse_template;
387 $main::lxdebug->leave_sub();
390 sub generate_balance_sheet {
391 $::lxdebug->enter_sub;
392 $::auth->assert('report');
394 $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
395 $::form->{padding} = " ";
396 $::form->{bold} = "<b>";
397 $::form->{endbold} = "</b>";
398 $::form->{br} = "<br>";
400 my $data = RP->balance_sheet(\%::myconfig, $::form);
402 $::form->{asofdate} ||= $::form->current_date;
403 $::form->{period} = $::locale->date(\%::myconfig, $::form->current_date, 1);
405 ($::form->{department}) = split /--/, $::form->{department};
407 # define Current Earnings account
408 my $padding = $::form->{l_heading} ? $::form->{padding} : "";
409 push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
411 $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
412 $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
414 # $::form->{IN} = "balance_sheet.html";
416 # setup company variables for the form
417 map { $::form->{$_} = $::myconfig{$_} } qw(company address businessnumber nativecurr);
419 $::form->{templates} = $::myconfig{templates};
422 print $::form->parse_html_template('rp/balance_sheet', $data);
424 $::lxdebug->leave_sub;
427 sub generate_projects {
428 $main::lxdebug->enter_sub();
430 $main::auth->assert('report');
432 my $form = $main::form;
433 my %myconfig = %main::myconfig;
434 my $locale = $main::locale;
436 my $project = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
437 $form->{projectnumber} = $project ? $project->projectnumber : '';
439 $form->{nextsub} = "generate_projects";
440 $form->{title} = $locale->text('Project Transactions');
441 RP->trial_balance(\%myconfig, \%$form);
443 list_accounts('generate_projects');
445 $main::lxdebug->leave_sub();
451 # included links to display transactions for period entered
452 # added headers and subtotals
454 sub generate_trial_balance {
455 $main::lxdebug->enter_sub();
457 $main::auth->assert('report');
459 my $form = $main::form;
460 my %myconfig = %main::myconfig;
461 my $locale = $main::locale;
463 if ($form->{reporttype} eq "custom") {
465 #forgotten the year --> thisyear
466 if ($form->{year} !~ m/^\d\d\d\d$/) {
467 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
473 if ($form->{duetyp} eq "13") {
474 $form->{fromdate} = "1.1.$form->{year}";
475 $form->{todate} = "31.12.$form->{year}";
479 if ($form->{duetyp} eq "A") {
480 $form->{fromdate} = "1.1.$form->{year}";
481 $form->{todate} = "31.3.$form->{year}";
483 if ($form->{duetyp} eq "B") {
484 $form->{fromdate} = "1.4.$form->{year}";
485 $form->{todate} = "30.6.$form->{year}";
487 if ($form->{duetyp} eq "C") {
488 $form->{fromdate} = "1.7.$form->{year}";
489 $form->{todate} = "30.9.$form->{year}";
491 if ($form->{duetyp} eq "D") {
492 $form->{fromdate} = "1.10.$form->{year}";
493 $form->{todate} = "31.12.$form->{year}";
498 $form->{duetyp} eq "1" && do {
499 $form->{fromdate} = "1.1.$form->{year}";
500 $form->{todate} = "31.1.$form->{year}";
503 $form->{duetyp} eq "2" && do {
504 $form->{fromdate} = "1.2.$form->{year}";
506 #this works from 1901 to 2099, 1900 and 2100 fail.
507 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
508 $form->{todate} = "$leap.2.$form->{year}";
511 $form->{duetyp} eq "3" && do {
512 $form->{fromdate} = "1.3.$form->{year}";
513 $form->{todate} = "31.3.$form->{year}";
516 $form->{duetyp} eq "4" && do {
517 $form->{fromdate} = "1.4.$form->{year}";
518 $form->{todate} = "30.4.$form->{year}";
521 $form->{duetyp} eq "5" && do {
522 $form->{fromdate} = "1.5.$form->{year}";
523 $form->{todate} = "31.5.$form->{year}";
526 $form->{duetyp} eq "6" && do {
527 $form->{fromdate} = "1.6.$form->{year}";
528 $form->{todate} = "30.6.$form->{year}";
531 $form->{duetyp} eq "7" && do {
532 $form->{fromdate} = "1.7.$form->{year}";
533 $form->{todate} = "31.7.$form->{year}";
536 $form->{duetyp} eq "8" && do {
537 $form->{fromdate} = "1.8.$form->{year}";
538 $form->{todate} = "31.8.$form->{year}";
541 $form->{duetyp} eq "9" && do {
542 $form->{fromdate} = "1.9.$form->{year}";
543 $form->{todate} = "30.9.$form->{year}";
546 $form->{duetyp} eq "10" && do {
547 $form->{fromdate} = "1.10.$form->{year}";
548 $form->{todate} = "31.10.$form->{year}";
551 $form->{duetyp} eq "11" && do {
552 $form->{fromdate} = "1.11.$form->{year}";
553 $form->{todate} = "30.11.$form->{year}";
556 $form->{duetyp} eq "12" && do {
557 $form->{fromdate} = "1.12.$form->{year}";
558 $form->{todate} = "31.12.$form->{year}";
562 hotfix_reformat_date();
566 # get for each account initial balance, debits and credits
567 RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
570 $form->{rowcount} = scalar @{ $form->{TB} || [] };
571 $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
574 "accno", "description",
575 "last_transaction", "soll_eb",
578 "soll_kumuliert", "haben_kumuliert",
579 "soll_saldo", "haben_saldo"
583 my $attachment_basename = $locale->text('trial_balance');
584 my $report = SL::ReportGenerator->new(\%myconfig, $form);
586 my @hidden_variables = qw(fromdate todate year cash);
588 my $href = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
591 'accno' => { 'text' => $locale->text('Account'), },
592 'description' => { 'text' => $locale->text('Description'), },
593 'last_transaction' => { 'text' => $locale->text('Last Transaction'), },
594 'soll_eb' => { 'text' => $locale->text('Debit Starting Balance'), },
595 'haben_eb' => { 'text' => $locale->text('Credit Starting Balance'), },
596 'soll' => { 'text' => $locale->text('Debit'), },
597 'haben' => { 'text' => $locale->text('Credit'), },
598 'soll_kumuliert' => { 'text' => $locale->text('Sum Debit'), },
599 'haben_kumuliert' => { 'text' => $locale->text('Sum Credit'), },
600 'soll_saldo' => { 'text' => $locale->text('Saldo Debit'), },
601 'haben_saldo' => { 'text' => $locale->text('Saldo Credit'), }
606 my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
608 map { $column_defs{$_}->{visible} = 1 } @columns;
610 $report->set_columns(%column_defs);
611 $report->set_column_order(@columns);
613 $report->set_export_options('generate_trial_balance', @hidden_variables);
618 $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
620 $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
621 push (@options, $form->{print_date});
623 $form->{company} = $locale->text('Company') . " " . $myconfig{company};
624 push (@options, $form->{company});
627 $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
629 my @custom_headers = ([
630 { text => $::locale->text('Account'), rowspan => 2, },
631 { text => $::locale->text('Description'), rowspan => 2, },
632 { text => $::locale->text('Last Transaction'), rowspan => 2, },
633 { text => $::locale->text('Starting Balance'), colspan => 2, },
634 { text => $::locale->text('Sum for') . " $form->{template_fromto}", colspan => 2, },
635 { text => $::locale->text('Sum per') . " $form->{template_to}", colspan => 2, },
636 { text => $::locale->text('Saldo per') . " $form->{template_to}", colspan => 2, },
641 { text => $::locale->text('Assets'), },
642 { text => $::locale->text('Equity'), },
643 { text => $::locale->text('Debit'), },
644 { text => $::locale->text('Credit'), },
645 { text => $::locale->text('Debit'), },
646 { text => $::locale->text('Credit'), },
647 { text => $::locale->text('Debit'), },
648 { text => $::locale->text('Credit'), },
651 $report->set_options('output_format' => 'HTML',
652 'top_info_text' => join("\n", @options),
653 'title' => $form->{title},
654 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
655 'html_template' => 'rp/html_report_susa',
656 'pdf_template' => 'rp/html_report_susa',
658 $report->set_custom_headers(@custom_headers);
659 $report->set_options_from_form();
660 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
662 # add sort and escape callback, this one we use for the add sub
663 $form->{callback} = $href .= "&sort=$form->{sort}";
665 # escape callback for href
666 my $callback = $form->escape($href);
668 my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
670 my %totals = map { $_ => 0 } @subtotal_columns;
672 my $edit_url = build_std_url('action=edit', 'type', 'vc');
675 foreach my $accno (@{ $form->{TB} || [] }) {
677 $accno->{soll} = $accno->{debit};
678 $accno->{haben} = $accno->{credit};
679 map { $totals{$_} += $accno->{$_} } @subtotal_columns;
681 map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
682 qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
686 foreach my $column (@columns) {
688 'data' => $accno->{$column},
689 'align' => $column_alignment{$column},
693 $row->{accno}->{link} = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($accno->{accno}), 'description=' . E($accno->{description}), 'fromdate=' . E($form->{fromdate}), 'todate=' . E($form->{todate}), 'method=' . E($form->{method}));
695 my $row_set = [ $row ];
698 $report->add_data($row_set);
703 $report->add_separator();
705 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
707 $report->generate_with_headers();
709 $main::lxdebug->leave_sub();
713 sub create_subtotal_row {
714 $main::lxdebug->enter_sub();
716 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
718 my $form = $main::form;
719 my %myconfig = %main::myconfig;
720 my $locale = $main::locale;
722 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
724 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
726 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
728 map { $totals->{$_} = 0 } @{ $subtotal_columns };
730 $main::lxdebug->leave_sub();
735 sub create_list_accounts_subtotal_row {
736 $main::lxdebug->enter_sub();
738 my ($subtotals, $columns, $fields, $class) = @_;
740 my $form = $main::form;
741 my %myconfig = %main::myconfig;
742 my $locale = $main::locale;
744 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
746 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
748 $main::lxdebug->leave_sub();
754 $main::lxdebug->enter_sub();
758 my $form = $main::form;
759 my %myconfig = %main::myconfig;
760 my $locale = $main::locale;
763 if ($form->{department}) {
764 my ($department) = split /--/, $form->{department};
765 push @options, $locale->text('Department') . " : $department";
767 if ($form->{projectnumber}) {
768 push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
771 # if there are any dates
772 if ($form->{fromdate} || $form->{todate}) {
773 my ($fromdate, $todate);
775 if ($form->{fromdate}) {
776 $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
778 if ($form->{todate}) {
779 $todate = $locale->date(\%myconfig, $form->{todate}, 1);
782 push @options, "$fromdate - $todate";
785 push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
788 my @columns = qw(accno description begbalance debit credit endbalance);
790 'accno' => { 'text' => $locale->text('Account'), },
791 'description' => { 'text' => $locale->text('Description'), },
792 'debit' => { 'text' => $locale->text('Debit'), },
793 'credit' => { 'text' => $locale->text('Credit'), },
794 'begbalance' => { 'text' => $locale->text('Balance'), },
795 'endbalance' => { 'text' => $locale->text('Balance'), },
797 my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
799 my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
801 $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
803 my $report = SL::ReportGenerator->new(\%myconfig, $form);
805 $report->set_options('top_info_text' => join("\n", @options),
806 'output_format' => 'HTML',
807 'title' => $form->{title},
808 'attachment_basename' => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
809 'std_column_visibility' => 1,
811 $report->set_options_from_form();
812 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
814 $report->set_columns(%column_defs);
815 $report->set_column_order(@columns);
817 $report->set_export_options($action, @hidden_variables);
819 my @totals_columns = qw(credit debit begbalance endbalance);
820 my %subtotals = map { $_ => 0 } @totals_columns;
821 my %totals = map { $_ => 0 } @totals_columns;
822 my $found_heading = 0;
823 my @tb = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
825 # sort the whole thing by account numbers and display
826 foreach my $idx (0 .. scalar(@tb) - 1) {
828 my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
830 my $ml = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
832 my $row = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
834 if ($ref->{charttype} eq 'H') {
835 next unless ($form->{l_heading});
837 %subtotals = map { $_ => 0 } @totals_columns;
839 $row->{description}->{class} = 'listheading';
840 $row->{description}->{data} = $ref->{description};
842 $report->add_data($row);
847 foreach (qw(debit credit)) {
848 $subtotals{$_} += $ref->{$_};
849 $totals{$_} += $ref->{$_};
852 $subtotals{begbalance} += $ref->{balance} * $ml;
853 $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
855 map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
856 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
858 $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
859 $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
861 $report->add_data($row);
863 if ($form->{l_heading} && $found_heading &&
864 (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
865 $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
869 $report->add_separator();
871 $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
873 $report->generate_with_headers();
875 $main::lxdebug->leave_sub();
878 sub generate_ar_aging {
879 $main::lxdebug->enter_sub();
881 $main::auth->assert('general_ledger');
883 my $form = $main::form;
884 my %myconfig = %main::myconfig;
885 my $locale = $main::locale;
888 ($form->{customer}) = split(/--/, $form->{customer});
890 $form->{ct} = "customer";
891 $form->{arap} = "ar";
893 $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
895 RP->aging(\%myconfig, \%$form);
898 $main::lxdebug->leave_sub();
901 sub generate_ap_aging {
902 $main::lxdebug->enter_sub();
904 $main::auth->assert('general_ledger');
906 my $form = $main::form;
907 my %myconfig = %main::myconfig;
908 my $locale = $main::locale;
911 ($form->{vendor}) = split(/--/, $form->{vendor});
913 $form->{ct} = "vendor";
914 $form->{arap} = "ap";
916 $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
918 RP->aging(\%myconfig, \%$form);
921 $main::lxdebug->leave_sub();
924 sub create_aging_subtotal_row {
925 $main::lxdebug->enter_sub();
927 my ($subtotals, $columns, $periods, $class) = @_;
929 my $form = $main::form;
930 my %myconfig = %main::myconfig;
931 my $locale = $main::locale;
933 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
935 foreach (@{ $periods }) {
936 $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
937 $subtotals->{$_} = 0;
940 $main::lxdebug->leave_sub();
946 $main::lxdebug->enter_sub();
948 $main::auth->assert('general_ledger');
950 my $form = $main::form;
951 my %myconfig = %main::myconfig;
952 my $locale = $main::locale;
953 my $cgi = $::request->{cgi};
955 my $report = SL::ReportGenerator->new(\%myconfig, $form);
957 my @columns = qw(statement ct invnumber transdate duedate amount open);
960 'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
961 'ct' => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
962 'invnumber' => { 'text' => $locale->text('Invoice'), },
963 'transdate' => { 'text' => $locale->text('Date'), },
964 'duedate' => { 'text' => $locale->text('Due'), },
965 'amount' => { 'text' => $locale->text('Amount'), },
966 'open' => { 'text' => $locale->text('Open'), },
969 my %column_alignment = ('statement' => 'center',
970 map { $_ => 'right' } qw(open amount));
972 $report->set_options('std_column_visibility' => 1);
973 $report->set_columns(%column_defs);
974 $report->set_column_order(@columns);
976 my @hidden_variables = qw(todate customer vendor arap title ct);
977 $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
980 my $attachment_basename;
982 if ($form->{department}) {
983 my ($department) = split /--/, $form->{department};
984 push @options, $locale->text('Department') . " : $department";
985 $form->{callback} .= "&department=" . E($department);
988 if (($form->{arap} eq 'ar') && $form->{customer}) {
989 push @options, $form->{customer};
990 $attachment_basename = $locale->text('ar_aging_list');
991 $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
994 if (($form->{arap} eq 'ap') && $form->{vendor}) {
995 push @options, $form->{vendor};
996 $attachment_basename = $locale->text('ap_aging_list');
997 $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
1000 if ($form->{fromdate}) {
1001 push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1003 push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1006 $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1008 $report->set_options('top_info_text' => join("\n", @options),
1009 'output_format' => 'HTML',
1010 'title' => $form->{title},
1011 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1013 $report->set_options_from_form();
1014 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1016 my $previous_ctid = 0;
1018 my @periods = qw(open amount);
1019 my %subtotals = map { $_ => 0 } @periods;
1020 my %totals = map { $_ => 0 } @periods;
1022 foreach my $ref (@{ $form->{AG} }) {
1023 if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1024 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1027 foreach my $key (@periods) {
1028 $subtotals{$key} += $ref->{"$key"};
1029 $totals{$key} += $ref->{"$key"};
1030 $ref->{"$key"} = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
1035 foreach my $column (@columns) {
1037 'data' => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1038 'align' => $column_alignment{$column},
1039 'valign' => $column eq 'statement' ? 'center' : '',
1043 $row->{invnumber}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1045 if ($previous_ctid != $ref->{ctid}) {
1046 $row->{statement}->{raw_data} =
1047 $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
1048 . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1049 $row->{ct}->{data} = $ref->{name};
1054 $previous_ctid = $ref->{ctid};
1056 $report->add_data($row);
1059 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1061 $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1063 if ($form->{arap} eq 'ar') {
1064 my $raw_top_info_text = $form->parse_html_template('rp/aging_ar_top');
1065 my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1066 'PRINT_OPTIONS' => print_options(inline => 1), });
1067 $report->set_options('raw_top_info_text' => $raw_top_info_text,
1068 'raw_bottom_info_text' => $raw_bottom_info_text);
1071 $report->generate_with_headers();
1073 $main::lxdebug->leave_sub();
1077 $main::lxdebug->enter_sub();
1079 my $form = $main::form;
1080 my %myconfig = %main::myconfig;
1081 my $locale = $main::locale;
1083 RP->aging(\%myconfig, \%$form);
1085 map { $_->{checked} = "checked" } @{ $form->{AG} };
1089 $main::lxdebug->leave_sub();
1093 $::lxdebug->enter_sub;
1094 $::auth->assert('general_ledger');
1096 # get name and email addresses
1098 for my $i (1 .. $::form->{rowcount}) {
1099 next unless $::form->{"statement_$i"};
1100 $::form->{"$::form->{ct}_id"} = $::form->{"$::form->{ct}_id_$i"};
1101 RP->get_customer(\%::myconfig, $::form);
1106 $::form->error($::locale->text('Nothing selected!')) unless $selected;
1108 $::form->{media} = "email";
1110 # save all other variables
1112 for my $key (keys %$::form) {
1113 next if any { $key eq $_ } qw(login password action email cc bcc subject message type sendmode format header);
1114 next unless '' eq ref $::form->{$key};
1115 push @hidden_values, $key;
1119 print $::form->parse_html_template('rp/e_mail', {
1120 show_bcc => $::auth->assert('email_bcc', 'may fail'),
1121 print_options => print_options(inline => 1),
1122 hidden_values => \@hidden_values,
1125 $::lxdebug->leave_sub;
1129 $main::lxdebug->enter_sub();
1131 $main::auth->assert('general_ledger');
1133 my $form = $main::form;
1134 my %myconfig = %main::myconfig;
1135 my $locale = $main::locale;
1137 $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1138 unless $form->{subject};
1140 RP->aging(\%myconfig, \%$form);
1142 $form->{"statement_1"} = 1;
1144 $form->{media} = 'email';
1147 $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1149 $main::lxdebug->leave_sub();
1153 $main::lxdebug->enter_sub();
1155 $main::auth->assert('general_ledger');
1157 my $form = $main::form;
1158 my %myconfig = %main::myconfig;
1159 my $locale = $main::locale;
1161 if ($form->{media} eq 'printer') {
1162 $form->error($locale->text('Select postscript or PDF!'))
1163 if ($form->{format} !~ /(postscript|pdf)/);
1167 for my $i (1 .. $form->{rowcount}) {
1168 if ($form->{"statement_$i"}) {
1169 $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1175 $form->error($locale->text('Nothing selected!')) unless $selected;
1177 if ($form->{media} eq 'printer') {
1178 $form->{"$form->{ct}_id"} = "";
1180 $form->{"statement_1"} = 1;
1183 RP->aging(\%myconfig, \%$form);
1187 $form->redirect($locale->text('Statements sent to printer!'))
1188 if ($form->{media} eq 'printer');
1190 $main::lxdebug->leave_sub();
1194 $main::lxdebug->enter_sub();
1196 $main::auth->assert('general_ledger');
1198 my $form = $main::form;
1199 my %myconfig = %main::myconfig;
1200 my $locale = $main::locale;
1202 $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1204 $form->{templates} = "$myconfig{templates}";
1206 my $suffix = "html";
1207 my $attachment_suffix = "html";
1208 if ($form->{format} eq 'postscript') {
1209 $form->{postscript} = 1;
1211 $attachment_suffix = "ps";
1212 } elsif ($form->{format} eq 'pdf') {
1215 $attachment_suffix = "pdf";
1218 $form->{IN} = "$form->{type}.$suffix";
1219 $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1221 # Save $form->{email} because it will be overwritten.
1222 $form->{EMAIL_RECIPIENT} = $form->{email};
1226 while (@{ $form->{AG} }) {
1228 my $ref = shift @{ $form->{AG} };
1230 if ($ctid != $ref->{ctid}) {
1232 $ctid = $ref->{ctid};
1235 if ($form->{"statement_$i"}) {
1238 ("name", "street", "zipcode", "city", "country", "contact", "email",
1239 "$form->{ct}phone", "$form->{ct}fax");
1240 map { $form->{$_} = $ref->{$_} } @a;
1242 $form->{ $form->{ct} } = $form->{name};
1243 $form->{"$form->{ct}_id"} = $ref->{ctid};
1245 map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
1247 foreach my $item (qw(c0 c30 c60 c90)) {
1248 $form->{$item} = ();
1249 $form->{"${item}total"} = 0;
1252 &statement_details($ref);
1256 if (scalar(@{ $form->{AG} }) > 0) {
1258 # one or more left to go
1259 if ($ctid == $form->{AG}->[0]->{ctid}) {
1260 $ref = shift @{ $form->{AG} };
1261 &statement_details($ref);
1264 $ref = scalar(@{ $form->{AG} });
1270 # set initial ref to 0
1277 $form->{"${_}total"} =
1278 $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1279 } ('c0', 'c30', 'c60', 'c90', "");
1281 $form->{attachment_filename} = $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
1282 $form->{attachment_filename} =~ s/\s+/_/g;
1284 $form->parse_template(\%myconfig);
1289 # saving the history
1290 if(!exists $form->{addition} && $form->{id} ne "") {
1291 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1292 $form->{addition} = "PRINTED";
1293 $form->{what_done} = $form->{type};
1294 $form->save_history;
1296 # /saving the history
1297 $main::lxdebug->leave_sub();
1300 sub statement_details {
1301 $main::lxdebug->enter_sub();
1303 $main::auth->assert('general_ledger');
1305 my $form = $main::form;
1306 my %myconfig = %main::myconfig;
1307 my $locale = $main::locale;
1311 push @{ $form->{invnumber} }, $ref->{invnumber};
1312 push @{ $form->{invdate} }, $ref->{transdate};
1313 push @{ $form->{duedate} }, $ref->{duedate};
1314 push @{ $form->{amount} }, $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
1315 push @{ $form->{open} }, $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
1317 foreach my $item (qw(c0 c30 c60 c90)) {
1318 if ($ref->{exchangerate} * 1) {
1319 # add only the open amount of the invoice to the aging, not the total amount
1320 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
1321 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
1322 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
1323 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
1325 $form->{"${item}total"} += $ref->{$item};
1326 $form->{total} += $ref->{$item};
1327 push @{ $form->{$item} },
1328 $form->format_amount(\%myconfig, $ref->{$item}, 2);
1331 $main::lxdebug->leave_sub();
1334 sub generate_tax_report {
1335 $::lxdebug->enter_sub;
1336 $::auth->assert('report');
1338 RP->tax_report(\%::myconfig, $::form);
1340 my $descvar = "$::form->{accno}_description";
1341 my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
1345 my $callback = build_std_url('action=generate_tax_report', $descvar,
1346 qw(fromdate todate db method accno department report title));
1348 my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1351 for my $item (@columns, 'subtotal') {
1352 if ($::form->{"l_$item"} eq "Y") {
1353 $callback .= "&l_$item=Y";
1354 $href .= "&l_$item=Y";
1358 for my $item (@columns) {
1359 if ($::form->{"l_$item"} eq "Y") {
1360 push @column_index, $item;
1365 if ($::form->{department}) {
1366 my ($department) = split /--/, $::form->{department};
1367 push @options, $::locale->text('Department') . " : $department";
1370 # if there are any dates
1371 if ($::form->{fromdate} || $::form->{todate}) {
1372 my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
1373 my $todate = $::form->{todate} ? $::locale->date(\%::myconfig, $::form->{todate}, 1) : '';
1374 push @options, "$fromdate - $todate";
1376 push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
1379 my ($name, $invoice, $arap);
1380 if ($::form->{db} eq 'ar') {
1381 $name = $::locale->text('Customer');
1385 if ($::form->{db} eq 'ap') {
1386 $name = $::locale->text('Vendor');
1391 my %column_header = (
1392 id => $::locale->text('ID'),
1393 invnumber => $::locale->text('Invoice'),
1394 transdate => $::locale->text('Date'),
1395 netamount => $::locale->text('Amount'),
1396 tax => $::locale->text('Tax'),
1397 amount => $::locale->text('Total'),
1401 my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
1403 $callback .= "&sort=$::form->{sort}";
1406 if (@{ $::form->{TR} }) {
1407 $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
1410 my ($totalnetamount, $totaltax, @data);
1411 for my $ref (@{ $::form->{TR} }) {
1413 my $module = ($ref->{invoice}) ? $invoice : $arap;
1415 if ($::form->{l_subtotal} eq 'Y') {
1416 if ($sameitem ne $ref->{ $::form->{sort} }) {
1419 netamount => $subtotalnetamount,
1420 tax => $subtotaltax,
1421 amount => $subtotal,
1423 $subtotalnetamount = 0;
1425 $sameitem = $ref->{ $::form->{sort} };
1429 $subtotalnetamount += $ref->{netamount};
1430 $subtotaltax += $ref->{tax};
1431 $totalnetamount += $ref->{netamount};
1432 $totaltax += $ref->{tax};
1433 $ref->{amount} = $ref->{netamount} + $ref->{tax};
1435 push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
1436 $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
1437 $data[-1]{$_}{numeric} = 1 for qw(netamount tax amount);
1440 if ($::form->{l_subtotal} eq 'Y') {
1443 netamount => $subtotalnetamount,
1444 tax => $subtotaltax,
1445 amount => $subtotal,
1451 netamount => $totalnetamount,
1453 amount => $totalnetamount + $totaltax,
1457 print $::form->parse_html_template('rp/tax_report', {
1458 column_index => \@column_index,
1459 column_header => \%column_header,
1460 column_sorted => \%column_sorted,
1463 options => \@options,
1466 $::lxdebug->leave_sub;
1470 $main::lxdebug->enter_sub();
1472 $main::auth->assert('cash');
1474 my $form = $main::form;
1475 my %myconfig = %main::myconfig;
1476 my $locale = $main::locale;
1478 if ($form->{account}) {
1479 ($form->{paymentaccounts}) = split /--/, $form->{account};
1483 if ($form->{department}) {
1484 (my $department, $form->{department_id}) = split /--/, $form->{department};
1485 $option = $locale->text('Department') . " : $department";
1488 report_generator_set_default_sort('transdate', 1);
1490 RP->payments(\%myconfig, \%$form);
1492 my @hidden_variables = qw(account title department reference source memo fromdate todate
1493 fx_transaction db prepayment paymentaccounts sort);
1495 my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
1496 $form->{callback} = $href;
1498 my @columns = qw(transdate invnumber name paid source memo);
1500 'name' => { 'text' => $locale->text('Description'), },
1501 'invnumber' => { 'text' => $locale->text('Reference'), },
1502 'transdate' => { 'text' => $locale->text('Date'), },
1503 'paid' => { 'text' => $locale->text('Amount'), },
1504 'source' => { 'text' => $locale->text('Source'), },
1505 'memo' => { 'text' => $locale->text('Memo'), },
1507 my %column_alignment = ('paid' => 'right');
1509 foreach my $name (grep { $_ ne 'paid' } @columns) {
1510 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1511 $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
1515 if ($form->{fromdate}) {
1516 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
1518 if ($form->{todate}) {
1519 push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1522 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1524 my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
1526 $report->set_options('top_info_text' => join("\n", @options),
1527 'output_format' => 'HTML',
1528 'title' => $form->{title},
1529 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1530 'std_column_visibility' => 1,
1532 $report->set_options_from_form();
1534 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1536 $report->set_columns(%column_defs);
1537 $report->set_column_order(@columns);
1539 $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
1541 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1545 foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1546 next unless @{ $form->{ $ref->{id} } };
1548 $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
1550 my $subtotal_paid = 0;
1552 foreach my $payment (@{ $form->{ $ref->{id} } }) {
1553 my $module = $payment->{module};
1554 $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1555 $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1557 $subtotal_paid += $payment->{paid};
1558 $total_paid += $payment->{paid};
1560 $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
1564 foreach my $column (@columns) {
1566 'data' => $payment->{$column},
1567 'align' => $column_alignment{$column},
1571 $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
1573 $report->add_data($row);
1576 my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
1578 'data' => $form->format_amount(\%myconfig, $subtotal_paid, 2),
1580 'class' => 'listsubtotal',
1583 $report->add_data($row);
1586 $report->add_separator();
1588 my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
1590 'data' => $form->format_amount(\%myconfig, $total_paid, 2),
1592 'class' => 'listtotal',
1595 $report->add_data($row);
1597 $report->generate_with_headers();
1599 $main::lxdebug->leave_sub();
1603 $::lxdebug->enter_sub;
1605 my ($dont_print) = @_;
1607 $::form->{sendmode} = "attachment";
1608 $::form->{format} ||= $::myconfig{template_format} || "pdf";
1609 $::form->{copies} ||= $::myconfig{copies} || 2;
1611 $::form->{PD}{ $::form->{type} } = "selected";
1612 $::form->{DF}{ $::form->{format} } = "selected";
1613 $::form->{OP}{ $::form->{media} } = "selected";
1614 $::form->{SM}{ $::form->{sendmode} } = "selected";
1616 my $output = $::form->parse_html_template('rp/print_options', {
1617 got_printer => $::myconfig{printer},
1618 show_latex => $::lx_office_conf{print_templates}->{latex},
1619 is_email => $::form->{media} eq 'email',
1622 print $output unless $dont_print;
1624 $::lxdebug->leave_sub;
1630 $main::lxdebug->enter_sub();
1632 $main::auth->assert('report');
1634 my $form = $main::form;
1635 my %myconfig = %main::myconfig;
1636 my $locale = $main::locale;
1638 $form->{padding} = " ";
1639 $form->{bold} = "<b>";
1640 $form->{endbold} = "</b>";
1641 $form->{br} = "<br>";
1643 if ($form->{reporttype} eq "custom") {
1645 #forgotten the year --> thisyear
1646 if ($form->{year} !~ m/^\d\d\d\d$/) {
1647 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1653 if ($form->{duetyp} eq "13") {
1654 $form->{fromdate} = "1.1.$form->{year}";
1655 $form->{todate} = "31.12.$form->{year}";
1656 $form->{comparefromdate} = "1.01.$form->{year}";
1657 $form->{comparetodate} = "31.12.$form->{year}";
1661 if ($form->{duetyp} eq "A") {
1662 $form->{fromdate} = "1.1.$form->{year}";
1663 $form->{todate} = "31.3.$form->{year}";
1664 $form->{comparefromdate} = "1.01.$form->{year}";
1665 $form->{comparetodate} = "31.03.$form->{year}";
1667 if ($form->{duetyp} eq "B") {
1668 $form->{fromdate} = "1.4.$form->{year}";
1669 $form->{todate} = "30.6.$form->{year}";
1670 $form->{comparefromdate} = "1.01.$form->{year}";
1671 $form->{comparetodate} = "30.06.$form->{year}";
1673 if ($form->{duetyp} eq "C") {
1674 $form->{fromdate} = "1.7.$form->{year}";
1675 $form->{todate} = "30.9.$form->{year}";
1676 $form->{comparefromdate} = "1.01.$form->{year}";
1677 $form->{comparetodate} = "30.09.$form->{year}";
1679 if ($form->{duetyp} eq "D") {
1680 $form->{fromdate} = "1.10.$form->{year}";
1681 $form->{todate} = "31.12.$form->{year}";
1682 $form->{comparefromdate} = "1.01.$form->{year}";
1683 $form->{comparetodate} = "31.12.$form->{year}";
1688 $form->{duetyp} eq "1" && do {
1689 $form->{fromdate} = "1.1.$form->{year}";
1690 $form->{todate} = "31.1.$form->{year}";
1691 $form->{comparefromdate} = "1.01.$form->{year}";
1692 $form->{comparetodate} = "31.01.$form->{year}";
1695 $form->{duetyp} eq "2" && do {
1696 $form->{fromdate} = "1.2.$form->{year}";
1698 #this works from 1901 to 2099, 1900 and 2100 fail.
1699 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1700 $form->{todate} = "$leap.2.$form->{year}";
1701 $form->{comparefromdate} = "1.01.$form->{year}";
1702 $form->{comparetodate} = "$leap.02.$form->{year}";
1705 $form->{duetyp} eq "3" && do {
1706 $form->{fromdate} = "1.3.$form->{year}";
1707 $form->{todate} = "31.3.$form->{year}";
1708 $form->{comparefromdate} = "1.01.$form->{year}";
1709 $form->{comparetodate} = "31.03.$form->{year}";
1712 $form->{duetyp} eq "4" && do {
1713 $form->{fromdate} = "1.4.$form->{year}";
1714 $form->{todate} = "30.4.$form->{year}";
1715 $form->{comparefromdate} = "1.01.$form->{year}";
1716 $form->{comparetodate} = "30.04.$form->{year}";
1719 $form->{duetyp} eq "5" && do {
1720 $form->{fromdate} = "1.5.$form->{year}";
1721 $form->{todate} = "31.5.$form->{year}";
1722 $form->{comparefromdate} = "1.01.$form->{year}";
1723 $form->{comparetodate} = "31.05.$form->{year}";
1726 $form->{duetyp} eq "6" && do {
1727 $form->{fromdate} = "1.6.$form->{year}";
1728 $form->{todate} = "30.6.$form->{year}";
1729 $form->{comparefromdate} = "1.01.$form->{year}";
1730 $form->{comparetodate} = "30.06.$form->{year}";
1733 $form->{duetyp} eq "7" && do {
1734 $form->{fromdate} = "1.7.$form->{year}";
1735 $form->{todate} = "31.7.$form->{year}";
1736 $form->{comparefromdate} = "1.01.$form->{year}";
1737 $form->{comparetodate} = "31.07.$form->{year}";
1740 $form->{duetyp} eq "8" && do {
1741 $form->{fromdate} = "1.8.$form->{year}";
1742 $form->{todate} = "31.8.$form->{year}";
1743 $form->{comparefromdate} = "1.01.$form->{year}";
1744 $form->{comparetodate} = "31.08.$form->{year}";
1747 $form->{duetyp} eq "9" && do {
1748 $form->{fromdate} = "1.9.$form->{year}";
1749 $form->{todate} = "30.9.$form->{year}";
1750 $form->{comparefromdate} = "1.01.$form->{year}";
1751 $form->{comparetodate} = "30.09.$form->{year}";
1754 $form->{duetyp} eq "10" && do {
1755 $form->{fromdate} = "1.10.$form->{year}";
1756 $form->{todate} = "31.10.$form->{year}";
1757 $form->{comparefromdate} = "1.01.$form->{year}";
1758 $form->{comparetodate} = "31.10.$form->{year}";
1761 $form->{duetyp} eq "11" && do {
1762 $form->{fromdate} = "1.11.$form->{year}";
1763 $form->{todate} = "30.11.$form->{year}";
1764 $form->{comparefromdate} = "1.01.$form->{year}";
1765 $form->{comparetodate} = "30.11.$form->{year}";
1768 $form->{duetyp} eq "12" && do {
1769 $form->{fromdate} = "1.12.$form->{year}";
1770 $form->{todate} = "31.12.$form->{year}";
1771 $form->{comparefromdate} = "1.01.$form->{year}";
1772 $form->{comparetodate} = "31.12.$form->{year}";
1776 hotfix_reformat_date();
1778 # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
1779 # ansonsten ist die prüfung in RP.pm
1780 # if (defined ($form->{fromdate|todate}=='..'))
1782 if ($form->{fromdate}){
1783 my ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
1784 my $datetime = $locale->parse_date_to_object(\%myconfig, $form->{fromdate});
1785 $datetime->set( month => 1,
1787 $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
1789 if ($form->{todate}){
1790 $form->{comparetodate} = $form->{todate};
1794 RP->bwa(\%myconfig, \%$form);
1796 ($form->{department}) = split /--/, $form->{department};
1799 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1800 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1802 # if there are any dates construct a where
1803 if ($form->{fromdate} || $form->{todate}) {
1805 unless ($form->{todate}) {
1806 $form->{todate} = $form->current_date(\%myconfig);
1809 my %germandate = ("dateformat" => "dd.mm.yyyy");
1811 my $longtodate = $locale->date(\%germandate, $form->{todate}, 1);
1812 my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
1814 my $longfromdate = $locale->date(\%germandate, $form->{fromdate}, 1);
1815 my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
1817 $form->{this_period} = "$shortfromdate\n$shorttodate";
1819 $locale->text('for Period')
1820 . qq|\n$longfromdate |
1821 . $locale->text('bis')
1825 # setup variables for the form
1826 my @a = qw(company address businessnumber);
1827 map { $form->{$_} = $myconfig{$_} } @a;
1828 $form->{templates} = $myconfig{templates};
1830 $form->{IN} = "bwa.html";
1832 $form->parse_template;
1834 $main::lxdebug->leave_sub();
1837 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
1838 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
1839 sub hotfix_reformat_date {
1841 $main::lxdebug->enter_sub();
1843 my $form = $main::form;
1844 my %myconfig = %main::myconfig;
1845 my $locale = $main::locale;
1847 if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
1848 my $current_dateformat = $myconfig{dateformat};
1849 $myconfig{dateformat} = 'dd.mm.yyyy';
1850 $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
1851 $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
1852 $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
1853 unless (!defined ($form->{comparefromdate}));
1854 $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
1855 unless (!defined ($form->{comparetodate}));
1857 # Und wieder zurücksetzen
1858 $myconfig{dateformat} = $current_dateformat; #'dd.mm.yyyy';
1859 } # Ende Hotifx Bug 1388
1861 $main::lxdebug->leave_sub();