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);
43 use SL::ReportGenerator;
46 require "bin/mozilla/arap.pl";
47 require "bin/mozilla/common.pl";
48 require "bin/mozilla/reportgenerator.pl";
50 # note: this file was particularly hard to strictify.
51 # alot of the vars are passed carelessly between invocations
52 # should there be any missing vars, declare them globally
55 # this is for our long dates
56 # $locale->text('January')
57 # $locale->text('February')
58 # $locale->text('March')
59 # $locale->text('April')
60 # $locale->text('May ')
61 # $locale->text('June')
62 # $locale->text('July')
63 # $locale->text('August')
64 # $locale->text('September')
65 # $locale->text('October')
66 # $locale->text('November')
67 # $locale->text('December')
69 # this is for our short month
70 # $locale->text('Jan')
71 # $locale->text('Feb')
72 # $locale->text('Mar')
73 # $locale->text('Apr')
74 # $locale->text('May')
75 # $locale->text('Jun')
76 # $locale->text('Jul')
77 # $locale->text('Aug')
78 # $locale->text('Sep')
79 # $locale->text('Oct')
80 # $locale->text('Nov')
81 # $locale->text('Dec')
83 # $locale->text('Balance Sheet')
84 # $locale->text('Income Statement')
85 # $locale->text('Trial Balance')
86 # $locale->text('AR Aging')
87 # $locale->text('AP Aging')
88 # $locale->text('Search AR Aging')
89 # $locale->text('Search AP Aging')
90 # $locale->text('Tax collected')
91 # $locale->text('Tax paid')
92 # $locale->text('Receipts')
93 # $locale->text('Payments')
94 # $locale->text('Project Transactions')
95 # $locale->text('Non-taxable Sales')
96 # $locale->text('Non-taxable Purchases')
97 # $locale->text('Business evaluation')
99 # $form->parse_html_template('rp/html_report_susa')
101 my $rp_access_map = {
102 'projects' => 'report',
103 'ar_aging' => 'general_ledger',
104 'ap_aging' => 'general_ledger',
105 'receipts' => 'cash',
106 'payments' => 'cash',
107 'trial_balance' => 'report',
108 'income_statement' => 'report',
110 'balance_sheet' => 'report',
113 sub check_rp_access {
114 my $form = $main::form;
116 my $right = $rp_access_map->{$form->{report}};
117 $right ||= 'DOES_NOT_EXIST';
119 $main::auth->assert($right);
123 $::lxdebug->enter_sub;
128 balance_sheet => $::locale->text('Balance Sheet'),
129 income_statement => $::locale->text('Income Statement'),
130 trial_balance => $::locale->text('Trial Balance'),
131 ar_aging => $::locale->text('Search AR Aging'),
132 ap_aging => $::locale->text('Search AP Aging'),
133 tax_collected => $::locale->text('Tax collected'),
134 tax_paid => $::locale->text('Tax paid'),
135 nontaxable_sales => $::locale->text('Non-taxable Sales'),
136 nontaxable_purchases => $::locale->text('Non-taxable Purchases'),
137 receipts => $::locale->text('Receipts'),
138 payments => $::locale->text('Payments'),
139 projects => $::locale->text('Project Transactions'),
140 bwa => $::locale->text('Business evaluation'),
143 $::form->{title} = $title{$::form->{report}};
146 $::form->all_departments(\%::myconfig);
147 if (@{ $::form->{all_departments} || [] }) {
148 $::form->{selectdepartment} = "<option>\n";
149 map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
152 $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
154 my $onload = qq|focus()|;
156 my $is_projects = $::form->{report} eq "projects";
157 my $is_income_statement = $::form->{report} eq "income_statement";
158 my $is_bwa = $::form->{report} eq "bwa";
159 my $is_balance_sheet = $::form->{report} eq "balance_sheet";
160 my $is_trial_balance = $::form->{report} eq "trial_balance";
161 my $is_taxreport = $::form->{report} =~ /^tax_/;
162 my $is_nontaxable = $::form->{report} =~ /^nontaxable_/;
163 my $is_aging = $::form->{report} =~ /^a[rp]_aging$/;
164 my $is_payments = $::form->{report} =~ /(receipts|payments)$/;
166 # if (is_taxreport) {
167 # $::form->{db} = ($::form->{report} =~ /_collected/) ? "ar" : "ap";
168 # RP->get_taxaccounts(\%::myconfig, $::form);
171 # if ($is_nontaxable) {
172 # $::form->{db} = ($::form->{report} =~ /_sales/) ? "ar" : "ap";
175 my ($label, $nextsub, $vc);
177 my $is_sales = $::form->{report} eq 'ar_aging';
178 $label = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
179 $::form->{vc} = $is_sales ? 'customer' : 'vendor';
181 $nextsub = "generate_$::form->{report}";
184 $::form->all_vc(\%::myconfig, $::form->{vc}, $is_sales ? "AR" : "AP");
185 $vc .= "<option>$_->{name}--$_->{id}\n" for @{ $::form->{"all_$::form->{vc}"} };
187 ? qq|<select name=$::form->{vc}><option>\n$vc</select>|
188 : qq|<input name=$::form->{vc} size=35>|;
191 my ($selection, $paymentaccounts);
193 $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
195 RP->paymentaccounts(\%::myconfig, $::form);
197 $selection = "<option>\n";
198 for my $ref (@{ $::form->{PR} }) {
199 $paymentaccounts .= "$ref->{accno} ";
200 $selection .= "<option>$ref->{accno}--$ref->{description}\n";
205 print $::form->parse_html_template('rp/report', {
206 paymentaccounts => $paymentaccounts,
207 selection => $selection,
208 is_aging => $is_aging,
211 year => DateTime->today->year,
214 accrual => $::instance_conf->get_accounting_method ne 'cash',
215 cash => $::instance_conf->get_accounting_method eq 'cash',
216 is_payments => $is_payments,
217 is_trial_balance => $is_trial_balance,
218 is_balance_sheet => $is_balance_sheet,
220 is_income_statement => $is_income_statement,
221 is_projects => $is_projects,
224 $::lxdebug->leave_sub;
227 sub continue { call_sub($main::form->{"nextsub"}); }
230 $main::lxdebug->enter_sub();
232 $main::auth->assert('report');
234 my $form = $main::form;
235 my %myconfig = %main::myconfig;
236 my $locale = $main::locale;
240 $form->{project_id} = $form->{project_id_1};
241 if ($form->{projectnumber} && !$form->{project_id}) {
242 $form->{rowcount} = 1;
244 # call this instead of update
245 $form->{update} = $nextsub;
246 $form->{projectnumber_1} = $form->{projectnumber};
248 delete $form->{sort};
249 check_project('generate_projects');
251 # if there is one only, assign id
252 $form->{project_id} = $form->{project_id_1};
255 $main::lxdebug->leave_sub();
258 sub generate_income_statement {
259 $main::lxdebug->enter_sub();
261 $main::auth->assert('report');
263 my $form = $main::form;
264 my %myconfig = %main::myconfig;
265 my $locale = $main::locale;
267 $form->{padding} = " ";
268 $form->{bold} = "<b>";
269 $form->{endbold} = "</b>";
270 $form->{br} = "<br>";
272 if ($form->{reporttype} eq "custom") {
274 #forgotten the year --> thisyear
275 if ($form->{year} !~ m/^\d\d\d\d$/) {
276 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
282 if ($form->{duetyp} eq "13") {
283 $form->{fromdate} = "1.1.$form->{year}";
284 $form->{todate} = "31.12.$form->{year}";
288 if ($form->{duetyp} eq "A") {
289 $form->{fromdate} = "1.1.$form->{year}";
290 $form->{todate} = "31.3.$form->{year}";
292 if ($form->{duetyp} eq "B") {
293 $form->{fromdate} = "1.4.$form->{year}";
294 $form->{todate} = "30.6.$form->{year}";
296 if ($form->{duetyp} eq "C") {
297 $form->{fromdate} = "1.7.$form->{year}";
298 $form->{todate} = "30.9.$form->{year}";
300 if ($form->{duetyp} eq "D") {
301 $form->{fromdate} = "1.10.$form->{year}";
302 $form->{todate} = "31.12.$form->{year}";
307 $form->{duetyp} eq "1" && do {
308 $form->{fromdate} = "1.1.$form->{year}";
309 $form->{todate} = "31.1.$form->{year}";
312 $form->{duetyp} eq "2" && do {
313 $form->{fromdate} = "1.2.$form->{year}";
315 #this works from 1901 to 2099, 1900 and 2100 fail.
316 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
317 $form->{todate} = "$leap.2.$form->{year}";
320 $form->{duetyp} eq "3" && do {
321 $form->{fromdate} = "1.3.$form->{year}";
322 $form->{todate} = "31.3.$form->{year}";
325 $form->{duetyp} eq "4" && do {
326 $form->{fromdate} = "1.4.$form->{year}";
327 $form->{todate} = "30.4.$form->{year}";
330 $form->{duetyp} eq "5" && do {
331 $form->{fromdate} = "1.5.$form->{year}";
332 $form->{todate} = "31.5.$form->{year}";
335 $form->{duetyp} eq "6" && do {
336 $form->{fromdate} = "1.6.$form->{year}";
337 $form->{todate} = "30.6.$form->{year}";
340 $form->{duetyp} eq "7" && do {
341 $form->{fromdate} = "1.7.$form->{year}";
342 $form->{todate} = "31.7.$form->{year}";
345 $form->{duetyp} eq "8" && do {
346 $form->{fromdate} = "1.8.$form->{year}";
347 $form->{todate} = "31.8.$form->{year}";
350 $form->{duetyp} eq "9" && do {
351 $form->{fromdate} = "1.9.$form->{year}";
352 $form->{todate} = "30.9.$form->{year}";
355 $form->{duetyp} eq "10" && do {
356 $form->{fromdate} = "1.10.$form->{year}";
357 $form->{todate} = "31.10.$form->{year}";
360 $form->{duetyp} eq "11" && do {
361 $form->{fromdate} = "1.11.$form->{year}";
362 $form->{todate} = "30.11.$form->{year}";
365 $form->{duetyp} eq "12" && do {
366 $form->{fromdate} = "1.12.$form->{year}";
367 $form->{todate} = "31.12.$form->{year}";
371 hotfix_reformat_date();
372 } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
374 RP->income_statement(\%myconfig, \%$form);
376 ($form->{department}) = split /--/, $form->{department};
379 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
380 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
382 # if there are any dates construct a where
383 if ($form->{fromdate} || $form->{todate}) {
385 unless ($form->{todate}) {
386 $form->{todate} = $form->current_date(\%myconfig);
389 my $longtodate = $locale->date(\%myconfig, $form->{todate}, 1);
390 my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
392 my $longfromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
393 my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
395 $form->{this_period} = "$shortfromdate\n$shorttodate";
397 $locale->text('for Period')
398 . qq|\n$longfromdate |
399 . $locale->text('Bis')
403 if ($form->{comparefromdate} || $form->{comparetodate}) {
404 my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
405 my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
407 my $longcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 1);
408 my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
410 $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
412 "\n$longcomparefromdate "
413 . $locale->text('Bis')
414 . qq| $longcomparetodate|;
417 # setup variables for the form
418 my @a = qw(company address businessnumber);
419 map { $form->{$_} = $myconfig{$_} } @a;
421 $form->{templates} = $myconfig{templates};
423 $form->{IN} = "income_statement.html";
425 $form->parse_template;
427 $main::lxdebug->leave_sub();
430 sub generate_balance_sheet {
431 $::lxdebug->enter_sub;
432 $::auth->assert('report');
434 $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
435 $::form->{padding} = " ";
436 $::form->{bold} = "<b>";
437 $::form->{endbold} = "</b>";
438 $::form->{br} = "<br>";
440 my $data = RP->balance_sheet(\%::myconfig, $::form);
442 $::form->{asofdate} ||= $::form->current_date;
443 $::form->{period} = $::locale->date(\%::myconfig, $::form->current_date, 1);
445 ($::form->{department}) = split /--/, $::form->{department};
447 # define Current Earnings account
448 my $padding = $::form->{l_heading} ? $::form->{padding} : "";
449 push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
451 $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
452 $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
454 # $::form->{IN} = "balance_sheet.html";
456 # setup company variables for the form
457 map { $::form->{$_} = $::myconfig{$_} } qw(company address businessnumber nativecurr);
459 $::form->{templates} = $::myconfig{templates};
462 print $::form->parse_html_template('rp/balance_sheet', $data);
464 $::lxdebug->leave_sub;
467 sub generate_projects {
468 $main::lxdebug->enter_sub();
470 $main::auth->assert('report');
472 my $form = $main::form;
473 my %myconfig = %main::myconfig;
474 my $locale = $main::locale;
476 &get_project("generate_projects");
477 $form->{projectnumber} = $form->{projectnumber_1};
479 $form->{nextsub} = "generate_projects";
480 $form->{title} = $locale->text('Project Transactions');
481 RP->trial_balance(\%myconfig, \%$form);
483 list_accounts('generate_projects');
485 $main::lxdebug->leave_sub();
491 # included links to display transactions for period entered
492 # added headers and subtotals
494 sub generate_trial_balance {
495 $main::lxdebug->enter_sub();
497 $main::auth->assert('report');
499 my $form = $main::form;
500 my %myconfig = %main::myconfig;
501 my $locale = $main::locale;
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 cash);
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') . " " . $myconfig{company};
664 push (@options, $form->{company});
667 $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
669 $report->set_options('output_format' => 'HTML',
670 'top_info_text' => join("\n", @options),
671 'title' => $form->{title},
672 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
673 'html_template' => 'rp/html_report_susa',
674 'pdf_template' => 'rp/html_report_susa',
676 $report->set_options_from_form();
677 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
679 # add sort and escape callback, this one we use for the add sub
680 $form->{callback} = $href .= "&sort=$form->{sort}";
682 # escape callback for href
683 my $callback = $form->escape($href);
685 my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
687 my %totals = map { $_ => 0 } @subtotal_columns;
689 my $edit_url = build_std_url('action=edit', 'type', 'vc');
692 foreach my $accno (@{ $form->{TB} || [] }) {
694 $accno->{soll} = $accno->{debit};
695 $accno->{haben} = $accno->{credit};
696 map { $totals{$_} += $accno->{$_} } @subtotal_columns;
698 map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
699 qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
703 foreach my $column (@columns) {
705 'data' => $accno->{$column},
706 'align' => $column_alignment{$column},
710 $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}));
712 my $row_set = [ $row ];
715 $report->add_data($row_set);
720 $report->add_separator();
722 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
724 $report->generate_with_headers();
726 $main::lxdebug->leave_sub();
730 sub create_subtotal_row {
731 $main::lxdebug->enter_sub();
733 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
735 my $form = $main::form;
736 my %myconfig = %main::myconfig;
737 my $locale = $main::locale;
739 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
741 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
743 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
745 map { $totals->{$_} = 0 } @{ $subtotal_columns };
747 $main::lxdebug->leave_sub();
752 sub create_list_accounts_subtotal_row {
753 $main::lxdebug->enter_sub();
755 my ($subtotals, $columns, $fields, $class) = @_;
757 my $form = $main::form;
758 my %myconfig = %main::myconfig;
759 my $locale = $main::locale;
761 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
763 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
765 $main::lxdebug->leave_sub();
771 $main::lxdebug->enter_sub();
775 my $form = $main::form;
776 my %myconfig = %main::myconfig;
777 my $locale = $main::locale;
780 if ($form->{department}) {
781 my ($department) = split /--/, $form->{department};
782 push @options, $locale->text('Department') . " : $department";
784 if ($form->{projectnumber}) {
785 push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
788 # if there are any dates
789 if ($form->{fromdate} || $form->{todate}) {
790 my ($fromdate, $todate);
792 if ($form->{fromdate}) {
793 $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
795 if ($form->{todate}) {
796 $todate = $locale->date(\%myconfig, $form->{todate}, 1);
799 push @options, "$fromdate - $todate";
802 push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
805 my @columns = qw(accno description begbalance debit credit endbalance);
807 'accno' => { 'text' => $locale->text('Account'), },
808 'description' => { 'text' => $locale->text('Description'), },
809 'debit' => { 'text' => $locale->text('Debit'), },
810 'credit' => { 'text' => $locale->text('Credit'), },
811 'begbalance' => { 'text' => $locale->text('Balance'), },
812 'endbalance' => { 'text' => $locale->text('Balance'), },
814 my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
816 my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
818 $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
820 my $report = SL::ReportGenerator->new(\%myconfig, $form);
822 $report->set_options('top_info_text' => join("\n", @options),
823 'output_format' => 'HTML',
824 'title' => $form->{title},
825 'attachment_basename' => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
826 'std_column_visibility' => 1,
828 $report->set_options_from_form();
829 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
831 $report->set_columns(%column_defs);
832 $report->set_column_order(@columns);
834 $report->set_export_options($action, @hidden_variables);
836 my @totals_columns = qw(credit debit begbalance endbalance);
837 my %subtotals = map { $_ => 0 } @totals_columns;
838 my %totals = map { $_ => 0 } @totals_columns;
839 my $found_heading = 0;
840 my @tb = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
842 # sort the whole thing by account numbers and display
843 foreach my $idx (0 .. scalar(@tb) - 1) {
845 my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
847 my $ml = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
849 my $row = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
851 if ($ref->{charttype} eq 'H') {
852 next unless ($form->{l_heading});
854 %subtotals = map { $_ => 0 } @totals_columns;
856 $row->{description}->{class} = 'listheading';
857 $row->{description}->{data} = $ref->{description};
859 $report->add_data($row);
864 foreach (qw(debit credit)) {
865 $subtotals{$_} += $ref->{$_};
866 $totals{$_} += $ref->{$_};
869 $subtotals{begbalance} += $ref->{balance} * $ml;
870 $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
872 map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
873 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
875 $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
876 $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
878 $report->add_data($row);
880 if ($form->{l_heading} && $found_heading &&
881 (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
882 $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
886 $report->add_separator();
888 $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
890 $report->generate_with_headers();
892 $main::lxdebug->leave_sub();
895 sub generate_ar_aging {
896 $main::lxdebug->enter_sub();
898 $main::auth->assert('general_ledger');
900 my $form = $main::form;
901 my %myconfig = %main::myconfig;
902 my $locale = $main::locale;
905 ($form->{customer}) = split(/--/, $form->{customer});
907 $form->{ct} = "customer";
908 $form->{arap} = "ar";
910 $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
912 RP->aging(\%myconfig, \%$form);
915 $main::lxdebug->leave_sub();
918 sub generate_ap_aging {
919 $main::lxdebug->enter_sub();
921 $main::auth->assert('general_ledger');
923 my $form = $main::form;
924 my %myconfig = %main::myconfig;
925 my $locale = $main::locale;
928 ($form->{vendor}) = split(/--/, $form->{vendor});
930 $form->{ct} = "vendor";
931 $form->{arap} = "ap";
933 $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
935 RP->aging(\%myconfig, \%$form);
938 $main::lxdebug->leave_sub();
941 sub create_aging_subtotal_row {
942 $main::lxdebug->enter_sub();
944 my ($subtotals, $columns, $periods, $class) = @_;
946 my $form = $main::form;
947 my %myconfig = %main::myconfig;
948 my $locale = $main::locale;
950 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
952 foreach (@{ $periods }) {
953 $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
954 $subtotals->{$_} = 0;
957 $main::lxdebug->leave_sub();
963 $main::lxdebug->enter_sub();
965 $main::auth->assert('general_ledger');
967 my $form = $main::form;
968 my %myconfig = %main::myconfig;
969 my $locale = $main::locale;
970 my $cgi = $::request->{cgi};
972 my $report = SL::ReportGenerator->new(\%myconfig, $form);
974 my @columns = qw(statement ct invnumber transdate duedate amount open);
977 'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
978 'ct' => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
979 'invnumber' => { 'text' => $locale->text('Invoice'), },
980 'transdate' => { 'text' => $locale->text('Date'), },
981 'duedate' => { 'text' => $locale->text('Due'), },
982 'amount' => { 'text' => $locale->text('Amount'), },
983 'open' => { 'text' => $locale->text('Open'), },
986 my %column_alignment = ('statement' => 'center',
987 map { $_ => 'right' } qw(open amount));
989 $report->set_options('std_column_visibility' => 1);
990 $report->set_columns(%column_defs);
991 $report->set_column_order(@columns);
993 my @hidden_variables = qw(todate customer vendor arap title ct);
994 $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
997 my $attachment_basename;
999 if ($form->{department}) {
1000 my ($department) = split /--/, $form->{department};
1001 push @options, $locale->text('Department') . " : $department";
1002 $form->{callback} .= "&department=" . E($department);
1005 if (($form->{arap} eq 'ar') && $form->{customer}) {
1006 push @options, $form->{customer};
1007 $attachment_basename = $locale->text('ar_aging_list');
1008 $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
1011 if (($form->{arap} eq 'ap') && $form->{vendor}) {
1012 push @options, $form->{vendor};
1013 $attachment_basename = $locale->text('ap_aging_list');
1014 $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
1017 if ($form->{fromdate}) {
1018 push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1020 push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1023 $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
1025 $report->set_options('top_info_text' => join("\n", @options),
1026 'output_format' => 'HTML',
1027 'title' => $form->{title},
1028 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1030 $report->set_options_from_form();
1031 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1033 my $previous_ctid = 0;
1035 my @periods = qw(open amount);
1036 my %subtotals = map { $_ => 0 } @periods;
1037 my %totals = map { $_ => 0 } @periods;
1039 foreach my $ref (@{ $form->{AG} }) {
1040 if ($row_idx && ($previous_ctid != $ref->{ctid})) {
1041 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
1044 foreach my $key (@periods) {
1045 $subtotals{$key} += $ref->{"$key"};
1046 $totals{$key} += $ref->{"$key"};
1047 $ref->{"$key"} = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
1052 foreach my $column (@columns) {
1054 'data' => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
1055 'align' => $column_alignment{$column},
1056 'valign' => $column eq 'statement' ? 'center' : '',
1060 $row->{invnumber}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
1062 if ($previous_ctid != $ref->{ctid}) {
1063 $row->{statement}->{raw_data} =
1064 $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
1065 . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
1066 $row->{ct}->{data} = $ref->{name};
1071 $previous_ctid = $ref->{ctid};
1073 $report->add_data($row);
1076 $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
1078 $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
1080 if ($form->{arap} eq 'ar') {
1081 my $raw_top_info_text = $form->parse_html_template('rp/aging_ar_top');
1082 my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
1083 'PRINT_OPTIONS' => print_options(inline => 1), });
1084 $report->set_options('raw_top_info_text' => $raw_top_info_text,
1085 'raw_bottom_info_text' => $raw_bottom_info_text);
1088 $report->generate_with_headers();
1090 $main::lxdebug->leave_sub();
1094 $main::lxdebug->enter_sub();
1096 my $form = $main::form;
1097 my %myconfig = %main::myconfig;
1098 my $locale = $main::locale;
1100 RP->aging(\%myconfig, \%$form);
1102 map { $_->{checked} = "checked" } @{ $form->{AG} };
1106 $main::lxdebug->leave_sub();
1110 $::lxdebug->enter_sub;
1111 $::auth->assert('general_ledger');
1113 # get name and email addresses
1115 for my $i (1 .. $::form->{rowcount}) {
1116 next unless $::form->{"statement_$i"};
1117 $::form->{"$::form->{ct}_id"} = $::form->{"$::form->{ct}_id_$i"};
1118 RP->get_customer(\%::myconfig, $::form);
1123 $::form->error($::locale->text('Nothing selected!')) unless $selected;
1125 $::form->{media} = "email";
1127 # save all other variables
1129 for my $key (keys %$::form) {
1130 next if any { $key eq $_ } qw(login password action email cc bcc subject message type sendmode format header);
1131 next unless '' eq ref $::form->{$key};
1132 push @hidden_values, $key;
1136 print $::form->parse_html_template('rp/e_mail', {
1137 show_bcc => $::auth->assert('email_bcc', 'may fail'),
1138 print_options => print_options(inline => 1),
1139 hidden_values => \@hidden_values,
1142 $::lxdebug->leave_sub;
1146 $main::lxdebug->enter_sub();
1148 $main::auth->assert('general_ledger');
1150 my $form = $main::form;
1151 my %myconfig = %main::myconfig;
1152 my $locale = $main::locale;
1154 $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
1155 unless $form->{subject};
1157 RP->aging(\%myconfig, \%$form);
1159 $form->{"statement_1"} = 1;
1161 $form->{media} = 'email';
1164 $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
1166 $main::lxdebug->leave_sub();
1170 $main::lxdebug->enter_sub();
1172 $main::auth->assert('general_ledger');
1174 my $form = $main::form;
1175 my %myconfig = %main::myconfig;
1176 my $locale = $main::locale;
1178 if ($form->{media} eq 'printer') {
1179 $form->error($locale->text('Select postscript or PDF!'))
1180 if ($form->{format} !~ /(postscript|pdf)/);
1184 for my $i (1 .. $form->{rowcount}) {
1185 if ($form->{"statement_$i"}) {
1186 $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
1192 $form->error($locale->text('Nothing selected!')) unless $selected;
1194 if ($form->{media} eq 'printer') {
1195 $form->{"$form->{ct}_id"} = "";
1197 $form->{"statement_1"} = 1;
1200 RP->aging(\%myconfig, \%$form);
1204 $form->redirect($locale->text('Statements sent to printer!'))
1205 if ($form->{media} eq 'printer');
1207 $main::lxdebug->leave_sub();
1211 $main::lxdebug->enter_sub();
1213 $main::auth->assert('general_ledger');
1215 my $form = $main::form;
1216 my %myconfig = %main::myconfig;
1217 my $locale = $main::locale;
1219 $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
1221 $form->{templates} = "$myconfig{templates}";
1223 my $suffix = "html";
1224 my $attachment_suffix = "html";
1225 if ($form->{format} eq 'postscript') {
1226 $form->{postscript} = 1;
1228 $attachment_suffix = "ps";
1229 } elsif ($form->{format} eq 'pdf') {
1232 $attachment_suffix = "pdf";
1235 $form->{IN} = "$form->{type}.$suffix";
1236 $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
1238 # Save $form->{email} because it will be overwritten.
1239 $form->{EMAIL_RECIPIENT} = $form->{email};
1243 while (@{ $form->{AG} }) {
1245 my $ref = shift @{ $form->{AG} };
1247 if ($ctid != $ref->{ctid}) {
1249 $ctid = $ref->{ctid};
1252 if ($form->{"statement_$i"}) {
1255 ("name", "street", "zipcode", "city", "country", "contact", "email",
1256 "$form->{ct}phone", "$form->{ct}fax");
1257 map { $form->{$_} = $ref->{$_} } @a;
1259 $form->{ $form->{ct} } = $form->{name};
1260 $form->{"$form->{ct}_id"} = $ref->{ctid};
1262 map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
1264 foreach my $item (qw(c0 c30 c60 c90)) {
1265 $form->{$item} = ();
1266 $form->{"${item}total"} = 0;
1269 &statement_details($ref);
1273 if (scalar(@{ $form->{AG} }) > 0) {
1275 # one or more left to go
1276 if ($ctid == $form->{AG}->[0]->{ctid}) {
1277 $ref = shift @{ $form->{AG} };
1278 &statement_details($ref);
1281 $ref = scalar(@{ $form->{AG} });
1287 # set initial ref to 0
1294 $form->{"${_}total"} =
1295 $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
1296 } ('c0', 'c30', 'c60', 'c90', "");
1298 $form->{attachment_filename} = $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
1299 $form->{attachment_filename} =~ s/\s+/_/g;
1301 $form->parse_template(\%myconfig);
1306 # saving the history
1307 if(!exists $form->{addition} && $form->{id} ne "") {
1308 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1309 $form->{addition} = "PRINTED";
1310 $form->{what_done} = $form->{type};
1311 $form->save_history;
1313 # /saving the history
1314 $main::lxdebug->leave_sub();
1317 sub statement_details {
1318 $main::lxdebug->enter_sub();
1320 $main::auth->assert('general_ledger');
1322 my $form = $main::form;
1323 my %myconfig = %main::myconfig;
1324 my $locale = $main::locale;
1328 push @{ $form->{invnumber} }, $ref->{invnumber};
1329 push @{ $form->{invdate} }, $ref->{transdate};
1330 push @{ $form->{duedate} }, $ref->{duedate};
1331 push @{ $form->{amount} }, $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
1332 push @{ $form->{open} }, $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
1334 foreach my $item (qw(c0 c30 c60 c90)) {
1335 if ($ref->{exchangerate} * 1) {
1336 # add only the open amount of the invoice to the aging, not the total amount
1337 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
1338 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
1339 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
1340 $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
1342 $form->{"${item}total"} += $ref->{$item};
1343 $form->{total} += $ref->{$item};
1344 push @{ $form->{$item} },
1345 $form->format_amount(\%myconfig, $ref->{$item}, 2);
1348 $main::lxdebug->leave_sub();
1351 sub generate_tax_report {
1352 $main::lxdebug->enter_sub();
1354 $main::auth->assert('report');
1356 my $form = $main::form;
1357 my %myconfig = %main::myconfig;
1358 my $locale = $main::locale;
1360 RP->tax_report(\%myconfig, \%$form);
1362 my $descvar = "$form->{accno}_description";
1363 my $description = $form->escape($form->{$descvar});
1364 my $ratevar = "$form->{accno}_rate";
1365 my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
1367 my $department = $form->escape($form->{department});
1371 "$form->{script}?&action=generate_tax_report&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&accno=$form->{accno}&$descvar=$description&department=$department&report=$form->{report}";
1373 # construct callback
1374 $description = $form->escape($form->{$descvar}, 1);
1375 $department = $form->escape($form->{department}, 1);
1377 "$form->{script}?&action=generate_tax_report&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&accno=$form->{accno}&$descvar=$description&department=$department&report=$form->{report}";
1379 my $title = $form->escape($form->{title});
1380 $href .= "&title=$title";
1381 $title = $form->escape($form->{title}, 1);
1382 $callback .= "&title=$title";
1384 $form->{title} = qq|$form->{title} $form->{"$form->{accno}_description"} |;
1387 $form->sort_columns(qw(id transdate invnumber name netamount tax amount));
1390 foreach my $item (@columns) {
1391 if ($form->{"l_$item"} eq "Y") {
1392 push @column_index, $item;
1394 # add column to href and callback
1395 $callback .= "&l_$item=Y";
1396 $href .= "&l_$item=Y";
1400 if ($form->{l_subtotal} eq 'Y') {
1401 $callback .= "&l_subtotal=Y";
1402 $href .= "&l_subtotal=Y";
1406 if ($form->{department}) {
1407 ($department) = split /--/, $form->{department};
1408 $option = $locale->text('Department') . " : $department";
1411 my ($fromdate, $todate);
1412 # if there are any dates
1413 if ($form->{fromdate} || $form->{todate}) {
1414 if ($form->{fromdate}) {
1415 $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
1417 if ($form->{todate}) {
1418 $todate = $locale->date(\%myconfig, $form->{todate}, 1);
1421 $form->{period} = "$fromdate - $todate";
1424 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1427 my ($name, $invoice, $arap);
1428 if ($form->{db} eq 'ar') {
1429 $name = $locale->text('Customer');
1433 if ($form->{db} eq 'ap') {
1434 $name = $locale->text('Vendor');
1439 $option .= "<br>" if $option;
1440 $option .= "$form->{period}";
1443 $column_header{id} = qq|<th><a class=listheading href=$href&sort=id>| . $locale->text('ID') . qq|</th>|;
1444 $column_header{invnumber} = qq|<th><a class=listheading href=$href&sort=invnumber>| . $locale->text('Invoice') . qq|</th>|;
1445 $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>| . $locale->text('Date') . qq|</th>|;
1446 $column_header{netamount} = qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
1447 $column_header{tax} = qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
1448 $column_header{amount} = qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
1450 $column_header{name} = qq|<th><a class=listheading href=$href&sort=name>$name</th>|;
1459 <th class=listtop>$form->{title}</th>
1461 <tr height="5"></tr>
1468 <tr class=listheading>
1471 map { print "$column_header{$_}\n" } @column_index;
1477 # add sort and escape callback
1478 $callback = $form->escape($callback . "&sort=$form->{sort}");
1481 if (@{ $form->{TR} }) {
1482 $sameitem = $form->{TR}->[0]->{ $form->{sort} };
1485 my ($totalnetamount, $totaltax);
1487 foreach my $ref (@{ $form->{TR} }) {
1489 my $module = ($ref->{invoice}) ? $invoice : $arap;
1491 if ($form->{l_subtotal} eq 'Y') {
1492 if ($sameitem ne $ref->{ $form->{sort} }) {
1493 tax_subtotal(\@column_index, \$subtotalnetamount, \$subtotaltax, \$subtotal);
1494 $sameitem = $ref->{ $form->{sort} };
1498 $totalnetamount += $ref->{netamount};
1499 $totaltax += $ref->{tax};
1500 $ref->{amount} = $ref->{netamount} + $ref->{tax};
1502 $subtotalnetamount += $ref->{netamount};
1503 $subtotaltax += $ref->{tax};
1506 $ref->{$_} = $form->format_amount(\%myconfig, $ref->{$_}, 2, " ");
1507 } qw(netamount tax amount);
1510 $column_data{id} = qq|<td>$ref->{id}</td>|;
1511 $column_data{invnumber} =
1512 qq|<td><a href=$module?action=edit&id=$ref->{id}&callback=$callback>$ref->{invnumber}</a></td>|;
1513 $column_data{transdate} = qq|<td>$ref->{transdate}</td>|;
1514 $column_data{name} = qq|<td>$ref->{name} </td>|;
1516 map { $column_data{$_} = qq|<td align=right>$ref->{$_}</td>| }
1517 qw(netamount tax amount);
1522 <tr class=listrow$i>
1525 map { print "$column_data{$_}\n" } @column_index;
1533 if ($form->{l_subtotal} eq 'Y') {
1534 tax_subtotal(\@column_index, \$subtotalnetamount, \$subtotaltax, \$subtotal);
1538 map { $column_data{$_} = qq|<th> </th>| } @column_index;
1542 <tr class=listtotal>
1545 my $total = $form->format_amount(\%myconfig, $totalnetamount + $totaltax, 2, " ");
1546 $totalnetamount = $form->format_amount(\%myconfig, $totalnetamount, 2, " ");
1547 $totaltax = $form->format_amount(\%myconfig, $totaltax, 2, " ");
1549 $column_data{netamount} = qq|<th class=listtotal align=right>$totalnetamount</th>|;
1550 $column_data{tax} = qq|<th class=listtotal align=right>$totaltax</th>|;
1551 $column_data{amount} = qq|<th class=listtotal align=right>$total</th>|;
1553 map { print "$column_data{$_}\n" } @column_index;
1561 <td><hr size=3 noshade></td>
1569 $main::lxdebug->leave_sub();
1573 $main::lxdebug->enter_sub();
1575 my ($column_index, $subtotalnetamount, $subtotaltax, $subtotal) = @_;
1577 my $form = $main::form;
1578 my %myconfig = %main::myconfig;
1579 my $locale = $main::locale;
1582 map { $column_data{$_} = "<td> </td>" } @{ $column_index };
1584 $$subtotalnetamount = $form->format_amount(\%myconfig, $$subtotalnetamount, 2, " ");
1585 $$subtotaltax = $form->format_amount(\%myconfig, $$subtotaltax, 2, " ");
1586 $$subtotal = $form->format_amount(\%myconfig, $$subtotalnetamount + $$subtotaltax, 2, " ");
1588 $column_data{netamount} = "<th class=listsubtotal align=right>$$subtotalnetamount</th>";
1589 $column_data{tax} = "<th class=listsubtotal align=right>$$subtotaltax</th>";
1590 $column_data{amount} = "<th class=listsubtotal align=right>$$subtotal</th>";
1592 $$subtotalnetamount = 0;
1596 <tr class=listsubtotal>
1598 map { print "\n$column_data{$_}" } @{ $column_index };
1604 $main::lxdebug->leave_sub();
1608 $main::lxdebug->enter_sub();
1610 $main::auth->assert('cash');
1612 my $form = $main::form;
1613 my %myconfig = %main::myconfig;
1614 my $locale = $main::locale;
1616 if ($form->{account}) {
1617 ($form->{paymentaccounts}) = split /--/, $form->{account};
1621 if ($form->{department}) {
1622 (my $department, $form->{department_id}) = split /--/, $form->{department};
1623 $option = $locale->text('Department') . " : $department";
1626 report_generator_set_default_sort('transdate', 1);
1628 RP->payments(\%myconfig, \%$form);
1630 my @hidden_variables = qw(account title department reference source memo fromdate todate
1631 fx_transaction db prepayment paymentaccounts sort);
1633 my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
1634 $form->{callback} = $href;
1636 my @columns = qw(transdate invnumber name paid source memo);
1638 'name' => { 'text' => $locale->text('Description'), },
1639 'invnumber' => { 'text' => $locale->text('Reference'), },
1640 'transdate' => { 'text' => $locale->text('Date'), },
1641 'paid' => { 'text' => $locale->text('Amount'), },
1642 'source' => { 'text' => $locale->text('Source'), },
1643 'memo' => { 'text' => $locale->text('Memo'), },
1645 my %column_alignment = ('paid' => 'right');
1647 foreach my $name (grep { $_ ne 'paid' } @columns) {
1648 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1649 $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
1653 if ($form->{fromdate}) {
1654 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
1656 if ($form->{todate}) {
1657 push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
1660 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1662 my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
1664 $report->set_options('top_info_text' => join("\n", @options),
1665 'output_format' => 'HTML',
1666 'title' => $form->{title},
1667 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1668 'std_column_visibility' => 1,
1670 $report->set_options_from_form();
1672 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
1674 $report->set_columns(%column_defs);
1675 $report->set_column_order(@columns);
1677 $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
1679 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1683 foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
1684 next unless @{ $form->{ $ref->{id} } };
1686 $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
1688 my $subtotal_paid = 0;
1690 foreach my $payment (@{ $form->{ $ref->{id} } }) {
1691 my $module = $payment->{module};
1692 $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
1693 $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
1695 $subtotal_paid += $payment->{paid};
1696 $total_paid += $payment->{paid};
1698 $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
1702 foreach my $column (@columns) {
1704 'data' => $payment->{$column},
1705 'align' => $column_alignment{$column},
1709 $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
1711 $report->add_data($row);
1714 my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
1716 'data' => $form->format_amount(\%myconfig, $subtotal_paid, 2),
1718 'class' => 'listsubtotal',
1721 $report->add_data($row);
1724 $report->add_separator();
1726 my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
1728 'data' => $form->format_amount(\%myconfig, $total_paid, 2),
1730 'class' => 'listtotal',
1733 $report->add_data($row);
1735 $report->generate_with_headers();
1737 $main::lxdebug->leave_sub();
1741 $::lxdebug->enter_sub;
1743 my ($dont_print) = @_;
1745 $::form->{sendmode} = "attachment";
1746 $::form->{format} ||= $::myconfig{template_format} || "pdf";
1747 $::form->{copies} ||= $::myconfig{copies} || 2;
1749 $::form->{PD}{ $::form->{type} } = "selected";
1750 $::form->{DF}{ $::form->{format} } = "selected";
1751 $::form->{OP}{ $::form->{media} } = "selected";
1752 $::form->{SM}{ $::form->{sendmode} } = "selected";
1754 my $output = $::form->parse_html_template('rp/print_options', {
1755 got_printer => $::myconfig{printer},
1756 show_latex => $::lx_office_conf{print_templates}->{latex},
1757 is_email => $::form->{media} eq 'email',
1760 print $output unless $dont_print;
1762 $::lxdebug->leave_sub;
1768 $main::lxdebug->enter_sub();
1770 $main::auth->assert('report');
1772 my $form = $main::form;
1773 my %myconfig = %main::myconfig;
1774 my $locale = $main::locale;
1776 $form->{padding} = " ";
1777 $form->{bold} = "<b>";
1778 $form->{endbold} = "</b>";
1779 $form->{br} = "<br>";
1781 if ($form->{reporttype} eq "custom") {
1783 #forgotten the year --> thisyear
1784 if ($form->{year} !~ m/^\d\d\d\d$/) {
1785 $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
1791 if ($form->{duetyp} eq "13") {
1792 $form->{fromdate} = "1.1.$form->{year}";
1793 $form->{todate} = "31.12.$form->{year}";
1794 $form->{comparefromdate} = "1.01.$form->{year}";
1795 $form->{comparetodate} = "31.12.$form->{year}";
1799 if ($form->{duetyp} eq "A") {
1800 $form->{fromdate} = "1.1.$form->{year}";
1801 $form->{todate} = "31.3.$form->{year}";
1802 $form->{comparefromdate} = "1.01.$form->{year}";
1803 $form->{comparetodate} = "31.03.$form->{year}";
1805 if ($form->{duetyp} eq "B") {
1806 $form->{fromdate} = "1.4.$form->{year}";
1807 $form->{todate} = "30.6.$form->{year}";
1808 $form->{comparefromdate} = "1.01.$form->{year}";
1809 $form->{comparetodate} = "30.06.$form->{year}";
1811 if ($form->{duetyp} eq "C") {
1812 $form->{fromdate} = "1.7.$form->{year}";
1813 $form->{todate} = "30.9.$form->{year}";
1814 $form->{comparefromdate} = "1.01.$form->{year}";
1815 $form->{comparetodate} = "30.09.$form->{year}";
1817 if ($form->{duetyp} eq "D") {
1818 $form->{fromdate} = "1.10.$form->{year}";
1819 $form->{todate} = "31.12.$form->{year}";
1820 $form->{comparefromdate} = "1.01.$form->{year}";
1821 $form->{comparetodate} = "31.12.$form->{year}";
1826 $form->{duetyp} eq "1" && do {
1827 $form->{fromdate} = "1.1.$form->{year}";
1828 $form->{todate} = "31.1.$form->{year}";
1829 $form->{comparefromdate} = "1.01.$form->{year}";
1830 $form->{comparetodate} = "31.01.$form->{year}";
1833 $form->{duetyp} eq "2" && do {
1834 $form->{fromdate} = "1.2.$form->{year}";
1836 #this works from 1901 to 2099, 1900 and 2100 fail.
1837 my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
1838 $form->{todate} = "$leap.2.$form->{year}";
1839 $form->{comparefromdate} = "1.01.$form->{year}";
1840 $form->{comparetodate} = "$leap.02.$form->{year}";
1843 $form->{duetyp} eq "3" && do {
1844 $form->{fromdate} = "1.3.$form->{year}";
1845 $form->{todate} = "31.3.$form->{year}";
1846 $form->{comparefromdate} = "1.01.$form->{year}";
1847 $form->{comparetodate} = "31.03.$form->{year}";
1850 $form->{duetyp} eq "4" && do {
1851 $form->{fromdate} = "1.4.$form->{year}";
1852 $form->{todate} = "30.4.$form->{year}";
1853 $form->{comparefromdate} = "1.01.$form->{year}";
1854 $form->{comparetodate} = "30.04.$form->{year}";
1857 $form->{duetyp} eq "5" && do {
1858 $form->{fromdate} = "1.5.$form->{year}";
1859 $form->{todate} = "31.5.$form->{year}";
1860 $form->{comparefromdate} = "1.01.$form->{year}";
1861 $form->{comparetodate} = "31.05.$form->{year}";
1864 $form->{duetyp} eq "6" && do {
1865 $form->{fromdate} = "1.6.$form->{year}";
1866 $form->{todate} = "30.6.$form->{year}";
1867 $form->{comparefromdate} = "1.01.$form->{year}";
1868 $form->{comparetodate} = "30.06.$form->{year}";
1871 $form->{duetyp} eq "7" && do {
1872 $form->{fromdate} = "1.7.$form->{year}";
1873 $form->{todate} = "31.7.$form->{year}";
1874 $form->{comparefromdate} = "1.01.$form->{year}";
1875 $form->{comparetodate} = "31.07.$form->{year}";
1878 $form->{duetyp} eq "8" && do {
1879 $form->{fromdate} = "1.8.$form->{year}";
1880 $form->{todate} = "31.8.$form->{year}";
1881 $form->{comparefromdate} = "1.01.$form->{year}";
1882 $form->{comparetodate} = "31.08.$form->{year}";
1885 $form->{duetyp} eq "9" && do {
1886 $form->{fromdate} = "1.9.$form->{year}";
1887 $form->{todate} = "30.9.$form->{year}";
1888 $form->{comparefromdate} = "1.01.$form->{year}";
1889 $form->{comparetodate} = "30.09.$form->{year}";
1892 $form->{duetyp} eq "10" && do {
1893 $form->{fromdate} = "1.10.$form->{year}";
1894 $form->{todate} = "31.10.$form->{year}";
1895 $form->{comparefromdate} = "1.01.$form->{year}";
1896 $form->{comparetodate} = "31.10.$form->{year}";
1899 $form->{duetyp} eq "11" && do {
1900 $form->{fromdate} = "1.11.$form->{year}";
1901 $form->{todate} = "30.11.$form->{year}";
1902 $form->{comparefromdate} = "1.01.$form->{year}";
1903 $form->{comparetodate} = "30.11.$form->{year}";
1906 $form->{duetyp} eq "12" && do {
1907 $form->{fromdate} = "1.12.$form->{year}";
1908 $form->{todate} = "31.12.$form->{year}";
1909 $form->{comparefromdate} = "1.01.$form->{year}";
1910 $form->{comparetodate} = "31.12.$form->{year}";
1914 hotfix_reformat_date();
1916 # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
1917 # ansonsten ist die prüfung in RP.pm
1918 # if (defined ($form->{fromdate|todate}=='..'))
1920 if ($form->{fromdate}){
1921 my ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
1922 my $datetime = $locale->parse_date_to_object(\%myconfig, $form->{fromdate});
1923 $datetime->set( month => 1,
1925 $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
1927 if ($form->{todate}){
1928 $form->{comparetodate} = $form->{todate};
1932 RP->bwa(\%myconfig, \%$form);
1934 ($form->{department}) = split /--/, $form->{department};
1937 $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
1938 $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
1940 # if there are any dates construct a where
1941 if ($form->{fromdate} || $form->{todate}) {
1943 unless ($form->{todate}) {
1944 $form->{todate} = $form->current_date(\%myconfig);
1947 my %germandate = ("dateformat" => "dd.mm.yyyy");
1949 my $longtodate = $locale->date(\%germandate, $form->{todate}, 1);
1950 my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
1952 my $longfromdate = $locale->date(\%germandate, $form->{fromdate}, 1);
1953 my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
1955 $form->{this_period} = "$shortfromdate\n$shorttodate";
1957 $locale->text('for Period')
1958 . qq|\n$longfromdate |
1959 . $locale->text('bis')
1963 # setup variables for the form
1964 my @a = qw(company address businessnumber);
1965 map { $form->{$_} = $myconfig{$_} } @a;
1966 $form->{templates} = $myconfig{templates};
1968 $form->{IN} = "bwa.html";
1970 $form->parse_template;
1972 $main::lxdebug->leave_sub();
1975 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
1976 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
1977 sub hotfix_reformat_date {
1979 $main::lxdebug->enter_sub();
1981 my $form = $main::form;
1982 my %myconfig = %main::myconfig;
1983 my $locale = $main::locale;
1985 if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
1986 my $current_dateformat = $myconfig{dateformat};
1987 $myconfig{dateformat} = 'dd.mm.yyyy';
1988 $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
1989 $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
1990 $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
1991 unless (!defined ($form->{comparefromdate}));
1992 $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
1993 unless (!defined ($form->{comparetodate}));
1995 # Und wieder zurücksetzen
1996 $myconfig{dateformat} = $current_dateformat; #'dd.mm.yyyy';
1997 } # Ende Hotifx Bug 1388
1999 $main::lxdebug->leave_sub();