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')
102 # $form->parse_html_template('rp/html_report_susa')
104 my $rp_access_map = {
105 'projects' => 'report',
106 'ar_aging' => 'general_ledger',
107 'ap_aging' => 'general_ledger',
108 'receipts' => 'cash',
109 'payments' => 'cash',
110 'trial_balance' => 'report',
111 'income_statement' => 'report',
112 'erfolgsrechnung' => 'report',
114 'balance_sheet' => 'report',
117 sub check_rp_access {
118 my $form = $main::form;
120 my $right = $rp_access_map->{$form->{report}};
121 $right ||= 'DOES_NOT_EXIST';
123 $main::auth->assert($right);
127 $::lxdebug->enter_sub;
132 balance_sheet => $::locale->text('Balance Sheet'),
133 income_statement => $::locale->text('Income Statement'),
134 erfolgsrechnung => $::locale->text('Erfolgsrechnung'),
135 trial_balance => $::locale->text('Trial Balance'),
136 ar_aging => $::locale->text('Search AR Aging'),
137 ap_aging => $::locale->text('Search AP Aging'),
138 tax_collected => $::locale->text('Tax collected'),
139 tax_paid => $::locale->text('Tax paid'),
140 receipts => $::locale->text('Receipts'),
141 payments => $::locale->text('Payments'),
142 projects => $::locale->text('Project Transactions'),
143 bwa => $::locale->text('Business evaluation'),
146 $::form->{title} = $title{$::form->{report}};
147 $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
148 $::request->{layout}->add_javascripts('autocomplete_project.js');
149 $::form->{fromdate} = DateTime->today->truncate(to => 'year')->to_kivitendo;
150 $::form->{todate} = DateTime->today->truncate(to => 'year')->add(years => 1)->add(days => -1)->to_kivitendo;
153 $::form->all_departments(\%::myconfig);
154 if (@{ $::form->{all_departments} || [] }) {
155 $::form->{selectdepartment} = "<option>\n";
156 map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
159 $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
161 my $is_projects = $::form->{report} eq "projects";
162 my $is_income_statement = $::form->{report} eq "income_statement";
163 my $is_erfolgsrechnung = $::form->{report} eq "erfolgsrechnung";
164 my $is_bwa = $::form->{report} eq "bwa";
165 my $is_balance_sheet = $::form->{report} eq "balance_sheet";
166 my $is_trial_balance = $::form->{report} eq "trial_balance";
167 my $is_aging = $::form->{report} =~ /^a[rp]_aging$/;
168 my $is_payments = $::form->{report} =~ /(receipts|payments)$/;
171 my ($label, $nextsub, $vc);
173 my $is_sales = $::form->{report} eq 'ar_aging';
174 $label = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
175 $::form->{vc} = $is_sales ? 'customer' : 'vendor';
177 $nextsub = "generate_$::form->{report}";
179 $vc = qq|<input name=$::form->{vc} size=35 class="initial_focus">|;
184 my ($selection, $paymentaccounts);
186 $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
188 RP->paymentaccounts(\%::myconfig, $::form);
190 $selection = "<option>\n";
191 for my $ref (@{ $::form->{PR} }) {
192 $paymentaccounts .= "$ref->{accno} ";
193 $selection .= "<option>$ref->{accno}--$ref->{description}\n";
197 setup_rp_report_action_bar();
200 print $::form->parse_html_template('rp/report', {
201 paymentaccounts => $paymentaccounts,
202 selection => $selection,
203 is_aging => $is_aging,
206 year => DateTime->today->year,
207 today => DateTime->today,
209 is_payments => $is_payments,
210 is_trial_balance => $is_trial_balance,
211 is_balance_sheet => $is_balance_sheet,
213 is_income_statement => $is_income_statement,
214 is_erfolgsrechnung => $is_erfolgsrechnung,
215 is_projects => $is_projects,
219 $::lxdebug->leave_sub;
222 sub continue { call_sub($main::form->{"nextsub"}); }
224 sub generate_income_statement {
225 $main::lxdebug->enter_sub();
227 $main::auth->assert('report');
229 my $form = $main::form;
230 my %myconfig = %main::myconfig;
231 my $locale = $main::locale;
233 $form->{padding} = " ";
234 $form->{bold} = "<b>";
235 $form->{endbold} = "</b>";
236 $form->{br} = "<br>";
238 if ($form->{reporttype} eq "custom") {
240 #forgotten the year --> thisyear
241 if ($form->{year} !~ m/^\d\d\d\d$/) {
242 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
248 if ($form->{duetyp} eq "13") {
249 $form->{fromdate} = "1.1.$form->{year}";
250 $form->{todate} = "31.12.$form->{year}";
254 if ($form->{duetyp} eq "A") {
255 $form->{fromdate} = "1.1.$form->{year}";
256 $form->{todate} = "31.3.$form->{year}";
258 if ($form->{duetyp} eq "B") {
259 $form->{fromdate} = "1.4.$form->{year}";
260 $form->{todate} = "30.6.$form->{year}";
262 if ($form->{duetyp} eq "C") {
263 $form->{fromdate} = "1.7.$form->{year}";
264 $form->{todate} = "30.9.$form->{year}";
266 if ($form->{duetyp} eq "D") {
267 $form->{fromdate} = "1.10.$form->{year}";
268 $form->{todate} = "31.12.$form->{year}";
273 $form->{duetyp} eq "1" && do {
274 $form->{fromdate} = "1.1.$form->{year}";
275 $form->{todate} = "31.1.$form->{year}";
278 $form->{duetyp} eq "2" && do {
279 $form->{fromdate} = "1.2.$form->{year}";
281 #this works from 1901 to 2099, 1900 and 2100 fail.
282 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
283 $form->{todate} = "$leap.2.$form->{year}";
286 $form->{duetyp} eq "3" && do {
287 $form->{fromdate} = "1.3.$form->{year}";
288 $form->{todate} = "31.3.$form->{year}";
291 $form->{duetyp} eq "4" && do {
292 $form->{fromdate} = "1.4.$form->{year}";
293 $form->{todate} = "30.4.$form->{year}";
296 $form->{duetyp} eq "5" && do {
297 $form->{fromdate} = "1.5.$form->{year}";
298 $form->{todate} = "31.5.$form->{year}";
301 $form->{duetyp} eq "6" && do {
302 $form->{fromdate} = "1.6.$form->{year}";
303 $form->{todate} = "30.6.$form->{year}";
306 $form->{duetyp} eq "7" && do {
307 $form->{fromdate} = "1.7.$form->{year}";
308 $form->{todate} = "31.7.$form->{year}";
311 $form->{duetyp} eq "8" && do {
312 $form->{fromdate} = "1.8.$form->{year}";
313 $form->{todate} = "31.8.$form->{year}";
316 $form->{duetyp} eq "9" && do {
317 $form->{fromdate} = "1.9.$form->{year}";
318 $form->{todate} = "30.9.$form->{year}";
321 $form->{duetyp} eq "10" && do {
322 $form->{fromdate} = "1.10.$form->{year}";
323 $form->{todate} = "31.10.$form->{year}";
326 $form->{duetyp} eq "11" && do {
327 $form->{fromdate} = "1.11.$form->{year}";
328 $form->{todate} = "30.11.$form->{year}";
331 $form->{duetyp} eq "12" && do {
332 $form->{fromdate} = "1.12.$form->{year}";
333 $form->{todate} = "31.12.$form->{year}";
337 hotfix_reformat_date();
338 } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
340 RP->income_statement(\%myconfig, \%$form);
342 ($form->{department}) = split /--/, $form->{department};
345 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
346 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
348 # if there are any dates construct a where
349 if ($form->{fromdate} || $form->{todate}) {
351 unless ($form->{todate}) {
352 $form->{todate} = $form->current_date(\%myconfig);
355 my $longtodate = $locale->date(\%myconfig, $form->{todate}, 1);
356 my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
358 my $longfromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
359 my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
361 $form->{this_period} = "$shortfromdate\n$shorttodate";
363 $locale->text('for Period')
364 . qq|\n$longfromdate |
365 . $locale->text('Bis')
369 if ($form->{comparefromdate} || $form->{comparetodate}) {
370 my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
371 my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
373 my $longcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 1);
374 my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
376 $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
378 "\n$longcomparefromdate "
379 . $locale->text('Bis')
380 . qq| $longcomparetodate|;
383 if ( $::instance_conf->get_profit_determination eq 'balance' ) {
384 $form->{title} = $locale->text('Income Statement');
385 } elsif ( $::instance_conf->get_profit_determination eq 'income' ) {
386 $form->{title} = $locale->text('Net Income Statement');
391 if ( $form->{method} eq 'cash' ) {
392 $form->{accounting_method} = $locale->text('Cash accounting');
393 } elsif ( $form->{method} eq 'accrual' ) {
394 $form->{accounting_method} = $locale->text('Accrual accounting');
396 $form->{accounting_method} = "";
399 $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
402 print $form->parse_html_template('rp/income_statement');
404 $main::lxdebug->leave_sub();
407 sub generate_erfolgsrechnung {
408 $::lxdebug->enter_sub;
409 $::auth->assert('report');
411 $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
412 $::form->{padding} = " ";
413 $::form->{bold} = "<b>";
414 $::form->{endbold} = "</b>";
415 $::form->{br} = "<br>";
417 my $data = RP->erfolgsrechnung(\%::myconfig, $::form);
420 print $::form->parse_html_template('rp/erfolgsrechnung', $data);
422 $::lxdebug->leave_sub;
426 sub generate_balance_sheet {
427 $::lxdebug->enter_sub;
428 $::auth->assert('report');
430 $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
431 $::form->{padding} = " ";
432 $::form->{bold} = "<b>";
433 $::form->{endbold} = "</b>";
434 $::form->{br} = "<br>";
436 my $data = RP->balance_sheet(\%::myconfig, $::form);
438 $::form->{asofdate} ||= $::form->current_date;
439 $::form->{report_title} = $::locale->text('Balance Sheet');
440 $::form->{report_date} ||= $::form->current_date;
442 ($::form->{department}) = split /--/, $::form->{department};
444 # define Current Earnings account
445 my $padding = $::form->{l_heading} ? $::form->{padding} : "";
446 push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
448 $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
449 $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
451 # balance sheet isn't read from print templates anymore,
452 # instead use template in rp
453 # $::form->{IN} = "balance_sheet.html";
456 print $::form->parse_html_template('rp/balance_sheet', $data);
458 $::lxdebug->leave_sub;
461 sub generate_projects {
462 $main::lxdebug->enter_sub();
464 $main::auth->assert('report');
466 my $form = $main::form;
467 my %myconfig = %main::myconfig;
468 my $locale = $main::locale;
470 my $project = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
471 $form->{projectnumber} = $project ? $project->projectnumber : '';
473 # make sure todate and fromdate always have a value, even if the date fields
474 # were left empty or the inputs weren't valid dates/couldn't be parsed
476 $project = SL::DB::Project->new() unless $project; # dummy object for dbh
477 unless ($::locale->parse_date_to_object($::form->{fromdate})) {
478 ($form->{fromdate}) = $project->db->dbh->selectrow_array('select min(transdate) from acc_trans');
481 unless ($::locale->parse_date_to_object($::form->{todate})) {
482 ($form->{todate}) = $project->db->dbh->selectrow_array('select max(transdate) from acc_trans');
485 $form->{nextsub} = "generate_projects";
486 $form->{title} = $locale->text('Project Transactions');
487 RP->trial_balance(\%myconfig, \%$form);
489 list_accounts('generate_projects');
491 $main::lxdebug->leave_sub();
497 # included links to display transactions for period entered
498 # added headers and subtotals
500 sub generate_trial_balance {
501 $main::lxdebug->enter_sub();
503 $main::auth->assert('report');
505 my $form = $main::form;
506 my %myconfig = %main::myconfig;
507 my $locale = $main::locale;
508 my $defaults = SL::DB::Default->get;
510 if ($form->{reporttype} eq "custom") {
512 #forgotten the year --> thisyear
513 if ($form->{year} !~ m/^\d\d\d\d$/) {
514 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
520 if ($form->{duetyp} eq "13") {
521 $form->{fromdate} = "1.1.$form->{year}";
522 $form->{todate} = "31.12.$form->{year}";
526 if ($form->{duetyp} eq "A") {
527 $form->{fromdate} = "1.1.$form->{year}";
528 $form->{todate} = "31.3.$form->{year}";
530 if ($form->{duetyp} eq "B") {
531 $form->{fromdate} = "1.4.$form->{year}";
532 $form->{todate} = "30.6.$form->{year}";
534 if ($form->{duetyp} eq "C") {
535 $form->{fromdate} = "1.7.$form->{year}";
536 $form->{todate} = "30.9.$form->{year}";
538 if ($form->{duetyp} eq "D") {
539 $form->{fromdate} = "1.10.$form->{year}";
540 $form->{todate} = "31.12.$form->{year}";
545 $form->{duetyp} eq "1" && do {
546 $form->{fromdate} = "1.1.$form->{year}";
547 $form->{todate} = "31.1.$form->{year}";
550 $form->{duetyp} eq "2" && do {
551 $form->{fromdate} = "1.2.$form->{year}";
553 #this works from 1901 to 2099, 1900 and 2100 fail.
554 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
555 $form->{todate} = "$leap.2.$form->{year}";
558 $form->{duetyp} eq "3" && do {
559 $form->{fromdate} = "1.3.$form->{year}";
560 $form->{todate} = "31.3.$form->{year}";
563 $form->{duetyp} eq "4" && do {
564 $form->{fromdate} = "1.4.$form->{year}";
565 $form->{todate} = "30.4.$form->{year}";
568 $form->{duetyp} eq "5" && do {
569 $form->{fromdate} = "1.5.$form->{year}";
570 $form->{todate} = "31.5.$form->{year}";
573 $form->{duetyp} eq "6" && do {
574 $form->{fromdate} = "1.6.$form->{year}";
575 $form->{todate} = "30.6.$form->{year}";
578 $form->{duetyp} eq "7" && do {
579 $form->{fromdate} = "1.7.$form->{year}";
580 $form->{todate} = "31.7.$form->{year}";
583 $form->{duetyp} eq "8" && do {
584 $form->{fromdate} = "1.8.$form->{year}";
585 $form->{todate} = "31.8.$form->{year}";
588 $form->{duetyp} eq "9" && do {
589 $form->{fromdate} = "1.9.$form->{year}";
590 $form->{todate} = "30.9.$form->{year}";
593 $form->{duetyp} eq "10" && do {
594 $form->{fromdate} = "1.10.$form->{year}";
595 $form->{todate} = "31.10.$form->{year}";
598 $form->{duetyp} eq "11" && do {
599 $form->{fromdate} = "1.11.$form->{year}";
600 $form->{todate} = "30.11.$form->{year}";
603 $form->{duetyp} eq "12" && do {
604 $form->{fromdate} = "1.12.$form->{year}";
605 $form->{todate} = "31.12.$form->{year}";
609 hotfix_reformat_date();
613 # get for each account initial balance, debits and credits
614 RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
617 $form->{rowcount} = scalar @{ $form->{TB} || [] };
618 $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
621 "accno", "description",
622 "last_transaction", "soll_eb",
625 "soll_kumuliert", "haben_kumuliert",
626 "soll_saldo", "haben_saldo"
630 my $attachment_basename = $locale->text('trial_balance');
631 my $report = SL::ReportGenerator->new(\%myconfig, $form);
633 my @hidden_variables = qw(fromdate todate year method department_id);
635 my $href = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
638 'accno' => { 'text' => $locale->text('Account'), },
639 'description' => { 'text' => $locale->text('Description'), },
640 'last_transaction' => { 'text' => $locale->text('Last Transaction'), },
641 'soll_eb' => { 'text' => $locale->text('Debit Starting Balance'), },
642 'haben_eb' => { 'text' => $locale->text('Credit Starting Balance'), },
643 'soll' => { 'text' => $locale->text('Debit'), },
644 'haben' => { 'text' => $locale->text('Credit'), },
645 'soll_kumuliert' => { 'text' => $locale->text('Sum Debit'), },
646 'haben_kumuliert' => { 'text' => $locale->text('Sum Credit'), },
647 'soll_saldo' => { 'text' => $locale->text('Saldo Debit'), },
648 'haben_saldo' => { 'text' => $locale->text('Saldo Credit'), }
653 my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
655 map { $column_defs{$_}->{visible} = 1 } @columns;
657 $report->set_columns(%column_defs);
658 $report->set_column_order(@columns);
660 $report->set_export_options('generate_trial_balance', @hidden_variables);
665 $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
667 $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
668 push (@options, $form->{print_date});
670 $form->{company} = $locale->text('Company') . " " . $defaults->company;
671 push (@options, $form->{company});
673 if ($::form->{customer_id}) {
674 my $customer = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id});
675 push @options, $::locale->text('Customer') . ' ' . $customer->displayable_name;
679 $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
681 my @custom_headers = ([
682 { text => $::locale->text('Account'), rowspan => 2, },
683 { text => $::locale->text('Description'), rowspan => 2, },
684 { text => $::locale->text('Last Transaction'), rowspan => 2, },
685 { text => $::locale->text('Starting Balance'), colspan => 2, },
686 { text => $::locale->text('Sum for') . " $form->{template_fromto}", colspan => 2, },
687 { text => $::locale->text('Sum per') . " $form->{template_to}", colspan => 2, },
688 { text => $::locale->text('Saldo per') . " $form->{template_to}", colspan => 2, },
693 { text => $::locale->text('Assets'), },
694 { text => $::locale->text('Equity'), },
695 { text => $::locale->text('Debit'), },
696 { text => $::locale->text('Credit'), },
697 { text => $::locale->text('Debit'), },
698 { text => $::locale->text('Credit'), },
699 { text => $::locale->text('Debit'), },
700 { text => $::locale->text('Credit'), },
703 $report->set_options('output_format' => 'HTML',
704 'top_info_text' => join("\n", @options),
705 'title' => $form->{title},
706 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
707 'html_template' => 'rp/html_report_susa',
708 'pdf_template' => 'rp/html_report_susa',
710 $report->set_custom_headers(@custom_headers);
711 $report->set_options_from_form();
712 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
714 # add sort and escape callback, this one we use for the add sub
715 $form->{callback} = $href .= "&sort=$form->{sort}";
717 # escape callback for href
718 my $callback = $form->escape($href);
720 my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
722 my %totals = map { $_ => 0 } @subtotal_columns;
724 my $edit_url = build_std_url('action=edit', 'type', 'vc');
727 foreach my $accno (@{ $form->{TB} || [] }) {
729 $accno->{soll} = $accno->{debit};
730 $accno->{haben} = $accno->{credit};
731 map { $totals{$_} += $accno->{$_} } @subtotal_columns;
733 map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
734 qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
738 foreach my $column (@columns) {
740 'data' => $accno->{$column},
741 'align' => $column_alignment{$column},
745 $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}));
747 my $row_set = [ $row ];
750 $report->add_data($row_set);
755 $report->add_separator();
757 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
759 $report->generate_with_headers();
761 $main::lxdebug->leave_sub();
765 sub create_subtotal_row {
766 $main::lxdebug->enter_sub();
768 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
770 my $form = $main::form;
771 my %myconfig = %main::myconfig;
772 my $locale = $main::locale;
774 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
776 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
778 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
780 map { $totals->{$_} = 0 } @{ $subtotal_columns };
782 $main::lxdebug->leave_sub();
787 sub create_list_accounts_subtotal_row {
788 $main::lxdebug->enter_sub();
790 my ($subtotals, $columns, $fields, $class) = @_;
792 my $form = $main::form;
793 my %myconfig = %main::myconfig;
794 my $locale = $main::locale;
796 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
798 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
800 $main::lxdebug->leave_sub();
806 $main::lxdebug->enter_sub();
810 my $form = $main::form;
811 my %myconfig = %main::myconfig;
812 my $locale = $main::locale;
815 if ($form->{department}) {
816 my ($department) = split /--/, $form->{department};
817 push @options, $locale->text('Department') . " : $department";
819 if ($form->{projectnumber}) {
820 push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
823 # if there are any dates
824 if ($form->{fromdate} || $form->{todate}) {
825 my ($fromdate, $todate);
827 if ($form->{fromdate}) {
828 $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
830 if ($form->{todate}) {
831 $todate = $locale->date(\%myconfig, $form->{todate}, 1);
834 push @options, "$fromdate - $todate";
837 push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
840 my @columns = qw(accno description begbalance debit credit endbalance);
842 'accno' => { 'text' => $locale->text('Account'), },
843 'description' => { 'text' => $locale->text('Description'), },
844 'debit' => { 'text' => $locale->text('Debit'), },
845 'credit' => { 'text' => $locale->text('Credit'), },
846 'begbalance' => { 'text' => $locale->text('Balance'), },
847 'endbalance' => { 'text' => $locale->text('Balance'), },
849 my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
851 my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
853 $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
855 my $report = SL::ReportGenerator->new(\%myconfig, $form);
857 $report->set_options('top_info_text' => join("\n", @options),
858 'output_format' => 'HTML',
859 'title' => $form->{title},
860 'attachment_basename' => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
861 'std_column_visibility' => 1,
863 $report->set_options_from_form();
864 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
866 $report->set_columns(%column_defs);
867 $report->set_column_order(@columns);
869 $report->set_export_options($action, @hidden_variables);
871 my @totals_columns = qw(credit debit begbalance endbalance);
872 my %subtotals = map { $_ => 0 } @totals_columns;
873 my %totals = map { $_ => 0 } @totals_columns;
874 my $found_heading = 0;
875 my @tb = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
877 # sort the whole thing by account numbers and display
878 foreach my $idx (0 .. scalar(@tb) - 1) {
880 my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
882 my $ml = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
884 my $row = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
886 if ($ref->{charttype} eq 'H') {
887 next unless ($form->{l_heading});
889 %subtotals = map { $_ => 0 } @totals_columns;
891 $row->{description}->{class} = 'listheading';
892 $row->{description}->{data} = $ref->{description};
894 $report->add_data($row);
899 foreach (qw(debit credit)) {
900 $subtotals{$_} += $ref->{$_};
901 $totals{$_} += $ref->{$_};
904 $subtotals{begbalance} += $ref->{balance} * $ml;
905 $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
907 map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
908 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
910 $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
911 $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
913 $report->add_data($row);
915 if ($form->{l_heading} && $found_heading &&
916 (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
917 $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
921 $report->add_separator();
923 $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
925 $report->generate_with_headers();
927 $main::lxdebug->leave_sub();
930 sub generate_ar_aging {
931 $main::lxdebug->enter_sub();
933 $main::auth->assert('general_ledger | ar_transactions');
935 my $form = $main::form;
936 my %myconfig = %main::myconfig;
937 my $locale = $main::locale;
940 ($form->{customer}) = split(/--/, $form->{customer});
942 $form->{ct} = "customer";
943 $form->{arap} = "ar";
945 $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
947 RP->aging(\%myconfig, \%$form);
950 $main::lxdebug->leave_sub();
953 sub generate_ap_aging {
954 $main::lxdebug->enter_sub();
956 $main::auth->assert('general_ledger | ap_transactions');
958 my $form = $main::form;
959 my %myconfig = %main::myconfig;
960 my $locale = $main::locale;
963 ($form->{vendor}) = split(/--/, $form->{vendor});
965 $form->{ct} = "vendor";
966 $form->{arap} = "ap";
968 $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
970 RP->aging(\%myconfig, \%$form);
973 $main::lxdebug->leave_sub();
976 sub create_aging_subtotal_row {
977 $main::lxdebug->enter_sub();
979 my ($subtotals, $columns, $periods, $class) = @_;
981 my $form = $main::form;
982 my %myconfig = %main::myconfig;
983 my $locale = $main::locale;
985 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
987 foreach (@{ $periods }) {
988 $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
989 $subtotals->{$_} = 0;
992 $main::lxdebug->leave_sub();
998 $main::lxdebug->enter_sub();
1000 $main::auth->assert('general_ledger');
1002 my $form = $main::form;
1003 my %myconfig = %main::myconfig;
1004 my $locale = $main::locale;
1005 my $cgi = $::request->{cgi};
1007 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1009 my @columns = qw(statement ct invnumber transdate duedate amount open);
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'), },
1021 my %column_alignment = ('statement' => 'center',
1022 map { $_ => 'right' } qw(open amount));
1024 $report->set_options('std_column_visibility' => 1);
1025 $report->set_columns(%column_defs);
1026 $report->set_column_order(@columns);
1027 my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype department fromdate);
1028 $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
1031 my $attachment_basename;
1033 if ($form->{department}) {
1034 my ($department) = split /--/, $form->{department};
1035 push @options, $locale->text('Department') . " : $department";
1036 $form->{callback} .= "&department=" . E($department);
1039 if (($form->{arap} eq 'ar') && $form->{customer}) {
1040 push @options, $form->{customer};
1041 $attachment_basename = $locale->text('ar_aging_list');
1042 $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
1045 if (($form->{arap} eq 'ap') && $form->{vendor}) {
1046 push @options, $form->{vendor};
1047 $attachment_basename = $locale->text('ap_aging_list');
1048 $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
1051 if ($form->{fromdate}) {
1052 push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1054 push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1057 $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1059 $report->set_options('top_info_text' => join("\n", @options),
1060 'output_format' => 'HTML',
1061 'title' => $form->{title},
1062 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1064 $report->set_options_from_form();
1065 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1067 my $previous_ctid = 0;
1069 my @periods = qw(open amount);
1070 my %subtotals = map { $_ => 0 } @periods;
1071 my %totals = map { $_ => 0 } @periods;
1073 foreach my $ref (@{ $form->{AG} }) {
1074 if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1075 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1078 foreach my $key (@periods) {
1079 $subtotals{$key} += $ref->{"$key"};
1080 $totals{$key} += $ref->{"$key"};
1081 $ref->{"$key"} = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
1086 foreach my $column (@columns) {
1088 'data' => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1089 'align' => $column_alignment{$column},
1090 'valign' => $column eq 'statement' ? 'center' : '',
1094 $row->{invnumber}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1096 if ($previous_ctid != $ref->{ctid}) {
1097 $row->{statement}->{raw_data} =
1098 $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
1099 . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1100 $row->{ct}->{data} = $ref->{name};
1105 $previous_ctid = $ref->{ctid};
1107 $report->add_data($row);
1110 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1112 $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1114 if ($form->{arap} eq 'ar') {
1115 my $raw_top_info_text = $form->parse_html_template('rp/aging_ar_top');
1116 my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1117 'PRINT_OPTIONS' => print_options(inline => 1), });
1118 $report->set_options('raw_top_info_text' => $raw_top_info_text,
1119 'raw_bottom_info_text' => $raw_bottom_info_text);
1122 setup_rp_aging_action_bar(arap => $form->{arap});
1123 $report->generate_with_headers();
1125 $main::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 my $email_form = delete $form->{email_form};
1145 my %field_names = (to => 'email');
1147 $form->{ $field_names{$_} // $_ } = $email_form->{$_} for keys %{ $email_form };
1149 $form->{media} = 'email';
1152 $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1154 $main::lxdebug->leave_sub();
1158 $main::lxdebug->enter_sub();
1160 $main::auth->assert('general_ledger');
1162 my $form = $main::form;
1163 my %myconfig = %main::myconfig;
1164 my $locale = $main::locale;
1166 if ($form->{media} eq 'printer') {
1167 $form->error($locale->text('Select postscript or PDF!'))
1168 if ($form->{format} !~ /(postscript|pdf)/);
1172 for my $i (1 .. $form->{rowcount}) {
1173 if ($form->{"statement_$i"}) {
1174 $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1180 $form->error($locale->text('Nothing selected!')) unless $selected;
1182 if ($form->{media} eq 'printer') {
1183 $form->{"$form->{ct}_id"} = "";
1185 $form->{"statement_1"} = 1;
1188 RP->aging(\%myconfig, \%$form);
1192 $form->redirect($locale->text('Statements sent to printer!'))
1193 if ($form->{media} eq 'printer');
1195 $main::lxdebug->leave_sub();
1199 $main::lxdebug->enter_sub();
1201 $main::auth->assert('general_ledger');
1203 my $form = $main::form;
1204 my %myconfig = %main::myconfig;
1205 my $locale = $main::locale;
1207 my $defaults = SL::DB::Default->get;
1208 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
1209 $form->{templates} = $defaults->templates;
1211 $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1213 my $suffix = "html";
1214 my $attachment_suffix = "html";
1215 if ($form->{format} eq 'postscript') {
1216 $form->{postscript} = 1;
1218 $attachment_suffix = "ps";
1219 } elsif ($form->{format} eq 'pdf') {
1222 $attachment_suffix = "pdf";
1225 $form->{IN} = "$form->{type}.$suffix";
1226 $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1228 # Save $form->{email} because it will be overwritten.
1229 $form->{EMAIL_RECIPIENT} = $form->{email};
1233 while (@{ $form->{AG} }) {
1235 my $ref = shift @{ $form->{AG} };
1237 if ($ctid != $ref->{ctid}) {
1239 $ctid = $ref->{ctid};
1242 if ($form->{"statement_$i"}) {
1245 ("name", "street", "zipcode", "city", "country", "contact", "email",
1246 "$form->{ct}phone", "$form->{ct}fax");
1247 map { $form->{$_} = $ref->{$_} } @a;
1249 $form->{ $form->{ct} } = $form->{name};
1250 $form->{"$form->{ct}_id"} = $ref->{ctid};
1252 map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
1254 foreach my $item (qw(c0 c30 c60 c90)) {
1255 $form->{$item} = ();
1256 $form->{"${item}total"} = 0;
1259 &statement_details($ref);
1263 if (scalar(@{ $form->{AG} }) > 0) {
1265 # one or more left to go
1266 if ($ctid == $form->{AG}->[0]->{ctid}) {
1267 $ref = shift @{ $form->{AG} };
1268 &statement_details($ref);
1271 $ref = scalar(@{ $form->{AG} });
1277 # set initial ref to 0
1284 $form->{"${_}total"} =
1285 $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1286 } ('c0', 'c30', 'c60', 'c90', "");
1288 $form->{attachment_filename} = $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
1289 $form->{attachment_filename} =~ s/\s+/_/g;
1291 $form->parse_template(\%myconfig);
1296 # saving the history
1297 if(!exists $form->{addition} && $form->{id} ne "") {
1298 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1299 $form->{addition} = "PRINTED";
1300 $form->{what_done} = $form->{type};
1301 $form->save_history;
1303 # /saving the history
1304 $main::lxdebug->leave_sub();
1307 sub statement_details {
1308 $main::lxdebug->enter_sub();
1310 $main::auth->assert('general_ledger');
1312 my $form = $main::form;
1313 my %myconfig = %main::myconfig;
1314 my $locale = $main::locale;
1318 push @{ $form->{invnumber} }, $ref->{invnumber};
1319 push @{ $form->{invdate} }, $ref->{transdate};
1320 push @{ $form->{duedate} }, $ref->{duedate};
1321 push @{ $form->{amount} }, $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
1322 push @{ $form->{open} }, $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
1324 foreach my $item (qw(c0 c30 c60 c90)) {
1325 if ($ref->{exchangerate} * 1) {
1326 # add only the open amount of the invoice to the aging, not the total amount
1327 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
1328 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
1329 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
1330 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
1332 $form->{"${item}total"} += $ref->{$item};
1333 $form->{total} += $ref->{$item};
1334 push @{ $form->{$item} },
1335 $form->format_amount(\%myconfig, $ref->{$item}, 2);
1338 $main::lxdebug->leave_sub();
1341 sub generate_tax_report {
1342 $::lxdebug->enter_sub;
1343 $::auth->assert('report');
1345 RP->tax_report(\%::myconfig, $::form);
1347 my $descvar = "$::form->{accno}_description";
1348 my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
1352 my $callback = build_std_url('action=generate_tax_report', $descvar,
1353 qw(fromdate todate db method accno department report title));
1355 my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1358 for my $item (@columns, 'subtotal') {
1359 if ($::form->{"l_$item"} eq "Y") {
1360 $callback .= "&l_$item=Y";
1361 $href .= "&l_$item=Y";
1365 for my $item (@columns) {
1366 if ($::form->{"l_$item"} eq "Y") {
1367 push @column_index, $item;
1372 if ($::form->{department}) {
1373 my ($department) = split /--/, $::form->{department};
1374 push @options, $::locale->text('Department') . " : $department";
1377 # if there are any dates
1378 if ($::form->{fromdate} || $::form->{todate}) {
1379 my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
1380 my $todate = $::form->{todate} ? $::locale->date(\%::myconfig, $::form->{todate}, 1) : '';
1381 push @options, "$fromdate - $todate";
1383 push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
1386 my ($name, $invoice, $arap);
1387 if ($::form->{db} eq 'ar') {
1388 $name = $::locale->text('Customer');
1392 if ($::form->{db} eq 'ap') {
1393 $name = $::locale->text('Vendor');
1398 my %column_header = (
1399 id => $::locale->text('ID'),
1400 invnumber => $::locale->text('Invoice'),
1401 transdate => $::locale->text('Date'),
1402 netamount => $::locale->text('Amount'),
1403 tax => $::locale->text('Tax'),
1404 amount => $::locale->text('Total'),
1408 my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
1410 $callback .= "&sort=$::form->{sort}";
1413 if (@{ $::form->{TR} }) {
1414 $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
1417 my ($totalnetamount, $totaltax, @data);
1418 for my $ref (@{ $::form->{TR} }) {
1420 my $module = ($ref->{invoice}) ? $invoice : $arap;
1422 if ($::form->{l_subtotal} eq 'Y') {
1423 if ($sameitem ne $ref->{ $::form->{sort} }) {
1426 netamount => $subtotalnetamount,
1427 tax => $subtotaltax,
1428 amount => $subtotal,
1430 $subtotalnetamount = 0;
1432 $sameitem = $ref->{ $::form->{sort} };
1436 $subtotalnetamount += $ref->{netamount};
1437 $subtotaltax += $ref->{tax};
1438 $totalnetamount += $ref->{netamount};
1439 $totaltax += $ref->{tax};
1440 $ref->{amount} = $ref->{netamount} + $ref->{tax};
1442 push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
1443 $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
1444 $data[-1]{$_}{numeric} = 1 for qw(netamount tax amount);
1447 if ($::form->{l_subtotal} eq 'Y') {
1450 netamount => $subtotalnetamount,
1451 tax => $subtotaltax,
1452 amount => $subtotal,
1458 netamount => $totalnetamount,
1460 amount => $totalnetamount + $totaltax,
1464 print $::form->parse_html_template('rp/tax_report', {
1465 column_index => \@column_index,
1466 column_header => \%column_header,
1467 column_sorted => \%column_sorted,
1470 options => \@options,
1473 $::lxdebug->leave_sub;
1477 $main::lxdebug->enter_sub();
1479 $main::auth->assert('cash');
1481 my $form = $main::form;
1482 my %myconfig = %main::myconfig;
1483 my $locale = $main::locale;
1485 if ($form->{account}) {
1486 ($form->{paymentaccounts}) = split /--/, $form->{account};
1490 if ($form->{department}) {
1491 (my $department, $form->{department_id}) = split /--/, $form->{department};
1492 $option = $locale->text('Department') . " : $department";
1495 report_generator_set_default_sort('transdate', 1);
1497 RP->payments(\%myconfig, \%$form);
1499 my @hidden_variables = qw(account title department reference source memo fromdate todate
1500 fx_transaction db prepayment paymentaccounts sort);
1502 my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
1503 $form->{callback} = $href;
1505 my @columns = qw(transdate invnumber name paid source memo);
1507 'name' => { 'text' => $locale->text('Description'), },
1508 'invnumber' => { 'text' => $locale->text('Reference'), },
1509 'transdate' => { 'text' => $locale->text('Date'), },
1510 'paid' => { 'text' => $locale->text('Amount'), },
1511 'source' => { 'text' => $locale->text('Source'), },
1512 'memo' => { 'text' => $locale->text('Memo'), },
1514 my %column_alignment = ('paid' => 'right');
1516 foreach my $name (grep { $_ ne 'paid' } @columns) {
1517 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1518 $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
1522 if ($form->{fromdate}) {
1523 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
1525 if ($form->{todate}) {
1526 push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1529 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1531 my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
1533 $report->set_options('top_info_text' => join("\n", @options),
1534 'output_format' => 'HTML',
1535 'title' => $form->{title},
1536 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1537 'std_column_visibility' => 1,
1539 $report->set_options_from_form();
1541 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1543 $report->set_columns(%column_defs);
1544 $report->set_column_order(@columns);
1546 $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
1548 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1552 foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1553 next unless @{ $form->{ $ref->{id} } };
1555 $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
1557 my $subtotal_paid = 0;
1559 foreach my $payment (@{ $form->{ $ref->{id} } }) {
1560 my $module = $payment->{module};
1561 $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1562 $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1564 $subtotal_paid += $payment->{paid};
1565 $total_paid += $payment->{paid};
1567 $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
1571 foreach my $column (@columns) {
1573 'data' => $payment->{$column},
1574 'align' => $column_alignment{$column},
1578 $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
1580 $report->add_data($row);
1583 my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
1585 'data' => $form->format_amount(\%myconfig, $subtotal_paid, 2),
1587 'class' => 'listsubtotal',
1590 $report->add_data($row);
1593 $report->add_separator();
1595 my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
1597 'data' => $form->format_amount(\%myconfig, $total_paid, 2),
1599 'class' => 'listtotal',
1602 $report->add_data($row);
1604 $report->generate_with_headers();
1606 $main::lxdebug->leave_sub();
1610 $::lxdebug->enter_sub;
1612 my ($dont_print) = @_;
1614 $::form->{sendmode} = "attachment";
1615 $::form->{format} ||= $::myconfig{template_format} || "pdf";
1616 $::form->{copies} ||= $::myconfig{copies} || 2;
1618 $::form->{PD}{ $::form->{type} } = "selected";
1619 $::form->{DF}{ $::form->{format} } = "selected";
1620 $::form->{OP}{ $::form->{media} } = "selected";
1621 $::form->{SM}{ $::form->{sendmode} } = "selected";
1623 my $output = $::form->parse_html_template('rp/print_options', {
1624 is_email => $::form->{media} eq 'email',
1627 print $output unless $dont_print;
1629 $::lxdebug->leave_sub;
1635 $main::lxdebug->enter_sub();
1637 $main::auth->assert('report');
1639 my $form = $main::form;
1640 my %myconfig = %main::myconfig;
1641 my $locale = $main::locale;
1643 $form->{padding} = " ";
1644 $form->{bold} = "<b>";
1645 $form->{endbold} = "</b>";
1646 $form->{br} = "<br>";
1648 if ($form->{reporttype} eq "custom") {
1650 #forgotten the year --> thisyear
1651 if ($form->{year} !~ m/^\d\d\d\d$/) {
1652 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1658 if ($form->{duetyp} eq "13") {
1659 $form->{fromdate} = "1.1.$form->{year}";
1660 $form->{todate} = "31.12.$form->{year}";
1661 $form->{comparefromdate} = "1.01.$form->{year}";
1662 $form->{comparetodate} = "31.12.$form->{year}";
1666 if ($form->{duetyp} eq "A") {
1667 $form->{fromdate} = "1.1.$form->{year}";
1668 $form->{todate} = "31.3.$form->{year}";
1669 $form->{comparefromdate} = "1.01.$form->{year}";
1670 $form->{comparetodate} = "31.03.$form->{year}";
1672 if ($form->{duetyp} eq "B") {
1673 $form->{fromdate} = "1.4.$form->{year}";
1674 $form->{todate} = "30.6.$form->{year}";
1675 $form->{comparefromdate} = "1.01.$form->{year}";
1676 $form->{comparetodate} = "30.06.$form->{year}";
1678 if ($form->{duetyp} eq "C") {
1679 $form->{fromdate} = "1.7.$form->{year}";
1680 $form->{todate} = "30.9.$form->{year}";
1681 $form->{comparefromdate} = "1.01.$form->{year}";
1682 $form->{comparetodate} = "30.09.$form->{year}";
1684 if ($form->{duetyp} eq "D") {
1685 $form->{fromdate} = "1.10.$form->{year}";
1686 $form->{todate} = "31.12.$form->{year}";
1687 $form->{comparefromdate} = "1.01.$form->{year}";
1688 $form->{comparetodate} = "31.12.$form->{year}";
1693 $form->{duetyp} eq "1" && do {
1694 $form->{fromdate} = "1.1.$form->{year}";
1695 $form->{todate} = "31.1.$form->{year}";
1696 $form->{comparefromdate} = "1.01.$form->{year}";
1697 $form->{comparetodate} = "31.01.$form->{year}";
1700 $form->{duetyp} eq "2" && do {
1701 $form->{fromdate} = "1.2.$form->{year}";
1703 #this works from 1901 to 2099, 1900 and 2100 fail.
1704 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1705 $form->{todate} = "$leap.2.$form->{year}";
1706 $form->{comparefromdate} = "1.01.$form->{year}";
1707 $form->{comparetodate} = "$leap.02.$form->{year}";
1710 $form->{duetyp} eq "3" && do {
1711 $form->{fromdate} = "1.3.$form->{year}";
1712 $form->{todate} = "31.3.$form->{year}";
1713 $form->{comparefromdate} = "1.01.$form->{year}";
1714 $form->{comparetodate} = "31.03.$form->{year}";
1717 $form->{duetyp} eq "4" && do {
1718 $form->{fromdate} = "1.4.$form->{year}";
1719 $form->{todate} = "30.4.$form->{year}";
1720 $form->{comparefromdate} = "1.01.$form->{year}";
1721 $form->{comparetodate} = "30.04.$form->{year}";
1724 $form->{duetyp} eq "5" && do {
1725 $form->{fromdate} = "1.5.$form->{year}";
1726 $form->{todate} = "31.5.$form->{year}";
1727 $form->{comparefromdate} = "1.01.$form->{year}";
1728 $form->{comparetodate} = "31.05.$form->{year}";
1731 $form->{duetyp} eq "6" && do {
1732 $form->{fromdate} = "1.6.$form->{year}";
1733 $form->{todate} = "30.6.$form->{year}";
1734 $form->{comparefromdate} = "1.01.$form->{year}";
1735 $form->{comparetodate} = "30.06.$form->{year}";
1738 $form->{duetyp} eq "7" && do {
1739 $form->{fromdate} = "1.7.$form->{year}";
1740 $form->{todate} = "31.7.$form->{year}";
1741 $form->{comparefromdate} = "1.01.$form->{year}";
1742 $form->{comparetodate} = "31.07.$form->{year}";
1745 $form->{duetyp} eq "8" && do {
1746 $form->{fromdate} = "1.8.$form->{year}";
1747 $form->{todate} = "31.8.$form->{year}";
1748 $form->{comparefromdate} = "1.01.$form->{year}";
1749 $form->{comparetodate} = "31.08.$form->{year}";
1752 $form->{duetyp} eq "9" && do {
1753 $form->{fromdate} = "1.9.$form->{year}";
1754 $form->{todate} = "30.9.$form->{year}";
1755 $form->{comparefromdate} = "1.01.$form->{year}";
1756 $form->{comparetodate} = "30.09.$form->{year}";
1759 $form->{duetyp} eq "10" && do {
1760 $form->{fromdate} = "1.10.$form->{year}";
1761 $form->{todate} = "31.10.$form->{year}";
1762 $form->{comparefromdate} = "1.01.$form->{year}";
1763 $form->{comparetodate} = "31.10.$form->{year}";
1766 $form->{duetyp} eq "11" && do {
1767 $form->{fromdate} = "1.11.$form->{year}";
1768 $form->{todate} = "30.11.$form->{year}";
1769 $form->{comparefromdate} = "1.01.$form->{year}";
1770 $form->{comparetodate} = "30.11.$form->{year}";
1773 $form->{duetyp} eq "12" && do {
1774 $form->{fromdate} = "1.12.$form->{year}";
1775 $form->{todate} = "31.12.$form->{year}";
1776 $form->{comparefromdate} = "1.01.$form->{year}";
1777 $form->{comparetodate} = "31.12.$form->{year}";
1781 hotfix_reformat_date();
1783 # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
1784 # ansonsten ist die prüfung in RP.pm
1785 # if (defined ($form->{fromdate|todate}=='..'))
1787 if ($form->{fromdate}){
1788 my $datetime = $locale->parse_date_to_object($form->{fromdate});
1789 $datetime->set( month => 1,
1791 $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
1793 if ($form->{todate}){
1794 $form->{comparetodate} = $form->{todate};
1798 RP->bwa(\%myconfig, \%$form);
1800 ($form->{department}) = split /--/, $form->{department};
1803 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1804 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1806 # if there are any dates construct a where
1807 if ($form->{fromdate} || $form->{todate}) {
1809 unless ($form->{todate}) {
1810 $form->{todate} = $form->current_date(\%myconfig);
1813 my %germandate = ("dateformat" => "dd.mm.yyyy");
1815 my $longtodate = $locale->date(\%germandate, $form->{todate}, 1);
1816 my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
1818 my $longfromdate = $locale->date(\%germandate, $form->{fromdate}, 1);
1819 my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
1821 $form->{this_period} = "$shortfromdate\n$shorttodate";
1823 $locale->text('for Period')
1824 . qq|\n$longfromdate |
1825 . $locale->text('bis')
1829 $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
1831 if ( $form->{method} eq 'cash' ) {
1832 $form->{accounting_method} = $locale->text('Cash accounting');
1833 } elsif ( $form->{method} eq 'accrual' ) {
1834 $form->{accounting_method} = $locale->text('Accrual accounting');
1836 $form->{accounting_method} = "";
1839 $form->{title} = $locale->text('BWA');
1841 $::request->layout->add_stylesheets('bwa.css');
1843 print $form->parse_html_template('rp/bwa');
1845 $main::lxdebug->leave_sub();
1848 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
1849 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
1850 sub hotfix_reformat_date {
1852 $main::lxdebug->enter_sub();
1854 my $form = $main::form;
1855 my %myconfig = %main::myconfig;
1856 my $locale = $main::locale;
1858 if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
1859 my $current_dateformat = $myconfig{dateformat};
1860 $myconfig{dateformat} = 'dd.mm.yyyy';
1861 $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
1862 $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
1863 $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
1864 unless (!defined ($form->{comparefromdate}));
1865 $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
1866 unless (!defined ($form->{comparetodate}));
1868 # Und wieder zurücksetzen
1869 $myconfig{dateformat} = $current_dateformat; #'dd.mm.yyyy';
1870 } # Ende Hotifx Bug 1388
1872 $main::lxdebug->leave_sub();
1876 sub setup_rp_aging_action_bar {
1879 return unless $params{arap} eq 'ar';
1881 for my $bar ($::request->layout->get('actionbar')) {
1886 call => [ 'kivi.SalesPurchase.show_print_dialog' ],
1887 checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
1891 call => [ 'kivi.SalesPurchase.show_email_dialog', 'send_email' ],
1892 checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
1899 sub setup_rp_report_action_bar {
1902 for my $bar ($::request->layout->get('actionbar')) {
1906 submit => [ '#form', { action => 'continue' } ],
1907 accesskey => 'enter',