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::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}";
177 $vc = qq|<input name=$::form->{vc} size=35 class="initial_focus">|;
180 my ($selection, $paymentaccounts);
182 $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
184 RP->paymentaccounts(\%::myconfig, $::form);
186 $selection = "<option>\n";
187 for my $ref (@{ $::form->{PR} }) {
188 $paymentaccounts .= "$ref->{accno} ";
189 $selection .= "<option>$ref->{accno}--$ref->{description}\n";
194 print $::form->parse_html_template('rp/report', {
195 paymentaccounts => $paymentaccounts,
196 selection => $selection,
197 is_aging => $is_aging,
200 year => DateTime->today->year,
201 today => DateTime->today,
203 is_payments => $is_payments,
204 is_trial_balance => $is_trial_balance,
205 is_balance_sheet => $is_balance_sheet,
207 is_income_statement => $is_income_statement,
208 is_erfolgsrechnung => $is_erfolgsrechnung,
209 is_projects => $is_projects,
212 $::lxdebug->leave_sub;
215 sub continue { call_sub($main::form->{"nextsub"}); }
217 sub generate_income_statement {
218 $main::lxdebug->enter_sub();
220 $main::auth->assert('report');
222 my $form = $main::form;
223 my %myconfig = %main::myconfig;
224 my $locale = $main::locale;
226 $form->{padding} = " ";
227 $form->{bold} = "<b>";
228 $form->{endbold} = "</b>";
229 $form->{br} = "<br>";
231 if ($form->{reporttype} eq "custom") {
233 #forgotten the year --> thisyear
234 if ($form->{year} !~ m/^\d\d\d\d$/) {
235 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
241 if ($form->{duetyp} eq "13") {
242 $form->{fromdate} = "1.1.$form->{year}";
243 $form->{todate} = "31.12.$form->{year}";
247 if ($form->{duetyp} eq "A") {
248 $form->{fromdate} = "1.1.$form->{year}";
249 $form->{todate} = "31.3.$form->{year}";
251 if ($form->{duetyp} eq "B") {
252 $form->{fromdate} = "1.4.$form->{year}";
253 $form->{todate} = "30.6.$form->{year}";
255 if ($form->{duetyp} eq "C") {
256 $form->{fromdate} = "1.7.$form->{year}";
257 $form->{todate} = "30.9.$form->{year}";
259 if ($form->{duetyp} eq "D") {
260 $form->{fromdate} = "1.10.$form->{year}";
261 $form->{todate} = "31.12.$form->{year}";
266 $form->{duetyp} eq "1" && do {
267 $form->{fromdate} = "1.1.$form->{year}";
268 $form->{todate} = "31.1.$form->{year}";
271 $form->{duetyp} eq "2" && do {
272 $form->{fromdate} = "1.2.$form->{year}";
274 #this works from 1901 to 2099, 1900 and 2100 fail.
275 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
276 $form->{todate} = "$leap.2.$form->{year}";
279 $form->{duetyp} eq "3" && do {
280 $form->{fromdate} = "1.3.$form->{year}";
281 $form->{todate} = "31.3.$form->{year}";
284 $form->{duetyp} eq "4" && do {
285 $form->{fromdate} = "1.4.$form->{year}";
286 $form->{todate} = "30.4.$form->{year}";
289 $form->{duetyp} eq "5" && do {
290 $form->{fromdate} = "1.5.$form->{year}";
291 $form->{todate} = "31.5.$form->{year}";
294 $form->{duetyp} eq "6" && do {
295 $form->{fromdate} = "1.6.$form->{year}";
296 $form->{todate} = "30.6.$form->{year}";
299 $form->{duetyp} eq "7" && do {
300 $form->{fromdate} = "1.7.$form->{year}";
301 $form->{todate} = "31.7.$form->{year}";
304 $form->{duetyp} eq "8" && do {
305 $form->{fromdate} = "1.8.$form->{year}";
306 $form->{todate} = "31.8.$form->{year}";
309 $form->{duetyp} eq "9" && do {
310 $form->{fromdate} = "1.9.$form->{year}";
311 $form->{todate} = "30.9.$form->{year}";
314 $form->{duetyp} eq "10" && do {
315 $form->{fromdate} = "1.10.$form->{year}";
316 $form->{todate} = "31.10.$form->{year}";
319 $form->{duetyp} eq "11" && do {
320 $form->{fromdate} = "1.11.$form->{year}";
321 $form->{todate} = "30.11.$form->{year}";
324 $form->{duetyp} eq "12" && do {
325 $form->{fromdate} = "1.12.$form->{year}";
326 $form->{todate} = "31.12.$form->{year}";
330 hotfix_reformat_date();
331 } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
333 RP->income_statement(\%myconfig, \%$form);
335 ($form->{department}) = split /--/, $form->{department};
338 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
339 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
341 # if there are any dates construct a where
342 if ($form->{fromdate} || $form->{todate}) {
344 unless ($form->{todate}) {
345 $form->{todate} = $form->current_date(\%myconfig);
348 my $longtodate = $locale->date(\%myconfig, $form->{todate}, 1);
349 my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
351 my $longfromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
352 my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
354 $form->{this_period} = "$shortfromdate\n$shorttodate";
356 $locale->text('for Period')
357 . qq|\n$longfromdate |
358 . $locale->text('Bis')
362 if ($form->{comparefromdate} || $form->{comparetodate}) {
363 my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
364 my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
366 my $longcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 1);
367 my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
369 $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
371 "\n$longcomparefromdate "
372 . $locale->text('Bis')
373 . qq| $longcomparetodate|;
376 if ( $::instance_conf->get_profit_determination eq 'balance' ) {
377 $form->{title} = $locale->text('Income Statement');
378 } elsif ( $::instance_conf->get_profit_determination eq 'income' ) {
379 $form->{title} = $locale->text('Net Income Statement');
384 if ( $form->{method} eq 'cash' ) {
385 $form->{accounting_method} = $locale->text('Cash accounting');
386 } elsif ( $form->{method} eq 'accrual' ) {
387 $form->{accounting_method} = $locale->text('Accrual accounting');
389 $form->{accounting_method} = "";
392 $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
395 print $form->parse_html_template('rp/income_statement');
397 $main::lxdebug->leave_sub();
400 sub generate_erfolgsrechnung {
401 $::lxdebug->enter_sub;
402 $::auth->assert('report');
404 $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
405 $::form->{padding} = " ";
406 $::form->{bold} = "<b>";
407 $::form->{endbold} = "</b>";
408 $::form->{br} = "<br>";
410 my $data = RP->erfolgsrechnung(\%::myconfig, $::form);
413 print $::form->parse_html_template('rp/erfolgsrechnung', $data);
415 $::lxdebug->leave_sub;
419 sub generate_balance_sheet {
420 $::lxdebug->enter_sub;
421 $::auth->assert('report');
423 $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
424 $::form->{padding} = " ";
425 $::form->{bold} = "<b>";
426 $::form->{endbold} = "</b>";
427 $::form->{br} = "<br>";
429 my $data = RP->balance_sheet(\%::myconfig, $::form);
431 $::form->{asofdate} ||= $::form->current_date;
432 $::form->{report_title} = $::locale->text('Balance Sheet');
433 $::form->{report_date} ||= $::form->current_date;
435 ($::form->{department}) = split /--/, $::form->{department};
437 # define Current Earnings account
438 my $padding = $::form->{l_heading} ? $::form->{padding} : "";
439 push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
441 $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
442 $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
444 # balance sheet isn't read from print templates anymore,
445 # instead use template in rp
446 # $::form->{IN} = "balance_sheet.html";
449 print $::form->parse_html_template('rp/balance_sheet', $data);
451 $::lxdebug->leave_sub;
454 sub generate_projects {
455 $main::lxdebug->enter_sub();
457 $main::auth->assert('report');
459 my $form = $main::form;
460 my %myconfig = %main::myconfig;
461 my $locale = $main::locale;
463 my $project = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
464 $form->{projectnumber} = $project ? $project->projectnumber : '';
466 # make sure todate and fromdate always have a value, even if the date fields
467 # were left empty or the inputs weren't valid dates/couldn't be parsed
469 $project = SL::DB::Project->new() unless $project; # dummy object for dbh
470 unless ($::locale->parse_date_to_object($::form->{fromdate})) {
471 ($form->{fromdate}) = $project->db->dbh->selectrow_array('select min(transdate) from acc_trans');
474 unless ($::locale->parse_date_to_object($::form->{todate})) {
475 ($form->{todate}) = $project->db->dbh->selectrow_array('select max(transdate) from acc_trans');
478 $form->{nextsub} = "generate_projects";
479 $form->{title} = $locale->text('Project Transactions');
480 RP->trial_balance(\%myconfig, \%$form);
482 list_accounts('generate_projects');
484 $main::lxdebug->leave_sub();
490 # included links to display transactions for period entered
491 # added headers and subtotals
493 sub generate_trial_balance {
494 $main::lxdebug->enter_sub();
496 $main::auth->assert('report');
498 my $form = $main::form;
499 my %myconfig = %main::myconfig;
500 my $locale = $main::locale;
501 my $defaults = SL::DB::Default->get;
503 if ($form->{reporttype} eq "custom") {
505 #forgotten the year --> thisyear
506 if ($form->{year} !~ m/^\d\d\d\d$/) {
507 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
513 if ($form->{duetyp} eq "13") {
514 $form->{fromdate} = "1.1.$form->{year}";
515 $form->{todate} = "31.12.$form->{year}";
519 if ($form->{duetyp} eq "A") {
520 $form->{fromdate} = "1.1.$form->{year}";
521 $form->{todate} = "31.3.$form->{year}";
523 if ($form->{duetyp} eq "B") {
524 $form->{fromdate} = "1.4.$form->{year}";
525 $form->{todate} = "30.6.$form->{year}";
527 if ($form->{duetyp} eq "C") {
528 $form->{fromdate} = "1.7.$form->{year}";
529 $form->{todate} = "30.9.$form->{year}";
531 if ($form->{duetyp} eq "D") {
532 $form->{fromdate} = "1.10.$form->{year}";
533 $form->{todate} = "31.12.$form->{year}";
538 $form->{duetyp} eq "1" && do {
539 $form->{fromdate} = "1.1.$form->{year}";
540 $form->{todate} = "31.1.$form->{year}";
543 $form->{duetyp} eq "2" && do {
544 $form->{fromdate} = "1.2.$form->{year}";
546 #this works from 1901 to 2099, 1900 and 2100 fail.
547 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
548 $form->{todate} = "$leap.2.$form->{year}";
551 $form->{duetyp} eq "3" && do {
552 $form->{fromdate} = "1.3.$form->{year}";
553 $form->{todate} = "31.3.$form->{year}";
556 $form->{duetyp} eq "4" && do {
557 $form->{fromdate} = "1.4.$form->{year}";
558 $form->{todate} = "30.4.$form->{year}";
561 $form->{duetyp} eq "5" && do {
562 $form->{fromdate} = "1.5.$form->{year}";
563 $form->{todate} = "31.5.$form->{year}";
566 $form->{duetyp} eq "6" && do {
567 $form->{fromdate} = "1.6.$form->{year}";
568 $form->{todate} = "30.6.$form->{year}";
571 $form->{duetyp} eq "7" && do {
572 $form->{fromdate} = "1.7.$form->{year}";
573 $form->{todate} = "31.7.$form->{year}";
576 $form->{duetyp} eq "8" && do {
577 $form->{fromdate} = "1.8.$form->{year}";
578 $form->{todate} = "31.8.$form->{year}";
581 $form->{duetyp} eq "9" && do {
582 $form->{fromdate} = "1.9.$form->{year}";
583 $form->{todate} = "30.9.$form->{year}";
586 $form->{duetyp} eq "10" && do {
587 $form->{fromdate} = "1.10.$form->{year}";
588 $form->{todate} = "31.10.$form->{year}";
591 $form->{duetyp} eq "11" && do {
592 $form->{fromdate} = "1.11.$form->{year}";
593 $form->{todate} = "30.11.$form->{year}";
596 $form->{duetyp} eq "12" && do {
597 $form->{fromdate} = "1.12.$form->{year}";
598 $form->{todate} = "31.12.$form->{year}";
602 hotfix_reformat_date();
606 # get for each account initial balance, debits and credits
607 RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
610 $form->{rowcount} = scalar @{ $form->{TB} || [] };
611 $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
614 "accno", "description",
615 "last_transaction", "soll_eb",
618 "soll_kumuliert", "haben_kumuliert",
619 "soll_saldo", "haben_saldo"
623 my $attachment_basename = $locale->text('trial_balance');
624 my $report = SL::ReportGenerator->new(\%myconfig, $form);
626 my @hidden_variables = qw(fromdate todate year method);
628 my $href = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
631 'accno' => { 'text' => $locale->text('Account'), },
632 'description' => { 'text' => $locale->text('Description'), },
633 'last_transaction' => { 'text' => $locale->text('Last Transaction'), },
634 'soll_eb' => { 'text' => $locale->text('Debit Starting Balance'), },
635 'haben_eb' => { 'text' => $locale->text('Credit Starting Balance'), },
636 'soll' => { 'text' => $locale->text('Debit'), },
637 'haben' => { 'text' => $locale->text('Credit'), },
638 'soll_kumuliert' => { 'text' => $locale->text('Sum Debit'), },
639 'haben_kumuliert' => { 'text' => $locale->text('Sum Credit'), },
640 'soll_saldo' => { 'text' => $locale->text('Saldo Debit'), },
641 'haben_saldo' => { 'text' => $locale->text('Saldo Credit'), }
646 my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
648 map { $column_defs{$_}->{visible} = 1 } @columns;
650 $report->set_columns(%column_defs);
651 $report->set_column_order(@columns);
653 $report->set_export_options('generate_trial_balance', @hidden_variables);
658 $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
660 $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
661 push (@options, $form->{print_date});
663 $form->{company} = $locale->text('Company') . " " . $defaults->company;
664 push (@options, $form->{company});
666 if ($::form->{customer_id}) {
667 my $customer = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id});
668 push @options, $::locale->text('Customer') . ' ' . $customer->displayable_name;
672 $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
674 my @custom_headers = ([
675 { text => $::locale->text('Account'), rowspan => 2, },
676 { text => $::locale->text('Description'), rowspan => 2, },
677 { text => $::locale->text('Last Transaction'), rowspan => 2, },
678 { text => $::locale->text('Starting Balance'), colspan => 2, },
679 { text => $::locale->text('Sum for') . " $form->{template_fromto}", colspan => 2, },
680 { text => $::locale->text('Sum per') . " $form->{template_to}", colspan => 2, },
681 { text => $::locale->text('Saldo per') . " $form->{template_to}", colspan => 2, },
686 { text => $::locale->text('Assets'), },
687 { text => $::locale->text('Equity'), },
688 { text => $::locale->text('Debit'), },
689 { text => $::locale->text('Credit'), },
690 { text => $::locale->text('Debit'), },
691 { text => $::locale->text('Credit'), },
692 { text => $::locale->text('Debit'), },
693 { text => $::locale->text('Credit'), },
696 $report->set_options('output_format' => 'HTML',
697 'top_info_text' => join("\n", @options),
698 'title' => $form->{title},
699 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
700 'html_template' => 'rp/html_report_susa',
701 'pdf_template' => 'rp/html_report_susa',
703 $report->set_custom_headers(@custom_headers);
704 $report->set_options_from_form();
705 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
707 # add sort and escape callback, this one we use for the add sub
708 $form->{callback} = $href .= "&sort=$form->{sort}";
710 # escape callback for href
711 my $callback = $form->escape($href);
713 my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
715 my %totals = map { $_ => 0 } @subtotal_columns;
717 my $edit_url = build_std_url('action=edit', 'type', 'vc');
720 foreach my $accno (@{ $form->{TB} || [] }) {
722 $accno->{soll} = $accno->{debit};
723 $accno->{haben} = $accno->{credit};
724 map { $totals{$_} += $accno->{$_} } @subtotal_columns;
726 map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
727 qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
731 foreach my $column (@columns) {
733 'data' => $accno->{$column},
734 'align' => $column_alignment{$column},
738 $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}));
740 my $row_set = [ $row ];
743 $report->add_data($row_set);
748 $report->add_separator();
750 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
752 $report->generate_with_headers();
754 $main::lxdebug->leave_sub();
758 sub create_subtotal_row {
759 $main::lxdebug->enter_sub();
761 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
763 my $form = $main::form;
764 my %myconfig = %main::myconfig;
765 my $locale = $main::locale;
767 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
769 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
771 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
773 map { $totals->{$_} = 0 } @{ $subtotal_columns };
775 $main::lxdebug->leave_sub();
780 sub create_list_accounts_subtotal_row {
781 $main::lxdebug->enter_sub();
783 my ($subtotals, $columns, $fields, $class) = @_;
785 my $form = $main::form;
786 my %myconfig = %main::myconfig;
787 my $locale = $main::locale;
789 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
791 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
793 $main::lxdebug->leave_sub();
799 $main::lxdebug->enter_sub();
803 my $form = $main::form;
804 my %myconfig = %main::myconfig;
805 my $locale = $main::locale;
808 if ($form->{department}) {
809 my ($department) = split /--/, $form->{department};
810 push @options, $locale->text('Department') . " : $department";
812 if ($form->{projectnumber}) {
813 push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
816 # if there are any dates
817 if ($form->{fromdate} || $form->{todate}) {
818 my ($fromdate, $todate);
820 if ($form->{fromdate}) {
821 $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
823 if ($form->{todate}) {
824 $todate = $locale->date(\%myconfig, $form->{todate}, 1);
827 push @options, "$fromdate - $todate";
830 push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
833 my @columns = qw(accno description begbalance debit credit endbalance);
835 'accno' => { 'text' => $locale->text('Account'), },
836 'description' => { 'text' => $locale->text('Description'), },
837 'debit' => { 'text' => $locale->text('Debit'), },
838 'credit' => { 'text' => $locale->text('Credit'), },
839 'begbalance' => { 'text' => $locale->text('Balance'), },
840 'endbalance' => { 'text' => $locale->text('Balance'), },
842 my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
844 my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
846 $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
848 my $report = SL::ReportGenerator->new(\%myconfig, $form);
850 $report->set_options('top_info_text' => join("\n", @options),
851 'output_format' => 'HTML',
852 'title' => $form->{title},
853 'attachment_basename' => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
854 'std_column_visibility' => 1,
856 $report->set_options_from_form();
857 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
859 $report->set_columns(%column_defs);
860 $report->set_column_order(@columns);
862 $report->set_export_options($action, @hidden_variables);
864 my @totals_columns = qw(credit debit begbalance endbalance);
865 my %subtotals = map { $_ => 0 } @totals_columns;
866 my %totals = map { $_ => 0 } @totals_columns;
867 my $found_heading = 0;
868 my @tb = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
870 # sort the whole thing by account numbers and display
871 foreach my $idx (0 .. scalar(@tb) - 1) {
873 my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
875 my $ml = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
877 my $row = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
879 if ($ref->{charttype} eq 'H') {
880 next unless ($form->{l_heading});
882 %subtotals = map { $_ => 0 } @totals_columns;
884 $row->{description}->{class} = 'listheading';
885 $row->{description}->{data} = $ref->{description};
887 $report->add_data($row);
892 foreach (qw(debit credit)) {
893 $subtotals{$_} += $ref->{$_};
894 $totals{$_} += $ref->{$_};
897 $subtotals{begbalance} += $ref->{balance} * $ml;
898 $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
900 map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
901 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
903 $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
904 $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
906 $report->add_data($row);
908 if ($form->{l_heading} && $found_heading &&
909 (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
910 $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
914 $report->add_separator();
916 $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
918 $report->generate_with_headers();
920 $main::lxdebug->leave_sub();
923 sub generate_ar_aging {
924 $main::lxdebug->enter_sub();
926 $main::auth->assert('general_ledger | ar_transactions');
928 my $form = $main::form;
929 my %myconfig = %main::myconfig;
930 my $locale = $main::locale;
933 ($form->{customer}) = split(/--/, $form->{customer});
935 $form->{ct} = "customer";
936 $form->{arap} = "ar";
938 $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
940 RP->aging(\%myconfig, \%$form);
943 $main::lxdebug->leave_sub();
946 sub generate_ap_aging {
947 $main::lxdebug->enter_sub();
949 $main::auth->assert('general_ledger | ap_transactions');
951 my $form = $main::form;
952 my %myconfig = %main::myconfig;
953 my $locale = $main::locale;
956 ($form->{vendor}) = split(/--/, $form->{vendor});
958 $form->{ct} = "vendor";
959 $form->{arap} = "ap";
961 $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
963 RP->aging(\%myconfig, \%$form);
966 $main::lxdebug->leave_sub();
969 sub create_aging_subtotal_row {
970 $main::lxdebug->enter_sub();
972 my ($subtotals, $columns, $periods, $class) = @_;
974 my $form = $main::form;
975 my %myconfig = %main::myconfig;
976 my $locale = $main::locale;
978 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
980 foreach (@{ $periods }) {
981 $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
982 $subtotals->{$_} = 0;
985 $main::lxdebug->leave_sub();
991 $main::lxdebug->enter_sub();
993 $main::auth->assert('general_ledger');
995 my $form = $main::form;
996 my %myconfig = %main::myconfig;
997 my $locale = $main::locale;
998 my $cgi = $::request->{cgi};
1000 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1002 my @columns = qw(statement ct invnumber transdate duedate amount open);
1005 'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
1006 'ct' => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1007 'invnumber' => { 'text' => $locale->text('Invoice'), },
1008 'transdate' => { 'text' => $locale->text('Date'), },
1009 'duedate' => { 'text' => $locale->text('Due'), },
1010 'amount' => { 'text' => $locale->text('Amount'), },
1011 'open' => { 'text' => $locale->text('Open'), },
1014 my %column_alignment = ('statement' => 'center',
1015 map { $_ => 'right' } qw(open amount));
1017 $report->set_options('std_column_visibility' => 1);
1018 $report->set_columns(%column_defs);
1019 $report->set_column_order(@columns);
1021 my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype department);
1022 $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
1025 my $attachment_basename;
1027 if ($form->{department}) {
1028 my ($department) = split /--/, $form->{department};
1029 push @options, $locale->text('Department') . " : $department";
1030 $form->{callback} .= "&department=" . E($department);
1033 if (($form->{arap} eq 'ar') && $form->{customer}) {
1034 push @options, $form->{customer};
1035 $attachment_basename = $locale->text('ar_aging_list');
1036 $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
1039 if (($form->{arap} eq 'ap') && $form->{vendor}) {
1040 push @options, $form->{vendor};
1041 $attachment_basename = $locale->text('ap_aging_list');
1042 $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
1045 if ($form->{fromdate}) {
1046 push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1048 push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1051 $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1053 $report->set_options('top_info_text' => join("\n", @options),
1054 'output_format' => 'HTML',
1055 'title' => $form->{title},
1056 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1058 $report->set_options_from_form();
1059 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1061 my $previous_ctid = 0;
1063 my @periods = qw(open amount);
1064 my %subtotals = map { $_ => 0 } @periods;
1065 my %totals = map { $_ => 0 } @periods;
1067 foreach my $ref (@{ $form->{AG} }) {
1068 if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1069 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1072 foreach my $key (@periods) {
1073 $subtotals{$key} += $ref->{"$key"};
1074 $totals{$key} += $ref->{"$key"};
1075 $ref->{"$key"} = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
1080 foreach my $column (@columns) {
1082 'data' => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1083 'align' => $column_alignment{$column},
1084 'valign' => $column eq 'statement' ? 'center' : '',
1088 $row->{invnumber}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1090 if ($previous_ctid != $ref->{ctid}) {
1091 $row->{statement}->{raw_data} =
1092 $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
1093 . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1094 $row->{ct}->{data} = $ref->{name};
1099 $previous_ctid = $ref->{ctid};
1101 $report->add_data($row);
1104 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1106 $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1108 if ($form->{arap} eq 'ar') {
1109 my $raw_top_info_text = $form->parse_html_template('rp/aging_ar_top');
1110 my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1111 'PRINT_OPTIONS' => print_options(inline => 1), });
1112 $report->set_options('raw_top_info_text' => $raw_top_info_text,
1113 'raw_bottom_info_text' => $raw_bottom_info_text);
1116 $report->generate_with_headers();
1118 $main::lxdebug->leave_sub();
1122 $main::lxdebug->enter_sub();
1124 my $form = $main::form;
1125 my %myconfig = %main::myconfig;
1126 my $locale = $main::locale;
1128 RP->aging(\%myconfig, \%$form);
1130 map { $_->{checked} = "checked" } @{ $form->{AG} };
1134 $main::lxdebug->leave_sub();
1138 $::lxdebug->enter_sub;
1139 $::auth->assert('general_ledger');
1141 # get name and email addresses
1143 for my $i (1 .. $::form->{rowcount}) {
1144 next unless $::form->{"statement_$i"};
1145 $::form->{"$::form->{ct}_id"} = $::form->{"$::form->{ct}_id_$i"};
1146 RP->get_customer(\%::myconfig, $::form);
1151 $::form->error($::locale->text('Nothing selected!')) unless $selected;
1153 $::form->{media} = "email";
1155 # save all other variables
1157 for my $key (keys %$::form) {
1158 next if any { $key eq $_ } qw(login password action email cc bcc subject message type sendmode format header);
1159 next unless '' eq ref $::form->{$key};
1160 push @hidden_values, $key;
1164 print $::form->parse_html_template('rp/e_mail', {
1165 print_options => print_options(inline => 1),
1166 hidden_values => \@hidden_values,
1169 $::lxdebug->leave_sub;
1173 $main::lxdebug->enter_sub();
1175 $main::auth->assert('general_ledger');
1177 my $form = $main::form;
1178 my %myconfig = %main::myconfig;
1179 my $locale = $main::locale;
1181 $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1182 unless $form->{subject};
1184 RP->aging(\%myconfig, \%$form);
1186 $form->{"statement_1"} = 1;
1188 $form->{media} = 'email';
1191 $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1193 $main::lxdebug->leave_sub();
1197 $main::lxdebug->enter_sub();
1199 $main::auth->assert('general_ledger');
1201 my $form = $main::form;
1202 my %myconfig = %main::myconfig;
1203 my $locale = $main::locale;
1205 if ($form->{media} eq 'printer') {
1206 $form->error($locale->text('Select postscript or PDF!'))
1207 if ($form->{format} !~ /(postscript|pdf)/);
1211 for my $i (1 .. $form->{rowcount}) {
1212 if ($form->{"statement_$i"}) {
1213 $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1219 $form->error($locale->text('Nothing selected!')) unless $selected;
1221 if ($form->{media} eq 'printer') {
1222 $form->{"$form->{ct}_id"} = "";
1224 $form->{"statement_1"} = 1;
1227 RP->aging(\%myconfig, \%$form);
1231 $form->redirect($locale->text('Statements sent to printer!'))
1232 if ($form->{media} eq 'printer');
1234 $main::lxdebug->leave_sub();
1238 $main::lxdebug->enter_sub();
1240 $main::auth->assert('general_ledger');
1242 my $form = $main::form;
1243 my %myconfig = %main::myconfig;
1244 my $locale = $main::locale;
1246 my $defaults = SL::DB::Default->get;
1247 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
1248 $form->{templates} = $defaults->templates;
1250 $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1252 my $suffix = "html";
1253 my $attachment_suffix = "html";
1254 if ($form->{format} eq 'postscript') {
1255 $form->{postscript} = 1;
1257 $attachment_suffix = "ps";
1258 } elsif ($form->{format} eq 'pdf') {
1261 $attachment_suffix = "pdf";
1264 $form->{IN} = "$form->{type}.$suffix";
1265 $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1267 # Save $form->{email} because it will be overwritten.
1268 $form->{EMAIL_RECIPIENT} = $form->{email};
1272 while (@{ $form->{AG} }) {
1274 my $ref = shift @{ $form->{AG} };
1276 if ($ctid != $ref->{ctid}) {
1278 $ctid = $ref->{ctid};
1281 if ($form->{"statement_$i"}) {
1284 ("name", "street", "zipcode", "city", "country", "contact", "email",
1285 "$form->{ct}phone", "$form->{ct}fax");
1286 map { $form->{$_} = $ref->{$_} } @a;
1288 $form->{ $form->{ct} } = $form->{name};
1289 $form->{"$form->{ct}_id"} = $ref->{ctid};
1291 map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
1293 foreach my $item (qw(c0 c30 c60 c90)) {
1294 $form->{$item} = ();
1295 $form->{"${item}total"} = 0;
1298 &statement_details($ref);
1302 if (scalar(@{ $form->{AG} }) > 0) {
1304 # one or more left to go
1305 if ($ctid == $form->{AG}->[0]->{ctid}) {
1306 $ref = shift @{ $form->{AG} };
1307 &statement_details($ref);
1310 $ref = scalar(@{ $form->{AG} });
1316 # set initial ref to 0
1323 $form->{"${_}total"} =
1324 $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1325 } ('c0', 'c30', 'c60', 'c90', "");
1327 $form->{attachment_filename} = $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
1328 $form->{attachment_filename} =~ s/\s+/_/g;
1330 $form->parse_template(\%myconfig);
1335 # saving the history
1336 if(!exists $form->{addition} && $form->{id} ne "") {
1337 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1338 $form->{addition} = "PRINTED";
1339 $form->{what_done} = $form->{type};
1340 $form->save_history;
1342 # /saving the history
1343 $main::lxdebug->leave_sub();
1346 sub statement_details {
1347 $main::lxdebug->enter_sub();
1349 $main::auth->assert('general_ledger');
1351 my $form = $main::form;
1352 my %myconfig = %main::myconfig;
1353 my $locale = $main::locale;
1357 push @{ $form->{invnumber} }, $ref->{invnumber};
1358 push @{ $form->{invdate} }, $ref->{transdate};
1359 push @{ $form->{duedate} }, $ref->{duedate};
1360 push @{ $form->{amount} }, $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
1361 push @{ $form->{open} }, $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
1363 foreach my $item (qw(c0 c30 c60 c90)) {
1364 if ($ref->{exchangerate} * 1) {
1365 # add only the open amount of the invoice to the aging, not the total amount
1366 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
1367 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
1368 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
1369 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
1371 $form->{"${item}total"} += $ref->{$item};
1372 $form->{total} += $ref->{$item};
1373 push @{ $form->{$item} },
1374 $form->format_amount(\%myconfig, $ref->{$item}, 2);
1377 $main::lxdebug->leave_sub();
1380 sub generate_tax_report {
1381 $::lxdebug->enter_sub;
1382 $::auth->assert('report');
1384 RP->tax_report(\%::myconfig, $::form);
1386 my $descvar = "$::form->{accno}_description";
1387 my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
1391 my $callback = build_std_url('action=generate_tax_report', $descvar,
1392 qw(fromdate todate db method accno department report title));
1394 my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1397 for my $item (@columns, 'subtotal') {
1398 if ($::form->{"l_$item"} eq "Y") {
1399 $callback .= "&l_$item=Y";
1400 $href .= "&l_$item=Y";
1404 for my $item (@columns) {
1405 if ($::form->{"l_$item"} eq "Y") {
1406 push @column_index, $item;
1411 if ($::form->{department}) {
1412 my ($department) = split /--/, $::form->{department};
1413 push @options, $::locale->text('Department') . " : $department";
1416 # if there are any dates
1417 if ($::form->{fromdate} || $::form->{todate}) {
1418 my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
1419 my $todate = $::form->{todate} ? $::locale->date(\%::myconfig, $::form->{todate}, 1) : '';
1420 push @options, "$fromdate - $todate";
1422 push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
1425 my ($name, $invoice, $arap);
1426 if ($::form->{db} eq 'ar') {
1427 $name = $::locale->text('Customer');
1431 if ($::form->{db} eq 'ap') {
1432 $name = $::locale->text('Vendor');
1437 my %column_header = (
1438 id => $::locale->text('ID'),
1439 invnumber => $::locale->text('Invoice'),
1440 transdate => $::locale->text('Date'),
1441 netamount => $::locale->text('Amount'),
1442 tax => $::locale->text('Tax'),
1443 amount => $::locale->text('Total'),
1447 my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
1449 $callback .= "&sort=$::form->{sort}";
1452 if (@{ $::form->{TR} }) {
1453 $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
1456 my ($totalnetamount, $totaltax, @data);
1457 for my $ref (@{ $::form->{TR} }) {
1459 my $module = ($ref->{invoice}) ? $invoice : $arap;
1461 if ($::form->{l_subtotal} eq 'Y') {
1462 if ($sameitem ne $ref->{ $::form->{sort} }) {
1465 netamount => $subtotalnetamount,
1466 tax => $subtotaltax,
1467 amount => $subtotal,
1469 $subtotalnetamount = 0;
1471 $sameitem = $ref->{ $::form->{sort} };
1475 $subtotalnetamount += $ref->{netamount};
1476 $subtotaltax += $ref->{tax};
1477 $totalnetamount += $ref->{netamount};
1478 $totaltax += $ref->{tax};
1479 $ref->{amount} = $ref->{netamount} + $ref->{tax};
1481 push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
1482 $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
1483 $data[-1]{$_}{numeric} = 1 for qw(netamount tax amount);
1486 if ($::form->{l_subtotal} eq 'Y') {
1489 netamount => $subtotalnetamount,
1490 tax => $subtotaltax,
1491 amount => $subtotal,
1497 netamount => $totalnetamount,
1499 amount => $totalnetamount + $totaltax,
1503 print $::form->parse_html_template('rp/tax_report', {
1504 column_index => \@column_index,
1505 column_header => \%column_header,
1506 column_sorted => \%column_sorted,
1509 options => \@options,
1512 $::lxdebug->leave_sub;
1516 $main::lxdebug->enter_sub();
1518 $main::auth->assert('cash');
1520 my $form = $main::form;
1521 my %myconfig = %main::myconfig;
1522 my $locale = $main::locale;
1524 if ($form->{account}) {
1525 ($form->{paymentaccounts}) = split /--/, $form->{account};
1529 if ($form->{department}) {
1530 (my $department, $form->{department_id}) = split /--/, $form->{department};
1531 $option = $locale->text('Department') . " : $department";
1534 report_generator_set_default_sort('transdate', 1);
1536 RP->payments(\%myconfig, \%$form);
1538 my @hidden_variables = qw(account title department reference source memo fromdate todate
1539 fx_transaction db prepayment paymentaccounts sort);
1541 my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
1542 $form->{callback} = $href;
1544 my @columns = qw(transdate invnumber name paid source memo);
1546 'name' => { 'text' => $locale->text('Description'), },
1547 'invnumber' => { 'text' => $locale->text('Reference'), },
1548 'transdate' => { 'text' => $locale->text('Date'), },
1549 'paid' => { 'text' => $locale->text('Amount'), },
1550 'source' => { 'text' => $locale->text('Source'), },
1551 'memo' => { 'text' => $locale->text('Memo'), },
1553 my %column_alignment = ('paid' => 'right');
1555 foreach my $name (grep { $_ ne 'paid' } @columns) {
1556 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1557 $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
1561 if ($form->{fromdate}) {
1562 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
1564 if ($form->{todate}) {
1565 push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1568 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1570 my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
1572 $report->set_options('top_info_text' => join("\n", @options),
1573 'output_format' => 'HTML',
1574 'title' => $form->{title},
1575 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1576 'std_column_visibility' => 1,
1578 $report->set_options_from_form();
1580 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1582 $report->set_columns(%column_defs);
1583 $report->set_column_order(@columns);
1585 $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
1587 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1591 foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1592 next unless @{ $form->{ $ref->{id} } };
1594 $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
1596 my $subtotal_paid = 0;
1598 foreach my $payment (@{ $form->{ $ref->{id} } }) {
1599 my $module = $payment->{module};
1600 $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1601 $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1603 $subtotal_paid += $payment->{paid};
1604 $total_paid += $payment->{paid};
1606 $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
1610 foreach my $column (@columns) {
1612 'data' => $payment->{$column},
1613 'align' => $column_alignment{$column},
1617 $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
1619 $report->add_data($row);
1622 my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
1624 'data' => $form->format_amount(\%myconfig, $subtotal_paid, 2),
1626 'class' => 'listsubtotal',
1629 $report->add_data($row);
1632 $report->add_separator();
1634 my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
1636 'data' => $form->format_amount(\%myconfig, $total_paid, 2),
1638 'class' => 'listtotal',
1641 $report->add_data($row);
1643 $report->generate_with_headers();
1645 $main::lxdebug->leave_sub();
1649 $::lxdebug->enter_sub;
1651 my ($dont_print) = @_;
1653 $::form->{sendmode} = "attachment";
1654 $::form->{format} ||= $::myconfig{template_format} || "pdf";
1655 $::form->{copies} ||= $::myconfig{copies} || 2;
1657 $::form->{PD}{ $::form->{type} } = "selected";
1658 $::form->{DF}{ $::form->{format} } = "selected";
1659 $::form->{OP}{ $::form->{media} } = "selected";
1660 $::form->{SM}{ $::form->{sendmode} } = "selected";
1662 my $output = $::form->parse_html_template('rp/print_options', {
1663 is_email => $::form->{media} eq 'email',
1666 print $output unless $dont_print;
1668 $::lxdebug->leave_sub;
1674 $main::lxdebug->enter_sub();
1676 $main::auth->assert('report');
1678 my $form = $main::form;
1679 my %myconfig = %main::myconfig;
1680 my $locale = $main::locale;
1682 $form->{padding} = " ";
1683 $form->{bold} = "<b>";
1684 $form->{endbold} = "</b>";
1685 $form->{br} = "<br>";
1687 if ($form->{reporttype} eq "custom") {
1689 #forgotten the year --> thisyear
1690 if ($form->{year} !~ m/^\d\d\d\d$/) {
1691 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1697 if ($form->{duetyp} eq "13") {
1698 $form->{fromdate} = "1.1.$form->{year}";
1699 $form->{todate} = "31.12.$form->{year}";
1700 $form->{comparefromdate} = "1.01.$form->{year}";
1701 $form->{comparetodate} = "31.12.$form->{year}";
1705 if ($form->{duetyp} eq "A") {
1706 $form->{fromdate} = "1.1.$form->{year}";
1707 $form->{todate} = "31.3.$form->{year}";
1708 $form->{comparefromdate} = "1.01.$form->{year}";
1709 $form->{comparetodate} = "31.03.$form->{year}";
1711 if ($form->{duetyp} eq "B") {
1712 $form->{fromdate} = "1.4.$form->{year}";
1713 $form->{todate} = "30.6.$form->{year}";
1714 $form->{comparefromdate} = "1.01.$form->{year}";
1715 $form->{comparetodate} = "30.06.$form->{year}";
1717 if ($form->{duetyp} eq "C") {
1718 $form->{fromdate} = "1.7.$form->{year}";
1719 $form->{todate} = "30.9.$form->{year}";
1720 $form->{comparefromdate} = "1.01.$form->{year}";
1721 $form->{comparetodate} = "30.09.$form->{year}";
1723 if ($form->{duetyp} eq "D") {
1724 $form->{fromdate} = "1.10.$form->{year}";
1725 $form->{todate} = "31.12.$form->{year}";
1726 $form->{comparefromdate} = "1.01.$form->{year}";
1727 $form->{comparetodate} = "31.12.$form->{year}";
1732 $form->{duetyp} eq "1" && do {
1733 $form->{fromdate} = "1.1.$form->{year}";
1734 $form->{todate} = "31.1.$form->{year}";
1735 $form->{comparefromdate} = "1.01.$form->{year}";
1736 $form->{comparetodate} = "31.01.$form->{year}";
1739 $form->{duetyp} eq "2" && do {
1740 $form->{fromdate} = "1.2.$form->{year}";
1742 #this works from 1901 to 2099, 1900 and 2100 fail.
1743 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1744 $form->{todate} = "$leap.2.$form->{year}";
1745 $form->{comparefromdate} = "1.01.$form->{year}";
1746 $form->{comparetodate} = "$leap.02.$form->{year}";
1749 $form->{duetyp} eq "3" && do {
1750 $form->{fromdate} = "1.3.$form->{year}";
1751 $form->{todate} = "31.3.$form->{year}";
1752 $form->{comparefromdate} = "1.01.$form->{year}";
1753 $form->{comparetodate} = "31.03.$form->{year}";
1756 $form->{duetyp} eq "4" && do {
1757 $form->{fromdate} = "1.4.$form->{year}";
1758 $form->{todate} = "30.4.$form->{year}";
1759 $form->{comparefromdate} = "1.01.$form->{year}";
1760 $form->{comparetodate} = "30.04.$form->{year}";
1763 $form->{duetyp} eq "5" && do {
1764 $form->{fromdate} = "1.5.$form->{year}";
1765 $form->{todate} = "31.5.$form->{year}";
1766 $form->{comparefromdate} = "1.01.$form->{year}";
1767 $form->{comparetodate} = "31.05.$form->{year}";
1770 $form->{duetyp} eq "6" && do {
1771 $form->{fromdate} = "1.6.$form->{year}";
1772 $form->{todate} = "30.6.$form->{year}";
1773 $form->{comparefromdate} = "1.01.$form->{year}";
1774 $form->{comparetodate} = "30.06.$form->{year}";
1777 $form->{duetyp} eq "7" && do {
1778 $form->{fromdate} = "1.7.$form->{year}";
1779 $form->{todate} = "31.7.$form->{year}";
1780 $form->{comparefromdate} = "1.01.$form->{year}";
1781 $form->{comparetodate} = "31.07.$form->{year}";
1784 $form->{duetyp} eq "8" && do {
1785 $form->{fromdate} = "1.8.$form->{year}";
1786 $form->{todate} = "31.8.$form->{year}";
1787 $form->{comparefromdate} = "1.01.$form->{year}";
1788 $form->{comparetodate} = "31.08.$form->{year}";
1791 $form->{duetyp} eq "9" && do {
1792 $form->{fromdate} = "1.9.$form->{year}";
1793 $form->{todate} = "30.9.$form->{year}";
1794 $form->{comparefromdate} = "1.01.$form->{year}";
1795 $form->{comparetodate} = "30.09.$form->{year}";
1798 $form->{duetyp} eq "10" && do {
1799 $form->{fromdate} = "1.10.$form->{year}";
1800 $form->{todate} = "31.10.$form->{year}";
1801 $form->{comparefromdate} = "1.01.$form->{year}";
1802 $form->{comparetodate} = "31.10.$form->{year}";
1805 $form->{duetyp} eq "11" && do {
1806 $form->{fromdate} = "1.11.$form->{year}";
1807 $form->{todate} = "30.11.$form->{year}";
1808 $form->{comparefromdate} = "1.01.$form->{year}";
1809 $form->{comparetodate} = "30.11.$form->{year}";
1812 $form->{duetyp} eq "12" && do {
1813 $form->{fromdate} = "1.12.$form->{year}";
1814 $form->{todate} = "31.12.$form->{year}";
1815 $form->{comparefromdate} = "1.01.$form->{year}";
1816 $form->{comparetodate} = "31.12.$form->{year}";
1820 hotfix_reformat_date();
1822 # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
1823 # ansonsten ist die prüfung in RP.pm
1824 # if (defined ($form->{fromdate|todate}=='..'))
1826 if ($form->{fromdate}){
1827 my $datetime = $locale->parse_date_to_object($form->{fromdate});
1828 $datetime->set( month => 1,
1830 $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
1832 if ($form->{todate}){
1833 $form->{comparetodate} = $form->{todate};
1837 RP->bwa(\%myconfig, \%$form);
1839 ($form->{department}) = split /--/, $form->{department};
1842 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1843 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1845 # if there are any dates construct a where
1846 if ($form->{fromdate} || $form->{todate}) {
1848 unless ($form->{todate}) {
1849 $form->{todate} = $form->current_date(\%myconfig);
1852 my %germandate = ("dateformat" => "dd.mm.yyyy");
1854 my $longtodate = $locale->date(\%germandate, $form->{todate}, 1);
1855 my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
1857 my $longfromdate = $locale->date(\%germandate, $form->{fromdate}, 1);
1858 my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
1860 $form->{this_period} = "$shortfromdate\n$shorttodate";
1862 $locale->text('for Period')
1863 . qq|\n$longfromdate |
1864 . $locale->text('bis')
1868 $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
1870 if ( $form->{method} eq 'cash' ) {
1871 $form->{accounting_method} = $locale->text('Cash accounting');
1872 } elsif ( $form->{method} eq 'accrual' ) {
1873 $form->{accounting_method} = $locale->text('Accrual accounting');
1875 $form->{accounting_method} = "";
1878 $form->{title} = $locale->text('BWA');
1880 $::request->layout->add_stylesheets('bwa.css');
1882 print $form->parse_html_template('rp/bwa');
1884 $main::lxdebug->leave_sub();
1887 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
1888 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
1889 sub hotfix_reformat_date {
1891 $main::lxdebug->enter_sub();
1893 my $form = $main::form;
1894 my %myconfig = %main::myconfig;
1895 my $locale = $main::locale;
1897 if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
1898 my $current_dateformat = $myconfig{dateformat};
1899 $myconfig{dateformat} = 'dd.mm.yyyy';
1900 $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
1901 $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
1902 $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
1903 unless (!defined ($form->{comparefromdate}));
1904 $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
1905 unless (!defined ($form->{comparetodate}));
1907 # Und wieder zurücksetzen
1908 $myconfig{dateformat} = $current_dateformat; #'dd.mm.yyyy';
1909 } # Ende Hotifx Bug 1388
1911 $main::lxdebug->leave_sub();