1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
   9 # Copyright (c) 1998-2002
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  15 #  Contributors: Antonio Gallardo <agssa@ibw.com.ni>
 
  16 #                Benjamin Lee <benjaminlee@consultant.com>
 
  17 #                Philip Reetz <p.reetz@linet-services.de>
 
  20 # This program is free software; you can redistribute it and/or modify
 
  21 # it under the terms of the GNU General Public License as published by
 
  22 # the Free Software Foundation; either version 2 of the License, or
 
  23 # (at your option) any later version.
 
  25 # This program is distributed in the hope that it will be useful,
 
  26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  28 # GNU General Public License for more details.
 
  29 # You should have received a copy of the GNU General Public License
 
  30 # along with this program; if not, write to the Free Software
 
  31 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
  33 #======================================================================
 
  35 # module for preparing Income Statement and Balance Sheet
 
  37 #======================================================================
 
  39 use POSIX qw(strftime);
 
  46 use SL::ReportGenerator;
 
  48 use List::MoreUtils qw(any);
 
  50 require "bin/mozilla/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',
 
 110   'erfolgsrechnung'    => 'report',
 
 112   'balance_sheet'      => 'report',
 
 115 sub check_rp_access {
 
 116   my $form     = $main::form;
 
 118   my $right   = $rp_access_map->{$form->{report}};
 
 119   $right    ||= 'DOES_NOT_EXIST';
 
 121   $main::auth->assert($right);
 
 125   $::lxdebug->enter_sub;
 
 130     balance_sheet        => $::locale->text('Balance Sheet'),
 
 131     income_statement     => $::locale->text('Income Statement'),
 
 132     erfolgsrechnung      => $::locale->text('Erfolgsrechnung'),
 
 133     trial_balance        => $::locale->text('Trial Balance'),
 
 134     ar_aging             => $::locale->text('Search AR Aging'),
 
 135     ap_aging             => $::locale->text('Search AP Aging'),
 
 136     tax_collected        => $::locale->text('Tax collected'),
 
 137     tax_paid             => $::locale->text('Tax paid'),
 
 138     receipts             => $::locale->text('Receipts'),
 
 139     payments             => $::locale->text('Payments'),
 
 140     projects             => $::locale->text('Project Transactions'),
 
 141     bwa                  => $::locale->text('Business evaluation'),
 
 144   $::form->{title} = $title{$::form->{report}};
 
 145   $::request->{layout}->add_javascripts('autocomplete_customer.js');
 
 146   $::request->{layout}->add_javascripts('autocomplete_project.js');
 
 147   $::form->{fromdate} = DateTime->today->truncate(to => 'year')->to_kivitendo;
 
 148   $::form->{todate} = DateTime->today->truncate(to => 'year')->add(years => 1)->add(days => -1)->to_kivitendo;
 
 151   $::form->all_departments(\%::myconfig);
 
 152   if (@{ $::form->{all_departments} || [] }) {
 
 153     $::form->{selectdepartment} = "<option>\n";
 
 154     map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
 
 157   $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
 
 159   my $is_projects            = $::form->{report} eq "projects";
 
 160   my $is_income_statement    = $::form->{report} eq "income_statement";
 
 161   my $is_erfolgsrechnung     = $::form->{report} eq "erfolgsrechnung";
 
 162   my $is_bwa                 = $::form->{report} eq "bwa";
 
 163   my $is_balance_sheet       = $::form->{report} eq "balance_sheet";
 
 164   my $is_trial_balance       = $::form->{report} eq "trial_balance";
 
 165   my $is_aging               = $::form->{report} =~ /^a[rp]_aging$/;
 
 166   my $is_payments            = $::form->{report} =~ /(receipts|payments)$/;
 
 168   my ($label, $nextsub, $vc);
 
 170     my $is_sales  = $::form->{report} eq 'ar_aging';
 
 171     $label        = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
 
 172     $::form->{vc} = $is_sales ? 'customer' : 'vendor';
 
 174     $nextsub = "generate_$::form->{report}";
 
 176     $vc = qq|<input name=$::form->{vc} size=35 class="initial_focus">|;
 
 179   my ($selection, $paymentaccounts);
 
 181     $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
 
 183     RP->paymentaccounts(\%::myconfig, $::form);
 
 185     $selection = "<option>\n";
 
 186     for my $ref (@{ $::form->{PR} }) {
 
 187       $paymentaccounts .= "$ref->{accno} ";
 
 188       $selection       .= "<option>$ref->{accno}--$ref->{description}\n";
 
 193   print $::form->parse_html_template('rp/report', {
 
 194     paymentaccounts        => $paymentaccounts,
 
 195     selection              => $selection,
 
 196     is_aging               => $is_aging,
 
 199     year                   => DateTime->today->year,
 
 200     today                  => DateTime->today,
 
 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_erfolgsrechnung     => $is_erfolgsrechnung,
 
 208     is_projects            => $is_projects,
 
 211   $::lxdebug->leave_sub;
 
 214 sub continue { call_sub($main::form->{"nextsub"}); }
 
 216 sub generate_income_statement {
 
 217   $main::lxdebug->enter_sub();
 
 219   $main::auth->assert('report');
 
 221   my $form     = $main::form;
 
 222   my %myconfig = %main::myconfig;
 
 223   my $locale   = $main::locale;
 
 225   $form->{padding} = "  ";
 
 226   $form->{bold}    = "<b>";
 
 227   $form->{endbold} = "</b>";
 
 228   $form->{br}      = "<br>";
 
 230   if ($form->{reporttype} eq "custom") {
 
 232     #forgotten the year --> thisyear
 
 233     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 234       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 240     if ($form->{duetyp} eq "13") {
 
 241       $form->{fromdate} = "1.1.$form->{year}";
 
 242       $form->{todate}   = "31.12.$form->{year}";
 
 246     if ($form->{duetyp} eq "A") {
 
 247       $form->{fromdate} = "1.1.$form->{year}";
 
 248       $form->{todate}   = "31.3.$form->{year}";
 
 250     if ($form->{duetyp} eq "B") {
 
 251       $form->{fromdate} = "1.4.$form->{year}";
 
 252       $form->{todate}   = "30.6.$form->{year}";
 
 254     if ($form->{duetyp} eq "C") {
 
 255       $form->{fromdate} = "1.7.$form->{year}";
 
 256       $form->{todate}   = "30.9.$form->{year}";
 
 258     if ($form->{duetyp} eq "D") {
 
 259       $form->{fromdate} = "1.10.$form->{year}";
 
 260       $form->{todate}   = "31.12.$form->{year}";
 
 265       $form->{duetyp} eq "1" && do {
 
 266         $form->{fromdate} = "1.1.$form->{year}";
 
 267         $form->{todate}   = "31.1.$form->{year}";
 
 270       $form->{duetyp} eq "2" && do {
 
 271         $form->{fromdate} = "1.2.$form->{year}";
 
 273         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 274         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 275         $form->{todate} = "$leap.2.$form->{year}";
 
 278       $form->{duetyp} eq "3" && do {
 
 279         $form->{fromdate} = "1.3.$form->{year}";
 
 280         $form->{todate}   = "31.3.$form->{year}";
 
 283       $form->{duetyp} eq "4" && do {
 
 284         $form->{fromdate} = "1.4.$form->{year}";
 
 285         $form->{todate}   = "30.4.$form->{year}";
 
 288       $form->{duetyp} eq "5" && do {
 
 289         $form->{fromdate} = "1.5.$form->{year}";
 
 290         $form->{todate}   = "31.5.$form->{year}";
 
 293       $form->{duetyp} eq "6" && do {
 
 294         $form->{fromdate} = "1.6.$form->{year}";
 
 295         $form->{todate}   = "30.6.$form->{year}";
 
 298       $form->{duetyp} eq "7" && do {
 
 299         $form->{fromdate} = "1.7.$form->{year}";
 
 300         $form->{todate}   = "31.7.$form->{year}";
 
 303       $form->{duetyp} eq "8" && do {
 
 304         $form->{fromdate} = "1.8.$form->{year}";
 
 305         $form->{todate}   = "31.8.$form->{year}";
 
 308       $form->{duetyp} eq "9" && do {
 
 309         $form->{fromdate} = "1.9.$form->{year}";
 
 310         $form->{todate}   = "30.9.$form->{year}";
 
 313       $form->{duetyp} eq "10" && do {
 
 314         $form->{fromdate} = "1.10.$form->{year}";
 
 315         $form->{todate}   = "31.10.$form->{year}";
 
 318       $form->{duetyp} eq "11" && do {
 
 319         $form->{fromdate} = "1.11.$form->{year}";
 
 320         $form->{todate}   = "30.11.$form->{year}";
 
 323       $form->{duetyp} eq "12" && do {
 
 324         $form->{fromdate} = "1.12.$form->{year}";
 
 325         $form->{todate}   = "31.12.$form->{year}";
 
 329     hotfix_reformat_date();
 
 330   } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
 
 332   RP->income_statement(\%myconfig, \%$form);
 
 334   ($form->{department}) = split /--/, $form->{department};
 
 337     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 338   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
 340   # if there are any dates construct a where
 
 341   if ($form->{fromdate} || $form->{todate}) {
 
 343     unless ($form->{todate}) {
 
 344       $form->{todate} = $form->current_date(\%myconfig);
 
 347     my $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
 
 348     my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
 
 350     my $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 351     my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
 
 353     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
 355         $locale->text('for Period')
 
 356       . qq|\n$longfromdate |
 
 357       . $locale->text('Bis')
 
 361   if ($form->{comparefromdate} || $form->{comparetodate}) {
 
 362     my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
 
 363     my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
 
 365     my $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
 
 366     my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
 
 368     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
 
 370         "\n$longcomparefromdate "
 
 371       . $locale->text('Bis')
 
 372       . qq| $longcomparetodate|;
 
 375   if ( $::instance_conf->get_profit_determination eq 'balance' ) {
 
 376     $form->{title} = $locale->text('Income Statement');
 
 377   } elsif ( $::instance_conf->get_profit_determination eq 'income' ) {
 
 378     $form->{title} = $locale->text('Net Income Statement');
 
 383   if ( $form->{method} eq 'cash' ) {
 
 384     $form->{accounting_method} = $locale->text('Cash accounting');
 
 385   } elsif ( $form->{method} eq 'accrual' ) {
 
 386     $form->{accounting_method} = $locale->text('Accrual accounting');
 
 388     $form->{accounting_method} = "";
 
 391   $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
 
 394   print $form->parse_html_template('rp/income_statement');
 
 396   $main::lxdebug->leave_sub();
 
 399 sub generate_erfolgsrechnung {
 
 400   $::lxdebug->enter_sub;
 
 401   $::auth->assert('report');
 
 403   $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
 
 404   $::form->{padding}       = " ";
 
 405   $::form->{bold}          = "<b>";
 
 406   $::form->{endbold}       = "</b>";
 
 407   $::form->{br}            = "<br>";
 
 409   my $data = RP->erfolgsrechnung(\%::myconfig, $::form);
 
 412   print $::form->parse_html_template('rp/erfolgsrechnung', $data);
 
 414   $::lxdebug->leave_sub;
 
 418 sub generate_balance_sheet {
 
 419   $::lxdebug->enter_sub;
 
 420   $::auth->assert('report');
 
 422   $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
 
 423   $::form->{padding}       = "  ";
 
 424   $::form->{bold}          = "<b>";
 
 425   $::form->{endbold}       = "</b>";
 
 426   $::form->{br}            = "<br>";
 
 428   my $data = RP->balance_sheet(\%::myconfig, $::form);
 
 430   $::form->{asofdate}    ||= $::form->current_date;
 
 431   $::form->{report_title}  = $::locale->text('Balance Sheet');
 
 432   $::form->{report_date} ||= $::form->current_date;
 
 434   ($::form->{department}) = split /--/, $::form->{department};
 
 436   # define Current Earnings account
 
 437   my $padding = $::form->{l_heading} ? $::form->{padding} : "";
 
 438   push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
 
 440   $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
 
 441   $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
 
 443 #  balance sheet isn't read from print templates anymore,
 
 444 #  instead use template in rp
 
 445 #  $::form->{IN} = "balance_sheet.html";
 
 448   print $::form->parse_html_template('rp/balance_sheet', $data);
 
 450   $::lxdebug->leave_sub;
 
 453 sub generate_projects {
 
 454   $main::lxdebug->enter_sub();
 
 456   $main::auth->assert('report');
 
 458   my $form     = $main::form;
 
 459   my %myconfig = %main::myconfig;
 
 460   my $locale   = $main::locale;
 
 462   my $project            = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
 
 463   $form->{projectnumber} = $project ? $project->projectnumber : '';
 
 465   # make sure todate and fromdate always have a value, even if the date fields
 
 466   # were left empty or the inputs weren't valid dates/couldn't be parsed
 
 468   $project = SL::DB::Project->new() unless $project;  # dummy object for dbh
 
 469   unless ($::locale->parse_date_to_object($::form->{fromdate})) {
 
 470     ($form->{fromdate}) = $project->db->dbh->selectrow_array('select min(transdate) from acc_trans');
 
 473   unless ($::locale->parse_date_to_object($::form->{todate})) {
 
 474     ($form->{todate})   = $project->db->dbh->selectrow_array('select max(transdate) from acc_trans');
 
 477   $form->{nextsub} = "generate_projects";
 
 478   $form->{title}   = $locale->text('Project Transactions');
 
 479   RP->trial_balance(\%myconfig, \%$form);
 
 481   list_accounts('generate_projects');
 
 483   $main::lxdebug->leave_sub();
 
 489 # included links to display transactions for period entered
 
 490 # added headers and subtotals
 
 492 sub generate_trial_balance {
 
 493   $main::lxdebug->enter_sub();
 
 495   $main::auth->assert('report');
 
 497   my $form     = $main::form;
 
 498   my %myconfig = %main::myconfig;
 
 499   my $locale   = $main::locale;
 
 500   my $defaults = SL::DB::Default->get;
 
 502   if ($form->{reporttype} eq "custom") {
 
 504     #forgotten the year --> thisyear
 
 505     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 506       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 512     if ($form->{duetyp} eq "13") {
 
 513       $form->{fromdate} = "1.1.$form->{year}";
 
 514       $form->{todate}   = "31.12.$form->{year}";
 
 518     if ($form->{duetyp} eq "A") {
 
 519       $form->{fromdate} = "1.1.$form->{year}";
 
 520       $form->{todate}   = "31.3.$form->{year}";
 
 522     if ($form->{duetyp} eq "B") {
 
 523       $form->{fromdate} = "1.4.$form->{year}";
 
 524       $form->{todate}   = "30.6.$form->{year}";
 
 526     if ($form->{duetyp} eq "C") {
 
 527       $form->{fromdate} = "1.7.$form->{year}";
 
 528       $form->{todate}   = "30.9.$form->{year}";
 
 530     if ($form->{duetyp} eq "D") {
 
 531       $form->{fromdate} = "1.10.$form->{year}";
 
 532       $form->{todate}   = "31.12.$form->{year}";
 
 537       $form->{duetyp} eq "1" && do {
 
 538         $form->{fromdate} = "1.1.$form->{year}";
 
 539         $form->{todate}   = "31.1.$form->{year}";
 
 542       $form->{duetyp} eq "2" && do {
 
 543         $form->{fromdate} = "1.2.$form->{year}";
 
 545         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 546         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 547         $form->{todate} = "$leap.2.$form->{year}";
 
 550       $form->{duetyp} eq "3" && do {
 
 551         $form->{fromdate} = "1.3.$form->{year}";
 
 552         $form->{todate}   = "31.3.$form->{year}";
 
 555       $form->{duetyp} eq "4" && do {
 
 556         $form->{fromdate} = "1.4.$form->{year}";
 
 557         $form->{todate}   = "30.4.$form->{year}";
 
 560       $form->{duetyp} eq "5" && do {
 
 561         $form->{fromdate} = "1.5.$form->{year}";
 
 562         $form->{todate}   = "31.5.$form->{year}";
 
 565       $form->{duetyp} eq "6" && do {
 
 566         $form->{fromdate} = "1.6.$form->{year}";
 
 567         $form->{todate}   = "30.6.$form->{year}";
 
 570       $form->{duetyp} eq "7" && do {
 
 571         $form->{fromdate} = "1.7.$form->{year}";
 
 572         $form->{todate}   = "31.7.$form->{year}";
 
 575       $form->{duetyp} eq "8" && do {
 
 576         $form->{fromdate} = "1.8.$form->{year}";
 
 577         $form->{todate}   = "31.8.$form->{year}";
 
 580       $form->{duetyp} eq "9" && do {
 
 581         $form->{fromdate} = "1.9.$form->{year}";
 
 582         $form->{todate}   = "30.9.$form->{year}";
 
 585       $form->{duetyp} eq "10" && do {
 
 586         $form->{fromdate} = "1.10.$form->{year}";
 
 587         $form->{todate}   = "31.10.$form->{year}";
 
 590       $form->{duetyp} eq "11" && do {
 
 591         $form->{fromdate} = "1.11.$form->{year}";
 
 592         $form->{todate}   = "30.11.$form->{year}";
 
 595       $form->{duetyp} eq "12" && do {
 
 596         $form->{fromdate} = "1.12.$form->{year}";
 
 597         $form->{todate}   = "31.12.$form->{year}";
 
 601     hotfix_reformat_date();
 
 605   # get for each account initial balance, debits and credits
 
 606   RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
 
 609   $form->{rowcount} = scalar @{ $form->{TB} || [] };
 
 610   $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
 
 613     "accno",               "description",
 
 614     "last_transaction",    "soll_eb",
 
 617     "soll_kumuliert",      "haben_kumuliert",
 
 618     "soll_saldo",          "haben_saldo"
 
 622   my $attachment_basename = $locale->text('trial_balance');
 
 623   my $report              = SL::ReportGenerator->new(\%myconfig, $form);
 
 625   my @hidden_variables    = qw(fromdate todate year method);
 
 627   my $href                = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
 
 630     'accno'               => { 'text' => $locale->text('Account'), },
 
 631     'description'         => { 'text' => $locale->text('Description'), },
 
 632     'last_transaction'    => { 'text' => $locale->text('Last Transaction'), },
 
 633     'soll_eb'             => { 'text' => $locale->text('Debit Starting Balance'), },
 
 634     'haben_eb'            => { 'text' => $locale->text('Credit Starting Balance'), },
 
 635     'soll'                => { 'text' => $locale->text('Debit'), },
 
 636     'haben'               => { 'text' => $locale->text('Credit'), },
 
 637     'soll_kumuliert'      => { 'text' => $locale->text('Sum Debit'), },
 
 638     'haben_kumuliert'     => { 'text' => $locale->text('Sum Credit'), },
 
 639     'soll_saldo'          => { 'text' => $locale->text('Saldo Debit'), },
 
 640     'haben_saldo'         => { 'text' => $locale->text('Saldo Credit'), }
 
 645   my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 647   map { $column_defs{$_}->{visible} =  1 } @columns;
 
 649   $report->set_columns(%column_defs);
 
 650   $report->set_column_order(@columns);
 
 652   $report->set_export_options('generate_trial_balance', @hidden_variables);
 
 657   $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
 
 659   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
 
 660   push (@options, $form->{print_date});
 
 662   $form->{company} = $locale->text('Company') . " " . $defaults->company;
 
 663   push (@options, $form->{company});
 
 665   if ($::form->{customer_id}) {
 
 666     my $customer = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id});
 
 667     push @options, $::locale->text('Customer') . ' ' . $customer->displayable_name;
 
 671   $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
 
 673   my @custom_headers = ([
 
 674     { text => $::locale->text('Account'),          rowspan => 2, },
 
 675     { text => $::locale->text('Description'),      rowspan => 2, },
 
 676     { text => $::locale->text('Last Transaction'), rowspan => 2, },
 
 677     { text => $::locale->text('Starting Balance'), colspan => 2, },
 
 678     { text => $::locale->text('Sum for')   . " $form->{template_fromto}", colspan => 2, },
 
 679     { text => $::locale->text('Sum per')   . " $form->{template_to}",     colspan => 2, },
 
 680     { text => $::locale->text('Saldo per') . " $form->{template_to}",     colspan => 2, },
 
 685     { text => $::locale->text('Assets'), },
 
 686     { text => $::locale->text('Equity'), },
 
 687     { text => $::locale->text('Debit'),  },
 
 688     { text => $::locale->text('Credit'), },
 
 689     { text => $::locale->text('Debit'),  },
 
 690     { text => $::locale->text('Credit'), },
 
 691     { text => $::locale->text('Debit'),  },
 
 692     { text => $::locale->text('Credit'), },
 
 695   $report->set_options('output_format'        => 'HTML',
 
 696                        'top_info_text'        => join("\n", @options),
 
 697                        'title'                => $form->{title},
 
 698                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
 699                        'html_template'        => 'rp/html_report_susa',
 
 700                        'pdf_template'         => 'rp/html_report_susa',
 
 702   $report->set_custom_headers(@custom_headers);
 
 703   $report->set_options_from_form();
 
 704   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 706   # add sort and escape callback, this one we use for the add sub
 
 707   $form->{callback} = $href .= "&sort=$form->{sort}";
 
 709   # escape callback for href
 
 710   my $callback = $form->escape($href);
 
 712   my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 714   my %totals    = map { $_ => 0 } @subtotal_columns;
 
 716   my $edit_url = build_std_url('action=edit', 'type', 'vc');
 
 719   foreach my $accno (@{ $form->{TB} || [] }) {
 
 721     $accno->{soll} = $accno->{debit};
 
 722     $accno->{haben} = $accno->{credit};
 
 723     map { $totals{$_}    += $accno->{$_} } @subtotal_columns;
 
 725     map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
 
 726       qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 730     foreach my $column (@columns) {
 
 732         'data'  => $accno->{$column},
 
 733         'align' => $column_alignment{$column},
 
 737     $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}));
 
 739     my $row_set = [ $row ];
 
 742     $report->add_data($row_set);
 
 747   $report->add_separator();
 
 749   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
 751   $report->generate_with_headers();
 
 753   $main::lxdebug->leave_sub();
 
 757 sub create_subtotal_row {
 
 758   $main::lxdebug->enter_sub();
 
 760   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
 762   my $form     = $main::form;
 
 763   my %myconfig = %main::myconfig;
 
 764   my $locale   = $main::locale;
 
 766   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
 768   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
 770   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
 
 772   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
 774   $main::lxdebug->leave_sub();
 
 779 sub create_list_accounts_subtotal_row {
 
 780   $main::lxdebug->enter_sub();
 
 782   my ($subtotals, $columns, $fields, $class) = @_;
 
 784   my $form     = $main::form;
 
 785   my %myconfig = %main::myconfig;
 
 786   my $locale   = $main::locale;
 
 788   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 790   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
 
 792   $main::lxdebug->leave_sub();
 
 798   $main::lxdebug->enter_sub();
 
 802   my $form     = $main::form;
 
 803   my %myconfig = %main::myconfig;
 
 804   my $locale   = $main::locale;
 
 807   if ($form->{department}) {
 
 808     my ($department) = split /--/, $form->{department};
 
 809     push @options, $locale->text('Department') . " : $department";
 
 811   if ($form->{projectnumber}) {
 
 812     push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
 
 815   # if there are any dates
 
 816   if ($form->{fromdate} || $form->{todate}) {
 
 817     my ($fromdate, $todate);
 
 819     if ($form->{fromdate}) {
 
 820       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 822     if ($form->{todate}) {
 
 823       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
 
 826     push @options, "$fromdate - $todate";
 
 829     push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 832   my @columns     = qw(accno description begbalance debit credit endbalance);
 
 834     'accno'       => { 'text' => $locale->text('Account'), },
 
 835     'description' => { 'text' => $locale->text('Description'), },
 
 836     'debit'       => { 'text' => $locale->text('Debit'), },
 
 837     'credit'      => { 'text' => $locale->text('Credit'), },
 
 838     'begbalance'  => { 'text' => $locale->text('Balance'), },
 
 839     'endbalance'  => { 'text' => $locale->text('Balance'), },
 
 841   my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
 
 843   my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
 
 845   $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
 
 847   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 849   $report->set_options('top_info_text'         => join("\n", @options),
 
 850                        'output_format'         => 'HTML',
 
 851                        'title'                 => $form->{title},
 
 852                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
 
 853                        'std_column_visibility' => 1,
 
 855   $report->set_options_from_form();
 
 856   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 858   $report->set_columns(%column_defs);
 
 859   $report->set_column_order(@columns);
 
 861   $report->set_export_options($action, @hidden_variables);
 
 863   my @totals_columns = qw(credit debit begbalance endbalance);
 
 864   my %subtotals      = map { $_ => 0 } @totals_columns;
 
 865   my %totals         = map { $_ => 0 } @totals_columns;
 
 866   my $found_heading  = 0;
 
 867   my @tb             = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
 
 869   # sort the whole thing by account numbers and display
 
 870   foreach my $idx (0 .. scalar(@tb) - 1) {
 
 872     my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
 
 874     my $ml   = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
 
 876     my $row  = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
 
 878     if ($ref->{charttype} eq 'H') {
 
 879       next unless ($form->{l_heading});
 
 881       %subtotals                   = map { $_ => 0 } @totals_columns;
 
 883       $row->{description}->{class} = 'listheading';
 
 884       $row->{description}->{data}  = $ref->{description};
 
 886       $report->add_data($row);
 
 891     foreach (qw(debit credit)) {
 
 892       $subtotals{$_} += $ref->{$_};
 
 893       $totals{$_}    += $ref->{$_};
 
 896     $subtotals{begbalance} += $ref->{balance} * $ml;
 
 897     $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
 
 899     map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
 
 900     map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
 
 902     $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
 
 903     $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
 
 905     $report->add_data($row);
 
 907     if ($form->{l_heading} && $found_heading &&
 
 908         (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
 
 909       $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
 
 913   $report->add_separator();
 
 915   $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
 
 917   $report->generate_with_headers();
 
 919   $main::lxdebug->leave_sub();
 
 922 sub generate_ar_aging {
 
 923   $main::lxdebug->enter_sub();
 
 925   $main::auth->assert('general_ledger | ar_transactions');
 
 927   my $form     = $main::form;
 
 928   my %myconfig = %main::myconfig;
 
 929   my $locale   = $main::locale;
 
 932   ($form->{customer}) = split(/--/, $form->{customer});
 
 934   $form->{ct}   = "customer";
 
 935   $form->{arap} = "ar";
 
 937   $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
 
 939   RP->aging(\%myconfig, \%$form);
 
 942   $main::lxdebug->leave_sub();
 
 945 sub generate_ap_aging {
 
 946   $main::lxdebug->enter_sub();
 
 948   $main::auth->assert('general_ledger | ap_transactions');
 
 950   my $form     = $main::form;
 
 951   my %myconfig = %main::myconfig;
 
 952   my $locale   = $main::locale;
 
 955   ($form->{vendor}) = split(/--/, $form->{vendor});
 
 957   $form->{ct}   = "vendor";
 
 958   $form->{arap} = "ap";
 
 960   $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
 
 962   RP->aging(\%myconfig, \%$form);
 
 965   $main::lxdebug->leave_sub();
 
 968 sub create_aging_subtotal_row {
 
 969   $main::lxdebug->enter_sub();
 
 971   my ($subtotals, $columns, $periods, $class) = @_;
 
 973   my $form     = $main::form;
 
 974   my %myconfig = %main::myconfig;
 
 975   my $locale   = $main::locale;
 
 977   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 979   foreach (@{ $periods }) {
 
 980     $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
 
 981     $subtotals->{$_}      = 0;
 
 984   $main::lxdebug->leave_sub();
 
 990   $main::lxdebug->enter_sub();
 
 992   $main::auth->assert('general_ledger');
 
 994   my $form     = $main::form;
 
 995   my %myconfig = %main::myconfig;
 
 996   my $locale   = $main::locale;
 
 997   my $cgi      = $::request->{cgi};
 
 999   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
1001   my @columns = qw(statement ct invnumber transdate duedate amount open);
 
1004     'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
 
1005     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
 
1006     'invnumber' => { 'text' => $locale->text('Invoice'), },
 
1007     'transdate' => { 'text' => $locale->text('Date'), },
 
1008     'duedate'   => { 'text' => $locale->text('Due'), },
 
1009     'amount'    => { 'text' => $locale->text('Amount'), },
 
1010     'open'      => { 'text' => $locale->text('Open'), },
 
1013   my %column_alignment = ('statement' => 'center',
 
1014                           map { $_ => 'right' } qw(open amount));
 
1016   $report->set_options('std_column_visibility' => 1);
 
1017   $report->set_columns(%column_defs);
 
1018   $report->set_column_order(@columns);
 
1020   my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype department);
 
1021   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
 
1024   my $attachment_basename;
 
1026   if ($form->{department}) {
 
1027     my ($department) = split /--/, $form->{department};
 
1028     push @options, $locale->text('Department') . " : $department";
 
1029     $form->{callback} .= "&department=" . E($department);
 
1032   if (($form->{arap} eq 'ar') && $form->{customer}) {
 
1033     push @options, $form->{customer};
 
1034     $attachment_basename = $locale->text('ar_aging_list');
 
1035     $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
 
1038   if (($form->{arap} eq 'ap') && $form->{vendor}) {
 
1039     push @options, $form->{vendor};
 
1040     $attachment_basename = $locale->text('ap_aging_list');
 
1041     $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
 
1044   if ($form->{fromdate}) {
 
1045     push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1047     push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1050   $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
 
1052   $report->set_options('top_info_text'        => join("\n", @options),
 
1053                        'output_format'        => 'HTML',
 
1054                        'title'                => $form->{title},
 
1055                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1057   $report->set_options_from_form();
 
1058   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1060   my $previous_ctid = 0;
 
1062   my @periods       = qw(open amount);
 
1063   my %subtotals     = map { $_ => 0 } @periods;
 
1064   my %totals        = map { $_ => 0 } @periods;
 
1066   foreach my $ref (@{ $form->{AG} }) {
 
1067     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
 
1068       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
 
1071     foreach my $key (@periods) {
 
1072       $subtotals{$key}  += $ref->{"$key"};
 
1073       $totals{$key}     += $ref->{"$key"};
 
1074       $ref->{"$key"}  = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
 
1079     foreach my $column (@columns) {
 
1081         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
 
1082         'align'  => $column_alignment{$column},
 
1083         'valign' => $column eq 'statement' ? 'center' : '',
 
1087     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
 
1089     if ($previous_ctid != $ref->{ctid}) {
 
1090       $row->{statement}->{raw_data} =
 
1091           $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
 
1092         . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
 
1093       $row->{ct}->{data} = $ref->{name};
 
1098     $previous_ctid = $ref->{ctid};
 
1100     $report->add_data($row);
 
1103   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
 
1105   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
 
1107   if ($form->{arap} eq 'ar') {
 
1108     my $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
 
1109     my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
 
1110                                                                                'PRINT_OPTIONS' => print_options(inline => 1), });
 
1111     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
 
1112                          'raw_bottom_info_text' => $raw_bottom_info_text);
 
1115   $report->generate_with_headers();
 
1117   $main::lxdebug->leave_sub();
 
1121   $main::lxdebug->enter_sub();
 
1123   my $form     = $main::form;
 
1124   my %myconfig = %main::myconfig;
 
1125   my $locale   = $main::locale;
 
1127   RP->aging(\%myconfig, \%$form);
 
1129   map { $_->{checked} = "checked" } @{ $form->{AG} };
 
1133   $main::lxdebug->leave_sub();
 
1137   $::lxdebug->enter_sub;
 
1138   $::auth->assert('general_ledger');
 
1140   # get name and email addresses
 
1142   for my $i (1 .. $::form->{rowcount}) {
 
1143     next unless $::form->{"statement_$i"};
 
1144     $::form->{"$::form->{ct}_id"} = $::form->{"$::form->{ct}_id_$i"};
 
1145     RP->get_customer(\%::myconfig, $::form);
 
1150   $::form->error($::locale->text('Nothing selected!')) unless $selected;
 
1152   $::form->{media} = "email";
 
1154   # save all other variables
 
1156   for my $key (keys %$::form) {
 
1157     next if any { $key eq $_ } qw(login password action email cc bcc subject message type sendmode format header);
 
1158     next unless '' eq ref $::form->{$key};
 
1159     push @hidden_values, $key;
 
1163   print $::form->parse_html_template('rp/e_mail', {
 
1164     print_options => print_options(inline => 1),
 
1165     hidden_values => \@hidden_values,
 
1168   $::lxdebug->leave_sub;
 
1172   $main::lxdebug->enter_sub();
 
1174   $main::auth->assert('general_ledger');
 
1176   my $form     = $main::form;
 
1177   my %myconfig = %main::myconfig;
 
1178   my $locale   = $main::locale;
 
1180   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
 
1181     unless $form->{subject};
 
1183   RP->aging(\%myconfig, \%$form);
 
1185   $form->{"statement_1"} = 1;
 
1187   $form->{media} = 'email';
 
1190   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
 
1192   $main::lxdebug->leave_sub();
 
1196   $main::lxdebug->enter_sub();
 
1198   $main::auth->assert('general_ledger');
 
1200   my $form     = $main::form;
 
1201   my %myconfig = %main::myconfig;
 
1202   my $locale   = $main::locale;
 
1204   if ($form->{media} eq 'printer') {
 
1205     $form->error($locale->text('Select postscript or PDF!'))
 
1206       if ($form->{format} !~ /(postscript|pdf)/);
 
1210   for my $i (1 .. $form->{rowcount}) {
 
1211     if ($form->{"statement_$i"}) {
 
1212       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
 
1218   $form->error($locale->text('Nothing selected!')) unless $selected;
 
1220   if ($form->{media} eq 'printer') {
 
1221     $form->{"$form->{ct}_id"} = "";
 
1223     $form->{"statement_1"} = 1;
 
1226   RP->aging(\%myconfig, \%$form);
 
1230   $form->redirect($locale->text('Statements sent to printer!'))
 
1231     if ($form->{media} eq 'printer');
 
1233   $main::lxdebug->leave_sub();
 
1237   $main::lxdebug->enter_sub();
 
1239   $main::auth->assert('general_ledger');
 
1241   my $form     = $main::form;
 
1242   my %myconfig = %main::myconfig;
 
1243   my $locale   = $main::locale;
 
1245   my $defaults = SL::DB::Default->get;
 
1246   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
 
1247   $form->{templates} = $defaults->templates;
 
1249   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
 
1251   my $suffix = "html";
 
1252   my $attachment_suffix = "html";
 
1253   if ($form->{format} eq 'postscript') {
 
1254     $form->{postscript} = 1;
 
1256     $attachment_suffix = "ps";
 
1257   } elsif ($form->{format} eq 'pdf') {
 
1260     $attachment_suffix = "pdf";
 
1263   $form->{IN}  = "$form->{type}.$suffix";
 
1264   $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
 
1266   # Save $form->{email} because it will be overwritten.
 
1267   $form->{EMAIL_RECIPIENT} = $form->{email};
 
1271   while (@{ $form->{AG} }) {
 
1273     my $ref = shift @{ $form->{AG} };
 
1275     if ($ctid != $ref->{ctid}) {
 
1277       $ctid = $ref->{ctid};
 
1280       if ($form->{"statement_$i"}) {
 
1283           ("name", "street", "zipcode", "city", "country", "contact", "email",
 
1284            "$form->{ct}phone", "$form->{ct}fax");
 
1285         map { $form->{$_} = $ref->{$_} } @a;
 
1287         $form->{ $form->{ct} } = $form->{name};
 
1288         $form->{"$form->{ct}_id"} = $ref->{ctid};
 
1290         map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
 
1292         foreach my $item (qw(c0 c30 c60 c90)) {
 
1293           $form->{$item} = ();
 
1294           $form->{"${item}total"} = 0;
 
1297         &statement_details($ref);
 
1301           if (scalar(@{ $form->{AG} }) > 0) {
 
1303             # one or more left to go
 
1304             if ($ctid == $form->{AG}->[0]->{ctid}) {
 
1305               $ref = shift @{ $form->{AG} };
 
1306               &statement_details($ref);
 
1309               $ref = scalar(@{ $form->{AG} });
 
1315             # set initial ref to 0
 
1322           $form->{"${_}total"} =
 
1323             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
 
1324         } ('c0', 'c30', 'c60', 'c90', "");
 
1326         $form->{attachment_filename} =  $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
 
1327         $form->{attachment_filename} =~ s/\s+/_/g;
 
1329         $form->parse_template(\%myconfig);
 
1334   # saving the history
 
1335   if(!exists $form->{addition} && $form->{id} ne "") {
 
1336     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1337     $form->{addition} = "PRINTED";
 
1338     $form->{what_done} = $form->{type};
 
1339     $form->save_history;
 
1341   # /saving the history
 
1342   $main::lxdebug->leave_sub();
 
1345 sub statement_details {
 
1346   $main::lxdebug->enter_sub();
 
1348   $main::auth->assert('general_ledger');
 
1350   my $form     = $main::form;
 
1351   my %myconfig = %main::myconfig;
 
1352   my $locale   = $main::locale;
 
1356   push @{ $form->{invnumber} }, $ref->{invnumber};
 
1357   push @{ $form->{invdate} },   $ref->{transdate};
 
1358   push @{ $form->{duedate} },   $ref->{duedate};
 
1359   push @{ $form->{amount} },    $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
 
1360   push @{ $form->{open} },      $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
 
1362   foreach my $item (qw(c0 c30 c60 c90)) {
 
1363     if ($ref->{exchangerate} * 1) {
 
1364       # add only the open amount of the invoice to the aging, not the total amount
 
1365       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
 
1366       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
 
1367       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
 
1368       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
 
1370     $form->{"${item}total"} += $ref->{$item};
 
1371     $form->{total}          += $ref->{$item};
 
1372     push @{ $form->{$item} },
 
1373       $form->format_amount(\%myconfig, $ref->{$item}, 2);
 
1376   $main::lxdebug->leave_sub();
 
1379 sub generate_tax_report {
 
1380   $::lxdebug->enter_sub;
 
1381   $::auth->assert('report');
 
1383   RP->tax_report(\%::myconfig, $::form);
 
1385   my $descvar     = "$::form->{accno}_description";
 
1386   my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
 
1390   my $callback = build_std_url('action=generate_tax_report', $descvar,
 
1391     qw(fromdate todate db method accno department report title));
 
1393   my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
 
1396   for my $item (@columns, 'subtotal') {
 
1397     if ($::form->{"l_$item"} eq "Y") {
 
1398       $callback .= "&l_$item=Y";
 
1399       $href     .= "&l_$item=Y";
 
1403   for my $item (@columns) {
 
1404     if ($::form->{"l_$item"} eq "Y") {
 
1405       push @column_index, $item;
 
1410   if ($::form->{department}) {
 
1411     my ($department) = split /--/, $::form->{department};
 
1412     push @options, $::locale->text('Department') . " : $department";
 
1415   # if there are any dates
 
1416   if ($::form->{fromdate} || $::form->{todate}) {
 
1417     my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
 
1418     my $todate   = $::form->{todate}   ? $::locale->date(\%::myconfig, $::form->{todate}, 1)   : '';
 
1419     push @options, "$fromdate - $todate";
 
1421     push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
 
1424   my ($name, $invoice, $arap);
 
1425   if ($::form->{db} eq 'ar') {
 
1426     $name    = $::locale->text('Customer');
 
1430   if ($::form->{db} eq 'ap') {
 
1431     $name    = $::locale->text('Vendor');
 
1436   my %column_header = (
 
1437     id        => $::locale->text('ID'),
 
1438     invnumber => $::locale->text('Invoice'),
 
1439     transdate => $::locale->text('Date'),
 
1440     netamount => $::locale->text('Amount'),
 
1441     tax       => $::locale->text('Tax'),
 
1442     amount    => $::locale->text('Total'),
 
1446   my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
 
1448   $callback .= "&sort=$::form->{sort}";
 
1451   if (@{ $::form->{TR} }) {
 
1452     $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
 
1455   my ($totalnetamount, $totaltax, @data);
 
1456   for my $ref (@{ $::form->{TR} }) {
 
1458     my $module = ($ref->{invoice}) ? $invoice : $arap;
 
1460     if ($::form->{l_subtotal} eq 'Y') {
 
1461       if ($sameitem ne $ref->{ $::form->{sort} }) {
 
1464           netamount => $subtotalnetamount,
 
1465           tax       => $subtotaltax,
 
1466           amount    => $subtotal,
 
1468         $subtotalnetamount = 0;
 
1470         $sameitem          = $ref->{ $::form->{sort} };
 
1474     $subtotalnetamount += $ref->{netamount};
 
1475     $subtotaltax       += $ref->{tax};
 
1476     $totalnetamount    += $ref->{netamount};
 
1477     $totaltax          += $ref->{tax};
 
1478     $ref->{amount}      = $ref->{netamount} + $ref->{tax};
 
1480     push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
 
1481     $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
 
1482     $data[-1]{$_}{numeric}     = 1 for qw(netamount tax amount);
 
1485   if ($::form->{l_subtotal} eq 'Y') {
 
1488       netamount => $subtotalnetamount,
 
1489       tax       => $subtotaltax,
 
1490       amount    => $subtotal,
 
1496     netamount => $totalnetamount,
 
1498     amount    => $totalnetamount + $totaltax,
 
1502   print $::form->parse_html_template('rp/tax_report', {
 
1503     column_index  => \@column_index,
 
1504     column_header => \%column_header,
 
1505     column_sorted => \%column_sorted,
 
1508     options       => \@options,
 
1511   $::lxdebug->leave_sub;
 
1515   $main::lxdebug->enter_sub();
 
1517   $main::auth->assert('cash');
 
1519   my $form     = $main::form;
 
1520   my %myconfig = %main::myconfig;
 
1521   my $locale   = $main::locale;
 
1523   if ($form->{account}) {
 
1524     ($form->{paymentaccounts}) = split /--/, $form->{account};
 
1528   if ($form->{department}) {
 
1529     (my $department, $form->{department_id}) = split /--/, $form->{department};
 
1530     $option = $locale->text('Department') . " : $department";
 
1533   report_generator_set_default_sort('transdate', 1);
 
1535   RP->payments(\%myconfig, \%$form);
 
1537   my @hidden_variables = qw(account title department reference source memo fromdate todate
 
1538                             fx_transaction db prepayment paymentaccounts sort);
 
1540   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
 
1541   $form->{callback} = $href;
 
1543   my @columns     = qw(transdate invnumber name paid source memo);
 
1545     'name'      => { 'text' => $locale->text('Description'), },
 
1546     'invnumber' => { 'text' => $locale->text('Reference'), },
 
1547     'transdate' => { 'text' => $locale->text('Date'), },
 
1548     'paid'      => { 'text' => $locale->text('Amount'), },
 
1549     'source'    => { 'text' => $locale->text('Source'), },
 
1550     'memo'      => { 'text' => $locale->text('Memo'), },
 
1552   my %column_alignment = ('paid' => 'right');
 
1554   foreach my $name (grep { $_ ne 'paid' } @columns) {
 
1555     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
1556     $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
 
1560   if ($form->{fromdate}) {
 
1561     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
 
1563   if ($form->{todate}) {
 
1564     push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1567   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
1569   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
 
1571   $report->set_options('top_info_text'         => join("\n", @options),
 
1572                        'output_format'         => 'HTML',
 
1573                        'title'                 => $form->{title},
 
1574                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1575                        'std_column_visibility' => 1,
 
1577   $report->set_options_from_form();
 
1579   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1581   $report->set_columns(%column_defs);
 
1582   $report->set_column_order(@columns);
 
1584   $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
 
1586   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
1590   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
 
1591     next unless @{ $form->{ $ref->{id} } };
 
1593     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
 
1595     my $subtotal_paid = 0;
 
1597     foreach my $payment (@{ $form->{ $ref->{id} } }) {
 
1598       my $module = $payment->{module};
 
1599       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
 
1600       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
 
1602       $subtotal_paid += $payment->{paid};
 
1603       $total_paid    += $payment->{paid};
 
1605       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
 
1609       foreach my $column (@columns) {
 
1611           'data'  => $payment->{$column},
 
1612           'align' => $column_alignment{$column},
 
1616       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
 
1618       $report->add_data($row);
 
1621     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
 
1623       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
 
1625       'class' => 'listsubtotal',
 
1628     $report->add_data($row);
 
1631   $report->add_separator();
 
1633   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
 
1635     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
 
1637     'class' => 'listtotal',
 
1640   $report->add_data($row);
 
1642   $report->generate_with_headers();
 
1644   $main::lxdebug->leave_sub();
 
1648   $::lxdebug->enter_sub;
 
1650   my ($dont_print) = @_;
 
1652   $::form->{sendmode} = "attachment";
 
1653   $::form->{format} ||= $::myconfig{template_format} || "pdf";
 
1654   $::form->{copies} ||= $::myconfig{copies}          || 2;
 
1656   $::form->{PD}{ $::form->{type} }     = "selected";
 
1657   $::form->{DF}{ $::form->{format} }   = "selected";
 
1658   $::form->{OP}{ $::form->{media} }    = "selected";
 
1659   $::form->{SM}{ $::form->{sendmode} } = "selected";
 
1661   my $output = $::form->parse_html_template('rp/print_options', {
 
1662     is_email    => $::form->{media} eq 'email',
 
1665   print $output unless $dont_print;
 
1667   $::lxdebug->leave_sub;
 
1673   $main::lxdebug->enter_sub();
 
1675   $main::auth->assert('report');
 
1677   my $form     = $main::form;
 
1678   my %myconfig = %main::myconfig;
 
1679   my $locale   = $main::locale;
 
1681   $form->{padding} = "  ";
 
1682   $form->{bold}    = "<b>";
 
1683   $form->{endbold} = "</b>";
 
1684   $form->{br}      = "<br>";
 
1686   if ($form->{reporttype} eq "custom") {
 
1688     #forgotten the year --> thisyear
 
1689     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
1690       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
1696     if ($form->{duetyp} eq "13") {
 
1697       $form->{fromdate}        = "1.1.$form->{year}";
 
1698       $form->{todate}          = "31.12.$form->{year}";
 
1699       $form->{comparefromdate} = "1.01.$form->{year}";
 
1700       $form->{comparetodate}   = "31.12.$form->{year}";
 
1704     if ($form->{duetyp} eq "A") {
 
1705       $form->{fromdate}        = "1.1.$form->{year}";
 
1706       $form->{todate}          = "31.3.$form->{year}";
 
1707       $form->{comparefromdate} = "1.01.$form->{year}";
 
1708       $form->{comparetodate}   = "31.03.$form->{year}";
 
1710     if ($form->{duetyp} eq "B") {
 
1711       $form->{fromdate}        = "1.4.$form->{year}";
 
1712       $form->{todate}          = "30.6.$form->{year}";
 
1713       $form->{comparefromdate} = "1.01.$form->{year}";
 
1714       $form->{comparetodate}   = "30.06.$form->{year}";
 
1716     if ($form->{duetyp} eq "C") {
 
1717       $form->{fromdate}        = "1.7.$form->{year}";
 
1718       $form->{todate}          = "30.9.$form->{year}";
 
1719       $form->{comparefromdate} = "1.01.$form->{year}";
 
1720       $form->{comparetodate}   = "30.09.$form->{year}";
 
1722     if ($form->{duetyp} eq "D") {
 
1723       $form->{fromdate}        = "1.10.$form->{year}";
 
1724       $form->{todate}          = "31.12.$form->{year}";
 
1725       $form->{comparefromdate} = "1.01.$form->{year}";
 
1726       $form->{comparetodate}   = "31.12.$form->{year}";
 
1731       $form->{duetyp} eq "1" && do {
 
1732         $form->{fromdate}        = "1.1.$form->{year}";
 
1733         $form->{todate}          = "31.1.$form->{year}";
 
1734         $form->{comparefromdate} = "1.01.$form->{year}";
 
1735         $form->{comparetodate}   = "31.01.$form->{year}";
 
1738       $form->{duetyp} eq "2" && do {
 
1739         $form->{fromdate} = "1.2.$form->{year}";
 
1741         #this works from 1901 to 2099, 1900 and 2100 fail.
 
1742         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
1743         $form->{todate}          = "$leap.2.$form->{year}";
 
1744         $form->{comparefromdate} = "1.01.$form->{year}";
 
1745         $form->{comparetodate}   = "$leap.02.$form->{year}";
 
1748       $form->{duetyp} eq "3" && do {
 
1749         $form->{fromdate}        = "1.3.$form->{year}";
 
1750         $form->{todate}          = "31.3.$form->{year}";
 
1751         $form->{comparefromdate} = "1.01.$form->{year}";
 
1752         $form->{comparetodate}   = "31.03.$form->{year}";
 
1755       $form->{duetyp} eq "4" && do {
 
1756         $form->{fromdate}        = "1.4.$form->{year}";
 
1757         $form->{todate}          = "30.4.$form->{year}";
 
1758         $form->{comparefromdate} = "1.01.$form->{year}";
 
1759         $form->{comparetodate}   = "30.04.$form->{year}";
 
1762       $form->{duetyp} eq "5" && do {
 
1763         $form->{fromdate}        = "1.5.$form->{year}";
 
1764         $form->{todate}          = "31.5.$form->{year}";
 
1765         $form->{comparefromdate} = "1.01.$form->{year}";
 
1766         $form->{comparetodate}   = "31.05.$form->{year}";
 
1769       $form->{duetyp} eq "6" && do {
 
1770         $form->{fromdate}        = "1.6.$form->{year}";
 
1771         $form->{todate}          = "30.6.$form->{year}";
 
1772         $form->{comparefromdate} = "1.01.$form->{year}";
 
1773         $form->{comparetodate}   = "30.06.$form->{year}";
 
1776       $form->{duetyp} eq "7" && do {
 
1777         $form->{fromdate}        = "1.7.$form->{year}";
 
1778         $form->{todate}          = "31.7.$form->{year}";
 
1779         $form->{comparefromdate} = "1.01.$form->{year}";
 
1780         $form->{comparetodate}   = "31.07.$form->{year}";
 
1783       $form->{duetyp} eq "8" && do {
 
1784         $form->{fromdate}        = "1.8.$form->{year}";
 
1785         $form->{todate}          = "31.8.$form->{year}";
 
1786         $form->{comparefromdate} = "1.01.$form->{year}";
 
1787         $form->{comparetodate}   = "31.08.$form->{year}";
 
1790       $form->{duetyp} eq "9" && do {
 
1791         $form->{fromdate}        = "1.9.$form->{year}";
 
1792         $form->{todate}          = "30.9.$form->{year}";
 
1793         $form->{comparefromdate} = "1.01.$form->{year}";
 
1794         $form->{comparetodate}   = "30.09.$form->{year}";
 
1797       $form->{duetyp} eq "10" && do {
 
1798         $form->{fromdate}        = "1.10.$form->{year}";
 
1799         $form->{todate}          = "31.10.$form->{year}";
 
1800         $form->{comparefromdate} = "1.01.$form->{year}";
 
1801         $form->{comparetodate}   = "31.10.$form->{year}";
 
1804       $form->{duetyp} eq "11" && do {
 
1805         $form->{fromdate}        = "1.11.$form->{year}";
 
1806         $form->{todate}          = "30.11.$form->{year}";
 
1807         $form->{comparefromdate} = "1.01.$form->{year}";
 
1808         $form->{comparetodate}   = "30.11.$form->{year}";
 
1811       $form->{duetyp} eq "12" && do {
 
1812         $form->{fromdate}        = "1.12.$form->{year}";
 
1813         $form->{todate}          = "31.12.$form->{year}";
 
1814         $form->{comparefromdate} = "1.01.$form->{year}";
 
1815         $form->{comparetodate}   = "31.12.$form->{year}";
 
1819     hotfix_reformat_date();
 
1821     # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
 
1822     # ansonsten ist die prüfung in RP.pm
 
1823     # if (defined ($form->{fromdate|todate}=='..'))
 
1825     if ($form->{fromdate}){
 
1826       my $datetime = $locale->parse_date_to_object($form->{fromdate});
 
1827       $datetime->set( month      => 1,
 
1829       $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
 
1831     if ($form->{todate}){
 
1832       $form->{comparetodate}   = $form->{todate};
 
1836   RP->bwa(\%myconfig, \%$form);
 
1838   ($form->{department}) = split /--/, $form->{department};
 
1841     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
1842   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
1844   # if there are any dates construct a where
 
1845   if ($form->{fromdate} || $form->{todate}) {
 
1847     unless ($form->{todate}) {
 
1848       $form->{todate} = $form->current_date(\%myconfig);
 
1851     my %germandate = ("dateformat" => "dd.mm.yyyy");
 
1853     my $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
 
1854     my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
 
1856     my $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
 
1857     my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
 
1859     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
1861         $locale->text('for Period')
 
1862       . qq|\n$longfromdate |
 
1863       . $locale->text('bis')
 
1867   $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
 
1869   if ( $form->{method} eq 'cash' ) {
 
1870     $form->{accounting_method} = $locale->text('Cash accounting');
 
1871   } elsif ( $form->{method} eq 'accrual' ) {
 
1872     $form->{accounting_method} = $locale->text('Accrual accounting');
 
1874     $form->{accounting_method} = "";
 
1877   $form->{title} = $locale->text('BWA');
 
1879   $::request->layout->add_stylesheets('bwa.css');
 
1881   print $form->parse_html_template('rp/bwa');
 
1883   $main::lxdebug->leave_sub();
 
1886 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
 
1887 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
 
1888 sub hotfix_reformat_date {
 
1890   $main::lxdebug->enter_sub();
 
1892   my $form     = $main::form;
 
1893   my %myconfig = %main::myconfig;
 
1894   my $locale   = $main::locale;
 
1896   if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
 
1897     my $current_dateformat = $myconfig{dateformat};
 
1898     $myconfig{dateformat} = 'dd.mm.yyyy';
 
1899     $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
 
1900     $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
 
1901     $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
 
1902       unless (!defined ($form->{comparefromdate}));
 
1903     $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
 
1904       unless (!defined ($form->{comparetodate}));
 
1906     # Und wieder zurücksetzen
 
1907     $myconfig{dateformat} =  $current_dateformat; #'dd.mm.yyyy';
 
1908   } # Ende Hotifx Bug 1388
 
1910   $main::lxdebug->leave_sub();