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);
45 use SL::ReportGenerator;
47 use List::MoreUtils qw(any);
49 require "bin/mozilla/arap.pl";
50 require "bin/mozilla/common.pl";
51 require "bin/mozilla/reportgenerator.pl";
53 # note: this file was particularly hard to strictify.
54 # alot of the vars are passed carelessly between invocations
55 # should there be any missing vars, declare them globally
58 # this is for our long dates
59 # $locale->text('January')
60 # $locale->text('February')
61 # $locale->text('March')
62 # $locale->text('April')
63 # $locale->text('May ')
64 # $locale->text('June')
65 # $locale->text('July')
66 # $locale->text('August')
67 # $locale->text('September')
68 # $locale->text('October')
69 # $locale->text('November')
70 # $locale->text('December')
72 # this is for our short month
73 # $locale->text('Jan')
74 # $locale->text('Feb')
75 # $locale->text('Mar')
76 # $locale->text('Apr')
77 # $locale->text('May')
78 # $locale->text('Jun')
79 # $locale->text('Jul')
80 # $locale->text('Aug')
81 # $locale->text('Sep')
82 # $locale->text('Oct')
83 # $locale->text('Nov')
84 # $locale->text('Dec')
86 # $locale->text('Balance Sheet')
87 # $locale->text('Income Statement')
88 # $locale->text('Trial Balance')
89 # $locale->text('AR Aging')
90 # $locale->text('AP Aging')
91 # $locale->text('Search AR Aging')
92 # $locale->text('Search AP Aging')
93 # $locale->text('Tax collected')
94 # $locale->text('Tax paid')
95 # $locale->text('Receipts')
96 # $locale->text('Payments')
97 # $locale->text('Project Transactions')
98 # $locale->text('Business evaluation')
100 # $form->parse_html_template('rp/html_report_susa')
102 my $rp_access_map = {
103 'projects' => 'report',
104 'ar_aging' => 'general_ledger',
105 'ap_aging' => 'general_ledger',
106 'receipts' => 'cash',
107 'payments' => 'cash',
108 'trial_balance' => 'report',
109 'income_statement' => 'report',
111 'balance_sheet' => 'report',
114 sub check_rp_access {
115 my $form = $main::form;
117 my $right = $rp_access_map->{$form->{report}};
118 $right ||= 'DOES_NOT_EXIST';
120 $main::auth->assert($right);
124 $::lxdebug->enter_sub;
129 balance_sheet => $::locale->text('Balance Sheet'),
130 income_statement => $::locale->text('Income Statement'),
131 trial_balance => $::locale->text('Trial Balance'),
132 ar_aging => $::locale->text('Search AR Aging'),
133 ap_aging => $::locale->text('Search AP Aging'),
134 tax_collected => $::locale->text('Tax collected'),
135 tax_paid => $::locale->text('Tax paid'),
136 receipts => $::locale->text('Receipts'),
137 payments => $::locale->text('Payments'),
138 projects => $::locale->text('Project Transactions'),
139 bwa => $::locale->text('Business evaluation'),
142 $::form->{title} = $title{$::form->{report}};
145 $::form->all_departments(\%::myconfig);
146 if (@{ $::form->{all_departments} || [] }) {
147 $::form->{selectdepartment} = "<option>\n";
148 map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
151 $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
153 my $is_projects = $::form->{report} eq "projects";
154 my $is_income_statement = $::form->{report} eq "income_statement";
155 my $is_bwa = $::form->{report} eq "bwa";
156 my $is_balance_sheet = $::form->{report} eq "balance_sheet";
157 my $is_trial_balance = $::form->{report} eq "trial_balance";
158 my $is_aging = $::form->{report} =~ /^a[rp]_aging$/;
159 my $is_payments = $::form->{report} =~ /(receipts|payments)$/;
161 my ($label, $nextsub, $vc);
163 my $is_sales = $::form->{report} eq 'ar_aging';
164 $label = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
165 $::form->{vc} = $is_sales ? 'customer' : 'vendor';
167 $nextsub = "generate_$::form->{report}";
170 $::form->all_vc(\%::myconfig, $::form->{vc}, $is_sales ? "AR" : "AP");
171 $vc .= "<option>$_->{name}--$_->{id}\n" for @{ $::form->{"all_$::form->{vc}"} };
173 ? qq|<select name=$::form->{vc} class="initial_focus"><option>\n$vc</select>|
174 : qq|<input name=$::form->{vc} size=35 class="initial_focus">|;
177 my ($selection, $paymentaccounts);
179 $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
181 RP->paymentaccounts(\%::myconfig, $::form);
183 $selection = "<option>\n";
184 for my $ref (@{ $::form->{PR} }) {
185 $paymentaccounts .= "$ref->{accno} ";
186 $selection .= "<option>$ref->{accno}--$ref->{description}\n";
191 print $::form->parse_html_template('rp/report', {
192 paymentaccounts => $paymentaccounts,
193 selection => $selection,
194 is_aging => $is_aging,
197 year => DateTime->today->year,
198 today => DateTime->today,
200 accrual => $::instance_conf->get_accounting_method ne 'cash',
201 cash => $::instance_conf->get_accounting_method eq 'cash',
202 is_payments => $is_payments,
203 is_trial_balance => $is_trial_balance,
204 is_balance_sheet => $is_balance_sheet,
206 is_income_statement => $is_income_statement,
207 is_projects => $is_projects,
210 $::lxdebug->leave_sub;
213 sub continue { call_sub($main::form->{"nextsub"}); }
215 sub generate_income_statement {
216 $main::lxdebug->enter_sub();
218 $main::auth->assert('report');
220 my $form = $main::form;
221 my %myconfig = %main::myconfig;
222 my $locale = $main::locale;
224 my $defaults = SL::DB::Default->get;
225 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
226 $form->{templates} = $defaults->templates;
228 $form->{padding} = " ";
229 $form->{bold} = "<b>";
230 $form->{endbold} = "</b>";
231 $form->{br} = "<br>";
233 if ($form->{reporttype} eq "custom") {
235 #forgotten the year --> thisyear
236 if ($form->{year} !~ m/^\d\d\d\d$/) {
237 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
243 if ($form->{duetyp} eq "13") {
244 $form->{fromdate} = "1.1.$form->{year}";
245 $form->{todate} = "31.12.$form->{year}";
249 if ($form->{duetyp} eq "A") {
250 $form->{fromdate} = "1.1.$form->{year}";
251 $form->{todate} = "31.3.$form->{year}";
253 if ($form->{duetyp} eq "B") {
254 $form->{fromdate} = "1.4.$form->{year}";
255 $form->{todate} = "30.6.$form->{year}";
257 if ($form->{duetyp} eq "C") {
258 $form->{fromdate} = "1.7.$form->{year}";
259 $form->{todate} = "30.9.$form->{year}";
261 if ($form->{duetyp} eq "D") {
262 $form->{fromdate} = "1.10.$form->{year}";
263 $form->{todate} = "31.12.$form->{year}";
268 $form->{duetyp} eq "1" && do {
269 $form->{fromdate} = "1.1.$form->{year}";
270 $form->{todate} = "31.1.$form->{year}";
273 $form->{duetyp} eq "2" && do {
274 $form->{fromdate} = "1.2.$form->{year}";
276 #this works from 1901 to 2099, 1900 and 2100 fail.
277 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
278 $form->{todate} = "$leap.2.$form->{year}";
281 $form->{duetyp} eq "3" && do {
282 $form->{fromdate} = "1.3.$form->{year}";
283 $form->{todate} = "31.3.$form->{year}";
286 $form->{duetyp} eq "4" && do {
287 $form->{fromdate} = "1.4.$form->{year}";
288 $form->{todate} = "30.4.$form->{year}";
291 $form->{duetyp} eq "5" && do {
292 $form->{fromdate} = "1.5.$form->{year}";
293 $form->{todate} = "31.5.$form->{year}";
296 $form->{duetyp} eq "6" && do {
297 $form->{fromdate} = "1.6.$form->{year}";
298 $form->{todate} = "30.6.$form->{year}";
301 $form->{duetyp} eq "7" && do {
302 $form->{fromdate} = "1.7.$form->{year}";
303 $form->{todate} = "31.7.$form->{year}";
306 $form->{duetyp} eq "8" && do {
307 $form->{fromdate} = "1.8.$form->{year}";
308 $form->{todate} = "31.8.$form->{year}";
311 $form->{duetyp} eq "9" && do {
312 $form->{fromdate} = "1.9.$form->{year}";
313 $form->{todate} = "30.9.$form->{year}";
316 $form->{duetyp} eq "10" && do {
317 $form->{fromdate} = "1.10.$form->{year}";
318 $form->{todate} = "31.10.$form->{year}";
321 $form->{duetyp} eq "11" && do {
322 $form->{fromdate} = "1.11.$form->{year}";
323 $form->{todate} = "30.11.$form->{year}";
326 $form->{duetyp} eq "12" && do {
327 $form->{fromdate} = "1.12.$form->{year}";
328 $form->{todate} = "31.12.$form->{year}";
332 hotfix_reformat_date();
333 } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
335 RP->income_statement(\%myconfig, \%$form);
337 ($form->{department}) = split /--/, $form->{department};
340 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
341 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
343 # if there are any dates construct a where
344 if ($form->{fromdate} || $form->{todate}) {
346 unless ($form->{todate}) {
347 $form->{todate} = $form->current_date(\%myconfig);
350 my $longtodate = $locale->date(\%myconfig, $form->{todate}, 1);
351 my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
353 my $longfromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
354 my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
356 $form->{this_period} = "$shortfromdate\n$shorttodate";
358 $locale->text('for Period')
359 . qq|\n$longfromdate |
360 . $locale->text('Bis')
364 if ($form->{comparefromdate} || $form->{comparetodate}) {
365 my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
366 my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
368 my $longcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 1);
369 my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
371 $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
373 "\n$longcomparefromdate "
374 . $locale->text('Bis')
375 . qq| $longcomparetodate|;
378 $form->{IN} = "income_statement.html";
380 $form->parse_template;
382 $main::lxdebug->leave_sub();
385 sub generate_balance_sheet {
386 $::lxdebug->enter_sub;
387 $::auth->assert('report');
389 my $defaults = SL::DB::Default->get;
390 $::form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
391 $::form->{templates} = $defaults->templates;
392 $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
393 $::form->{padding} = " ";
394 $::form->{bold} = "<b>";
395 $::form->{endbold} = "</b>";
396 $::form->{br} = "<br>";
398 my $data = RP->balance_sheet(\%::myconfig, $::form);
400 $::form->{asofdate} ||= $::form->current_date;
401 $::form->{report_title} = $::locale->text('Balance Sheet');
402 $::form->{report_date} ||= $::form->current_date;
404 ($::form->{department}) = split /--/, $::form->{department};
406 # define Current Earnings account
407 my $padding = $::form->{l_heading} ? $::form->{padding} : "";
408 push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
410 $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
411 $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
413 # balance sheet isn't read from print templates anymore,
414 # instead use template in rp
415 # $::form->{IN} = "balance_sheet.html";
418 print $::form->parse_html_template('rp/balance_sheet', $data);
420 $::lxdebug->leave_sub;
423 sub generate_projects {
424 $main::lxdebug->enter_sub();
426 $main::auth->assert('report');
428 my $form = $main::form;
429 my %myconfig = %main::myconfig;
430 my $locale = $main::locale;
432 my $project = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
433 $form->{projectnumber} = $project ? $project->projectnumber : '';
435 $form->{nextsub} = "generate_projects";
436 $form->{title} = $locale->text('Project Transactions');
437 RP->trial_balance(\%myconfig, \%$form);
439 list_accounts('generate_projects');
441 $main::lxdebug->leave_sub();
447 # included links to display transactions for period entered
448 # added headers and subtotals
450 sub generate_trial_balance {
451 $main::lxdebug->enter_sub();
453 $main::auth->assert('report');
455 my $form = $main::form;
456 my %myconfig = %main::myconfig;
457 my $locale = $main::locale;
458 my $defaults = SL::DB::Default->get;
460 if ($form->{reporttype} eq "custom") {
462 #forgotten the year --> thisyear
463 if ($form->{year} !~ m/^\d\d\d\d$/) {
464 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
470 if ($form->{duetyp} eq "13") {
471 $form->{fromdate} = "1.1.$form->{year}";
472 $form->{todate} = "31.12.$form->{year}";
476 if ($form->{duetyp} eq "A") {
477 $form->{fromdate} = "1.1.$form->{year}";
478 $form->{todate} = "31.3.$form->{year}";
480 if ($form->{duetyp} eq "B") {
481 $form->{fromdate} = "1.4.$form->{year}";
482 $form->{todate} = "30.6.$form->{year}";
484 if ($form->{duetyp} eq "C") {
485 $form->{fromdate} = "1.7.$form->{year}";
486 $form->{todate} = "30.9.$form->{year}";
488 if ($form->{duetyp} eq "D") {
489 $form->{fromdate} = "1.10.$form->{year}";
490 $form->{todate} = "31.12.$form->{year}";
495 $form->{duetyp} eq "1" && do {
496 $form->{fromdate} = "1.1.$form->{year}";
497 $form->{todate} = "31.1.$form->{year}";
500 $form->{duetyp} eq "2" && do {
501 $form->{fromdate} = "1.2.$form->{year}";
503 #this works from 1901 to 2099, 1900 and 2100 fail.
504 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
505 $form->{todate} = "$leap.2.$form->{year}";
508 $form->{duetyp} eq "3" && do {
509 $form->{fromdate} = "1.3.$form->{year}";
510 $form->{todate} = "31.3.$form->{year}";
513 $form->{duetyp} eq "4" && do {
514 $form->{fromdate} = "1.4.$form->{year}";
515 $form->{todate} = "30.4.$form->{year}";
518 $form->{duetyp} eq "5" && do {
519 $form->{fromdate} = "1.5.$form->{year}";
520 $form->{todate} = "31.5.$form->{year}";
523 $form->{duetyp} eq "6" && do {
524 $form->{fromdate} = "1.6.$form->{year}";
525 $form->{todate} = "30.6.$form->{year}";
528 $form->{duetyp} eq "7" && do {
529 $form->{fromdate} = "1.7.$form->{year}";
530 $form->{todate} = "31.7.$form->{year}";
533 $form->{duetyp} eq "8" && do {
534 $form->{fromdate} = "1.8.$form->{year}";
535 $form->{todate} = "31.8.$form->{year}";
538 $form->{duetyp} eq "9" && do {
539 $form->{fromdate} = "1.9.$form->{year}";
540 $form->{todate} = "30.9.$form->{year}";
543 $form->{duetyp} eq "10" && do {
544 $form->{fromdate} = "1.10.$form->{year}";
545 $form->{todate} = "31.10.$form->{year}";
548 $form->{duetyp} eq "11" && do {
549 $form->{fromdate} = "1.11.$form->{year}";
550 $form->{todate} = "30.11.$form->{year}";
553 $form->{duetyp} eq "12" && do {
554 $form->{fromdate} = "1.12.$form->{year}";
555 $form->{todate} = "31.12.$form->{year}";
559 hotfix_reformat_date();
563 # get for each account initial balance, debits and credits
564 RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
567 $form->{rowcount} = scalar @{ $form->{TB} || [] };
568 $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
571 "accno", "description",
572 "last_transaction", "soll_eb",
575 "soll_kumuliert", "haben_kumuliert",
576 "soll_saldo", "haben_saldo"
580 my $attachment_basename = $locale->text('trial_balance');
581 my $report = SL::ReportGenerator->new(\%myconfig, $form);
583 my @hidden_variables = qw(fromdate todate year method);
585 my $href = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
588 'accno' => { 'text' => $locale->text('Account'), },
589 'description' => { 'text' => $locale->text('Description'), },
590 'last_transaction' => { 'text' => $locale->text('Last Transaction'), },
591 'soll_eb' => { 'text' => $locale->text('Debit Starting Balance'), },
592 'haben_eb' => { 'text' => $locale->text('Credit Starting Balance'), },
593 'soll' => { 'text' => $locale->text('Debit'), },
594 'haben' => { 'text' => $locale->text('Credit'), },
595 'soll_kumuliert' => { 'text' => $locale->text('Sum Debit'), },
596 'haben_kumuliert' => { 'text' => $locale->text('Sum Credit'), },
597 'soll_saldo' => { 'text' => $locale->text('Saldo Debit'), },
598 'haben_saldo' => { 'text' => $locale->text('Saldo Credit'), }
603 my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
605 map { $column_defs{$_}->{visible} = 1 } @columns;
607 $report->set_columns(%column_defs);
608 $report->set_column_order(@columns);
610 $report->set_export_options('generate_trial_balance', @hidden_variables);
615 $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
617 $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
618 push (@options, $form->{print_date});
620 $form->{company} = $locale->text('Company') . " " . $defaults->company;
621 push (@options, $form->{company});
624 $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
626 my @custom_headers = ([
627 { text => $::locale->text('Account'), rowspan => 2, },
628 { text => $::locale->text('Description'), rowspan => 2, },
629 { text => $::locale->text('Last Transaction'), rowspan => 2, },
630 { text => $::locale->text('Starting Balance'), colspan => 2, },
631 { text => $::locale->text('Sum for') . " $form->{template_fromto}", colspan => 2, },
632 { text => $::locale->text('Sum per') . " $form->{template_to}", colspan => 2, },
633 { text => $::locale->text('Saldo per') . " $form->{template_to}", colspan => 2, },
638 { text => $::locale->text('Assets'), },
639 { text => $::locale->text('Equity'), },
640 { text => $::locale->text('Debit'), },
641 { text => $::locale->text('Credit'), },
642 { text => $::locale->text('Debit'), },
643 { text => $::locale->text('Credit'), },
644 { text => $::locale->text('Debit'), },
645 { text => $::locale->text('Credit'), },
648 $report->set_options('output_format' => 'HTML',
649 'top_info_text' => join("\n", @options),
650 'title' => $form->{title},
651 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
652 'html_template' => 'rp/html_report_susa',
653 'pdf_template' => 'rp/html_report_susa',
655 $report->set_custom_headers(@custom_headers);
656 $report->set_options_from_form();
657 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
659 # add sort and escape callback, this one we use for the add sub
660 $form->{callback} = $href .= "&sort=$form->{sort}";
662 # escape callback for href
663 my $callback = $form->escape($href);
665 my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
667 my %totals = map { $_ => 0 } @subtotal_columns;
669 my $edit_url = build_std_url('action=edit', 'type', 'vc');
672 foreach my $accno (@{ $form->{TB} || [] }) {
674 $accno->{soll} = $accno->{debit};
675 $accno->{haben} = $accno->{credit};
676 map { $totals{$_} += $accno->{$_} } @subtotal_columns;
678 map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
679 qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
683 foreach my $column (@columns) {
685 'data' => $accno->{$column},
686 'align' => $column_alignment{$column},
690 $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}));
692 my $row_set = [ $row ];
695 $report->add_data($row_set);
700 $report->add_separator();
702 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
704 $report->generate_with_headers();
706 $main::lxdebug->leave_sub();
710 sub create_subtotal_row {
711 $main::lxdebug->enter_sub();
713 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
715 my $form = $main::form;
716 my %myconfig = %main::myconfig;
717 my $locale = $main::locale;
719 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
721 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
723 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
725 map { $totals->{$_} = 0 } @{ $subtotal_columns };
727 $main::lxdebug->leave_sub();
732 sub create_list_accounts_subtotal_row {
733 $main::lxdebug->enter_sub();
735 my ($subtotals, $columns, $fields, $class) = @_;
737 my $form = $main::form;
738 my %myconfig = %main::myconfig;
739 my $locale = $main::locale;
741 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
743 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
745 $main::lxdebug->leave_sub();
751 $main::lxdebug->enter_sub();
755 my $form = $main::form;
756 my %myconfig = %main::myconfig;
757 my $locale = $main::locale;
760 if ($form->{department}) {
761 my ($department) = split /--/, $form->{department};
762 push @options, $locale->text('Department') . " : $department";
764 if ($form->{projectnumber}) {
765 push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
768 # if there are any dates
769 if ($form->{fromdate} || $form->{todate}) {
770 my ($fromdate, $todate);
772 if ($form->{fromdate}) {
773 $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
775 if ($form->{todate}) {
776 $todate = $locale->date(\%myconfig, $form->{todate}, 1);
779 push @options, "$fromdate - $todate";
782 push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
785 my @columns = qw(accno description begbalance debit credit endbalance);
787 'accno' => { 'text' => $locale->text('Account'), },
788 'description' => { 'text' => $locale->text('Description'), },
789 'debit' => { 'text' => $locale->text('Debit'), },
790 'credit' => { 'text' => $locale->text('Credit'), },
791 'begbalance' => { 'text' => $locale->text('Balance'), },
792 'endbalance' => { 'text' => $locale->text('Balance'), },
794 my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
796 my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
798 $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
800 my $report = SL::ReportGenerator->new(\%myconfig, $form);
802 $report->set_options('top_info_text' => join("\n", @options),
803 'output_format' => 'HTML',
804 'title' => $form->{title},
805 'attachment_basename' => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
806 'std_column_visibility' => 1,
808 $report->set_options_from_form();
809 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
811 $report->set_columns(%column_defs);
812 $report->set_column_order(@columns);
814 $report->set_export_options($action, @hidden_variables);
816 my @totals_columns = qw(credit debit begbalance endbalance);
817 my %subtotals = map { $_ => 0 } @totals_columns;
818 my %totals = map { $_ => 0 } @totals_columns;
819 my $found_heading = 0;
820 my @tb = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
822 # sort the whole thing by account numbers and display
823 foreach my $idx (0 .. scalar(@tb) - 1) {
825 my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
827 my $ml = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
829 my $row = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
831 if ($ref->{charttype} eq 'H') {
832 next unless ($form->{l_heading});
834 %subtotals = map { $_ => 0 } @totals_columns;
836 $row->{description}->{class} = 'listheading';
837 $row->{description}->{data} = $ref->{description};
839 $report->add_data($row);
844 foreach (qw(debit credit)) {
845 $subtotals{$_} += $ref->{$_};
846 $totals{$_} += $ref->{$_};
849 $subtotals{begbalance} += $ref->{balance} * $ml;
850 $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
852 map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
853 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
855 $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
856 $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
858 $report->add_data($row);
860 if ($form->{l_heading} && $found_heading &&
861 (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
862 $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
866 $report->add_separator();
868 $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
870 $report->generate_with_headers();
872 $main::lxdebug->leave_sub();
875 sub generate_ar_aging {
876 $main::lxdebug->enter_sub();
878 $main::auth->assert('general_ledger');
880 my $form = $main::form;
881 my %myconfig = %main::myconfig;
882 my $locale = $main::locale;
885 ($form->{customer}) = split(/--/, $form->{customer});
887 $form->{ct} = "customer";
888 $form->{arap} = "ar";
890 $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
892 RP->aging(\%myconfig, \%$form);
895 $main::lxdebug->leave_sub();
898 sub generate_ap_aging {
899 $main::lxdebug->enter_sub();
901 $main::auth->assert('general_ledger');
903 my $form = $main::form;
904 my %myconfig = %main::myconfig;
905 my $locale = $main::locale;
908 ($form->{vendor}) = split(/--/, $form->{vendor});
910 $form->{ct} = "vendor";
911 $form->{arap} = "ap";
913 $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
915 RP->aging(\%myconfig, \%$form);
918 $main::lxdebug->leave_sub();
921 sub create_aging_subtotal_row {
922 $main::lxdebug->enter_sub();
924 my ($subtotals, $columns, $periods, $class) = @_;
926 my $form = $main::form;
927 my %myconfig = %main::myconfig;
928 my $locale = $main::locale;
930 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
932 foreach (@{ $periods }) {
933 $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
934 $subtotals->{$_} = 0;
937 $main::lxdebug->leave_sub();
943 $main::lxdebug->enter_sub();
945 $main::auth->assert('general_ledger');
947 my $form = $main::form;
948 my %myconfig = %main::myconfig;
949 my $locale = $main::locale;
950 my $cgi = $::request->{cgi};
952 my $report = SL::ReportGenerator->new(\%myconfig, $form);
954 my @columns = qw(statement ct invnumber transdate duedate amount open);
957 'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
958 'ct' => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
959 'invnumber' => { 'text' => $locale->text('Invoice'), },
960 'transdate' => { 'text' => $locale->text('Date'), },
961 'duedate' => { 'text' => $locale->text('Due'), },
962 'amount' => { 'text' => $locale->text('Amount'), },
963 'open' => { 'text' => $locale->text('Open'), },
966 my %column_alignment = ('statement' => 'center',
967 map { $_ => 'right' } qw(open amount));
969 $report->set_options('std_column_visibility' => 1);
970 $report->set_columns(%column_defs);
971 $report->set_column_order(@columns);
973 my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype);
974 $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
977 my $attachment_basename;
979 if ($form->{department}) {
980 my ($department) = split /--/, $form->{department};
981 push @options, $locale->text('Department') . " : $department";
982 $form->{callback} .= "&department=" . E($department);
985 if (($form->{arap} eq 'ar') && $form->{customer}) {
986 push @options, $form->{customer};
987 $attachment_basename = $locale->text('ar_aging_list');
988 $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
991 if (($form->{arap} eq 'ap') && $form->{vendor}) {
992 push @options, $form->{vendor};
993 $attachment_basename = $locale->text('ap_aging_list');
994 $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
997 if ($form->{fromdate}) {
998 push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1000 push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1003 $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1005 $report->set_options('top_info_text' => join("\n", @options),
1006 'output_format' => 'HTML',
1007 'title' => $form->{title},
1008 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1010 $report->set_options_from_form();
1011 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1013 my $previous_ctid = 0;
1015 my @periods = qw(open amount);
1016 my %subtotals = map { $_ => 0 } @periods;
1017 my %totals = map { $_ => 0 } @periods;
1019 foreach my $ref (@{ $form->{AG} }) {
1020 if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1021 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1024 foreach my $key (@periods) {
1025 $subtotals{$key} += $ref->{"$key"};
1026 $totals{$key} += $ref->{"$key"};
1027 $ref->{"$key"} = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
1032 foreach my $column (@columns) {
1034 'data' => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1035 'align' => $column_alignment{$column},
1036 'valign' => $column eq 'statement' ? 'center' : '',
1040 $row->{invnumber}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1042 if ($previous_ctid != $ref->{ctid}) {
1043 $row->{statement}->{raw_data} =
1044 $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
1045 . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1046 $row->{ct}->{data} = $ref->{name};
1051 $previous_ctid = $ref->{ctid};
1053 $report->add_data($row);
1056 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1058 $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1060 if ($form->{arap} eq 'ar') {
1061 my $raw_top_info_text = $form->parse_html_template('rp/aging_ar_top');
1062 my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1063 'PRINT_OPTIONS' => print_options(inline => 1), });
1064 $report->set_options('raw_top_info_text' => $raw_top_info_text,
1065 'raw_bottom_info_text' => $raw_bottom_info_text);
1068 $report->generate_with_headers();
1070 $main::lxdebug->leave_sub();
1074 $main::lxdebug->enter_sub();
1076 my $form = $main::form;
1077 my %myconfig = %main::myconfig;
1078 my $locale = $main::locale;
1080 RP->aging(\%myconfig, \%$form);
1082 map { $_->{checked} = "checked" } @{ $form->{AG} };
1086 $main::lxdebug->leave_sub();
1090 $::lxdebug->enter_sub;
1091 $::auth->assert('general_ledger');
1093 # get name and email addresses
1095 for my $i (1 .. $::form->{rowcount}) {
1096 next unless $::form->{"statement_$i"};
1097 $::form->{"$::form->{ct}_id"} = $::form->{"$::form->{ct}_id_$i"};
1098 RP->get_customer(\%::myconfig, $::form);
1103 $::form->error($::locale->text('Nothing selected!')) unless $selected;
1105 $::form->{media} = "email";
1107 # save all other variables
1109 for my $key (keys %$::form) {
1110 next if any { $key eq $_ } qw(login password action email cc bcc subject message type sendmode format header);
1111 next unless '' eq ref $::form->{$key};
1112 push @hidden_values, $key;
1116 print $::form->parse_html_template('rp/e_mail', {
1117 show_bcc => $::auth->assert('email_bcc', 'may fail'),
1118 print_options => print_options(inline => 1),
1119 hidden_values => \@hidden_values,
1122 $::lxdebug->leave_sub;
1126 $main::lxdebug->enter_sub();
1128 $main::auth->assert('general_ledger');
1130 my $form = $main::form;
1131 my %myconfig = %main::myconfig;
1132 my $locale = $main::locale;
1134 $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1135 unless $form->{subject};
1137 RP->aging(\%myconfig, \%$form);
1139 $form->{"statement_1"} = 1;
1141 $form->{media} = 'email';
1144 $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1146 $main::lxdebug->leave_sub();
1150 $main::lxdebug->enter_sub();
1152 $main::auth->assert('general_ledger');
1154 my $form = $main::form;
1155 my %myconfig = %main::myconfig;
1156 my $locale = $main::locale;
1158 if ($form->{media} eq 'printer') {
1159 $form->error($locale->text('Select postscript or PDF!'))
1160 if ($form->{format} !~ /(postscript|pdf)/);
1164 for my $i (1 .. $form->{rowcount}) {
1165 if ($form->{"statement_$i"}) {
1166 $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1172 $form->error($locale->text('Nothing selected!')) unless $selected;
1174 if ($form->{media} eq 'printer') {
1175 $form->{"$form->{ct}_id"} = "";
1177 $form->{"statement_1"} = 1;
1180 RP->aging(\%myconfig, \%$form);
1184 $form->redirect($locale->text('Statements sent to printer!'))
1185 if ($form->{media} eq 'printer');
1187 $main::lxdebug->leave_sub();
1191 $main::lxdebug->enter_sub();
1193 $main::auth->assert('general_ledger');
1195 my $form = $main::form;
1196 my %myconfig = %main::myconfig;
1197 my $locale = $main::locale;
1199 my $defaults = SL::DB::Default->get;
1200 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
1201 $form->{templates} = $defaults->templates;
1203 $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1205 my $suffix = "html";
1206 my $attachment_suffix = "html";
1207 if ($form->{format} eq 'postscript') {
1208 $form->{postscript} = 1;
1210 $attachment_suffix = "ps";
1211 } elsif ($form->{format} eq 'pdf') {
1214 $attachment_suffix = "pdf";
1217 $form->{IN} = "$form->{type}.$suffix";
1218 $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1220 # Save $form->{email} because it will be overwritten.
1221 $form->{EMAIL_RECIPIENT} = $form->{email};
1225 while (@{ $form->{AG} }) {
1227 my $ref = shift @{ $form->{AG} };
1229 if ($ctid != $ref->{ctid}) {
1231 $ctid = $ref->{ctid};
1234 if ($form->{"statement_$i"}) {
1237 ("name", "street", "zipcode", "city", "country", "contact", "email",
1238 "$form->{ct}phone", "$form->{ct}fax");
1239 map { $form->{$_} = $ref->{$_} } @a;
1241 $form->{ $form->{ct} } = $form->{name};
1242 $form->{"$form->{ct}_id"} = $ref->{ctid};
1244 map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
1246 foreach my $item (qw(c0 c30 c60 c90)) {
1247 $form->{$item} = ();
1248 $form->{"${item}total"} = 0;
1251 &statement_details($ref);
1255 if (scalar(@{ $form->{AG} }) > 0) {
1257 # one or more left to go
1258 if ($ctid == $form->{AG}->[0]->{ctid}) {
1259 $ref = shift @{ $form->{AG} };
1260 &statement_details($ref);
1263 $ref = scalar(@{ $form->{AG} });
1269 # set initial ref to 0
1276 $form->{"${_}total"} =
1277 $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1278 } ('c0', 'c30', 'c60', 'c90', "");
1280 $form->{attachment_filename} = $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
1281 $form->{attachment_filename} =~ s/\s+/_/g;
1283 $form->parse_template(\%myconfig);
1288 # saving the history
1289 if(!exists $form->{addition} && $form->{id} ne "") {
1290 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1291 $form->{addition} = "PRINTED";
1292 $form->{what_done} = $form->{type};
1293 $form->save_history;
1295 # /saving the history
1296 $main::lxdebug->leave_sub();
1299 sub statement_details {
1300 $main::lxdebug->enter_sub();
1302 $main::auth->assert('general_ledger');
1304 my $form = $main::form;
1305 my %myconfig = %main::myconfig;
1306 my $locale = $main::locale;
1310 push @{ $form->{invnumber} }, $ref->{invnumber};
1311 push @{ $form->{invdate} }, $ref->{transdate};
1312 push @{ $form->{duedate} }, $ref->{duedate};
1313 push @{ $form->{amount} }, $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
1314 push @{ $form->{open} }, $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
1316 foreach my $item (qw(c0 c30 c60 c90)) {
1317 if ($ref->{exchangerate} * 1) {
1318 # add only the open amount of the invoice to the aging, not the total amount
1319 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
1320 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
1321 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
1322 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
1324 $form->{"${item}total"} += $ref->{$item};
1325 $form->{total} += $ref->{$item};
1326 push @{ $form->{$item} },
1327 $form->format_amount(\%myconfig, $ref->{$item}, 2);
1330 $main::lxdebug->leave_sub();
1333 sub generate_tax_report {
1334 $::lxdebug->enter_sub;
1335 $::auth->assert('report');
1337 RP->tax_report(\%::myconfig, $::form);
1339 my $descvar = "$::form->{accno}_description";
1340 my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
1344 my $callback = build_std_url('action=generate_tax_report', $descvar,
1345 qw(fromdate todate db method accno department report title));
1347 my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1350 for my $item (@columns, 'subtotal') {
1351 if ($::form->{"l_$item"} eq "Y") {
1352 $callback .= "&l_$item=Y";
1353 $href .= "&l_$item=Y";
1357 for my $item (@columns) {
1358 if ($::form->{"l_$item"} eq "Y") {
1359 push @column_index, $item;
1364 if ($::form->{department}) {
1365 my ($department) = split /--/, $::form->{department};
1366 push @options, $::locale->text('Department') . " : $department";
1369 # if there are any dates
1370 if ($::form->{fromdate} || $::form->{todate}) {
1371 my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
1372 my $todate = $::form->{todate} ? $::locale->date(\%::myconfig, $::form->{todate}, 1) : '';
1373 push @options, "$fromdate - $todate";
1375 push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
1378 my ($name, $invoice, $arap);
1379 if ($::form->{db} eq 'ar') {
1380 $name = $::locale->text('Customer');
1384 if ($::form->{db} eq 'ap') {
1385 $name = $::locale->text('Vendor');
1390 my %column_header = (
1391 id => $::locale->text('ID'),
1392 invnumber => $::locale->text('Invoice'),
1393 transdate => $::locale->text('Date'),
1394 netamount => $::locale->text('Amount'),
1395 tax => $::locale->text('Tax'),
1396 amount => $::locale->text('Total'),
1400 my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
1402 $callback .= "&sort=$::form->{sort}";
1405 if (@{ $::form->{TR} }) {
1406 $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
1409 my ($totalnetamount, $totaltax, @data);
1410 for my $ref (@{ $::form->{TR} }) {
1412 my $module = ($ref->{invoice}) ? $invoice : $arap;
1414 if ($::form->{l_subtotal} eq 'Y') {
1415 if ($sameitem ne $ref->{ $::form->{sort} }) {
1418 netamount => $subtotalnetamount,
1419 tax => $subtotaltax,
1420 amount => $subtotal,
1422 $subtotalnetamount = 0;
1424 $sameitem = $ref->{ $::form->{sort} };
1428 $subtotalnetamount += $ref->{netamount};
1429 $subtotaltax += $ref->{tax};
1430 $totalnetamount += $ref->{netamount};
1431 $totaltax += $ref->{tax};
1432 $ref->{amount} = $ref->{netamount} + $ref->{tax};
1434 push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
1435 $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
1436 $data[-1]{$_}{numeric} = 1 for qw(netamount tax amount);
1439 if ($::form->{l_subtotal} eq 'Y') {
1442 netamount => $subtotalnetamount,
1443 tax => $subtotaltax,
1444 amount => $subtotal,
1450 netamount => $totalnetamount,
1452 amount => $totalnetamount + $totaltax,
1456 print $::form->parse_html_template('rp/tax_report', {
1457 column_index => \@column_index,
1458 column_header => \%column_header,
1459 column_sorted => \%column_sorted,
1462 options => \@options,
1465 $::lxdebug->leave_sub;
1469 $main::lxdebug->enter_sub();
1471 $main::auth->assert('cash');
1473 my $form = $main::form;
1474 my %myconfig = %main::myconfig;
1475 my $locale = $main::locale;
1477 if ($form->{account}) {
1478 ($form->{paymentaccounts}) = split /--/, $form->{account};
1482 if ($form->{department}) {
1483 (my $department, $form->{department_id}) = split /--/, $form->{department};
1484 $option = $locale->text('Department') . " : $department";
1487 report_generator_set_default_sort('transdate', 1);
1489 RP->payments(\%myconfig, \%$form);
1491 my @hidden_variables = qw(account title department reference source memo fromdate todate
1492 fx_transaction db prepayment paymentaccounts sort);
1494 my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
1495 $form->{callback} = $href;
1497 my @columns = qw(transdate invnumber name paid source memo);
1499 'name' => { 'text' => $locale->text('Description'), },
1500 'invnumber' => { 'text' => $locale->text('Reference'), },
1501 'transdate' => { 'text' => $locale->text('Date'), },
1502 'paid' => { 'text' => $locale->text('Amount'), },
1503 'source' => { 'text' => $locale->text('Source'), },
1504 'memo' => { 'text' => $locale->text('Memo'), },
1506 my %column_alignment = ('paid' => 'right');
1508 foreach my $name (grep { $_ ne 'paid' } @columns) {
1509 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1510 $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
1514 if ($form->{fromdate}) {
1515 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
1517 if ($form->{todate}) {
1518 push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1521 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1523 my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
1525 $report->set_options('top_info_text' => join("\n", @options),
1526 'output_format' => 'HTML',
1527 'title' => $form->{title},
1528 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1529 'std_column_visibility' => 1,
1531 $report->set_options_from_form();
1533 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1535 $report->set_columns(%column_defs);
1536 $report->set_column_order(@columns);
1538 $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
1540 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1544 foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1545 next unless @{ $form->{ $ref->{id} } };
1547 $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
1549 my $subtotal_paid = 0;
1551 foreach my $payment (@{ $form->{ $ref->{id} } }) {
1552 my $module = $payment->{module};
1553 $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1554 $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1556 $subtotal_paid += $payment->{paid};
1557 $total_paid += $payment->{paid};
1559 $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
1563 foreach my $column (@columns) {
1565 'data' => $payment->{$column},
1566 'align' => $column_alignment{$column},
1570 $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
1572 $report->add_data($row);
1575 my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
1577 'data' => $form->format_amount(\%myconfig, $subtotal_paid, 2),
1579 'class' => 'listsubtotal',
1582 $report->add_data($row);
1585 $report->add_separator();
1587 my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
1589 'data' => $form->format_amount(\%myconfig, $total_paid, 2),
1591 'class' => 'listtotal',
1594 $report->add_data($row);
1596 $report->generate_with_headers();
1598 $main::lxdebug->leave_sub();
1602 $::lxdebug->enter_sub;
1604 my ($dont_print) = @_;
1606 $::form->{sendmode} = "attachment";
1607 $::form->{format} ||= $::myconfig{template_format} || "pdf";
1608 $::form->{copies} ||= $::myconfig{copies} || 2;
1610 $::form->{PD}{ $::form->{type} } = "selected";
1611 $::form->{DF}{ $::form->{format} } = "selected";
1612 $::form->{OP}{ $::form->{media} } = "selected";
1613 $::form->{SM}{ $::form->{sendmode} } = "selected";
1615 my $output = $::form->parse_html_template('rp/print_options', {
1616 got_printer => $::myconfig{printer},
1617 show_latex => $::lx_office_conf{print_templates}->{latex},
1618 is_email => $::form->{media} eq 'email',
1621 print $output unless $dont_print;
1623 $::lxdebug->leave_sub;
1629 $main::lxdebug->enter_sub();
1631 $main::auth->assert('report');
1633 my $form = $main::form;
1634 my %myconfig = %main::myconfig;
1635 my $locale = $main::locale;
1637 my $defaults = SL::DB::Default->get;
1638 $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
1639 $form->{templates} = $defaults->templates;
1641 $form->{padding} = " ";
1642 $form->{bold} = "<b>";
1643 $form->{endbold} = "</b>";
1644 $form->{br} = "<br>";
1646 if ($form->{reporttype} eq "custom") {
1648 #forgotten the year --> thisyear
1649 if ($form->{year} !~ m/^\d\d\d\d$/) {
1650 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1656 if ($form->{duetyp} eq "13") {
1657 $form->{fromdate} = "1.1.$form->{year}";
1658 $form->{todate} = "31.12.$form->{year}";
1659 $form->{comparefromdate} = "1.01.$form->{year}";
1660 $form->{comparetodate} = "31.12.$form->{year}";
1664 if ($form->{duetyp} eq "A") {
1665 $form->{fromdate} = "1.1.$form->{year}";
1666 $form->{todate} = "31.3.$form->{year}";
1667 $form->{comparefromdate} = "1.01.$form->{year}";
1668 $form->{comparetodate} = "31.03.$form->{year}";
1670 if ($form->{duetyp} eq "B") {
1671 $form->{fromdate} = "1.4.$form->{year}";
1672 $form->{todate} = "30.6.$form->{year}";
1673 $form->{comparefromdate} = "1.01.$form->{year}";
1674 $form->{comparetodate} = "30.06.$form->{year}";
1676 if ($form->{duetyp} eq "C") {
1677 $form->{fromdate} = "1.7.$form->{year}";
1678 $form->{todate} = "30.9.$form->{year}";
1679 $form->{comparefromdate} = "1.01.$form->{year}";
1680 $form->{comparetodate} = "30.09.$form->{year}";
1682 if ($form->{duetyp} eq "D") {
1683 $form->{fromdate} = "1.10.$form->{year}";
1684 $form->{todate} = "31.12.$form->{year}";
1685 $form->{comparefromdate} = "1.01.$form->{year}";
1686 $form->{comparetodate} = "31.12.$form->{year}";
1691 $form->{duetyp} eq "1" && do {
1692 $form->{fromdate} = "1.1.$form->{year}";
1693 $form->{todate} = "31.1.$form->{year}";
1694 $form->{comparefromdate} = "1.01.$form->{year}";
1695 $form->{comparetodate} = "31.01.$form->{year}";
1698 $form->{duetyp} eq "2" && do {
1699 $form->{fromdate} = "1.2.$form->{year}";
1701 #this works from 1901 to 2099, 1900 and 2100 fail.
1702 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1703 $form->{todate} = "$leap.2.$form->{year}";
1704 $form->{comparefromdate} = "1.01.$form->{year}";
1705 $form->{comparetodate} = "$leap.02.$form->{year}";
1708 $form->{duetyp} eq "3" && do {
1709 $form->{fromdate} = "1.3.$form->{year}";
1710 $form->{todate} = "31.3.$form->{year}";
1711 $form->{comparefromdate} = "1.01.$form->{year}";
1712 $form->{comparetodate} = "31.03.$form->{year}";
1715 $form->{duetyp} eq "4" && do {
1716 $form->{fromdate} = "1.4.$form->{year}";
1717 $form->{todate} = "30.4.$form->{year}";
1718 $form->{comparefromdate} = "1.01.$form->{year}";
1719 $form->{comparetodate} = "30.04.$form->{year}";
1722 $form->{duetyp} eq "5" && do {
1723 $form->{fromdate} = "1.5.$form->{year}";
1724 $form->{todate} = "31.5.$form->{year}";
1725 $form->{comparefromdate} = "1.01.$form->{year}";
1726 $form->{comparetodate} = "31.05.$form->{year}";
1729 $form->{duetyp} eq "6" && do {
1730 $form->{fromdate} = "1.6.$form->{year}";
1731 $form->{todate} = "30.6.$form->{year}";
1732 $form->{comparefromdate} = "1.01.$form->{year}";
1733 $form->{comparetodate} = "30.06.$form->{year}";
1736 $form->{duetyp} eq "7" && do {
1737 $form->{fromdate} = "1.7.$form->{year}";
1738 $form->{todate} = "31.7.$form->{year}";
1739 $form->{comparefromdate} = "1.01.$form->{year}";
1740 $form->{comparetodate} = "31.07.$form->{year}";
1743 $form->{duetyp} eq "8" && do {
1744 $form->{fromdate} = "1.8.$form->{year}";
1745 $form->{todate} = "31.8.$form->{year}";
1746 $form->{comparefromdate} = "1.01.$form->{year}";
1747 $form->{comparetodate} = "31.08.$form->{year}";
1750 $form->{duetyp} eq "9" && do {
1751 $form->{fromdate} = "1.9.$form->{year}";
1752 $form->{todate} = "30.9.$form->{year}";
1753 $form->{comparefromdate} = "1.01.$form->{year}";
1754 $form->{comparetodate} = "30.09.$form->{year}";
1757 $form->{duetyp} eq "10" && do {
1758 $form->{fromdate} = "1.10.$form->{year}";
1759 $form->{todate} = "31.10.$form->{year}";
1760 $form->{comparefromdate} = "1.01.$form->{year}";
1761 $form->{comparetodate} = "31.10.$form->{year}";
1764 $form->{duetyp} eq "11" && do {
1765 $form->{fromdate} = "1.11.$form->{year}";
1766 $form->{todate} = "30.11.$form->{year}";
1767 $form->{comparefromdate} = "1.01.$form->{year}";
1768 $form->{comparetodate} = "30.11.$form->{year}";
1771 $form->{duetyp} eq "12" && do {
1772 $form->{fromdate} = "1.12.$form->{year}";
1773 $form->{todate} = "31.12.$form->{year}";
1774 $form->{comparefromdate} = "1.01.$form->{year}";
1775 $form->{comparetodate} = "31.12.$form->{year}";
1779 hotfix_reformat_date();
1781 # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
1782 # ansonsten ist die prüfung in RP.pm
1783 # if (defined ($form->{fromdate|todate}=='..'))
1785 if ($form->{fromdate}){
1786 my ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
1787 my $datetime = $locale->parse_date_to_object(\%myconfig, $form->{fromdate});
1788 $datetime->set( month => 1,
1790 $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
1792 if ($form->{todate}){
1793 $form->{comparetodate} = $form->{todate};
1797 RP->bwa(\%myconfig, \%$form);
1799 ($form->{department}) = split /--/, $form->{department};
1802 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1803 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1805 # if there are any dates construct a where
1806 if ($form->{fromdate} || $form->{todate}) {
1808 unless ($form->{todate}) {
1809 $form->{todate} = $form->current_date(\%myconfig);
1812 my %germandate = ("dateformat" => "dd.mm.yyyy");
1814 my $longtodate = $locale->date(\%germandate, $form->{todate}, 1);
1815 my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
1817 my $longfromdate = $locale->date(\%germandate, $form->{fromdate}, 1);
1818 my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
1820 $form->{this_period} = "$shortfromdate\n$shorttodate";
1822 $locale->text('for Period')
1823 . qq|\n$longfromdate |
1824 . $locale->text('bis')
1828 $form->{IN} = "bwa.html";
1830 $form->parse_template;
1832 $main::lxdebug->leave_sub();
1835 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
1836 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
1837 sub hotfix_reformat_date {
1839 $main::lxdebug->enter_sub();
1841 my $form = $main::form;
1842 my %myconfig = %main::myconfig;
1843 my $locale = $main::locale;
1845 if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
1846 my $current_dateformat = $myconfig{dateformat};
1847 $myconfig{dateformat} = 'dd.mm.yyyy';
1848 $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
1849 $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
1850 $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
1851 unless (!defined ($form->{comparefromdate}));
1852 $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
1853 unless (!defined ($form->{comparetodate}));
1855 # Und wieder zurücksetzen
1856 $myconfig{dateformat} = $current_dateformat; #'dd.mm.yyyy';
1857 } # Ende Hotifx Bug 1388
1859 $main::lxdebug->leave_sub();