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 all_accounts);
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 datepaid current_open);
1011 'statement' => { raw_header_data => SL::Presenter::Tag::checkbox_tag("checkall", checkall => '[name^=statement_]'), 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, align => "center" },
1012 'ct' => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1013 'invnumber' => { 'text' => $locale->text('Invoice'), },
1014 'transdate' => { 'text' => $locale->text('Date'), },
1015 'duedate' => { 'text' => $locale->text('Due'), },
1016 'amount' => { 'text' => $locale->text('Amount'), },
1017 'open' => { 'text' => $locale->text('Open'), },
1018 'datepaid' => { 'text' => $locale->text('Date of Last Payment'), visible => ($form->{reporttype} eq 'custom') },
1019 'current_open' => { 'text' => $locale->text('Open Amount at Last Payment Date'), visible => ($form->{reporttype} eq 'custom') },
1022 my %column_alignment = ('statement' => 'center',
1023 map { $_ => 'right' } qw(open amount current_open datepaid));
1025 $report->set_options('std_column_visibility' => 1);
1026 $report->set_columns(%column_defs);
1027 $report->set_column_order(@columns);
1028 my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype department fromdate);
1029 $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
1032 my $attachment_basename;
1034 if ($form->{department}) {
1035 my ($department) = split /--/, $form->{department};
1036 push @options, $locale->text('Department') . " : $department";
1037 $form->{callback} .= "&department=" . E($department);
1040 if (($form->{arap} eq 'ar') && $form->{customer}) {
1041 push @options, $form->{customer};
1042 $attachment_basename = $locale->text('ar_aging_list');
1043 $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
1046 if (($form->{arap} eq 'ap') && $form->{vendor}) {
1047 push @options, $form->{vendor};
1048 $attachment_basename = $locale->text('ap_aging_list');
1049 $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
1052 $form->{callback} .= "&reporttype=" . E($form->{reporttype});
1053 if ($form->{reporttype} eq 'free') {
1054 if ($form->{fromdate}) {
1055 push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .
1056 $locale->date(\%myconfig, $form->{fromdate}, 1) . " " .
1057 $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1059 push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " .
1060 $locale->date(\%myconfig, $form->{todate}, 1);
1062 } elsif ($form->{reporttype} eq 'custom') {
1063 push @options, $locale->text('Reference day') . " " . $locale->date(\%myconfig, $form->{fordate}, 1);
1065 die "Unknown reporttype for aging";
1068 $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1070 $report->set_options('top_info_text' => join("\n", @options),
1071 'output_format' => 'HTML',
1072 'title' => $form->{title},
1073 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1075 $report->set_options_from_form();
1076 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1078 my $previous_ctid = 0;
1080 my @periods = qw(open amount current_open);
1081 my %subtotals = map { $_ => 0 } @periods;
1082 my %totals = map { $_ => 0 } @periods;
1084 foreach my $ref (@{ $form->{AG} }) {
1085 if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1086 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1089 foreach my $key (@periods) {
1090 $subtotals{$key} += $ref->{"$key"};
1091 $totals{$key} += $ref->{"$key"};
1092 $ref->{"$key"} = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
1097 foreach my $column (@columns) {
1099 'data' => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1100 'align' => $column_alignment{$column},
1101 'valign' => $column eq 'statement' ? 'center' : '',
1105 $row->{invnumber}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1107 if ($previous_ctid != $ref->{ctid}) {
1108 $row->{statement}->{raw_data} =
1109 $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
1110 . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1111 $row->{ct}->{data} = $ref->{name};
1116 $previous_ctid = $ref->{ctid};
1118 $report->add_data($row);
1121 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1123 $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1125 if ($form->{arap} eq 'ar') {
1126 my $raw_top_info_text = $form->parse_html_template('rp/aging_ar_top');
1127 my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1128 'PRINT_OPTIONS' => print_options(inline => 1), });
1129 $report->set_options('raw_top_info_text' => $raw_top_info_text,
1130 'raw_bottom_info_text' => $raw_bottom_info_text);
1133 setup_rp_aging_action_bar(arap => $form->{arap});
1134 $report->generate_with_headers();
1136 $main::lxdebug->leave_sub();
1140 $main::lxdebug->enter_sub();
1142 $main::auth->assert('general_ledger');
1144 my $form = $main::form;
1145 my %myconfig = %main::myconfig;
1146 my $locale = $main::locale;
1148 $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1149 unless $form->{subject};
1151 RP->aging(\%myconfig, \%$form);
1154 my $email_form = delete $form->{email_form};
1155 my %field_names = (to => 'email');
1157 $form->{ $field_names{$_} // $_ } = $email_form->{$_} for keys %{ $email_form };
1158 $form->{media} = 'email';
1161 $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1163 $main::lxdebug->leave_sub();
1167 $main::lxdebug->enter_sub();
1169 $main::auth->assert('general_ledger');
1171 my $form = $main::form;
1172 my %myconfig = %main::myconfig;
1173 my $locale = $main::locale;
1175 if ($form->{media} eq 'printer') {
1176 $form->error($locale->text('Select postscript or PDF!'))
1177 if ($form->{format} !~ /(postscript|pdf)/);
1181 for my $i (1 .. $form->{rowcount}) {
1182 if ($form->{"statement_$i"}) {
1183 $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1189 $form->error($locale->text('Nothing selected!')) unless $selected;
1191 if ($form->{media} eq 'printer') {
1192 $form->{"$form->{ct}_id"} = "";
1194 $form->{"statement_1"} = 1;
1197 RP->aging(\%myconfig, \%$form);
1201 $form->redirect($locale->text('Statements sent to printer!'))
1202 if ($form->{media} eq 'printer');
1204 $main::lxdebug->leave_sub();
1208 $main::lxdebug->enter_sub();
1210 $main::auth->assert('general_ledger');
1212 my $form = $main::form;
1213 my %myconfig = %main::myconfig;
1214 my $locale = $main::locale;
1216 my $defaults = SL::DB::Default->get;
1217 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
1218 $form->{templates} = $defaults->templates;
1220 $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1222 my $suffix = "html";
1223 my $attachment_suffix = "html";
1224 if ($form->{format} eq 'postscript') {
1225 $form->{postscript} = 1;
1227 $attachment_suffix = "ps";
1228 } elsif ($form->{format} eq 'pdf') {
1231 $attachment_suffix = "pdf";
1234 $form->{IN} = "$form->{type}.$suffix";
1235 $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1237 # Save $form->{email} because it will be overwritten.
1238 $form->{EMAIL_RECIPIENT} = $form->{email};
1242 while (@{ $form->{AG} }) {
1244 my $ref = shift @{ $form->{AG} };
1246 if ($ctid != $ref->{ctid}) {
1248 $ctid = $ref->{ctid};
1251 if ($form->{"statement_$i"}) {
1254 ("name", "street", "zipcode", "city", "country", "contact", "email",
1255 "$form->{ct}phone", "$form->{ct}fax");
1256 map { $form->{$_} = $ref->{$_} } @a;
1258 $form->{ $form->{ct} } = $form->{name};
1259 $form->{"$form->{ct}_id"} = $ref->{ctid};
1261 map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
1263 foreach my $item (qw(c0 c30 c60 c90)) {
1264 $form->{$item} = ();
1265 $form->{"${item}total"} = 0;
1268 &statement_details($ref);
1272 if (scalar(@{ $form->{AG} }) > 0) {
1274 # one or more left to go
1275 if ($ctid == $form->{AG}->[0]->{ctid}) {
1276 $ref = shift @{ $form->{AG} };
1277 &statement_details($ref);
1280 $ref = scalar(@{ $form->{AG} });
1286 # set initial ref to 0
1293 $form->{"${_}total"} =
1294 $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1295 } ('c0', 'c30', 'c60', 'c90', "");
1297 $form->{attachment_filename} = $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
1298 $form->{attachment_filename} =~ s/\s+/_/g;
1300 $form->parse_template(\%myconfig);
1305 # saving the history
1306 if(!exists $form->{addition} && $form->{id} ne "") {
1307 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1308 $form->{addition} = "PRINTED";
1309 $form->{what_done} = $form->{type};
1310 $form->save_history;
1312 # /saving the history
1313 $main::lxdebug->leave_sub();
1316 sub statement_details {
1317 $main::lxdebug->enter_sub();
1319 $main::auth->assert('general_ledger');
1321 my $form = $main::form;
1322 my %myconfig = %main::myconfig;
1323 my $locale = $main::locale;
1327 push @{ $form->{invnumber} }, $ref->{invnumber};
1328 push @{ $form->{invdate} }, $ref->{transdate};
1329 push @{ $form->{duedate} }, $ref->{duedate};
1330 push @{ $form->{amount} }, $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
1331 push @{ $form->{open} }, $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
1333 foreach my $item (qw(c0 c30 c60 c90)) {
1334 if ($ref->{exchangerate} * 1) {
1335 # add only the open amount of the invoice to the aging, not the total amount
1336 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
1337 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
1338 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
1339 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
1341 $form->{"${item}total"} += $ref->{$item};
1342 $form->{total} += $ref->{$item};
1343 push @{ $form->{$item} },
1344 $form->format_amount(\%myconfig, $ref->{$item}, 2);
1347 $main::lxdebug->leave_sub();
1350 sub generate_tax_report {
1351 $::lxdebug->enter_sub;
1352 $::auth->assert('report');
1354 RP->tax_report(\%::myconfig, $::form);
1356 my $descvar = "$::form->{accno}_description";
1357 my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
1361 my $callback = build_std_url('action=generate_tax_report', $descvar,
1362 qw(fromdate todate db method accno department report title));
1364 my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1367 for my $item (@columns, 'subtotal') {
1368 if ($::form->{"l_$item"} eq "Y") {
1369 $callback .= "&l_$item=Y";
1370 $href .= "&l_$item=Y";
1374 for my $item (@columns) {
1375 if ($::form->{"l_$item"} eq "Y") {
1376 push @column_index, $item;
1381 if ($::form->{department}) {
1382 my ($department) = split /--/, $::form->{department};
1383 push @options, $::locale->text('Department') . " : $department";
1386 # if there are any dates
1387 if ($::form->{fromdate} || $::form->{todate}) {
1388 my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
1389 my $todate = $::form->{todate} ? $::locale->date(\%::myconfig, $::form->{todate}, 1) : '';
1390 push @options, "$fromdate - $todate";
1392 push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
1395 my ($name, $invoice, $arap);
1396 if ($::form->{db} eq 'ar') {
1397 $name = $::locale->text('Customer');
1401 if ($::form->{db} eq 'ap') {
1402 $name = $::locale->text('Vendor');
1407 my %column_header = (
1408 id => $::locale->text('ID'),
1409 invnumber => $::locale->text('Invoice'),
1410 transdate => $::locale->text('Date'),
1411 netamount => $::locale->text('Amount'),
1412 tax => $::locale->text('Tax'),
1413 amount => $::locale->text('Total'),
1417 my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
1419 $callback .= "&sort=$::form->{sort}";
1422 if (@{ $::form->{TR} }) {
1423 $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
1426 my ($totalnetamount, $totaltax, @data);
1427 for my $ref (@{ $::form->{TR} }) {
1429 my $module = ($ref->{invoice}) ? $invoice : $arap;
1431 if ($::form->{l_subtotal} eq 'Y') {
1432 if ($sameitem ne $ref->{ $::form->{sort} }) {
1435 netamount => $subtotalnetamount,
1436 tax => $subtotaltax,
1437 amount => $subtotal,
1439 $subtotalnetamount = 0;
1441 $sameitem = $ref->{ $::form->{sort} };
1445 $subtotalnetamount += $ref->{netamount};
1446 $subtotaltax += $ref->{tax};
1447 $totalnetamount += $ref->{netamount};
1448 $totaltax += $ref->{tax};
1449 $ref->{amount} = $ref->{netamount} + $ref->{tax};
1451 push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
1452 $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
1453 $data[-1]{$_}{numeric} = 1 for qw(netamount tax amount);
1456 if ($::form->{l_subtotal} eq 'Y') {
1459 netamount => $subtotalnetamount,
1460 tax => $subtotaltax,
1461 amount => $subtotal,
1467 netamount => $totalnetamount,
1469 amount => $totalnetamount + $totaltax,
1473 print $::form->parse_html_template('rp/tax_report', {
1474 column_index => \@column_index,
1475 column_header => \%column_header,
1476 column_sorted => \%column_sorted,
1479 options => \@options,
1482 $::lxdebug->leave_sub;
1486 $main::lxdebug->enter_sub();
1488 $main::auth->assert('cash');
1490 my $form = $main::form;
1491 my %myconfig = %main::myconfig;
1492 my $locale = $main::locale;
1494 if ($form->{account}) {
1495 ($form->{paymentaccounts}) = split /--/, $form->{account};
1499 if ($form->{department}) {
1500 (my $department, $form->{department_id}) = split /--/, $form->{department};
1501 $option = $locale->text('Department') . " : $department";
1504 report_generator_set_default_sort('transdate', 1);
1506 RP->payments(\%myconfig, \%$form);
1508 my @hidden_variables = qw(account title department reference source memo fromdate todate
1509 fx_transaction db prepayment paymentaccounts sort);
1511 my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
1512 $form->{callback} = $href;
1514 my @columns = qw(transdate invnumber name paid source memo);
1516 'name' => { 'text' => $locale->text('Description'), },
1517 'invnumber' => { 'text' => $locale->text('Reference'), },
1518 'transdate' => { 'text' => $locale->text('Date'), },
1519 'paid' => { 'text' => $locale->text('Amount'), },
1520 'source' => { 'text' => $locale->text('Source'), },
1521 'memo' => { 'text' => $locale->text('Memo'), },
1523 my %column_alignment = ('paid' => 'right');
1525 foreach my $name (grep { $_ ne 'paid' } @columns) {
1526 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1527 $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
1531 if ($form->{fromdate}) {
1532 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
1534 if ($form->{todate}) {
1535 push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1538 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1540 my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
1542 $report->set_options('top_info_text' => join("\n", @options),
1543 'output_format' => 'HTML',
1544 'title' => $form->{title},
1545 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1546 'std_column_visibility' => 1,
1548 $report->set_options_from_form();
1550 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1552 $report->set_columns(%column_defs);
1553 $report->set_column_order(@columns);
1555 $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
1557 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1561 foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1562 next unless @{ $form->{ $ref->{id} } };
1564 $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
1566 my $subtotal_paid = 0;
1568 foreach my $payment (@{ $form->{ $ref->{id} } }) {
1569 my $module = $payment->{module};
1570 $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1571 $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1573 $subtotal_paid += $payment->{paid};
1574 $total_paid += $payment->{paid};
1576 $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
1580 foreach my $column (@columns) {
1582 'data' => $payment->{$column},
1583 'align' => $column_alignment{$column},
1587 $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
1589 $report->add_data($row);
1592 my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
1594 'data' => $form->format_amount(\%myconfig, $subtotal_paid, 2),
1596 'class' => 'listsubtotal',
1599 $report->add_data($row);
1602 $report->add_separator();
1604 my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
1606 'data' => $form->format_amount(\%myconfig, $total_paid, 2),
1608 'class' => 'listtotal',
1611 $report->add_data($row);
1613 $report->generate_with_headers();
1615 $main::lxdebug->leave_sub();
1619 $::lxdebug->enter_sub;
1621 my ($dont_print) = @_;
1623 $::form->{sendmode} = "attachment";
1624 $::form->{format} ||= $::myconfig{template_format} || "pdf";
1625 $::form->{copies} ||= $::myconfig{copies} || 2;
1627 $::form->{PD}{ $::form->{type} } = "selected";
1628 $::form->{DF}{ $::form->{format} } = "selected";
1629 $::form->{OP}{ $::form->{media} } = "selected";
1630 $::form->{SM}{ $::form->{sendmode} } = "selected";
1632 my $output = $::form->parse_html_template('rp/print_options', {
1633 is_email => $::form->{media} eq 'email',
1636 print $output unless $dont_print;
1638 $::lxdebug->leave_sub;
1644 $main::lxdebug->enter_sub();
1646 $main::auth->assert('report');
1648 my $form = $main::form;
1649 my %myconfig = %main::myconfig;
1650 my $locale = $main::locale;
1652 $form->{padding} = " ";
1653 $form->{bold} = "<b>";
1654 $form->{endbold} = "</b>";
1655 $form->{br} = "<br>";
1657 if ($form->{reporttype} eq "custom") {
1659 #forgotten the year --> thisyear
1660 if ($form->{year} !~ m/^\d\d\d\d$/) {
1661 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1667 if ($form->{duetyp} eq "13") {
1668 $form->{fromdate} = "1.1.$form->{year}";
1669 $form->{todate} = "31.12.$form->{year}";
1670 $form->{comparefromdate} = "1.01.$form->{year}";
1671 $form->{comparetodate} = "31.12.$form->{year}";
1675 if ($form->{duetyp} eq "A") {
1676 $form->{fromdate} = "1.1.$form->{year}";
1677 $form->{todate} = "31.3.$form->{year}";
1678 $form->{comparefromdate} = "1.01.$form->{year}";
1679 $form->{comparetodate} = "31.03.$form->{year}";
1681 if ($form->{duetyp} eq "B") {
1682 $form->{fromdate} = "1.4.$form->{year}";
1683 $form->{todate} = "30.6.$form->{year}";
1684 $form->{comparefromdate} = "1.01.$form->{year}";
1685 $form->{comparetodate} = "30.06.$form->{year}";
1687 if ($form->{duetyp} eq "C") {
1688 $form->{fromdate} = "1.7.$form->{year}";
1689 $form->{todate} = "30.9.$form->{year}";
1690 $form->{comparefromdate} = "1.01.$form->{year}";
1691 $form->{comparetodate} = "30.09.$form->{year}";
1693 if ($form->{duetyp} eq "D") {
1694 $form->{fromdate} = "1.10.$form->{year}";
1695 $form->{todate} = "31.12.$form->{year}";
1696 $form->{comparefromdate} = "1.01.$form->{year}";
1697 $form->{comparetodate} = "31.12.$form->{year}";
1702 $form->{duetyp} eq "1" && do {
1703 $form->{fromdate} = "1.1.$form->{year}";
1704 $form->{todate} = "31.1.$form->{year}";
1705 $form->{comparefromdate} = "1.01.$form->{year}";
1706 $form->{comparetodate} = "31.01.$form->{year}";
1709 $form->{duetyp} eq "2" && do {
1710 $form->{fromdate} = "1.2.$form->{year}";
1712 #this works from 1901 to 2099, 1900 and 2100 fail.
1713 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1714 $form->{todate} = "$leap.2.$form->{year}";
1715 $form->{comparefromdate} = "1.01.$form->{year}";
1716 $form->{comparetodate} = "$leap.02.$form->{year}";
1719 $form->{duetyp} eq "3" && do {
1720 $form->{fromdate} = "1.3.$form->{year}";
1721 $form->{todate} = "31.3.$form->{year}";
1722 $form->{comparefromdate} = "1.01.$form->{year}";
1723 $form->{comparetodate} = "31.03.$form->{year}";
1726 $form->{duetyp} eq "4" && do {
1727 $form->{fromdate} = "1.4.$form->{year}";
1728 $form->{todate} = "30.4.$form->{year}";
1729 $form->{comparefromdate} = "1.01.$form->{year}";
1730 $form->{comparetodate} = "30.04.$form->{year}";
1733 $form->{duetyp} eq "5" && do {
1734 $form->{fromdate} = "1.5.$form->{year}";
1735 $form->{todate} = "31.5.$form->{year}";
1736 $form->{comparefromdate} = "1.01.$form->{year}";
1737 $form->{comparetodate} = "31.05.$form->{year}";
1740 $form->{duetyp} eq "6" && do {
1741 $form->{fromdate} = "1.6.$form->{year}";
1742 $form->{todate} = "30.6.$form->{year}";
1743 $form->{comparefromdate} = "1.01.$form->{year}";
1744 $form->{comparetodate} = "30.06.$form->{year}";
1747 $form->{duetyp} eq "7" && do {
1748 $form->{fromdate} = "1.7.$form->{year}";
1749 $form->{todate} = "31.7.$form->{year}";
1750 $form->{comparefromdate} = "1.01.$form->{year}";
1751 $form->{comparetodate} = "31.07.$form->{year}";
1754 $form->{duetyp} eq "8" && do {
1755 $form->{fromdate} = "1.8.$form->{year}";
1756 $form->{todate} = "31.8.$form->{year}";
1757 $form->{comparefromdate} = "1.01.$form->{year}";
1758 $form->{comparetodate} = "31.08.$form->{year}";
1761 $form->{duetyp} eq "9" && do {
1762 $form->{fromdate} = "1.9.$form->{year}";
1763 $form->{todate} = "30.9.$form->{year}";
1764 $form->{comparefromdate} = "1.01.$form->{year}";
1765 $form->{comparetodate} = "30.09.$form->{year}";
1768 $form->{duetyp} eq "10" && do {
1769 $form->{fromdate} = "1.10.$form->{year}";
1770 $form->{todate} = "31.10.$form->{year}";
1771 $form->{comparefromdate} = "1.01.$form->{year}";
1772 $form->{comparetodate} = "31.10.$form->{year}";
1775 $form->{duetyp} eq "11" && do {
1776 $form->{fromdate} = "1.11.$form->{year}";
1777 $form->{todate} = "30.11.$form->{year}";
1778 $form->{comparefromdate} = "1.01.$form->{year}";
1779 $form->{comparetodate} = "30.11.$form->{year}";
1782 $form->{duetyp} eq "12" && do {
1783 $form->{fromdate} = "1.12.$form->{year}";
1784 $form->{todate} = "31.12.$form->{year}";
1785 $form->{comparefromdate} = "1.01.$form->{year}";
1786 $form->{comparetodate} = "31.12.$form->{year}";
1790 hotfix_reformat_date();
1792 # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
1793 # ansonsten ist die prüfung in RP.pm
1794 # if (defined ($form->{fromdate|todate}=='..'))
1796 if ($form->{fromdate}){
1797 my $datetime = $locale->parse_date_to_object($form->{fromdate});
1798 $datetime->set( month => 1,
1800 $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
1802 if ($form->{todate}){
1803 $form->{comparetodate} = $form->{todate};
1807 RP->bwa(\%myconfig, \%$form);
1809 ($form->{department}) = split /--/, $form->{department};
1812 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1813 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1815 # if there are any dates construct a where
1816 if ($form->{fromdate} || $form->{todate}) {
1818 unless ($form->{todate}) {
1819 $form->{todate} = $form->current_date(\%myconfig);
1822 my %germandate = ("dateformat" => "dd.mm.yyyy");
1824 my $longtodate = $locale->date(\%germandate, $form->{todate}, 1);
1825 my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
1827 my $longfromdate = $locale->date(\%germandate, $form->{fromdate}, 1);
1828 my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
1830 $form->{this_period} = "$shortfromdate\n$shorttodate";
1832 $locale->text('for Period')
1833 . qq|\n$longfromdate |
1834 . $locale->text('bis')
1838 $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
1840 if ( $form->{method} eq 'cash' ) {
1841 $form->{accounting_method} = $locale->text('Cash accounting');
1842 } elsif ( $form->{method} eq 'accrual' ) {
1843 $form->{accounting_method} = $locale->text('Accrual accounting');
1845 $form->{accounting_method} = "";
1848 $form->{title} = $locale->text('BWA');
1850 $::request->layout->add_stylesheets('bwa.css');
1852 print $form->parse_html_template('rp/bwa');
1854 $main::lxdebug->leave_sub();
1857 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
1858 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
1859 sub hotfix_reformat_date {
1861 $main::lxdebug->enter_sub();
1863 my $form = $main::form;
1864 my %myconfig = %main::myconfig;
1865 my $locale = $main::locale;
1867 if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
1868 my $current_dateformat = $myconfig{dateformat};
1869 $myconfig{dateformat} = 'dd.mm.yyyy';
1870 $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
1871 $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
1872 $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
1873 unless (!defined ($form->{comparefromdate}));
1874 $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
1875 unless (!defined ($form->{comparetodate}));
1877 # Und wieder zurücksetzen
1878 $myconfig{dateformat} = $current_dateformat; #'dd.mm.yyyy';
1879 } # Ende Hotifx Bug 1388
1881 $main::lxdebug->leave_sub();
1885 sub setup_rp_aging_action_bar {
1888 return unless $params{arap} eq 'ar';
1890 for my $bar ($::request->layout->get('actionbar')) {
1895 call => [ 'kivi.SalesPurchase.show_print_dialog' ],
1896 checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
1900 call => [ 'kivi.SalesPurchase.show_email_dialog', 'send_email' ],
1901 checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
1908 sub setup_rp_report_action_bar {
1911 for my $bar ($::request->layout->get('actionbar')) {
1915 submit => [ '#form', { action => 'continue' } ],
1916 accesskey => 'enter',