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., 51 Franklin Street, Fifth Floor, Boston,
33 #======================================================================
35 # module for preparing Income Statement and Balance Sheet
37 #======================================================================
39 use POSIX qw(strftime);
46 use SL::Locale::String qw(t8);
47 use SL::Presenter::Tag;
48 use SL::ReportGenerator;
50 use List::MoreUtils qw(any);
52 require "bin/mozilla/common.pl";
53 require "bin/mozilla/reportgenerator.pl";
55 # note: this file was particularly hard to strictify.
56 # alot of the vars are passed carelessly between invocations
57 # should there be any missing vars, declare them globally
60 # this is for our long dates
61 # $locale->text('January')
62 # $locale->text('February')
63 # $locale->text('March')
64 # $locale->text('April')
65 # $locale->text('May ')
66 # $locale->text('June')
67 # $locale->text('July')
68 # $locale->text('August')
69 # $locale->text('September')
70 # $locale->text('October')
71 # $locale->text('November')
72 # $locale->text('December')
74 # this is for our short month
75 # $locale->text('Jan')
76 # $locale->text('Feb')
77 # $locale->text('Mar')
78 # $locale->text('Apr')
79 # $locale->text('May')
80 # $locale->text('Jun')
81 # $locale->text('Jul')
82 # $locale->text('Aug')
83 # $locale->text('Sep')
84 # $locale->text('Oct')
85 # $locale->text('Nov')
86 # $locale->text('Dec')
88 # $locale->text('Balance Sheet')
89 # $locale->text('Income Statement')
90 # $locale->text('Trial Balance')
91 # $locale->text('AR Aging')
92 # $locale->text('AP Aging')
93 # $locale->text('Search AR Aging')
94 # $locale->text('Search AP Aging')
95 # $locale->text('Tax collected')
96 # $locale->text('Tax paid')
97 # $locale->text('Receipts')
98 # $locale->text('Payments')
99 # $locale->text('Project Transactions')
100 # $locale->text('Business evaluation')
101 # $locale->text('Final Invoice, please use mark as paid manually')
103 # $form->parse_html_template('rp/html_report_susa')
105 my $rp_access_map = {
106 'projects' => 'report',
107 'ar_aging' => 'general_ledger',
108 'ap_aging' => 'general_ledger',
109 'receipts' => 'cash',
110 'payments' => 'cash',
111 'trial_balance' => 'report',
112 'income_statement' => 'report',
113 'erfolgsrechnung' => 'report',
115 'balance_sheet' => 'report',
118 sub check_rp_access {
119 my $form = $main::form;
121 my $right = $rp_access_map->{$form->{report}};
122 $right ||= 'DOES_NOT_EXIST';
124 $main::auth->assert($right);
128 $::lxdebug->enter_sub;
133 balance_sheet => $::locale->text('Balance Sheet'),
134 income_statement => $::locale->text('Income Statement'),
135 erfolgsrechnung => $::locale->text('Erfolgsrechnung'),
136 trial_balance => $::locale->text('Trial Balance'),
137 ar_aging => $::locale->text('Search AR Aging'),
138 ap_aging => $::locale->text('Search AP Aging'),
139 tax_collected => $::locale->text('Tax collected'),
140 tax_paid => $::locale->text('Tax paid'),
141 receipts => $::locale->text('Receipts'),
142 payments => $::locale->text('Payments'),
143 projects => $::locale->text('Project Transactions'),
144 bwa => $::locale->text('Business evaluation'),
147 $::form->{title} = $title{$::form->{report}};
148 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
149 $::request->{layout}->add_javascripts('autocomplete_project.js');
150 $::form->{fromdate} = DateTime->today->truncate(to => 'year')->to_kivitendo;
151 $::form->{todate} = DateTime->today->truncate(to => 'year')->add(years => 1)->add(days => -1)->to_kivitendo;
154 $::form->all_departments(\%::myconfig);
155 if (@{ $::form->{all_departments} || [] }) {
156 $::form->{selectdepartment} = "<option>\n";
157 map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
160 $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
162 my $is_projects = $::form->{report} eq "projects";
163 my $is_income_statement = $::form->{report} eq "income_statement";
164 my $is_erfolgsrechnung = $::form->{report} eq "erfolgsrechnung";
165 my $is_bwa = $::form->{report} eq "bwa";
166 my $is_balance_sheet = $::form->{report} eq "balance_sheet";
167 my $is_trial_balance = $::form->{report} eq "trial_balance";
168 my $is_aging = $::form->{report} =~ /^a[rp]_aging$/;
169 my $is_payments = $::form->{report} =~ /(receipts|payments)$/;
172 my ($label, $nextsub, $vc);
174 my $is_sales = $::form->{report} eq 'ar_aging';
175 $label = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
176 $::form->{vc} = $is_sales ? 'customer' : 'vendor';
178 $nextsub = "generate_$::form->{report}";
180 $vc = qq|<input name=$::form->{vc} size=35 class="initial_focus">|;
185 my ($selection, $paymentaccounts);
187 $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
189 RP->paymentaccounts(\%::myconfig, $::form);
191 $selection = "<option>\n";
192 for my $ref (@{ $::form->{PR} }) {
193 $paymentaccounts .= "$ref->{accno} ";
194 $selection .= "<option>$ref->{accno}--$ref->{description}\n";
198 setup_rp_report_action_bar();
201 print $::form->parse_html_template('rp/report', {
202 paymentaccounts => $paymentaccounts,
203 selection => $selection,
204 is_aging => $is_aging,
207 year => DateTime->today->year,
208 today => DateTime->today,
210 is_payments => $is_payments,
211 is_trial_balance => $is_trial_balance,
212 is_balance_sheet => $is_balance_sheet,
214 is_income_statement => $is_income_statement,
215 is_erfolgsrechnung => $is_erfolgsrechnung,
216 is_projects => $is_projects,
220 $::lxdebug->leave_sub;
223 sub continue { call_sub($main::form->{"nextsub"}); }
225 sub generate_income_statement {
226 $main::lxdebug->enter_sub();
228 $main::auth->assert('report');
230 my $form = $main::form;
231 my %myconfig = %main::myconfig;
232 my $locale = $main::locale;
234 $form->{padding} = " ";
235 $form->{bold} = "<b>";
236 $form->{endbold} = "</b>";
237 $form->{br} = "<br>";
239 if ($form->{reporttype} eq "custom") {
241 #forgotten the year --> thisyear
242 if ($form->{year} !~ m/^\d\d\d\d$/) {
243 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
249 if ($form->{duetyp} eq "13") {
250 $form->{fromdate} = "1.1.$form->{year}";
251 $form->{todate} = "31.12.$form->{year}";
255 if ($form->{duetyp} eq "A") {
256 $form->{fromdate} = "1.1.$form->{year}";
257 $form->{todate} = "31.3.$form->{year}";
259 if ($form->{duetyp} eq "B") {
260 $form->{fromdate} = "1.4.$form->{year}";
261 $form->{todate} = "30.6.$form->{year}";
263 if ($form->{duetyp} eq "C") {
264 $form->{fromdate} = "1.7.$form->{year}";
265 $form->{todate} = "30.9.$form->{year}";
267 if ($form->{duetyp} eq "D") {
268 $form->{fromdate} = "1.10.$form->{year}";
269 $form->{todate} = "31.12.$form->{year}";
274 $form->{duetyp} eq "1" && do {
275 $form->{fromdate} = "1.1.$form->{year}";
276 $form->{todate} = "31.1.$form->{year}";
279 $form->{duetyp} eq "2" && do {
280 $form->{fromdate} = "1.2.$form->{year}";
282 #this works from 1901 to 2099, 1900 and 2100 fail.
283 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
284 $form->{todate} = "$leap.2.$form->{year}";
287 $form->{duetyp} eq "3" && do {
288 $form->{fromdate} = "1.3.$form->{year}";
289 $form->{todate} = "31.3.$form->{year}";
292 $form->{duetyp} eq "4" && do {
293 $form->{fromdate} = "1.4.$form->{year}";
294 $form->{todate} = "30.4.$form->{year}";
297 $form->{duetyp} eq "5" && do {
298 $form->{fromdate} = "1.5.$form->{year}";
299 $form->{todate} = "31.5.$form->{year}";
302 $form->{duetyp} eq "6" && do {
303 $form->{fromdate} = "1.6.$form->{year}";
304 $form->{todate} = "30.6.$form->{year}";
307 $form->{duetyp} eq "7" && do {
308 $form->{fromdate} = "1.7.$form->{year}";
309 $form->{todate} = "31.7.$form->{year}";
312 $form->{duetyp} eq "8" && do {
313 $form->{fromdate} = "1.8.$form->{year}";
314 $form->{todate} = "31.8.$form->{year}";
317 $form->{duetyp} eq "9" && do {
318 $form->{fromdate} = "1.9.$form->{year}";
319 $form->{todate} = "30.9.$form->{year}";
322 $form->{duetyp} eq "10" && do {
323 $form->{fromdate} = "1.10.$form->{year}";
324 $form->{todate} = "31.10.$form->{year}";
327 $form->{duetyp} eq "11" && do {
328 $form->{fromdate} = "1.11.$form->{year}";
329 $form->{todate} = "30.11.$form->{year}";
332 $form->{duetyp} eq "12" && do {
333 $form->{fromdate} = "1.12.$form->{year}";
334 $form->{todate} = "31.12.$form->{year}";
338 hotfix_reformat_date();
339 } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
341 RP->income_statement(\%myconfig, \%$form);
343 ($form->{department}) = split /--/, $form->{department};
346 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
347 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
349 # if there are any dates construct a where
350 if ($form->{fromdate} || $form->{todate}) {
352 unless ($form->{todate}) {
353 $form->{todate} = $form->current_date(\%myconfig);
356 my $longtodate = $locale->date(\%myconfig, $form->{todate}, 1);
357 my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
359 my $longfromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
360 my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
362 $form->{this_period} = "$shortfromdate\n$shorttodate";
364 $locale->text('for Period')
365 . qq|\n$longfromdate |
366 . $locale->text('Bis')
370 if ($form->{comparefromdate} || $form->{comparetodate}) {
371 my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
372 my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
374 my $longcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 1);
375 my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
377 $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
379 "\n$longcomparefromdate "
380 . $locale->text('Bis')
381 . qq| $longcomparetodate|;
384 if ( $::instance_conf->get_profit_determination eq 'balance' ) {
385 $form->{title} = $locale->text('Income Statement');
386 } elsif ( $::instance_conf->get_profit_determination eq 'income' ) {
387 $form->{title} = $locale->text('Net Income Statement');
392 if ( $form->{method} eq 'cash' ) {
393 $form->{accounting_method} = $locale->text('Cash accounting');
394 } elsif ( $form->{method} eq 'accrual' ) {
395 $form->{accounting_method} = $locale->text('Accrual accounting');
397 $form->{accounting_method} = "";
400 $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
403 print $form->parse_html_template('rp/income_statement');
405 $main::lxdebug->leave_sub();
408 sub generate_erfolgsrechnung {
409 $::lxdebug->enter_sub;
410 $::auth->assert('report');
412 $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
413 $::form->{padding} = " ";
414 $::form->{bold} = "<b>";
415 $::form->{endbold} = "</b>";
416 $::form->{br} = "<br>";
418 my $data = RP->erfolgsrechnung(\%::myconfig, $::form);
421 print $::form->parse_html_template('rp/erfolgsrechnung', $data);
423 $::lxdebug->leave_sub;
427 sub generate_balance_sheet {
428 $::lxdebug->enter_sub;
429 $::auth->assert('report');
431 $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
432 $::form->{padding} = " ";
433 $::form->{bold} = "<b>";
434 $::form->{endbold} = "</b>";
435 $::form->{br} = "<br>";
437 my $data = RP->balance_sheet(\%::myconfig, $::form);
439 $::form->{asofdate} ||= $::form->current_date;
440 $::form->{report_title} = $::locale->text('Balance Sheet');
441 $::form->{report_date} ||= $::form->current_date;
443 ($::form->{department}) = split /--/, $::form->{department};
445 # define Current Earnings account
446 my $padding = $::form->{l_heading} ? $::form->{padding} : "";
447 push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
449 $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
450 $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
452 # balance sheet isn't read from print templates anymore,
453 # instead use template in rp
454 # $::form->{IN} = "balance_sheet.html";
457 print $::form->parse_html_template('rp/balance_sheet', $data);
459 $::lxdebug->leave_sub;
462 sub generate_projects {
463 $main::lxdebug->enter_sub();
465 $main::auth->assert('report');
467 my $form = $main::form;
468 my %myconfig = %main::myconfig;
469 my $locale = $main::locale;
471 my $project = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
472 $form->{projectnumber} = $project ? $project->projectnumber : '';
474 # make sure todate and fromdate always have a value, even if the date fields
475 # were left empty or the inputs weren't valid dates/couldn't be parsed
477 $project = SL::DB::Project->new() unless $project; # dummy object for dbh
478 unless ($::locale->parse_date_to_object($::form->{fromdate})) {
479 ($form->{fromdate}) = $project->db->dbh->selectrow_array('select min(transdate) from acc_trans');
482 unless ($::locale->parse_date_to_object($::form->{todate})) {
483 ($form->{todate}) = $project->db->dbh->selectrow_array('select max(transdate) from acc_trans');
486 $form->{nextsub} = "generate_projects";
487 $form->{title} = $locale->text('Project Transactions');
488 RP->trial_balance(\%myconfig, \%$form);
490 list_accounts('generate_projects');
492 $main::lxdebug->leave_sub();
498 # included links to display transactions for period entered
499 # added headers and subtotals
501 sub generate_trial_balance {
502 $main::lxdebug->enter_sub();
504 $main::auth->assert('report');
506 my $form = $main::form;
507 my %myconfig = %main::myconfig;
508 my $locale = $main::locale;
509 my $defaults = SL::DB::Default->get;
511 if ($form->{reporttype} eq "custom") {
513 #forgotten the year --> thisyear
514 if ($form->{year} !~ m/^\d\d\d\d$/) {
515 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
521 if ($form->{duetyp} eq "13") {
522 $form->{fromdate} = "1.1.$form->{year}";
523 $form->{todate} = "31.12.$form->{year}";
527 if ($form->{duetyp} eq "A") {
528 $form->{fromdate} = "1.1.$form->{year}";
529 $form->{todate} = "31.3.$form->{year}";
531 if ($form->{duetyp} eq "B") {
532 $form->{fromdate} = "1.4.$form->{year}";
533 $form->{todate} = "30.6.$form->{year}";
535 if ($form->{duetyp} eq "C") {
536 $form->{fromdate} = "1.7.$form->{year}";
537 $form->{todate} = "30.9.$form->{year}";
539 if ($form->{duetyp} eq "D") {
540 $form->{fromdate} = "1.10.$form->{year}";
541 $form->{todate} = "31.12.$form->{year}";
546 $form->{duetyp} eq "1" && do {
547 $form->{fromdate} = "1.1.$form->{year}";
548 $form->{todate} = "31.1.$form->{year}";
551 $form->{duetyp} eq "2" && do {
552 $form->{fromdate} = "1.2.$form->{year}";
554 #this works from 1901 to 2099, 1900 and 2100 fail.
555 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
556 $form->{todate} = "$leap.2.$form->{year}";
559 $form->{duetyp} eq "3" && do {
560 $form->{fromdate} = "1.3.$form->{year}";
561 $form->{todate} = "31.3.$form->{year}";
564 $form->{duetyp} eq "4" && do {
565 $form->{fromdate} = "1.4.$form->{year}";
566 $form->{todate} = "30.4.$form->{year}";
569 $form->{duetyp} eq "5" && do {
570 $form->{fromdate} = "1.5.$form->{year}";
571 $form->{todate} = "31.5.$form->{year}";
574 $form->{duetyp} eq "6" && do {
575 $form->{fromdate} = "1.6.$form->{year}";
576 $form->{todate} = "30.6.$form->{year}";
579 $form->{duetyp} eq "7" && do {
580 $form->{fromdate} = "1.7.$form->{year}";
581 $form->{todate} = "31.7.$form->{year}";
584 $form->{duetyp} eq "8" && do {
585 $form->{fromdate} = "1.8.$form->{year}";
586 $form->{todate} = "31.8.$form->{year}";
589 $form->{duetyp} eq "9" && do {
590 $form->{fromdate} = "1.9.$form->{year}";
591 $form->{todate} = "30.9.$form->{year}";
594 $form->{duetyp} eq "10" && do {
595 $form->{fromdate} = "1.10.$form->{year}";
596 $form->{todate} = "31.10.$form->{year}";
599 $form->{duetyp} eq "11" && do {
600 $form->{fromdate} = "1.11.$form->{year}";
601 $form->{todate} = "30.11.$form->{year}";
604 $form->{duetyp} eq "12" && do {
605 $form->{fromdate} = "1.12.$form->{year}";
606 $form->{todate} = "31.12.$form->{year}";
610 hotfix_reformat_date();
614 # get for each account initial balance, debits and credits
615 RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
618 $form->{rowcount} = scalar @{ $form->{TB} || [] };
619 $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
622 "accno", "description",
623 "last_transaction", "soll_eb",
626 "soll_kumuliert", "haben_kumuliert",
627 "soll_saldo", "haben_saldo"
631 my $attachment_basename = $locale->text('trial_balance');
632 my $report = SL::ReportGenerator->new(\%myconfig, $form);
634 my @hidden_variables = qw(fromdate todate year method department_id all_accounts);
636 my $href = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
639 'accno' => { 'text' => $locale->text('Account'), },
640 'description' => { 'text' => $locale->text('Description'), },
641 'last_transaction' => { 'text' => $locale->text('Last Transaction'), },
642 'soll_eb' => { 'text' => $locale->text('Debit Starting Balance'), },
643 'haben_eb' => { 'text' => $locale->text('Credit Starting Balance'), },
644 'soll' => { 'text' => $locale->text('Debit'), },
645 'haben' => { 'text' => $locale->text('Credit'), },
646 'soll_kumuliert' => { 'text' => $locale->text('Sum Debit'), },
647 'haben_kumuliert' => { 'text' => $locale->text('Sum Credit'), },
648 'soll_saldo' => { 'text' => $locale->text('Saldo Debit'), },
649 'haben_saldo' => { 'text' => $locale->text('Saldo Credit'), }
654 my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
656 map { $column_defs{$_}->{visible} = 1 } @columns;
658 $report->set_columns(%column_defs);
659 $report->set_column_order(@columns);
661 $report->set_export_options('generate_trial_balance', @hidden_variables);
666 $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
668 $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
669 push (@options, $form->{print_date});
671 $form->{company} = $locale->text('Company') . " " . $defaults->company;
672 push (@options, $form->{company});
674 if ($::form->{customer_id}) {
675 my $customer = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id});
676 push @options, $::locale->text('Customer') . ' ' . $customer->displayable_name;
680 $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
682 my @custom_headers = ([
683 { text => $::locale->text('Account'), rowspan => 2, },
684 { text => $::locale->text('Description'), rowspan => 2, },
685 { text => $::locale->text('Last Transaction'), rowspan => 2, },
686 { text => $::locale->text('Starting Balance'), colspan => 2, },
687 { text => $::locale->text('Sum for') . " $form->{template_fromto}", colspan => 2, },
688 { text => $::locale->text('Sum per') . " $form->{template_to}", colspan => 2, },
689 { text => $::locale->text('Saldo per') . " $form->{template_to}", colspan => 2, },
694 { text => $::locale->text('Assets'), },
695 { text => $::locale->text('Equity'), },
696 { text => $::locale->text('Debit'), },
697 { text => $::locale->text('Credit'), },
698 { text => $::locale->text('Debit'), },
699 { text => $::locale->text('Credit'), },
700 { text => $::locale->text('Debit'), },
701 { text => $::locale->text('Credit'), },
704 $report->set_options('output_format' => 'HTML',
705 'top_info_text' => join("\n", @options),
706 'title' => $form->{title},
707 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
708 'html_template' => 'rp/html_report_susa',
709 'pdf_template' => 'rp/html_report_susa',
711 $report->set_custom_headers(@custom_headers);
712 $report->set_options_from_form();
713 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
715 # add sort and escape callback, this one we use for the add sub
716 $form->{callback} = $href .= "&sort=$form->{sort}";
718 # escape callback for href
719 my $callback = $form->escape($href);
721 my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
723 my %totals = map { $_ => 0 } @subtotal_columns;
725 my $edit_url = build_std_url('action=edit', 'type', 'vc');
728 foreach my $accno (@{ $form->{TB} || [] }) {
730 $accno->{soll} = $accno->{debit};
731 $accno->{haben} = $accno->{credit};
732 map { $totals{$_} += $accno->{$_} } @subtotal_columns;
734 map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
735 qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
739 foreach my $column (@columns) {
741 'data' => $accno->{$column},
742 'align' => $column_alignment{$column},
746 $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}));
748 my $row_set = [ $row ];
751 $report->add_data($row_set);
756 $report->add_separator();
758 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
760 $report->generate_with_headers();
762 $main::lxdebug->leave_sub();
766 sub create_subtotal_row {
767 $main::lxdebug->enter_sub();
769 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
771 my $form = $main::form;
772 my %myconfig = %main::myconfig;
773 my $locale = $main::locale;
775 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
777 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
779 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
781 map { $totals->{$_} = 0 } @{ $subtotal_columns };
783 $main::lxdebug->leave_sub();
788 sub create_list_accounts_subtotal_row {
789 $main::lxdebug->enter_sub();
791 my ($subtotals, $columns, $fields, $class) = @_;
793 my $form = $main::form;
794 my %myconfig = %main::myconfig;
795 my $locale = $main::locale;
797 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
799 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
801 $main::lxdebug->leave_sub();
807 $main::lxdebug->enter_sub();
811 my $form = $main::form;
812 my %myconfig = %main::myconfig;
813 my $locale = $main::locale;
816 if ($form->{department}) {
817 my ($department) = split /--/, $form->{department};
818 push @options, $locale->text('Department') . " : $department";
820 if ($form->{projectnumber}) {
821 push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
824 # if there are any dates
825 if ($form->{fromdate} || $form->{todate}) {
826 my ($fromdate, $todate);
828 if ($form->{fromdate}) {
829 $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
831 if ($form->{todate}) {
832 $todate = $locale->date(\%myconfig, $form->{todate}, 1);
835 push @options, "$fromdate - $todate";
838 push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
841 my @columns = qw(accno description begbalance debit credit endbalance);
843 'accno' => { 'text' => $locale->text('Account'), },
844 'description' => { 'text' => $locale->text('Description'), },
845 'debit' => { 'text' => $locale->text('Debit'), },
846 'credit' => { 'text' => $locale->text('Credit'), },
847 'begbalance' => { 'text' => $locale->text('Balance'), },
848 'endbalance' => { 'text' => $locale->text('Balance'), },
850 my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
852 my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
854 $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
856 my $report = SL::ReportGenerator->new(\%myconfig, $form);
858 $report->set_options('top_info_text' => join("\n", @options),
859 'output_format' => 'HTML',
860 'title' => $form->{title},
861 'attachment_basename' => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
862 'std_column_visibility' => 1,
864 $report->set_options_from_form();
865 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
867 $report->set_columns(%column_defs);
868 $report->set_column_order(@columns);
870 $report->set_export_options($action, @hidden_variables);
872 my @totals_columns = qw(credit debit begbalance endbalance);
873 my %subtotals = map { $_ => 0 } @totals_columns;
874 my %totals = map { $_ => 0 } @totals_columns;
875 my $found_heading = 0;
876 my @tb = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
878 # sort the whole thing by account numbers and display
879 foreach my $idx (0 .. scalar(@tb) - 1) {
881 my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
883 my $ml = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
885 my $row = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
887 if ($ref->{charttype} eq 'H') {
888 next unless ($form->{l_heading});
890 %subtotals = map { $_ => 0 } @totals_columns;
892 $row->{description}->{class} = 'listheading';
893 $row->{description}->{data} = $ref->{description};
895 $report->add_data($row);
900 foreach (qw(debit credit)) {
901 $subtotals{$_} += $ref->{$_};
902 $totals{$_} += $ref->{$_};
905 $subtotals{begbalance} += $ref->{balance} * $ml;
906 $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
908 map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
909 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
911 $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
912 $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
914 $report->add_data($row);
916 if ($form->{l_heading} && $found_heading &&
917 (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
918 $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
922 $report->add_separator();
924 $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
926 $report->generate_with_headers();
928 $main::lxdebug->leave_sub();
931 sub generate_ar_aging {
932 $main::lxdebug->enter_sub();
934 $main::auth->assert('general_ledger | ar_transactions');
936 my $form = $main::form;
937 my %myconfig = %main::myconfig;
938 my $locale = $main::locale;
941 ($form->{customer}) = split(/--/, $form->{customer});
943 $form->{ct} = "customer";
944 $form->{arap} = "ar";
946 $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
948 RP->aging(\%myconfig, \%$form);
951 $main::lxdebug->leave_sub();
954 sub generate_ap_aging {
955 $main::lxdebug->enter_sub();
957 $main::auth->assert('general_ledger | ap_transactions');
959 my $form = $main::form;
960 my %myconfig = %main::myconfig;
961 my $locale = $main::locale;
964 ($form->{vendor}) = split(/--/, $form->{vendor});
966 $form->{ct} = "vendor";
967 $form->{arap} = "ap";
969 $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
971 RP->aging(\%myconfig, \%$form);
974 $main::lxdebug->leave_sub();
977 sub create_aging_subtotal_row {
978 $main::lxdebug->enter_sub();
980 my ($subtotals, $columns, $periods, $class) = @_;
982 my $form = $main::form;
983 my %myconfig = %main::myconfig;
984 my $locale = $main::locale;
986 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
988 foreach (@{ $periods }) {
989 $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
990 $subtotals->{$_} = 0;
993 $main::lxdebug->leave_sub();
999 $main::lxdebug->enter_sub();
1001 $main::auth->assert('general_ledger');
1003 my $form = $main::form;
1004 my %myconfig = %main::myconfig;
1005 my $locale = $main::locale;
1006 my $cgi = $::request->{cgi};
1008 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1010 my @columns = qw(statement ct invnumber transdate duedate amount open datepaid current_open type);
1012 'statement' => { raw_header_data => SL::Presenter::Tag::checkbox_tag("checkall", checkall => '[name^=statement_]'), 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, align => "center" },
1013 'ct' => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1014 'invnumber' => { 'text' => $locale->text('Invoice'), },
1015 'transdate' => { 'text' => $locale->text('Date'), },
1016 'duedate' => { 'text' => $locale->text('Due'), },
1017 'amount' => { 'text' => $locale->text('Amount'), },
1018 'open' => { 'text' => $locale->text('Open'), },
1019 'datepaid' => { 'text' => $locale->text('Date of Last Payment'), visible => ($form->{reporttype} eq 'custom') },
1020 'current_open' => { 'text' => $locale->text('Open Amount at Last Payment Date'), visible => ($form->{reporttype} eq 'custom') },
1021 'type' => { 'text' => $locale->text('Note'), },
1024 my %column_alignment = ('statement' => 'center',
1025 map { $_ => 'right' } qw(open amount current_open datepaid));
1027 $report->set_options('std_column_visibility' => 1);
1028 $report->set_columns(%column_defs);
1029 $report->set_column_order(@columns);
1030 my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype department fromdate);
1031 $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
1034 my $attachment_basename;
1036 if ($form->{department}) {
1037 my ($department) = split /--/, $form->{department};
1038 push @options, $locale->text('Department') . " : $department";
1039 $form->{callback} .= "&department=" . E($department);
1042 if (($form->{arap} eq 'ar') && $form->{customer}) {
1043 push @options, $form->{customer};
1044 $attachment_basename = $locale->text('ar_aging_list');
1045 $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
1048 if (($form->{arap} eq 'ap') && $form->{vendor}) {
1049 push @options, $form->{vendor};
1050 $attachment_basename = $locale->text('ap_aging_list');
1051 $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
1054 $form->{callback} .= "&reporttype=" . E($form->{reporttype});
1055 if ($form->{reporttype} eq 'free') {
1056 if ($form->{fromdate}) {
1057 push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .
1058 $locale->date(\%myconfig, $form->{fromdate}, 1) . " " .
1059 $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1061 push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " .
1062 $locale->date(\%myconfig, $form->{todate}, 1);
1064 } elsif ($form->{reporttype} eq 'custom') {
1065 push @options, $locale->text('Reference day') . " " . $locale->date(\%myconfig, $form->{fordate}, 1);
1067 die "Unknown reporttype for aging";
1070 $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1072 $report->set_options('top_info_text' => join("\n", @options),
1073 'output_format' => 'HTML',
1074 'title' => $form->{title},
1075 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1077 $report->set_options_from_form();
1078 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1080 my $previous_ctid = 0;
1082 my @periods = qw(open amount current_open);
1083 my %subtotals = map { $_ => 0 } @periods;
1084 my %totals = map { $_ => 0 } @periods;
1086 foreach my $ref (@{ $form->{AG} }) {
1087 if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1088 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1091 foreach my $key (@periods) {
1092 $subtotals{$key} += $ref->{"$key"};
1093 $totals{$key} += $ref->{"$key"};
1094 $ref->{"$key"} = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
1099 foreach my $column (@columns) {
1101 'data' => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1102 'align' => $column_alignment{$column},
1103 'valign' => $column eq 'statement' ? 'center' : '',
1107 $row->{invnumber}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1108 if ($row->{type}->{data} eq 'final_invoice') {
1109 $row->{type}->{data} = $locale->text('Final Invoice, please use mark as paid manually');
1110 $row->{type}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1112 $row->{type}->{data} = '';
1115 if ($previous_ctid != $ref->{ctid}) {
1116 $row->{statement}->{raw_data} =
1117 $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
1118 . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1119 $row->{ct}->{data} = $ref->{name};
1124 $previous_ctid = $ref->{ctid};
1126 $report->add_data($row);
1129 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1131 $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1133 if ($form->{arap} eq 'ar') {
1134 my $raw_top_info_text = $form->parse_html_template('rp/aging_ar_top');
1135 my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1136 'PRINT_OPTIONS' => print_options(inline => 1), });
1137 $report->set_options('raw_top_info_text' => $raw_top_info_text,
1138 'raw_bottom_info_text' => $raw_bottom_info_text);
1141 setup_rp_aging_action_bar(arap => $form->{arap});
1142 $report->generate_with_headers();
1144 $main::lxdebug->leave_sub();
1148 $main::lxdebug->enter_sub();
1150 $main::auth->assert('general_ledger');
1152 my $form = $main::form;
1153 my %myconfig = %main::myconfig;
1154 my $locale = $main::locale;
1156 $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1157 unless $form->{subject};
1159 RP->aging(\%myconfig, \%$form);
1162 my $email_form = delete $form->{email_form};
1163 my %field_names = (to => 'email');
1165 $form->{ $field_names{$_} // $_ } = $email_form->{$_} for keys %{ $email_form };
1166 $form->{media} = 'email';
1169 $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1171 $main::lxdebug->leave_sub();
1175 $main::lxdebug->enter_sub();
1177 $main::auth->assert('general_ledger');
1179 my $form = $main::form;
1180 my %myconfig = %main::myconfig;
1181 my $locale = $main::locale;
1183 if ($form->{media} eq 'printer') {
1184 $form->error($locale->text('Select postscript or PDF!'))
1185 if ($form->{format} !~ /(postscript|pdf)/);
1189 for my $i (1 .. $form->{rowcount}) {
1190 if ($form->{"statement_$i"}) {
1191 $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1197 $form->error($locale->text('Nothing selected!')) unless $selected;
1199 if ($form->{media} eq 'printer') {
1200 $form->{"$form->{ct}_id"} = "";
1202 $form->{"statement_1"} = 1;
1205 RP->aging(\%myconfig, \%$form);
1209 $form->redirect($locale->text('Statements sent to printer!'))
1210 if ($form->{media} eq 'printer');
1212 $main::lxdebug->leave_sub();
1216 $main::lxdebug->enter_sub();
1218 $main::auth->assert('general_ledger');
1220 my $form = $main::form;
1221 my %myconfig = %main::myconfig;
1222 my $locale = $main::locale;
1224 my $defaults = SL::DB::Default->get;
1225 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
1226 $form->{templates} = $defaults->templates;
1228 $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1230 my $suffix = "html";
1231 my $attachment_suffix = "html";
1232 if ($form->{format} eq 'postscript') {
1233 $form->{postscript} = 1;
1235 $attachment_suffix = "ps";
1236 } elsif ($form->{format} eq 'pdf') {
1239 $attachment_suffix = "pdf";
1242 $form->{IN} = "$form->{type}.$suffix";
1243 $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1245 # Save $form->{email} because it will be overwritten.
1246 $form->{EMAIL_RECIPIENT} = $form->{email};
1250 while (@{ $form->{AG} }) {
1252 my $ref = shift @{ $form->{AG} };
1254 if ($ctid != $ref->{ctid}) {
1256 $ctid = $ref->{ctid};
1259 if ($form->{"statement_$i"}) {
1262 ("name", "street", "zipcode", "city", "country", "contact", "email",
1263 "$form->{ct}phone", "$form->{ct}fax");
1264 map { $form->{$_} = $ref->{$_} } @a;
1266 $form->{ $form->{ct} } = $form->{name};
1267 $form->{"$form->{ct}_id"} = $ref->{ctid};
1269 map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
1271 foreach my $item (qw(c0 c30 c60 c90)) {
1272 $form->{$item} = ();
1273 $form->{"${item}total"} = 0;
1276 &statement_details($ref);
1280 if (scalar(@{ $form->{AG} }) > 0) {
1282 # one or more left to go
1283 if ($ctid == $form->{AG}->[0]->{ctid}) {
1284 $ref = shift @{ $form->{AG} };
1285 &statement_details($ref);
1288 $ref = scalar(@{ $form->{AG} });
1294 # set initial ref to 0
1301 $form->{"${_}total"} =
1302 $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1303 } ('c0', 'c30', 'c60', 'c90', "");
1305 $form->{attachment_filename} = $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
1306 $form->{attachment_filename} =~ s/\s+/_/g;
1308 $form->parse_template(\%myconfig);
1313 # saving the history
1314 if(!exists $form->{addition} && $form->{id} ne "") {
1315 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1316 $form->{addition} = "PRINTED";
1317 $form->{what_done} = $form->{type};
1318 $form->save_history;
1320 # /saving the history
1321 $main::lxdebug->leave_sub();
1324 sub statement_details {
1325 $main::lxdebug->enter_sub();
1327 $main::auth->assert('general_ledger');
1329 my $form = $main::form;
1330 my %myconfig = %main::myconfig;
1331 my $locale = $main::locale;
1335 push @{ $form->{invnumber} }, $ref->{invnumber};
1336 push @{ $form->{invdate} }, $ref->{transdate};
1337 push @{ $form->{duedate} }, $ref->{duedate};
1338 push @{ $form->{amount} }, $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
1339 push @{ $form->{open} }, $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
1341 foreach my $item (qw(c0 c30 c60 c90)) {
1342 if ($ref->{exchangerate} * 1) {
1343 # add only the open amount of the invoice to the aging, not the total amount
1344 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
1345 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
1346 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
1347 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
1349 $form->{"${item}total"} += $ref->{$item};
1350 $form->{total} += $ref->{$item};
1351 push @{ $form->{$item} },
1352 $form->format_amount(\%myconfig, $ref->{$item}, 2);
1355 $main::lxdebug->leave_sub();
1358 sub generate_tax_report {
1359 $::lxdebug->enter_sub;
1360 $::auth->assert('report');
1362 RP->tax_report(\%::myconfig, $::form);
1364 my $descvar = "$::form->{accno}_description";
1365 my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
1369 my $callback = build_std_url('action=generate_tax_report', $descvar,
1370 qw(fromdate todate db method accno department report title));
1372 my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1375 for my $item (@columns, 'subtotal') {
1376 if ($::form->{"l_$item"} eq "Y") {
1377 $callback .= "&l_$item=Y";
1378 $href .= "&l_$item=Y";
1382 for my $item (@columns) {
1383 if ($::form->{"l_$item"} eq "Y") {
1384 push @column_index, $item;
1389 if ($::form->{department}) {
1390 my ($department) = split /--/, $::form->{department};
1391 push @options, $::locale->text('Department') . " : $department";
1394 # if there are any dates
1395 if ($::form->{fromdate} || $::form->{todate}) {
1396 my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
1397 my $todate = $::form->{todate} ? $::locale->date(\%::myconfig, $::form->{todate}, 1) : '';
1398 push @options, "$fromdate - $todate";
1400 push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
1403 my ($name, $invoice, $arap);
1404 if ($::form->{db} eq 'ar') {
1405 $name = $::locale->text('Customer');
1409 if ($::form->{db} eq 'ap') {
1410 $name = $::locale->text('Vendor');
1415 my %column_header = (
1416 id => $::locale->text('ID'),
1417 invnumber => $::locale->text('Invoice'),
1418 transdate => $::locale->text('Date'),
1419 netamount => $::locale->text('Amount'),
1420 tax => $::locale->text('Tax'),
1421 amount => $::locale->text('Total'),
1425 my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
1427 $callback .= "&sort=$::form->{sort}";
1430 if (@{ $::form->{TR} }) {
1431 $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
1434 my ($totalnetamount, $totaltax, @data);
1435 for my $ref (@{ $::form->{TR} }) {
1437 my $module = ($ref->{invoice}) ? $invoice : $arap;
1439 if ($::form->{l_subtotal} eq 'Y') {
1440 if ($sameitem ne $ref->{ $::form->{sort} }) {
1443 netamount => $subtotalnetamount,
1444 tax => $subtotaltax,
1445 amount => $subtotal,
1447 $subtotalnetamount = 0;
1449 $sameitem = $ref->{ $::form->{sort} };
1453 $subtotalnetamount += $ref->{netamount};
1454 $subtotaltax += $ref->{tax};
1455 $totalnetamount += $ref->{netamount};
1456 $totaltax += $ref->{tax};
1457 $ref->{amount} = $ref->{netamount} + $ref->{tax};
1459 push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
1460 $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
1461 $data[-1]{$_}{numeric} = 1 for qw(netamount tax amount);
1464 if ($::form->{l_subtotal} eq 'Y') {
1467 netamount => $subtotalnetamount,
1468 tax => $subtotaltax,
1469 amount => $subtotal,
1475 netamount => $totalnetamount,
1477 amount => $totalnetamount + $totaltax,
1481 print $::form->parse_html_template('rp/tax_report', {
1482 column_index => \@column_index,
1483 column_header => \%column_header,
1484 column_sorted => \%column_sorted,
1487 options => \@options,
1490 $::lxdebug->leave_sub;
1494 $main::lxdebug->enter_sub();
1496 $main::auth->assert('cash');
1498 my $form = $main::form;
1499 my %myconfig = %main::myconfig;
1500 my $locale = $main::locale;
1502 if ($form->{account}) {
1503 ($form->{paymentaccounts}) = split /--/, $form->{account};
1507 if ($form->{department}) {
1508 (my $department, $form->{department_id}) = split /--/, $form->{department};
1509 $option = $locale->text('Department') . " : $department";
1512 report_generator_set_default_sort('transdate', 1);
1514 RP->payments(\%myconfig, \%$form);
1516 my @hidden_variables = qw(account title department reference source memo fromdate todate
1517 fx_transaction db prepayment paymentaccounts sort);
1519 my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
1520 $form->{callback} = $href;
1522 my @columns = qw(transdate invnumber name paid source memo);
1524 'name' => { 'text' => $locale->text('Description'), },
1525 'invnumber' => { 'text' => $locale->text('Reference'), },
1526 'transdate' => { 'text' => $locale->text('Date'), },
1527 'paid' => { 'text' => $locale->text('Amount'), },
1528 'source' => { 'text' => $locale->text('Source'), },
1529 'memo' => { 'text' => $locale->text('Memo'), },
1531 my %column_alignment = ('paid' => 'right');
1533 foreach my $name (grep { $_ ne 'paid' } @columns) {
1534 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1535 $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
1539 if ($form->{fromdate}) {
1540 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
1542 if ($form->{todate}) {
1543 push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1546 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1548 my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
1550 $report->set_options('top_info_text' => join("\n", @options),
1551 'output_format' => 'HTML',
1552 'title' => $form->{title},
1553 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1554 'std_column_visibility' => 1,
1556 $report->set_options_from_form();
1558 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1560 $report->set_columns(%column_defs);
1561 $report->set_column_order(@columns);
1563 $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
1565 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1569 foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1570 next unless @{ $form->{ $ref->{id} } };
1572 $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
1574 my $subtotal_paid = 0;
1576 foreach my $payment (@{ $form->{ $ref->{id} } }) {
1577 my $module = $payment->{module};
1578 $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1579 $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1581 $subtotal_paid += $payment->{paid};
1582 $total_paid += $payment->{paid};
1584 $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
1588 foreach my $column (@columns) {
1590 'data' => $payment->{$column},
1591 'align' => $column_alignment{$column},
1595 $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
1597 $report->add_data($row);
1600 my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
1602 'data' => $form->format_amount(\%myconfig, $subtotal_paid, 2),
1604 'class' => 'listsubtotal',
1607 $report->add_data($row);
1610 $report->add_separator();
1612 my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
1614 'data' => $form->format_amount(\%myconfig, $total_paid, 2),
1616 'class' => 'listtotal',
1619 $report->add_data($row);
1621 $report->generate_with_headers();
1623 $main::lxdebug->leave_sub();
1627 $::lxdebug->enter_sub;
1629 my ($dont_print) = @_;
1631 $::form->{sendmode} = "attachment";
1632 $::form->{format} ||= $::myconfig{template_format} || "pdf";
1633 $::form->{copies} ||= $::myconfig{copies} || 2;
1635 $::form->{PD}{ $::form->{type} } = "selected";
1636 $::form->{DF}{ $::form->{format} } = "selected";
1637 $::form->{OP}{ $::form->{media} } = "selected";
1638 $::form->{SM}{ $::form->{sendmode} } = "selected";
1640 my $output = $::form->parse_html_template('rp/print_options', {
1641 is_email => $::form->{media} eq 'email',
1644 print $output unless $dont_print;
1646 $::lxdebug->leave_sub;
1652 $main::lxdebug->enter_sub();
1654 $main::auth->assert('report');
1656 my $form = $main::form;
1657 my %myconfig = %main::myconfig;
1658 my $locale = $main::locale;
1660 $form->{padding} = " ";
1661 $form->{bold} = "<b>";
1662 $form->{endbold} = "</b>";
1663 $form->{br} = "<br>";
1665 if ($form->{reporttype} eq "custom") {
1667 #forgotten the year --> thisyear
1668 if ($form->{year} !~ m/^\d\d\d\d$/) {
1669 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1675 if ($form->{duetyp} eq "13") {
1676 $form->{fromdate} = "1.1.$form->{year}";
1677 $form->{todate} = "31.12.$form->{year}";
1678 $form->{comparefromdate} = "1.01.$form->{year}";
1679 $form->{comparetodate} = "31.12.$form->{year}";
1683 if ($form->{duetyp} eq "A") {
1684 $form->{fromdate} = "1.1.$form->{year}";
1685 $form->{todate} = "31.3.$form->{year}";
1686 $form->{comparefromdate} = "1.01.$form->{year}";
1687 $form->{comparetodate} = "31.03.$form->{year}";
1689 if ($form->{duetyp} eq "B") {
1690 $form->{fromdate} = "1.4.$form->{year}";
1691 $form->{todate} = "30.6.$form->{year}";
1692 $form->{comparefromdate} = "1.01.$form->{year}";
1693 $form->{comparetodate} = "30.06.$form->{year}";
1695 if ($form->{duetyp} eq "C") {
1696 $form->{fromdate} = "1.7.$form->{year}";
1697 $form->{todate} = "30.9.$form->{year}";
1698 $form->{comparefromdate} = "1.01.$form->{year}";
1699 $form->{comparetodate} = "30.09.$form->{year}";
1701 if ($form->{duetyp} eq "D") {
1702 $form->{fromdate} = "1.10.$form->{year}";
1703 $form->{todate} = "31.12.$form->{year}";
1704 $form->{comparefromdate} = "1.01.$form->{year}";
1705 $form->{comparetodate} = "31.12.$form->{year}";
1710 $form->{duetyp} eq "1" && do {
1711 $form->{fromdate} = "1.1.$form->{year}";
1712 $form->{todate} = "31.1.$form->{year}";
1713 $form->{comparefromdate} = "1.01.$form->{year}";
1714 $form->{comparetodate} = "31.01.$form->{year}";
1717 $form->{duetyp} eq "2" && do {
1718 $form->{fromdate} = "1.2.$form->{year}";
1720 #this works from 1901 to 2099, 1900 and 2100 fail.
1721 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1722 $form->{todate} = "$leap.2.$form->{year}";
1723 $form->{comparefromdate} = "1.01.$form->{year}";
1724 $form->{comparetodate} = "$leap.02.$form->{year}";
1727 $form->{duetyp} eq "3" && do {
1728 $form->{fromdate} = "1.3.$form->{year}";
1729 $form->{todate} = "31.3.$form->{year}";
1730 $form->{comparefromdate} = "1.01.$form->{year}";
1731 $form->{comparetodate} = "31.03.$form->{year}";
1734 $form->{duetyp} eq "4" && do {
1735 $form->{fromdate} = "1.4.$form->{year}";
1736 $form->{todate} = "30.4.$form->{year}";
1737 $form->{comparefromdate} = "1.01.$form->{year}";
1738 $form->{comparetodate} = "30.04.$form->{year}";
1741 $form->{duetyp} eq "5" && do {
1742 $form->{fromdate} = "1.5.$form->{year}";
1743 $form->{todate} = "31.5.$form->{year}";
1744 $form->{comparefromdate} = "1.01.$form->{year}";
1745 $form->{comparetodate} = "31.05.$form->{year}";
1748 $form->{duetyp} eq "6" && do {
1749 $form->{fromdate} = "1.6.$form->{year}";
1750 $form->{todate} = "30.6.$form->{year}";
1751 $form->{comparefromdate} = "1.01.$form->{year}";
1752 $form->{comparetodate} = "30.06.$form->{year}";
1755 $form->{duetyp} eq "7" && do {
1756 $form->{fromdate} = "1.7.$form->{year}";
1757 $form->{todate} = "31.7.$form->{year}";
1758 $form->{comparefromdate} = "1.01.$form->{year}";
1759 $form->{comparetodate} = "31.07.$form->{year}";
1762 $form->{duetyp} eq "8" && do {
1763 $form->{fromdate} = "1.8.$form->{year}";
1764 $form->{todate} = "31.8.$form->{year}";
1765 $form->{comparefromdate} = "1.01.$form->{year}";
1766 $form->{comparetodate} = "31.08.$form->{year}";
1769 $form->{duetyp} eq "9" && do {
1770 $form->{fromdate} = "1.9.$form->{year}";
1771 $form->{todate} = "30.9.$form->{year}";
1772 $form->{comparefromdate} = "1.01.$form->{year}";
1773 $form->{comparetodate} = "30.09.$form->{year}";
1776 $form->{duetyp} eq "10" && do {
1777 $form->{fromdate} = "1.10.$form->{year}";
1778 $form->{todate} = "31.10.$form->{year}";
1779 $form->{comparefromdate} = "1.01.$form->{year}";
1780 $form->{comparetodate} = "31.10.$form->{year}";
1783 $form->{duetyp} eq "11" && do {
1784 $form->{fromdate} = "1.11.$form->{year}";
1785 $form->{todate} = "30.11.$form->{year}";
1786 $form->{comparefromdate} = "1.01.$form->{year}";
1787 $form->{comparetodate} = "30.11.$form->{year}";
1790 $form->{duetyp} eq "12" && do {
1791 $form->{fromdate} = "1.12.$form->{year}";
1792 $form->{todate} = "31.12.$form->{year}";
1793 $form->{comparefromdate} = "1.01.$form->{year}";
1794 $form->{comparetodate} = "31.12.$form->{year}";
1798 hotfix_reformat_date();
1800 # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
1801 # ansonsten ist die prüfung in RP.pm
1802 # if (defined ($form->{fromdate|todate}=='..'))
1804 if ($form->{fromdate}){
1805 my $datetime = $locale->parse_date_to_object($form->{fromdate});
1806 $datetime->set( month => 1,
1808 $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
1810 if ($form->{todate}){
1811 $form->{comparetodate} = $form->{todate};
1815 RP->bwa(\%myconfig, \%$form);
1817 ($form->{department}) = split /--/, $form->{department};
1820 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1821 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1823 # if there are any dates construct a where
1824 if ($form->{fromdate} || $form->{todate}) {
1826 unless ($form->{todate}) {
1827 $form->{todate} = $form->current_date(\%myconfig);
1830 my %germandate = ("dateformat" => "dd.mm.yyyy");
1832 my $longtodate = $locale->date(\%germandate, $form->{todate}, 1);
1833 my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
1835 my $longfromdate = $locale->date(\%germandate, $form->{fromdate}, 1);
1836 my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
1838 $form->{this_period} = "$shortfromdate\n$shorttodate";
1840 $locale->text('for Period')
1841 . qq|\n$longfromdate |
1842 . $locale->text('bis')
1846 $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
1848 if ( $form->{method} eq 'cash' ) {
1849 $form->{accounting_method} = $locale->text('Cash accounting');
1850 } elsif ( $form->{method} eq 'accrual' ) {
1851 $form->{accounting_method} = $locale->text('Accrual accounting');
1853 $form->{accounting_method} = "";
1856 $form->{title} = $locale->text('BWA');
1858 $::request->layout->add_stylesheets('bwa.css');
1860 print $form->parse_html_template('rp/bwa');
1862 $main::lxdebug->leave_sub();
1865 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
1866 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
1867 sub hotfix_reformat_date {
1869 $main::lxdebug->enter_sub();
1871 my $form = $main::form;
1872 my %myconfig = %main::myconfig;
1873 my $locale = $main::locale;
1875 if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
1876 my $current_dateformat = $myconfig{dateformat};
1877 $myconfig{dateformat} = 'dd.mm.yyyy';
1878 $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
1879 $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
1880 $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
1881 unless (!defined ($form->{comparefromdate}));
1882 $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
1883 unless (!defined ($form->{comparetodate}));
1885 # Und wieder zurücksetzen
1886 $myconfig{dateformat} = $current_dateformat; #'dd.mm.yyyy';
1887 } # Ende Hotifx Bug 1388
1889 $main::lxdebug->leave_sub();
1893 sub setup_rp_aging_action_bar {
1896 return unless $params{arap} eq 'ar';
1898 for my $bar ($::request->layout->get('actionbar')) {
1903 call => [ 'kivi.SalesPurchase.show_print_dialog' ],
1904 checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
1908 call => [ 'kivi.SalesPurchase.show_email_dialog', 'send_email' ],
1909 checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
1916 sub setup_rp_report_action_bar {
1919 for my $bar ($::request->layout->get('actionbar')) {
1923 submit => [ '#form', { action => 'continue' } ],
1924 accesskey => 'enter',