1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
15 # Contributors: Antonio Gallardo <agssa@ibw.com.ni>
16 # Benjamin Lee <benjaminlee@consultant.com>
17 # Philip Reetz <p.reetz@linet-services.de>
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 2 of the License, or
23 # (at your option) any later version.
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 # GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #======================================================================
34 # module for preparing Income Statement and Balance Sheet
36 #======================================================================
38 use POSIX qw(strftime);
46 use SL::ReportGenerator;
48 use List::MoreUtils qw(any);
50 require "bin/mozilla/arap.pl";
51 require "bin/mozilla/common.pl";
52 require "bin/mozilla/reportgenerator.pl";
54 # note: this file was particularly hard to strictify.
55 # alot of the vars are passed carelessly between invocations
56 # should there be any missing vars, declare them globally
59 # this is for our long dates
60 # $locale->text('January')
61 # $locale->text('February')
62 # $locale->text('March')
63 # $locale->text('April')
64 # $locale->text('May ')
65 # $locale->text('June')
66 # $locale->text('July')
67 # $locale->text('August')
68 # $locale->text('September')
69 # $locale->text('October')
70 # $locale->text('November')
71 # $locale->text('December')
73 # this is for our short month
74 # $locale->text('Jan')
75 # $locale->text('Feb')
76 # $locale->text('Mar')
77 # $locale->text('Apr')
78 # $locale->text('May')
79 # $locale->text('Jun')
80 # $locale->text('Jul')
81 # $locale->text('Aug')
82 # $locale->text('Sep')
83 # $locale->text('Oct')
84 # $locale->text('Nov')
85 # $locale->text('Dec')
87 # $locale->text('Balance Sheet')
88 # $locale->text('Income Statement')
89 # $locale->text('Trial Balance')
90 # $locale->text('AR Aging')
91 # $locale->text('AP Aging')
92 # $locale->text('Search AR Aging')
93 # $locale->text('Search AP Aging')
94 # $locale->text('Tax collected')
95 # $locale->text('Tax paid')
96 # $locale->text('Receipts')
97 # $locale->text('Payments')
98 # $locale->text('Project Transactions')
99 # $locale->text('Business evaluation')
101 # $form->parse_html_template('rp/html_report_susa')
103 my $rp_access_map = {
104 'projects' => 'report',
105 'ar_aging' => 'general_ledger',
106 'ap_aging' => 'general_ledger',
107 'receipts' => 'cash',
108 'payments' => 'cash',
109 'trial_balance' => 'report',
110 'income_statement' => 'report',
111 'erfolgsrechnung' => 'report',
113 'balance_sheet' => 'report',
116 sub check_rp_access {
117 my $form = $main::form;
119 my $right = $rp_access_map->{$form->{report}};
120 $right ||= 'DOES_NOT_EXIST';
122 $main::auth->assert($right);
126 $::lxdebug->enter_sub;
131 balance_sheet => $::locale->text('Balance Sheet'),
132 income_statement => $::locale->text('Income Statement'),
133 erfolgsrechnung => $::locale->text('Erfolgsrechnung'),
134 trial_balance => $::locale->text('Trial Balance'),
135 ar_aging => $::locale->text('Search AR Aging'),
136 ap_aging => $::locale->text('Search AP Aging'),
137 tax_collected => $::locale->text('Tax collected'),
138 tax_paid => $::locale->text('Tax paid'),
139 receipts => $::locale->text('Receipts'),
140 payments => $::locale->text('Payments'),
141 projects => $::locale->text('Project Transactions'),
142 bwa => $::locale->text('Business evaluation'),
145 $::form->{title} = $title{$::form->{report}};
146 $::request->{layout}->add_javascripts('autocomplete_customer.js');
147 $::request->{layout}->add_javascripts('autocomplete_project.js');
148 $::form->{fromdate} = DateTime->today->truncate(to => 'year')->to_kivitendo;
149 $::form->{todate} = DateTime->today->truncate(to => 'year')->add(years => 1)->add(days => -1)->to_kivitendo;
152 $::form->all_departments(\%::myconfig);
153 if (@{ $::form->{all_departments} || [] }) {
154 $::form->{selectdepartment} = "<option>\n";
155 map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
158 $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
160 my $is_projects = $::form->{report} eq "projects";
161 my $is_income_statement = $::form->{report} eq "income_statement";
162 my $is_erfolgsrechnung = $::form->{report} eq "erfolgsrechnung";
163 my $is_bwa = $::form->{report} eq "bwa";
164 my $is_balance_sheet = $::form->{report} eq "balance_sheet";
165 my $is_trial_balance = $::form->{report} eq "trial_balance";
166 my $is_aging = $::form->{report} =~ /^a[rp]_aging$/;
167 my $is_payments = $::form->{report} =~ /(receipts|payments)$/;
169 my ($label, $nextsub, $vc);
171 my $is_sales = $::form->{report} eq 'ar_aging';
172 $label = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
173 $::form->{vc} = $is_sales ? 'customer' : 'vendor';
175 $nextsub = "generate_$::form->{report}";
178 $::form->all_vc(\%::myconfig, $::form->{vc}, $is_sales ? "AR" : "AP");
179 $vc .= "<option>$_->{name}--$_->{id}\n" for @{ $::form->{"all_$::form->{vc}"} };
181 ? qq|<select name=$::form->{vc} class="initial_focus"><option>\n$vc</select>|
182 : qq|<input name=$::form->{vc} size=35 class="initial_focus">|;
185 my ($selection, $paymentaccounts);
187 $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
189 RP->paymentaccounts(\%::myconfig, $::form);
191 $selection = "<option>\n";
192 for my $ref (@{ $::form->{PR} }) {
193 $paymentaccounts .= "$ref->{accno} ";
194 $selection .= "<option>$ref->{accno}--$ref->{description}\n";
199 print $::form->parse_html_template('rp/report', {
200 paymentaccounts => $paymentaccounts,
201 selection => $selection,
202 is_aging => $is_aging,
205 year => DateTime->today->year,
206 today => DateTime->today,
208 is_payments => $is_payments,
209 is_trial_balance => $is_trial_balance,
210 is_balance_sheet => $is_balance_sheet,
212 is_income_statement => $is_income_statement,
213 is_erfolgsrechnung => $is_erfolgsrechnung,
214 is_projects => $is_projects,
217 $::lxdebug->leave_sub;
220 sub continue { call_sub($main::form->{"nextsub"}); }
222 sub generate_income_statement {
223 $main::lxdebug->enter_sub();
225 $main::auth->assert('report');
227 my $form = $main::form;
228 my %myconfig = %main::myconfig;
229 my $locale = $main::locale;
231 $form->{padding} = " ";
232 $form->{bold} = "<b>";
233 $form->{endbold} = "</b>";
234 $form->{br} = "<br>";
236 if ($form->{reporttype} eq "custom") {
238 #forgotten the year --> thisyear
239 if ($form->{year} !~ m/^\d\d\d\d$/) {
240 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
246 if ($form->{duetyp} eq "13") {
247 $form->{fromdate} = "1.1.$form->{year}";
248 $form->{todate} = "31.12.$form->{year}";
252 if ($form->{duetyp} eq "A") {
253 $form->{fromdate} = "1.1.$form->{year}";
254 $form->{todate} = "31.3.$form->{year}";
256 if ($form->{duetyp} eq "B") {
257 $form->{fromdate} = "1.4.$form->{year}";
258 $form->{todate} = "30.6.$form->{year}";
260 if ($form->{duetyp} eq "C") {
261 $form->{fromdate} = "1.7.$form->{year}";
262 $form->{todate} = "30.9.$form->{year}";
264 if ($form->{duetyp} eq "D") {
265 $form->{fromdate} = "1.10.$form->{year}";
266 $form->{todate} = "31.12.$form->{year}";
271 $form->{duetyp} eq "1" && do {
272 $form->{fromdate} = "1.1.$form->{year}";
273 $form->{todate} = "31.1.$form->{year}";
276 $form->{duetyp} eq "2" && do {
277 $form->{fromdate} = "1.2.$form->{year}";
279 #this works from 1901 to 2099, 1900 and 2100 fail.
280 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
281 $form->{todate} = "$leap.2.$form->{year}";
284 $form->{duetyp} eq "3" && do {
285 $form->{fromdate} = "1.3.$form->{year}";
286 $form->{todate} = "31.3.$form->{year}";
289 $form->{duetyp} eq "4" && do {
290 $form->{fromdate} = "1.4.$form->{year}";
291 $form->{todate} = "30.4.$form->{year}";
294 $form->{duetyp} eq "5" && do {
295 $form->{fromdate} = "1.5.$form->{year}";
296 $form->{todate} = "31.5.$form->{year}";
299 $form->{duetyp} eq "6" && do {
300 $form->{fromdate} = "1.6.$form->{year}";
301 $form->{todate} = "30.6.$form->{year}";
304 $form->{duetyp} eq "7" && do {
305 $form->{fromdate} = "1.7.$form->{year}";
306 $form->{todate} = "31.7.$form->{year}";
309 $form->{duetyp} eq "8" && do {
310 $form->{fromdate} = "1.8.$form->{year}";
311 $form->{todate} = "31.8.$form->{year}";
314 $form->{duetyp} eq "9" && do {
315 $form->{fromdate} = "1.9.$form->{year}";
316 $form->{todate} = "30.9.$form->{year}";
319 $form->{duetyp} eq "10" && do {
320 $form->{fromdate} = "1.10.$form->{year}";
321 $form->{todate} = "31.10.$form->{year}";
324 $form->{duetyp} eq "11" && do {
325 $form->{fromdate} = "1.11.$form->{year}";
326 $form->{todate} = "30.11.$form->{year}";
329 $form->{duetyp} eq "12" && do {
330 $form->{fromdate} = "1.12.$form->{year}";
331 $form->{todate} = "31.12.$form->{year}";
335 hotfix_reformat_date();
336 } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
338 RP->income_statement(\%myconfig, \%$form);
340 ($form->{department}) = split /--/, $form->{department};
343 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
344 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
346 # if there are any dates construct a where
347 if ($form->{fromdate} || $form->{todate}) {
349 unless ($form->{todate}) {
350 $form->{todate} = $form->current_date(\%myconfig);
353 my $longtodate = $locale->date(\%myconfig, $form->{todate}, 1);
354 my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
356 my $longfromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
357 my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
359 $form->{this_period} = "$shortfromdate\n$shorttodate";
361 $locale->text('for Period')
362 . qq|\n$longfromdate |
363 . $locale->text('Bis')
367 if ($form->{comparefromdate} || $form->{comparetodate}) {
368 my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
369 my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
371 my $longcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 1);
372 my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
374 $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
376 "\n$longcomparefromdate "
377 . $locale->text('Bis')
378 . qq| $longcomparetodate|;
381 if ( $::instance_conf->get_profit_determination eq 'balance' ) {
382 $form->{title} = $locale->text('Income Statement');
383 } elsif ( $::instance_conf->get_profit_determination eq 'income' ) {
384 $form->{title} = $locale->text('Net Income Statement');
389 if ( $form->{method} eq 'cash' ) {
390 $form->{accounting_method} = $locale->text('Cash accounting');
391 } elsif ( $form->{method} eq 'accrual' ) {
392 $form->{accounting_method} = $locale->text('Accrual accounting');
394 $form->{accounting_method} = "";
397 $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
400 print $form->parse_html_template('rp/income_statement');
402 $main::lxdebug->leave_sub();
405 sub generate_erfolgsrechnung {
406 $::lxdebug->enter_sub;
407 $::auth->assert('report');
409 $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
410 $::form->{padding} = " ";
411 $::form->{bold} = "<b>";
412 $::form->{endbold} = "</b>";
413 $::form->{br} = "<br>";
415 my $data = RP->erfolgsrechnung(\%::myconfig, $::form);
418 print $::form->parse_html_template('rp/erfolgsrechnung', $data);
420 $::lxdebug->leave_sub;
424 sub generate_balance_sheet {
425 $::lxdebug->enter_sub;
426 $::auth->assert('report');
428 $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
429 $::form->{padding} = " ";
430 $::form->{bold} = "<b>";
431 $::form->{endbold} = "</b>";
432 $::form->{br} = "<br>";
434 my $data = RP->balance_sheet(\%::myconfig, $::form);
436 $::form->{asofdate} ||= $::form->current_date;
437 $::form->{report_title} = $::locale->text('Balance Sheet');
438 $::form->{report_date} ||= $::form->current_date;
440 ($::form->{department}) = split /--/, $::form->{department};
442 # define Current Earnings account
443 my $padding = $::form->{l_heading} ? $::form->{padding} : "";
444 push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
446 $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
447 $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
449 # balance sheet isn't read from print templates anymore,
450 # instead use template in rp
451 # $::form->{IN} = "balance_sheet.html";
454 print $::form->parse_html_template('rp/balance_sheet', $data);
456 $::lxdebug->leave_sub;
459 sub generate_projects {
460 $main::lxdebug->enter_sub();
462 $main::auth->assert('report');
464 my $form = $main::form;
465 my %myconfig = %main::myconfig;
466 my $locale = $main::locale;
468 my $project = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
469 $form->{projectnumber} = $project ? $project->projectnumber : '';
471 # make sure todate and fromdate always have a value, even if the date fields
472 # were left empty or the inputs weren't valid dates/couldn't be parsed
474 $project = SL::DB::Project->new() unless $project; # dummy object for dbh
475 unless ($::locale->parse_date_to_object($::form->{fromdate})) {
476 ($form->{fromdate}) = $project->db->dbh->selectrow_array('select min(transdate) from acc_trans');
479 unless ($::locale->parse_date_to_object($::form->{todate})) {
480 ($form->{todate}) = $project->db->dbh->selectrow_array('select max(transdate) from acc_trans');
483 $form->{nextsub} = "generate_projects";
484 $form->{title} = $locale->text('Project Transactions');
485 RP->trial_balance(\%myconfig, \%$form);
487 list_accounts('generate_projects');
489 $main::lxdebug->leave_sub();
495 # included links to display transactions for period entered
496 # added headers and subtotals
498 sub generate_trial_balance {
499 $main::lxdebug->enter_sub();
501 $main::auth->assert('report');
503 my $form = $main::form;
504 my %myconfig = %main::myconfig;
505 my $locale = $main::locale;
506 my $defaults = SL::DB::Default->get;
508 if ($form->{reporttype} eq "custom") {
510 #forgotten the year --> thisyear
511 if ($form->{year} !~ m/^\d\d\d\d$/) {
512 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
518 if ($form->{duetyp} eq "13") {
519 $form->{fromdate} = "1.1.$form->{year}";
520 $form->{todate} = "31.12.$form->{year}";
524 if ($form->{duetyp} eq "A") {
525 $form->{fromdate} = "1.1.$form->{year}";
526 $form->{todate} = "31.3.$form->{year}";
528 if ($form->{duetyp} eq "B") {
529 $form->{fromdate} = "1.4.$form->{year}";
530 $form->{todate} = "30.6.$form->{year}";
532 if ($form->{duetyp} eq "C") {
533 $form->{fromdate} = "1.7.$form->{year}";
534 $form->{todate} = "30.9.$form->{year}";
536 if ($form->{duetyp} eq "D") {
537 $form->{fromdate} = "1.10.$form->{year}";
538 $form->{todate} = "31.12.$form->{year}";
543 $form->{duetyp} eq "1" && do {
544 $form->{fromdate} = "1.1.$form->{year}";
545 $form->{todate} = "31.1.$form->{year}";
548 $form->{duetyp} eq "2" && do {
549 $form->{fromdate} = "1.2.$form->{year}";
551 #this works from 1901 to 2099, 1900 and 2100 fail.
552 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
553 $form->{todate} = "$leap.2.$form->{year}";
556 $form->{duetyp} eq "3" && do {
557 $form->{fromdate} = "1.3.$form->{year}";
558 $form->{todate} = "31.3.$form->{year}";
561 $form->{duetyp} eq "4" && do {
562 $form->{fromdate} = "1.4.$form->{year}";
563 $form->{todate} = "30.4.$form->{year}";
566 $form->{duetyp} eq "5" && do {
567 $form->{fromdate} = "1.5.$form->{year}";
568 $form->{todate} = "31.5.$form->{year}";
571 $form->{duetyp} eq "6" && do {
572 $form->{fromdate} = "1.6.$form->{year}";
573 $form->{todate} = "30.6.$form->{year}";
576 $form->{duetyp} eq "7" && do {
577 $form->{fromdate} = "1.7.$form->{year}";
578 $form->{todate} = "31.7.$form->{year}";
581 $form->{duetyp} eq "8" && do {
582 $form->{fromdate} = "1.8.$form->{year}";
583 $form->{todate} = "31.8.$form->{year}";
586 $form->{duetyp} eq "9" && do {
587 $form->{fromdate} = "1.9.$form->{year}";
588 $form->{todate} = "30.9.$form->{year}";
591 $form->{duetyp} eq "10" && do {
592 $form->{fromdate} = "1.10.$form->{year}";
593 $form->{todate} = "31.10.$form->{year}";
596 $form->{duetyp} eq "11" && do {
597 $form->{fromdate} = "1.11.$form->{year}";
598 $form->{todate} = "30.11.$form->{year}";
601 $form->{duetyp} eq "12" && do {
602 $form->{fromdate} = "1.12.$form->{year}";
603 $form->{todate} = "31.12.$form->{year}";
607 hotfix_reformat_date();
611 # get for each account initial balance, debits and credits
612 RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
615 $form->{rowcount} = scalar @{ $form->{TB} || [] };
616 $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
619 "accno", "description",
620 "last_transaction", "soll_eb",
623 "soll_kumuliert", "haben_kumuliert",
624 "soll_saldo", "haben_saldo"
628 my $attachment_basename = $locale->text('trial_balance');
629 my $report = SL::ReportGenerator->new(\%myconfig, $form);
631 my @hidden_variables = qw(fromdate todate year method);
633 my $href = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
636 'accno' => { 'text' => $locale->text('Account'), },
637 'description' => { 'text' => $locale->text('Description'), },
638 'last_transaction' => { 'text' => $locale->text('Last Transaction'), },
639 'soll_eb' => { 'text' => $locale->text('Debit Starting Balance'), },
640 'haben_eb' => { 'text' => $locale->text('Credit Starting Balance'), },
641 'soll' => { 'text' => $locale->text('Debit'), },
642 'haben' => { 'text' => $locale->text('Credit'), },
643 'soll_kumuliert' => { 'text' => $locale->text('Sum Debit'), },
644 'haben_kumuliert' => { 'text' => $locale->text('Sum Credit'), },
645 'soll_saldo' => { 'text' => $locale->text('Saldo Debit'), },
646 'haben_saldo' => { 'text' => $locale->text('Saldo Credit'), }
651 my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
653 map { $column_defs{$_}->{visible} = 1 } @columns;
655 $report->set_columns(%column_defs);
656 $report->set_column_order(@columns);
658 $report->set_export_options('generate_trial_balance', @hidden_variables);
663 $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
665 $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
666 push (@options, $form->{print_date});
668 $form->{company} = $locale->text('Company') . " " . $defaults->company;
669 push (@options, $form->{company});
671 if ($::form->{customer_id}) {
672 my $customer = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id});
673 push @options, $::locale->text('Customer') . ' ' . $customer->displayable_name;
677 $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
679 my @custom_headers = ([
680 { text => $::locale->text('Account'), rowspan => 2, },
681 { text => $::locale->text('Description'), rowspan => 2, },
682 { text => $::locale->text('Last Transaction'), rowspan => 2, },
683 { text => $::locale->text('Starting Balance'), colspan => 2, },
684 { text => $::locale->text('Sum for') . " $form->{template_fromto}", colspan => 2, },
685 { text => $::locale->text('Sum per') . " $form->{template_to}", colspan => 2, },
686 { text => $::locale->text('Saldo per') . " $form->{template_to}", colspan => 2, },
691 { text => $::locale->text('Assets'), },
692 { text => $::locale->text('Equity'), },
693 { text => $::locale->text('Debit'), },
694 { text => $::locale->text('Credit'), },
695 { text => $::locale->text('Debit'), },
696 { text => $::locale->text('Credit'), },
697 { text => $::locale->text('Debit'), },
698 { text => $::locale->text('Credit'), },
701 $report->set_options('output_format' => 'HTML',
702 'top_info_text' => join("\n", @options),
703 'title' => $form->{title},
704 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
705 'html_template' => 'rp/html_report_susa',
706 'pdf_template' => 'rp/html_report_susa',
708 $report->set_custom_headers(@custom_headers);
709 $report->set_options_from_form();
710 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
712 # add sort and escape callback, this one we use for the add sub
713 $form->{callback} = $href .= "&sort=$form->{sort}";
715 # escape callback for href
716 my $callback = $form->escape($href);
718 my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
720 my %totals = map { $_ => 0 } @subtotal_columns;
722 my $edit_url = build_std_url('action=edit', 'type', 'vc');
725 foreach my $accno (@{ $form->{TB} || [] }) {
727 $accno->{soll} = $accno->{debit};
728 $accno->{haben} = $accno->{credit};
729 map { $totals{$_} += $accno->{$_} } @subtotal_columns;
731 map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
732 qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
736 foreach my $column (@columns) {
738 'data' => $accno->{$column},
739 'align' => $column_alignment{$column},
743 $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}));
745 my $row_set = [ $row ];
748 $report->add_data($row_set);
753 $report->add_separator();
755 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
757 $report->generate_with_headers();
759 $main::lxdebug->leave_sub();
763 sub create_subtotal_row {
764 $main::lxdebug->enter_sub();
766 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
768 my $form = $main::form;
769 my %myconfig = %main::myconfig;
770 my $locale = $main::locale;
772 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
774 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
776 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
778 map { $totals->{$_} = 0 } @{ $subtotal_columns };
780 $main::lxdebug->leave_sub();
785 sub create_list_accounts_subtotal_row {
786 $main::lxdebug->enter_sub();
788 my ($subtotals, $columns, $fields, $class) = @_;
790 my $form = $main::form;
791 my %myconfig = %main::myconfig;
792 my $locale = $main::locale;
794 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
796 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
798 $main::lxdebug->leave_sub();
804 $main::lxdebug->enter_sub();
808 my $form = $main::form;
809 my %myconfig = %main::myconfig;
810 my $locale = $main::locale;
813 if ($form->{department}) {
814 my ($department) = split /--/, $form->{department};
815 push @options, $locale->text('Department') . " : $department";
817 if ($form->{projectnumber}) {
818 push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
821 # if there are any dates
822 if ($form->{fromdate} || $form->{todate}) {
823 my ($fromdate, $todate);
825 if ($form->{fromdate}) {
826 $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
828 if ($form->{todate}) {
829 $todate = $locale->date(\%myconfig, $form->{todate}, 1);
832 push @options, "$fromdate - $todate";
835 push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
838 my @columns = qw(accno description begbalance debit credit endbalance);
840 'accno' => { 'text' => $locale->text('Account'), },
841 'description' => { 'text' => $locale->text('Description'), },
842 'debit' => { 'text' => $locale->text('Debit'), },
843 'credit' => { 'text' => $locale->text('Credit'), },
844 'begbalance' => { 'text' => $locale->text('Balance'), },
845 'endbalance' => { 'text' => $locale->text('Balance'), },
847 my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
849 my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
851 $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
853 my $report = SL::ReportGenerator->new(\%myconfig, $form);
855 $report->set_options('top_info_text' => join("\n", @options),
856 'output_format' => 'HTML',
857 'title' => $form->{title},
858 'attachment_basename' => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
859 'std_column_visibility' => 1,
861 $report->set_options_from_form();
862 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
864 $report->set_columns(%column_defs);
865 $report->set_column_order(@columns);
867 $report->set_export_options($action, @hidden_variables);
869 my @totals_columns = qw(credit debit begbalance endbalance);
870 my %subtotals = map { $_ => 0 } @totals_columns;
871 my %totals = map { $_ => 0 } @totals_columns;
872 my $found_heading = 0;
873 my @tb = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
875 # sort the whole thing by account numbers and display
876 foreach my $idx (0 .. scalar(@tb) - 1) {
878 my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
880 my $ml = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
882 my $row = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
884 if ($ref->{charttype} eq 'H') {
885 next unless ($form->{l_heading});
887 %subtotals = map { $_ => 0 } @totals_columns;
889 $row->{description}->{class} = 'listheading';
890 $row->{description}->{data} = $ref->{description};
892 $report->add_data($row);
897 foreach (qw(debit credit)) {
898 $subtotals{$_} += $ref->{$_};
899 $totals{$_} += $ref->{$_};
902 $subtotals{begbalance} += $ref->{balance} * $ml;
903 $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
905 map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
906 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
908 $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
909 $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
911 $report->add_data($row);
913 if ($form->{l_heading} && $found_heading &&
914 (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
915 $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
919 $report->add_separator();
921 $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
923 $report->generate_with_headers();
925 $main::lxdebug->leave_sub();
928 sub generate_ar_aging {
929 $main::lxdebug->enter_sub();
931 $main::auth->assert('general_ledger | ar_transactions');
933 my $form = $main::form;
934 my %myconfig = %main::myconfig;
935 my $locale = $main::locale;
938 ($form->{customer}) = split(/--/, $form->{customer});
940 $form->{ct} = "customer";
941 $form->{arap} = "ar";
943 $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
945 RP->aging(\%myconfig, \%$form);
948 $main::lxdebug->leave_sub();
951 sub generate_ap_aging {
952 $main::lxdebug->enter_sub();
954 $main::auth->assert('general_ledger | ap_transactions');
956 my $form = $main::form;
957 my %myconfig = %main::myconfig;
958 my $locale = $main::locale;
961 ($form->{vendor}) = split(/--/, $form->{vendor});
963 $form->{ct} = "vendor";
964 $form->{arap} = "ap";
966 $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
968 RP->aging(\%myconfig, \%$form);
971 $main::lxdebug->leave_sub();
974 sub create_aging_subtotal_row {
975 $main::lxdebug->enter_sub();
977 my ($subtotals, $columns, $periods, $class) = @_;
979 my $form = $main::form;
980 my %myconfig = %main::myconfig;
981 my $locale = $main::locale;
983 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
985 foreach (@{ $periods }) {
986 $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
987 $subtotals->{$_} = 0;
990 $main::lxdebug->leave_sub();
996 $main::lxdebug->enter_sub();
998 $main::auth->assert('general_ledger');
1000 my $form = $main::form;
1001 my %myconfig = %main::myconfig;
1002 my $locale = $main::locale;
1003 my $cgi = $::request->{cgi};
1005 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1007 my @columns = qw(statement ct invnumber transdate duedate amount open);
1010 'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
1011 'ct' => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1012 'invnumber' => { 'text' => $locale->text('Invoice'), },
1013 'transdate' => { 'text' => $locale->text('Date'), },
1014 'duedate' => { 'text' => $locale->text('Due'), },
1015 'amount' => { 'text' => $locale->text('Amount'), },
1016 'open' => { 'text' => $locale->text('Open'), },
1019 my %column_alignment = ('statement' => 'center',
1020 map { $_ => 'right' } qw(open amount));
1022 $report->set_options('std_column_visibility' => 1);
1023 $report->set_columns(%column_defs);
1024 $report->set_column_order(@columns);
1026 my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype department);
1027 $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
1030 my $attachment_basename;
1032 if ($form->{department}) {
1033 my ($department) = split /--/, $form->{department};
1034 push @options, $locale->text('Department') . " : $department";
1035 $form->{callback} .= "&department=" . E($department);
1038 if (($form->{arap} eq 'ar') && $form->{customer}) {
1039 push @options, $form->{customer};
1040 $attachment_basename = $locale->text('ar_aging_list');
1041 $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
1044 if (($form->{arap} eq 'ap') && $form->{vendor}) {
1045 push @options, $form->{vendor};
1046 $attachment_basename = $locale->text('ap_aging_list');
1047 $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
1050 if ($form->{fromdate}) {
1051 push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1053 push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1056 $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1058 $report->set_options('top_info_text' => join("\n", @options),
1059 'output_format' => 'HTML',
1060 'title' => $form->{title},
1061 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1063 $report->set_options_from_form();
1064 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1066 my $previous_ctid = 0;
1068 my @periods = qw(open amount);
1069 my %subtotals = map { $_ => 0 } @periods;
1070 my %totals = map { $_ => 0 } @periods;
1072 foreach my $ref (@{ $form->{AG} }) {
1073 if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1074 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1077 foreach my $key (@periods) {
1078 $subtotals{$key} += $ref->{"$key"};
1079 $totals{$key} += $ref->{"$key"};
1080 $ref->{"$key"} = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
1085 foreach my $column (@columns) {
1087 'data' => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1088 'align' => $column_alignment{$column},
1089 'valign' => $column eq 'statement' ? 'center' : '',
1093 $row->{invnumber}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1095 if ($previous_ctid != $ref->{ctid}) {
1096 $row->{statement}->{raw_data} =
1097 $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
1098 . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1099 $row->{ct}->{data} = $ref->{name};
1104 $previous_ctid = $ref->{ctid};
1106 $report->add_data($row);
1109 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1111 $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1113 if ($form->{arap} eq 'ar') {
1114 my $raw_top_info_text = $form->parse_html_template('rp/aging_ar_top');
1115 my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1116 'PRINT_OPTIONS' => print_options(inline => 1), });
1117 $report->set_options('raw_top_info_text' => $raw_top_info_text,
1118 'raw_bottom_info_text' => $raw_bottom_info_text);
1121 $report->generate_with_headers();
1123 $main::lxdebug->leave_sub();
1127 $main::lxdebug->enter_sub();
1129 my $form = $main::form;
1130 my %myconfig = %main::myconfig;
1131 my $locale = $main::locale;
1133 RP->aging(\%myconfig, \%$form);
1135 map { $_->{checked} = "checked" } @{ $form->{AG} };
1139 $main::lxdebug->leave_sub();
1143 $::lxdebug->enter_sub;
1144 $::auth->assert('general_ledger');
1146 # get name and email addresses
1148 for my $i (1 .. $::form->{rowcount}) {
1149 next unless $::form->{"statement_$i"};
1150 $::form->{"$::form->{ct}_id"} = $::form->{"$::form->{ct}_id_$i"};
1151 RP->get_customer(\%::myconfig, $::form);
1156 $::form->error($::locale->text('Nothing selected!')) unless $selected;
1158 $::form->{media} = "email";
1160 # save all other variables
1162 for my $key (keys %$::form) {
1163 next if any { $key eq $_ } qw(login password action email cc bcc subject message type sendmode format header);
1164 next unless '' eq ref $::form->{$key};
1165 push @hidden_values, $key;
1169 print $::form->parse_html_template('rp/e_mail', {
1170 print_options => print_options(inline => 1),
1171 hidden_values => \@hidden_values,
1174 $::lxdebug->leave_sub;
1178 $main::lxdebug->enter_sub();
1180 $main::auth->assert('general_ledger');
1182 my $form = $main::form;
1183 my %myconfig = %main::myconfig;
1184 my $locale = $main::locale;
1186 $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1187 unless $form->{subject};
1189 RP->aging(\%myconfig, \%$form);
1191 $form->{"statement_1"} = 1;
1193 $form->{media} = 'email';
1196 $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1198 $main::lxdebug->leave_sub();
1202 $main::lxdebug->enter_sub();
1204 $main::auth->assert('general_ledger');
1206 my $form = $main::form;
1207 my %myconfig = %main::myconfig;
1208 my $locale = $main::locale;
1210 if ($form->{media} eq 'printer') {
1211 $form->error($locale->text('Select postscript or PDF!'))
1212 if ($form->{format} !~ /(postscript|pdf)/);
1216 for my $i (1 .. $form->{rowcount}) {
1217 if ($form->{"statement_$i"}) {
1218 $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1224 $form->error($locale->text('Nothing selected!')) unless $selected;
1226 if ($form->{media} eq 'printer') {
1227 $form->{"$form->{ct}_id"} = "";
1229 $form->{"statement_1"} = 1;
1232 RP->aging(\%myconfig, \%$form);
1236 $form->redirect($locale->text('Statements sent to printer!'))
1237 if ($form->{media} eq 'printer');
1239 $main::lxdebug->leave_sub();
1243 $main::lxdebug->enter_sub();
1245 $main::auth->assert('general_ledger');
1247 my $form = $main::form;
1248 my %myconfig = %main::myconfig;
1249 my $locale = $main::locale;
1251 my $defaults = SL::DB::Default->get;
1252 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
1253 $form->{templates} = $defaults->templates;
1255 $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1257 my $suffix = "html";
1258 my $attachment_suffix = "html";
1259 if ($form->{format} eq 'postscript') {
1260 $form->{postscript} = 1;
1262 $attachment_suffix = "ps";
1263 } elsif ($form->{format} eq 'pdf') {
1266 $attachment_suffix = "pdf";
1269 $form->{IN} = "$form->{type}.$suffix";
1270 $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1272 # Save $form->{email} because it will be overwritten.
1273 $form->{EMAIL_RECIPIENT} = $form->{email};
1277 while (@{ $form->{AG} }) {
1279 my $ref = shift @{ $form->{AG} };
1281 if ($ctid != $ref->{ctid}) {
1283 $ctid = $ref->{ctid};
1286 if ($form->{"statement_$i"}) {
1289 ("name", "street", "zipcode", "city", "country", "contact", "email",
1290 "$form->{ct}phone", "$form->{ct}fax");
1291 map { $form->{$_} = $ref->{$_} } @a;
1293 $form->{ $form->{ct} } = $form->{name};
1294 $form->{"$form->{ct}_id"} = $ref->{ctid};
1296 map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
1298 foreach my $item (qw(c0 c30 c60 c90)) {
1299 $form->{$item} = ();
1300 $form->{"${item}total"} = 0;
1303 &statement_details($ref);
1307 if (scalar(@{ $form->{AG} }) > 0) {
1309 # one or more left to go
1310 if ($ctid == $form->{AG}->[0]->{ctid}) {
1311 $ref = shift @{ $form->{AG} };
1312 &statement_details($ref);
1315 $ref = scalar(@{ $form->{AG} });
1321 # set initial ref to 0
1328 $form->{"${_}total"} =
1329 $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1330 } ('c0', 'c30', 'c60', 'c90', "");
1332 $form->{attachment_filename} = $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
1333 $form->{attachment_filename} =~ s/\s+/_/g;
1335 $form->parse_template(\%myconfig);
1340 # saving the history
1341 if(!exists $form->{addition} && $form->{id} ne "") {
1342 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1343 $form->{addition} = "PRINTED";
1344 $form->{what_done} = $form->{type};
1345 $form->save_history;
1347 # /saving the history
1348 $main::lxdebug->leave_sub();
1351 sub statement_details {
1352 $main::lxdebug->enter_sub();
1354 $main::auth->assert('general_ledger');
1356 my $form = $main::form;
1357 my %myconfig = %main::myconfig;
1358 my $locale = $main::locale;
1362 push @{ $form->{invnumber} }, $ref->{invnumber};
1363 push @{ $form->{invdate} }, $ref->{transdate};
1364 push @{ $form->{duedate} }, $ref->{duedate};
1365 push @{ $form->{amount} }, $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
1366 push @{ $form->{open} }, $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
1368 foreach my $item (qw(c0 c30 c60 c90)) {
1369 if ($ref->{exchangerate} * 1) {
1370 # add only the open amount of the invoice to the aging, not the total amount
1371 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
1372 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
1373 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
1374 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
1376 $form->{"${item}total"} += $ref->{$item};
1377 $form->{total} += $ref->{$item};
1378 push @{ $form->{$item} },
1379 $form->format_amount(\%myconfig, $ref->{$item}, 2);
1382 $main::lxdebug->leave_sub();
1385 sub generate_tax_report {
1386 $::lxdebug->enter_sub;
1387 $::auth->assert('report');
1389 RP->tax_report(\%::myconfig, $::form);
1391 my $descvar = "$::form->{accno}_description";
1392 my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
1396 my $callback = build_std_url('action=generate_tax_report', $descvar,
1397 qw(fromdate todate db method accno department report title));
1399 my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1402 for my $item (@columns, 'subtotal') {
1403 if ($::form->{"l_$item"} eq "Y") {
1404 $callback .= "&l_$item=Y";
1405 $href .= "&l_$item=Y";
1409 for my $item (@columns) {
1410 if ($::form->{"l_$item"} eq "Y") {
1411 push @column_index, $item;
1416 if ($::form->{department}) {
1417 my ($department) = split /--/, $::form->{department};
1418 push @options, $::locale->text('Department') . " : $department";
1421 # if there are any dates
1422 if ($::form->{fromdate} || $::form->{todate}) {
1423 my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
1424 my $todate = $::form->{todate} ? $::locale->date(\%::myconfig, $::form->{todate}, 1) : '';
1425 push @options, "$fromdate - $todate";
1427 push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
1430 my ($name, $invoice, $arap);
1431 if ($::form->{db} eq 'ar') {
1432 $name = $::locale->text('Customer');
1436 if ($::form->{db} eq 'ap') {
1437 $name = $::locale->text('Vendor');
1442 my %column_header = (
1443 id => $::locale->text('ID'),
1444 invnumber => $::locale->text('Invoice'),
1445 transdate => $::locale->text('Date'),
1446 netamount => $::locale->text('Amount'),
1447 tax => $::locale->text('Tax'),
1448 amount => $::locale->text('Total'),
1452 my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
1454 $callback .= "&sort=$::form->{sort}";
1457 if (@{ $::form->{TR} }) {
1458 $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
1461 my ($totalnetamount, $totaltax, @data);
1462 for my $ref (@{ $::form->{TR} }) {
1464 my $module = ($ref->{invoice}) ? $invoice : $arap;
1466 if ($::form->{l_subtotal} eq 'Y') {
1467 if ($sameitem ne $ref->{ $::form->{sort} }) {
1470 netamount => $subtotalnetamount,
1471 tax => $subtotaltax,
1472 amount => $subtotal,
1474 $subtotalnetamount = 0;
1476 $sameitem = $ref->{ $::form->{sort} };
1480 $subtotalnetamount += $ref->{netamount};
1481 $subtotaltax += $ref->{tax};
1482 $totalnetamount += $ref->{netamount};
1483 $totaltax += $ref->{tax};
1484 $ref->{amount} = $ref->{netamount} + $ref->{tax};
1486 push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
1487 $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
1488 $data[-1]{$_}{numeric} = 1 for qw(netamount tax amount);
1491 if ($::form->{l_subtotal} eq 'Y') {
1494 netamount => $subtotalnetamount,
1495 tax => $subtotaltax,
1496 amount => $subtotal,
1502 netamount => $totalnetamount,
1504 amount => $totalnetamount + $totaltax,
1508 print $::form->parse_html_template('rp/tax_report', {
1509 column_index => \@column_index,
1510 column_header => \%column_header,
1511 column_sorted => \%column_sorted,
1514 options => \@options,
1517 $::lxdebug->leave_sub;
1521 $main::lxdebug->enter_sub();
1523 $main::auth->assert('cash');
1525 my $form = $main::form;
1526 my %myconfig = %main::myconfig;
1527 my $locale = $main::locale;
1529 if ($form->{account}) {
1530 ($form->{paymentaccounts}) = split /--/, $form->{account};
1534 if ($form->{department}) {
1535 (my $department, $form->{department_id}) = split /--/, $form->{department};
1536 $option = $locale->text('Department') . " : $department";
1539 report_generator_set_default_sort('transdate', 1);
1541 RP->payments(\%myconfig, \%$form);
1543 my @hidden_variables = qw(account title department reference source memo fromdate todate
1544 fx_transaction db prepayment paymentaccounts sort);
1546 my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
1547 $form->{callback} = $href;
1549 my @columns = qw(transdate invnumber name paid source memo);
1551 'name' => { 'text' => $locale->text('Description'), },
1552 'invnumber' => { 'text' => $locale->text('Reference'), },
1553 'transdate' => { 'text' => $locale->text('Date'), },
1554 'paid' => { 'text' => $locale->text('Amount'), },
1555 'source' => { 'text' => $locale->text('Source'), },
1556 'memo' => { 'text' => $locale->text('Memo'), },
1558 my %column_alignment = ('paid' => 'right');
1560 foreach my $name (grep { $_ ne 'paid' } @columns) {
1561 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1562 $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
1566 if ($form->{fromdate}) {
1567 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
1569 if ($form->{todate}) {
1570 push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1573 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1575 my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
1577 $report->set_options('top_info_text' => join("\n", @options),
1578 'output_format' => 'HTML',
1579 'title' => $form->{title},
1580 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1581 'std_column_visibility' => 1,
1583 $report->set_options_from_form();
1585 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1587 $report->set_columns(%column_defs);
1588 $report->set_column_order(@columns);
1590 $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
1592 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1596 foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1597 next unless @{ $form->{ $ref->{id} } };
1599 $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
1601 my $subtotal_paid = 0;
1603 foreach my $payment (@{ $form->{ $ref->{id} } }) {
1604 my $module = $payment->{module};
1605 $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1606 $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1608 $subtotal_paid += $payment->{paid};
1609 $total_paid += $payment->{paid};
1611 $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
1615 foreach my $column (@columns) {
1617 'data' => $payment->{$column},
1618 'align' => $column_alignment{$column},
1622 $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
1624 $report->add_data($row);
1627 my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
1629 'data' => $form->format_amount(\%myconfig, $subtotal_paid, 2),
1631 'class' => 'listsubtotal',
1634 $report->add_data($row);
1637 $report->add_separator();
1639 my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
1641 'data' => $form->format_amount(\%myconfig, $total_paid, 2),
1643 'class' => 'listtotal',
1646 $report->add_data($row);
1648 $report->generate_with_headers();
1650 $main::lxdebug->leave_sub();
1654 $::lxdebug->enter_sub;
1656 my ($dont_print) = @_;
1658 $::form->{sendmode} = "attachment";
1659 $::form->{format} ||= $::myconfig{template_format} || "pdf";
1660 $::form->{copies} ||= $::myconfig{copies} || 2;
1662 $::form->{PD}{ $::form->{type} } = "selected";
1663 $::form->{DF}{ $::form->{format} } = "selected";
1664 $::form->{OP}{ $::form->{media} } = "selected";
1665 $::form->{SM}{ $::form->{sendmode} } = "selected";
1667 my $output = $::form->parse_html_template('rp/print_options', {
1668 is_email => $::form->{media} eq 'email',
1671 print $output unless $dont_print;
1673 $::lxdebug->leave_sub;
1679 $main::lxdebug->enter_sub();
1681 $main::auth->assert('report');
1683 my $form = $main::form;
1684 my %myconfig = %main::myconfig;
1685 my $locale = $main::locale;
1687 $form->{padding} = " ";
1688 $form->{bold} = "<b>";
1689 $form->{endbold} = "</b>";
1690 $form->{br} = "<br>";
1692 if ($form->{reporttype} eq "custom") {
1694 #forgotten the year --> thisyear
1695 if ($form->{year} !~ m/^\d\d\d\d$/) {
1696 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1702 if ($form->{duetyp} eq "13") {
1703 $form->{fromdate} = "1.1.$form->{year}";
1704 $form->{todate} = "31.12.$form->{year}";
1705 $form->{comparefromdate} = "1.01.$form->{year}";
1706 $form->{comparetodate} = "31.12.$form->{year}";
1710 if ($form->{duetyp} eq "A") {
1711 $form->{fromdate} = "1.1.$form->{year}";
1712 $form->{todate} = "31.3.$form->{year}";
1713 $form->{comparefromdate} = "1.01.$form->{year}";
1714 $form->{comparetodate} = "31.03.$form->{year}";
1716 if ($form->{duetyp} eq "B") {
1717 $form->{fromdate} = "1.4.$form->{year}";
1718 $form->{todate} = "30.6.$form->{year}";
1719 $form->{comparefromdate} = "1.01.$form->{year}";
1720 $form->{comparetodate} = "30.06.$form->{year}";
1722 if ($form->{duetyp} eq "C") {
1723 $form->{fromdate} = "1.7.$form->{year}";
1724 $form->{todate} = "30.9.$form->{year}";
1725 $form->{comparefromdate} = "1.01.$form->{year}";
1726 $form->{comparetodate} = "30.09.$form->{year}";
1728 if ($form->{duetyp} eq "D") {
1729 $form->{fromdate} = "1.10.$form->{year}";
1730 $form->{todate} = "31.12.$form->{year}";
1731 $form->{comparefromdate} = "1.01.$form->{year}";
1732 $form->{comparetodate} = "31.12.$form->{year}";
1737 $form->{duetyp} eq "1" && do {
1738 $form->{fromdate} = "1.1.$form->{year}";
1739 $form->{todate} = "31.1.$form->{year}";
1740 $form->{comparefromdate} = "1.01.$form->{year}";
1741 $form->{comparetodate} = "31.01.$form->{year}";
1744 $form->{duetyp} eq "2" && do {
1745 $form->{fromdate} = "1.2.$form->{year}";
1747 #this works from 1901 to 2099, 1900 and 2100 fail.
1748 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1749 $form->{todate} = "$leap.2.$form->{year}";
1750 $form->{comparefromdate} = "1.01.$form->{year}";
1751 $form->{comparetodate} = "$leap.02.$form->{year}";
1754 $form->{duetyp} eq "3" && do {
1755 $form->{fromdate} = "1.3.$form->{year}";
1756 $form->{todate} = "31.3.$form->{year}";
1757 $form->{comparefromdate} = "1.01.$form->{year}";
1758 $form->{comparetodate} = "31.03.$form->{year}";
1761 $form->{duetyp} eq "4" && do {
1762 $form->{fromdate} = "1.4.$form->{year}";
1763 $form->{todate} = "30.4.$form->{year}";
1764 $form->{comparefromdate} = "1.01.$form->{year}";
1765 $form->{comparetodate} = "30.04.$form->{year}";
1768 $form->{duetyp} eq "5" && do {
1769 $form->{fromdate} = "1.5.$form->{year}";
1770 $form->{todate} = "31.5.$form->{year}";
1771 $form->{comparefromdate} = "1.01.$form->{year}";
1772 $form->{comparetodate} = "31.05.$form->{year}";
1775 $form->{duetyp} eq "6" && do {
1776 $form->{fromdate} = "1.6.$form->{year}";
1777 $form->{todate} = "30.6.$form->{year}";
1778 $form->{comparefromdate} = "1.01.$form->{year}";
1779 $form->{comparetodate} = "30.06.$form->{year}";
1782 $form->{duetyp} eq "7" && do {
1783 $form->{fromdate} = "1.7.$form->{year}";
1784 $form->{todate} = "31.7.$form->{year}";
1785 $form->{comparefromdate} = "1.01.$form->{year}";
1786 $form->{comparetodate} = "31.07.$form->{year}";
1789 $form->{duetyp} eq "8" && do {
1790 $form->{fromdate} = "1.8.$form->{year}";
1791 $form->{todate} = "31.8.$form->{year}";
1792 $form->{comparefromdate} = "1.01.$form->{year}";
1793 $form->{comparetodate} = "31.08.$form->{year}";
1796 $form->{duetyp} eq "9" && do {
1797 $form->{fromdate} = "1.9.$form->{year}";
1798 $form->{todate} = "30.9.$form->{year}";
1799 $form->{comparefromdate} = "1.01.$form->{year}";
1800 $form->{comparetodate} = "30.09.$form->{year}";
1803 $form->{duetyp} eq "10" && do {
1804 $form->{fromdate} = "1.10.$form->{year}";
1805 $form->{todate} = "31.10.$form->{year}";
1806 $form->{comparefromdate} = "1.01.$form->{year}";
1807 $form->{comparetodate} = "31.10.$form->{year}";
1810 $form->{duetyp} eq "11" && do {
1811 $form->{fromdate} = "1.11.$form->{year}";
1812 $form->{todate} = "30.11.$form->{year}";
1813 $form->{comparefromdate} = "1.01.$form->{year}";
1814 $form->{comparetodate} = "30.11.$form->{year}";
1817 $form->{duetyp} eq "12" && do {
1818 $form->{fromdate} = "1.12.$form->{year}";
1819 $form->{todate} = "31.12.$form->{year}";
1820 $form->{comparefromdate} = "1.01.$form->{year}";
1821 $form->{comparetodate} = "31.12.$form->{year}";
1825 hotfix_reformat_date();
1827 # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
1828 # ansonsten ist die prüfung in RP.pm
1829 # if (defined ($form->{fromdate|todate}=='..'))
1831 if ($form->{fromdate}){
1832 my $datetime = $locale->parse_date_to_object($form->{fromdate});
1833 $datetime->set( month => 1,
1835 $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
1837 if ($form->{todate}){
1838 $form->{comparetodate} = $form->{todate};
1842 RP->bwa(\%myconfig, \%$form);
1844 ($form->{department}) = split /--/, $form->{department};
1847 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1848 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1850 # if there are any dates construct a where
1851 if ($form->{fromdate} || $form->{todate}) {
1853 unless ($form->{todate}) {
1854 $form->{todate} = $form->current_date(\%myconfig);
1857 my %germandate = ("dateformat" => "dd.mm.yyyy");
1859 my $longtodate = $locale->date(\%germandate, $form->{todate}, 1);
1860 my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
1862 my $longfromdate = $locale->date(\%germandate, $form->{fromdate}, 1);
1863 my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
1865 $form->{this_period} = "$shortfromdate\n$shorttodate";
1867 $locale->text('for Period')
1868 . qq|\n$longfromdate |
1869 . $locale->text('bis')
1873 $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
1875 if ( $form->{method} eq 'cash' ) {
1876 $form->{accounting_method} = $locale->text('Cash accounting');
1877 } elsif ( $form->{method} eq 'accrual' ) {
1878 $form->{accounting_method} = $locale->text('Accrual accounting');
1880 $form->{accounting_method} = "";
1883 $form->{title} = $locale->text('BWA');
1885 $::request->layout->add_stylesheets('bwa.css');
1887 print $form->parse_html_template('rp/bwa');
1889 $main::lxdebug->leave_sub();
1892 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
1893 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
1894 sub hotfix_reformat_date {
1896 $main::lxdebug->enter_sub();
1898 my $form = $main::form;
1899 my %myconfig = %main::myconfig;
1900 my $locale = $main::locale;
1902 if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
1903 my $current_dateformat = $myconfig{dateformat};
1904 $myconfig{dateformat} = 'dd.mm.yyyy';
1905 $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
1906 $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
1907 $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
1908 unless (!defined ($form->{comparefromdate}));
1909 $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
1910 unless (!defined ($form->{comparetodate}));
1912 # Und wieder zurücksetzen
1913 $myconfig{dateformat} = $current_dateformat; #'dd.mm.yyyy';
1914 } # Ende Hotifx Bug 1388
1916 $main::lxdebug->leave_sub();