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::Locale::String qw(t8);
 
  47 use SL::Presenter::Tag;
 
  48 use SL::ReportGenerator;
 
  50 use List::MoreUtils qw(any);
 
  52 require "bin/mozilla/common.pl";
 
  53 require "bin/mozilla/reportgenerator.pl";
 
  55 # note: this file was particularly hard to strictify.
 
  56 # alot of the vars are passed carelessly between invocations
 
  57 # should there be any missing vars, declare them globally
 
  60 # this is for our long dates
 
  61 # $locale->text('January')
 
  62 # $locale->text('February')
 
  63 # $locale->text('March')
 
  64 # $locale->text('April')
 
  65 # $locale->text('May ')
 
  66 # $locale->text('June')
 
  67 # $locale->text('July')
 
  68 # $locale->text('August')
 
  69 # $locale->text('September')
 
  70 # $locale->text('October')
 
  71 # $locale->text('November')
 
  72 # $locale->text('December')
 
  74 # this is for our short month
 
  75 # $locale->text('Jan')
 
  76 # $locale->text('Feb')
 
  77 # $locale->text('Mar')
 
  78 # $locale->text('Apr')
 
  79 # $locale->text('May')
 
  80 # $locale->text('Jun')
 
  81 # $locale->text('Jul')
 
  82 # $locale->text('Aug')
 
  83 # $locale->text('Sep')
 
  84 # $locale->text('Oct')
 
  85 # $locale->text('Nov')
 
  86 # $locale->text('Dec')
 
  88 # $locale->text('Balance Sheet')
 
  89 # $locale->text('Income Statement')
 
  90 # $locale->text('Trial Balance')
 
  91 # $locale->text('AR Aging')
 
  92 # $locale->text('AP Aging')
 
  93 # $locale->text('Search AR Aging')
 
  94 # $locale->text('Search AP Aging')
 
  95 # $locale->text('Tax collected')
 
  96 # $locale->text('Tax paid')
 
  97 # $locale->text('Receipts')
 
  98 # $locale->text('Payments')
 
  99 # $locale->text('Project Transactions')
 
 100 # $locale->text('Business evaluation')
 
 102 # $form->parse_html_template('rp/html_report_susa')
 
 104 my $rp_access_map = {
 
 105   'projects'           => 'report',
 
 106   'ar_aging'           => 'general_ledger',
 
 107   'ap_aging'           => 'general_ledger',
 
 108   'receipts'           => 'cash',
 
 109   'payments'           => 'cash',
 
 110   'trial_balance'      => 'report',
 
 111   'income_statement'   => 'report',
 
 112   'erfolgsrechnung'    => 'report',
 
 114   'balance_sheet'      => 'report',
 
 117 sub check_rp_access {
 
 118   my $form     = $main::form;
 
 120   my $right   = $rp_access_map->{$form->{report}};
 
 121   $right    ||= 'DOES_NOT_EXIST';
 
 123   $main::auth->assert($right);
 
 127   $::lxdebug->enter_sub;
 
 132     balance_sheet        => $::locale->text('Balance Sheet'),
 
 133     income_statement     => $::locale->text('Income Statement'),
 
 134     erfolgsrechnung      => $::locale->text('Erfolgsrechnung'),
 
 135     trial_balance        => $::locale->text('Trial Balance'),
 
 136     ar_aging             => $::locale->text('Search AR Aging'),
 
 137     ap_aging             => $::locale->text('Search AP Aging'),
 
 138     tax_collected        => $::locale->text('Tax collected'),
 
 139     tax_paid             => $::locale->text('Tax paid'),
 
 140     receipts             => $::locale->text('Receipts'),
 
 141     payments             => $::locale->text('Payments'),
 
 142     projects             => $::locale->text('Project Transactions'),
 
 143     bwa                  => $::locale->text('Business evaluation'),
 
 146   $::form->{title} = $title{$::form->{report}};
 
 147   $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
 
 148   $::request->{layout}->add_javascripts('autocomplete_project.js');
 
 149   $::form->{fromdate} = DateTime->today->truncate(to => 'year')->to_kivitendo;
 
 150   $::form->{todate} = DateTime->today->truncate(to => 'year')->add(years => 1)->add(days => -1)->to_kivitendo;
 
 153   $::form->all_departments(\%::myconfig);
 
 154   if (@{ $::form->{all_departments} || [] }) {
 
 155     $::form->{selectdepartment} = "<option>\n";
 
 156     map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
 
 159   $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
 
 161   my $is_projects            = $::form->{report} eq "projects";
 
 162   my $is_income_statement    = $::form->{report} eq "income_statement";
 
 163   my $is_erfolgsrechnung     = $::form->{report} eq "erfolgsrechnung";
 
 164   my $is_bwa                 = $::form->{report} eq "bwa";
 
 165   my $is_balance_sheet       = $::form->{report} eq "balance_sheet";
 
 166   my $is_trial_balance       = $::form->{report} eq "trial_balance";
 
 167   my $is_aging               = $::form->{report} =~ /^a[rp]_aging$/;
 
 168   my $is_payments            = $::form->{report} =~ /(receipts|payments)$/;
 
 171   my ($label, $nextsub, $vc);
 
 173     my $is_sales  = $::form->{report} eq 'ar_aging';
 
 174     $label        = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
 
 175     $::form->{vc} = $is_sales ? 'customer' : 'vendor';
 
 177     $nextsub = "generate_$::form->{report}";
 
 179     $vc = qq|<input name=$::form->{vc} size=35 class="initial_focus">|;
 
 184   my ($selection, $paymentaccounts);
 
 186     $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
 
 188     RP->paymentaccounts(\%::myconfig, $::form);
 
 190     $selection = "<option>\n";
 
 191     for my $ref (@{ $::form->{PR} }) {
 
 192       $paymentaccounts .= "$ref->{accno} ";
 
 193       $selection       .= "<option>$ref->{accno}--$ref->{description}\n";
 
 197   setup_rp_report_action_bar();
 
 200   print $::form->parse_html_template('rp/report', {
 
 201     paymentaccounts        => $paymentaccounts,
 
 202     selection              => $selection,
 
 203     is_aging               => $is_aging,
 
 206     year                   => DateTime->today->year,
 
 207     today                  => DateTime->today,
 
 209     is_payments            => $is_payments,
 
 210     is_trial_balance       => $is_trial_balance,
 
 211     is_balance_sheet       => $is_balance_sheet,
 
 213     is_income_statement    => $is_income_statement,
 
 214     is_erfolgsrechnung     => $is_erfolgsrechnung,
 
 215     is_projects            => $is_projects,
 
 219   $::lxdebug->leave_sub;
 
 222 sub continue { call_sub($main::form->{"nextsub"}); }
 
 224 sub generate_income_statement {
 
 225   $main::lxdebug->enter_sub();
 
 227   $main::auth->assert('report');
 
 229   my $form     = $main::form;
 
 230   my %myconfig = %main::myconfig;
 
 231   my $locale   = $main::locale;
 
 233   $form->{padding} = "  ";
 
 234   $form->{bold}    = "<b>";
 
 235   $form->{endbold} = "</b>";
 
 236   $form->{br}      = "<br>";
 
 238   if ($form->{reporttype} eq "custom") {
 
 240     #forgotten the year --> thisyear
 
 241     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 242       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 248     if ($form->{duetyp} eq "13") {
 
 249       $form->{fromdate} = "1.1.$form->{year}";
 
 250       $form->{todate}   = "31.12.$form->{year}";
 
 254     if ($form->{duetyp} eq "A") {
 
 255       $form->{fromdate} = "1.1.$form->{year}";
 
 256       $form->{todate}   = "31.3.$form->{year}";
 
 258     if ($form->{duetyp} eq "B") {
 
 259       $form->{fromdate} = "1.4.$form->{year}";
 
 260       $form->{todate}   = "30.6.$form->{year}";
 
 262     if ($form->{duetyp} eq "C") {
 
 263       $form->{fromdate} = "1.7.$form->{year}";
 
 264       $form->{todate}   = "30.9.$form->{year}";
 
 266     if ($form->{duetyp} eq "D") {
 
 267       $form->{fromdate} = "1.10.$form->{year}";
 
 268       $form->{todate}   = "31.12.$form->{year}";
 
 273       $form->{duetyp} eq "1" && do {
 
 274         $form->{fromdate} = "1.1.$form->{year}";
 
 275         $form->{todate}   = "31.1.$form->{year}";
 
 278       $form->{duetyp} eq "2" && do {
 
 279         $form->{fromdate} = "1.2.$form->{year}";
 
 281         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 282         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 283         $form->{todate} = "$leap.2.$form->{year}";
 
 286       $form->{duetyp} eq "3" && do {
 
 287         $form->{fromdate} = "1.3.$form->{year}";
 
 288         $form->{todate}   = "31.3.$form->{year}";
 
 291       $form->{duetyp} eq "4" && do {
 
 292         $form->{fromdate} = "1.4.$form->{year}";
 
 293         $form->{todate}   = "30.4.$form->{year}";
 
 296       $form->{duetyp} eq "5" && do {
 
 297         $form->{fromdate} = "1.5.$form->{year}";
 
 298         $form->{todate}   = "31.5.$form->{year}";
 
 301       $form->{duetyp} eq "6" && do {
 
 302         $form->{fromdate} = "1.6.$form->{year}";
 
 303         $form->{todate}   = "30.6.$form->{year}";
 
 306       $form->{duetyp} eq "7" && do {
 
 307         $form->{fromdate} = "1.7.$form->{year}";
 
 308         $form->{todate}   = "31.7.$form->{year}";
 
 311       $form->{duetyp} eq "8" && do {
 
 312         $form->{fromdate} = "1.8.$form->{year}";
 
 313         $form->{todate}   = "31.8.$form->{year}";
 
 316       $form->{duetyp} eq "9" && do {
 
 317         $form->{fromdate} = "1.9.$form->{year}";
 
 318         $form->{todate}   = "30.9.$form->{year}";
 
 321       $form->{duetyp} eq "10" && do {
 
 322         $form->{fromdate} = "1.10.$form->{year}";
 
 323         $form->{todate}   = "31.10.$form->{year}";
 
 326       $form->{duetyp} eq "11" && do {
 
 327         $form->{fromdate} = "1.11.$form->{year}";
 
 328         $form->{todate}   = "30.11.$form->{year}";
 
 331       $form->{duetyp} eq "12" && do {
 
 332         $form->{fromdate} = "1.12.$form->{year}";
 
 333         $form->{todate}   = "31.12.$form->{year}";
 
 337     hotfix_reformat_date();
 
 338   } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
 
 340   RP->income_statement(\%myconfig, \%$form);
 
 342   ($form->{department}) = split /--/, $form->{department};
 
 345     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 346   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
 348   # if there are any dates construct a where
 
 349   if ($form->{fromdate} || $form->{todate}) {
 
 351     unless ($form->{todate}) {
 
 352       $form->{todate} = $form->current_date(\%myconfig);
 
 355     my $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
 
 356     my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
 
 358     my $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 359     my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
 
 361     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
 363         $locale->text('for Period')
 
 364       . qq|\n$longfromdate |
 
 365       . $locale->text('Bis')
 
 369   if ($form->{comparefromdate} || $form->{comparetodate}) {
 
 370     my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
 
 371     my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
 
 373     my $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
 
 374     my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
 
 376     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
 
 378         "\n$longcomparefromdate "
 
 379       . $locale->text('Bis')
 
 380       . qq| $longcomparetodate|;
 
 383   if ( $::instance_conf->get_profit_determination eq 'balance' ) {
 
 384     $form->{title} = $locale->text('Income Statement');
 
 385   } elsif ( $::instance_conf->get_profit_determination eq 'income' ) {
 
 386     $form->{title} = $locale->text('Net Income Statement');
 
 391   if ( $form->{method} eq 'cash' ) {
 
 392     $form->{accounting_method} = $locale->text('Cash accounting');
 
 393   } elsif ( $form->{method} eq 'accrual' ) {
 
 394     $form->{accounting_method} = $locale->text('Accrual accounting');
 
 396     $form->{accounting_method} = "";
 
 399   $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
 
 402   print $form->parse_html_template('rp/income_statement');
 
 404   $main::lxdebug->leave_sub();
 
 407 sub generate_erfolgsrechnung {
 
 408   $::lxdebug->enter_sub;
 
 409   $::auth->assert('report');
 
 411   $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
 
 412   $::form->{padding}       = " ";
 
 413   $::form->{bold}          = "<b>";
 
 414   $::form->{endbold}       = "</b>";
 
 415   $::form->{br}            = "<br>";
 
 417   my $data = RP->erfolgsrechnung(\%::myconfig, $::form);
 
 420   print $::form->parse_html_template('rp/erfolgsrechnung', $data);
 
 422   $::lxdebug->leave_sub;
 
 426 sub generate_balance_sheet {
 
 427   $::lxdebug->enter_sub;
 
 428   $::auth->assert('report');
 
 430   $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
 
 431   $::form->{padding}       = "  ";
 
 432   $::form->{bold}          = "<b>";
 
 433   $::form->{endbold}       = "</b>";
 
 434   $::form->{br}            = "<br>";
 
 436   my $data = RP->balance_sheet(\%::myconfig, $::form);
 
 438   $::form->{asofdate}    ||= $::form->current_date;
 
 439   $::form->{report_title}  = $::locale->text('Balance Sheet');
 
 440   $::form->{report_date} ||= $::form->current_date;
 
 442   ($::form->{department}) = split /--/, $::form->{department};
 
 444   # define Current Earnings account
 
 445   my $padding = $::form->{l_heading} ? $::form->{padding} : "";
 
 446   push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
 
 448   $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
 
 449   $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
 
 451 #  balance sheet isn't read from print templates anymore,
 
 452 #  instead use template in rp
 
 453 #  $::form->{IN} = "balance_sheet.html";
 
 456   print $::form->parse_html_template('rp/balance_sheet', $data);
 
 458   $::lxdebug->leave_sub;
 
 461 sub generate_projects {
 
 462   $main::lxdebug->enter_sub();
 
 464   $main::auth->assert('report');
 
 466   my $form     = $main::form;
 
 467   my %myconfig = %main::myconfig;
 
 468   my $locale   = $main::locale;
 
 470   my $project            = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
 
 471   $form->{projectnumber} = $project ? $project->projectnumber : '';
 
 473   # make sure todate and fromdate always have a value, even if the date fields
 
 474   # were left empty or the inputs weren't valid dates/couldn't be parsed
 
 476   $project = SL::DB::Project->new() unless $project;  # dummy object for dbh
 
 477   unless ($::locale->parse_date_to_object($::form->{fromdate})) {
 
 478     ($form->{fromdate}) = $project->db->dbh->selectrow_array('select min(transdate) from acc_trans');
 
 481   unless ($::locale->parse_date_to_object($::form->{todate})) {
 
 482     ($form->{todate})   = $project->db->dbh->selectrow_array('select max(transdate) from acc_trans');
 
 485   $form->{nextsub} = "generate_projects";
 
 486   $form->{title}   = $locale->text('Project Transactions');
 
 487   RP->trial_balance(\%myconfig, \%$form);
 
 489   list_accounts('generate_projects');
 
 491   $main::lxdebug->leave_sub();
 
 497 # included links to display transactions for period entered
 
 498 # added headers and subtotals
 
 500 sub generate_trial_balance {
 
 501   $main::lxdebug->enter_sub();
 
 503   $main::auth->assert('report');
 
 505   my $form     = $main::form;
 
 506   my %myconfig = %main::myconfig;
 
 507   my $locale   = $main::locale;
 
 508   my $defaults = SL::DB::Default->get;
 
 510   if ($form->{reporttype} eq "custom") {
 
 512     #forgotten the year --> thisyear
 
 513     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 514       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 520     if ($form->{duetyp} eq "13") {
 
 521       $form->{fromdate} = "1.1.$form->{year}";
 
 522       $form->{todate}   = "31.12.$form->{year}";
 
 526     if ($form->{duetyp} eq "A") {
 
 527       $form->{fromdate} = "1.1.$form->{year}";
 
 528       $form->{todate}   = "31.3.$form->{year}";
 
 530     if ($form->{duetyp} eq "B") {
 
 531       $form->{fromdate} = "1.4.$form->{year}";
 
 532       $form->{todate}   = "30.6.$form->{year}";
 
 534     if ($form->{duetyp} eq "C") {
 
 535       $form->{fromdate} = "1.7.$form->{year}";
 
 536       $form->{todate}   = "30.9.$form->{year}";
 
 538     if ($form->{duetyp} eq "D") {
 
 539       $form->{fromdate} = "1.10.$form->{year}";
 
 540       $form->{todate}   = "31.12.$form->{year}";
 
 545       $form->{duetyp} eq "1" && do {
 
 546         $form->{fromdate} = "1.1.$form->{year}";
 
 547         $form->{todate}   = "31.1.$form->{year}";
 
 550       $form->{duetyp} eq "2" && do {
 
 551         $form->{fromdate} = "1.2.$form->{year}";
 
 553         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 554         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 555         $form->{todate} = "$leap.2.$form->{year}";
 
 558       $form->{duetyp} eq "3" && do {
 
 559         $form->{fromdate} = "1.3.$form->{year}";
 
 560         $form->{todate}   = "31.3.$form->{year}";
 
 563       $form->{duetyp} eq "4" && do {
 
 564         $form->{fromdate} = "1.4.$form->{year}";
 
 565         $form->{todate}   = "30.4.$form->{year}";
 
 568       $form->{duetyp} eq "5" && do {
 
 569         $form->{fromdate} = "1.5.$form->{year}";
 
 570         $form->{todate}   = "31.5.$form->{year}";
 
 573       $form->{duetyp} eq "6" && do {
 
 574         $form->{fromdate} = "1.6.$form->{year}";
 
 575         $form->{todate}   = "30.6.$form->{year}";
 
 578       $form->{duetyp} eq "7" && do {
 
 579         $form->{fromdate} = "1.7.$form->{year}";
 
 580         $form->{todate}   = "31.7.$form->{year}";
 
 583       $form->{duetyp} eq "8" && do {
 
 584         $form->{fromdate} = "1.8.$form->{year}";
 
 585         $form->{todate}   = "31.8.$form->{year}";
 
 588       $form->{duetyp} eq "9" && do {
 
 589         $form->{fromdate} = "1.9.$form->{year}";
 
 590         $form->{todate}   = "30.9.$form->{year}";
 
 593       $form->{duetyp} eq "10" && do {
 
 594         $form->{fromdate} = "1.10.$form->{year}";
 
 595         $form->{todate}   = "31.10.$form->{year}";
 
 598       $form->{duetyp} eq "11" && do {
 
 599         $form->{fromdate} = "1.11.$form->{year}";
 
 600         $form->{todate}   = "30.11.$form->{year}";
 
 603       $form->{duetyp} eq "12" && do {
 
 604         $form->{fromdate} = "1.12.$form->{year}";
 
 605         $form->{todate}   = "31.12.$form->{year}";
 
 609     hotfix_reformat_date();
 
 613   # get for each account initial balance, debits and credits
 
 614   RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
 
 617   $form->{rowcount} = scalar @{ $form->{TB} || [] };
 
 618   $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
 
 621     "accno",               "description",
 
 622     "last_transaction",    "soll_eb",
 
 625     "soll_kumuliert",      "haben_kumuliert",
 
 626     "soll_saldo",          "haben_saldo"
 
 630   my $attachment_basename = $locale->text('trial_balance');
 
 631   my $report              = SL::ReportGenerator->new(\%myconfig, $form);
 
 633   my @hidden_variables    = qw(fromdate todate year method department_id);
 
 635   my $href                = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
 
 638     'accno'               => { 'text' => $locale->text('Account'), },
 
 639     'description'         => { 'text' => $locale->text('Description'), },
 
 640     'last_transaction'    => { 'text' => $locale->text('Last Transaction'), },
 
 641     'soll_eb'             => { 'text' => $locale->text('Debit Starting Balance'), },
 
 642     'haben_eb'            => { 'text' => $locale->text('Credit Starting Balance'), },
 
 643     'soll'                => { 'text' => $locale->text('Debit'), },
 
 644     'haben'               => { 'text' => $locale->text('Credit'), },
 
 645     'soll_kumuliert'      => { 'text' => $locale->text('Sum Debit'), },
 
 646     'haben_kumuliert'     => { 'text' => $locale->text('Sum Credit'), },
 
 647     'soll_saldo'          => { 'text' => $locale->text('Saldo Debit'), },
 
 648     'haben_saldo'         => { 'text' => $locale->text('Saldo Credit'), }
 
 653   my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 655   map { $column_defs{$_}->{visible} =  1 } @columns;
 
 657   $report->set_columns(%column_defs);
 
 658   $report->set_column_order(@columns);
 
 660   $report->set_export_options('generate_trial_balance', @hidden_variables);
 
 665   $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
 
 667   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
 
 668   push (@options, $form->{print_date});
 
 670   $form->{company} = $locale->text('Company') . " " . $defaults->company;
 
 671   push (@options, $form->{company});
 
 673   if ($::form->{customer_id}) {
 
 674     my $customer = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id});
 
 675     push @options, $::locale->text('Customer') . ' ' . $customer->displayable_name;
 
 679   $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
 
 681   my @custom_headers = ([
 
 682     { text => $::locale->text('Account'),          rowspan => 2, },
 
 683     { text => $::locale->text('Description'),      rowspan => 2, },
 
 684     { text => $::locale->text('Last Transaction'), rowspan => 2, },
 
 685     { text => $::locale->text('Starting Balance'), colspan => 2, },
 
 686     { text => $::locale->text('Sum for')   . " $form->{template_fromto}", colspan => 2, },
 
 687     { text => $::locale->text('Sum per')   . " $form->{template_to}",     colspan => 2, },
 
 688     { text => $::locale->text('Saldo per') . " $form->{template_to}",     colspan => 2, },
 
 693     { text => $::locale->text('Assets'), },
 
 694     { text => $::locale->text('Equity'), },
 
 695     { text => $::locale->text('Debit'),  },
 
 696     { text => $::locale->text('Credit'), },
 
 697     { text => $::locale->text('Debit'),  },
 
 698     { text => $::locale->text('Credit'), },
 
 699     { text => $::locale->text('Debit'),  },
 
 700     { text => $::locale->text('Credit'), },
 
 703   $report->set_options('output_format'        => 'HTML',
 
 704                        'top_info_text'        => join("\n", @options),
 
 705                        'title'                => $form->{title},
 
 706                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
 707                        'html_template'        => 'rp/html_report_susa',
 
 708                        'pdf_template'         => 'rp/html_report_susa',
 
 710   $report->set_custom_headers(@custom_headers);
 
 711   $report->set_options_from_form();
 
 712   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 714   # add sort and escape callback, this one we use for the add sub
 
 715   $form->{callback} = $href .= "&sort=$form->{sort}";
 
 717   # escape callback for href
 
 718   my $callback = $form->escape($href);
 
 720   my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 722   my %totals    = map { $_ => 0 } @subtotal_columns;
 
 724   my $edit_url = build_std_url('action=edit', 'type', 'vc');
 
 727   foreach my $accno (@{ $form->{TB} || [] }) {
 
 729     $accno->{soll} = $accno->{debit};
 
 730     $accno->{haben} = $accno->{credit};
 
 731     map { $totals{$_}    += $accno->{$_} } @subtotal_columns;
 
 733     map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
 
 734       qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 738     foreach my $column (@columns) {
 
 740         'data'  => $accno->{$column},
 
 741         'align' => $column_alignment{$column},
 
 745     $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}));
 
 747     my $row_set = [ $row ];
 
 750     $report->add_data($row_set);
 
 755   $report->add_separator();
 
 757   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
 759   $report->generate_with_headers();
 
 761   $main::lxdebug->leave_sub();
 
 765 sub create_subtotal_row {
 
 766   $main::lxdebug->enter_sub();
 
 768   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
 770   my $form     = $main::form;
 
 771   my %myconfig = %main::myconfig;
 
 772   my $locale   = $main::locale;
 
 774   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
 776   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
 778   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
 
 780   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
 782   $main::lxdebug->leave_sub();
 
 787 sub create_list_accounts_subtotal_row {
 
 788   $main::lxdebug->enter_sub();
 
 790   my ($subtotals, $columns, $fields, $class) = @_;
 
 792   my $form     = $main::form;
 
 793   my %myconfig = %main::myconfig;
 
 794   my $locale   = $main::locale;
 
 796   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 798   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
 
 800   $main::lxdebug->leave_sub();
 
 806   $main::lxdebug->enter_sub();
 
 810   my $form     = $main::form;
 
 811   my %myconfig = %main::myconfig;
 
 812   my $locale   = $main::locale;
 
 815   if ($form->{department}) {
 
 816     my ($department) = split /--/, $form->{department};
 
 817     push @options, $locale->text('Department') . " : $department";
 
 819   if ($form->{projectnumber}) {
 
 820     push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
 
 823   # if there are any dates
 
 824   if ($form->{fromdate} || $form->{todate}) {
 
 825     my ($fromdate, $todate);
 
 827     if ($form->{fromdate}) {
 
 828       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 830     if ($form->{todate}) {
 
 831       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
 
 834     push @options, "$fromdate - $todate";
 
 837     push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 840   my @columns     = qw(accno description begbalance debit credit endbalance);
 
 842     'accno'       => { 'text' => $locale->text('Account'), },
 
 843     'description' => { 'text' => $locale->text('Description'), },
 
 844     'debit'       => { 'text' => $locale->text('Debit'), },
 
 845     'credit'      => { 'text' => $locale->text('Credit'), },
 
 846     'begbalance'  => { 'text' => $locale->text('Balance'), },
 
 847     'endbalance'  => { 'text' => $locale->text('Balance'), },
 
 849   my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
 
 851   my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
 
 853   $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
 
 855   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 857   $report->set_options('top_info_text'         => join("\n", @options),
 
 858                        'output_format'         => 'HTML',
 
 859                        'title'                 => $form->{title},
 
 860                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
 
 861                        'std_column_visibility' => 1,
 
 863   $report->set_options_from_form();
 
 864   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 866   $report->set_columns(%column_defs);
 
 867   $report->set_column_order(@columns);
 
 869   $report->set_export_options($action, @hidden_variables);
 
 871   my @totals_columns = qw(credit debit begbalance endbalance);
 
 872   my %subtotals      = map { $_ => 0 } @totals_columns;
 
 873   my %totals         = map { $_ => 0 } @totals_columns;
 
 874   my $found_heading  = 0;
 
 875   my @tb             = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
 
 877   # sort the whole thing by account numbers and display
 
 878   foreach my $idx (0 .. scalar(@tb) - 1) {
 
 880     my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
 
 882     my $ml   = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
 
 884     my $row  = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
 
 886     if ($ref->{charttype} eq 'H') {
 
 887       next unless ($form->{l_heading});
 
 889       %subtotals                   = map { $_ => 0 } @totals_columns;
 
 891       $row->{description}->{class} = 'listheading';
 
 892       $row->{description}->{data}  = $ref->{description};
 
 894       $report->add_data($row);
 
 899     foreach (qw(debit credit)) {
 
 900       $subtotals{$_} += $ref->{$_};
 
 901       $totals{$_}    += $ref->{$_};
 
 904     $subtotals{begbalance} += $ref->{balance} * $ml;
 
 905     $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
 
 907     map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
 
 908     map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
 
 910     $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
 
 911     $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
 
 913     $report->add_data($row);
 
 915     if ($form->{l_heading} && $found_heading &&
 
 916         (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
 
 917       $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
 
 921   $report->add_separator();
 
 923   $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
 
 925   $report->generate_with_headers();
 
 927   $main::lxdebug->leave_sub();
 
 930 sub generate_ar_aging {
 
 931   $main::lxdebug->enter_sub();
 
 933   $main::auth->assert('general_ledger | ar_transactions');
 
 935   my $form     = $main::form;
 
 936   my %myconfig = %main::myconfig;
 
 937   my $locale   = $main::locale;
 
 940   ($form->{customer}) = split(/--/, $form->{customer});
 
 942   $form->{ct}   = "customer";
 
 943   $form->{arap} = "ar";
 
 945   $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
 
 947   RP->aging(\%myconfig, \%$form);
 
 950   $main::lxdebug->leave_sub();
 
 953 sub generate_ap_aging {
 
 954   $main::lxdebug->enter_sub();
 
 956   $main::auth->assert('general_ledger | ap_transactions');
 
 958   my $form     = $main::form;
 
 959   my %myconfig = %main::myconfig;
 
 960   my $locale   = $main::locale;
 
 963   ($form->{vendor}) = split(/--/, $form->{vendor});
 
 965   $form->{ct}   = "vendor";
 
 966   $form->{arap} = "ap";
 
 968   $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
 
 970   RP->aging(\%myconfig, \%$form);
 
 973   $main::lxdebug->leave_sub();
 
 976 sub create_aging_subtotal_row {
 
 977   $main::lxdebug->enter_sub();
 
 979   my ($subtotals, $columns, $periods, $class) = @_;
 
 981   my $form     = $main::form;
 
 982   my %myconfig = %main::myconfig;
 
 983   my $locale   = $main::locale;
 
 985   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 987   foreach (@{ $periods }) {
 
 988     $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
 
 989     $subtotals->{$_}      = 0;
 
 992   $main::lxdebug->leave_sub();
 
 998   $main::lxdebug->enter_sub();
 
1000   $main::auth->assert('general_ledger');
 
1002   my $form     = $main::form;
 
1003   my %myconfig = %main::myconfig;
 
1004   my $locale   = $main::locale;
 
1005   my $cgi      = $::request->{cgi};
 
1007   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
1009   my @columns = qw(statement ct invnumber transdate duedate amount open);
 
1012     'statement' => { raw_header_data => SL::Presenter::Tag::checkbox_tag("checkall", checkall => '[name^=statement_]'), 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, align => "center" },
 
1013     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
 
1014     'invnumber' => { 'text' => $locale->text('Invoice'), },
 
1015     'transdate' => { 'text' => $locale->text('Date'), },
 
1016     'duedate'   => { 'text' => $locale->text('Due'), },
 
1017     'amount'    => { 'text' => $locale->text('Amount'), },
 
1018     'open'      => { 'text' => $locale->text('Open'), },
 
1021   my %column_alignment = ('statement' => 'center',
 
1022                           map { $_ => 'right' } qw(open amount));
 
1024   $report->set_options('std_column_visibility' => 1);
 
1025   $report->set_columns(%column_defs);
 
1026   $report->set_column_order(@columns);
 
1027   my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype department fromdate);
 
1028   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
 
1031   my $attachment_basename;
 
1033   if ($form->{department}) {
 
1034     my ($department) = split /--/, $form->{department};
 
1035     push @options, $locale->text('Department') . " : $department";
 
1036     $form->{callback} .= "&department=" . E($department);
 
1039   if (($form->{arap} eq 'ar') && $form->{customer}) {
 
1040     push @options, $form->{customer};
 
1041     $attachment_basename = $locale->text('ar_aging_list');
 
1042     $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
 
1045   if (($form->{arap} eq 'ap') && $form->{vendor}) {
 
1046     push @options, $form->{vendor};
 
1047     $attachment_basename = $locale->text('ap_aging_list');
 
1048     $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
 
1051   if ($form->{fromdate}) {
 
1052     push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1054     push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1057   $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
 
1059   $report->set_options('top_info_text'        => join("\n", @options),
 
1060                        'output_format'        => 'HTML',
 
1061                        'title'                => $form->{title},
 
1062                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1064   $report->set_options_from_form();
 
1065   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1067   my $previous_ctid = 0;
 
1069   my @periods       = qw(open amount);
 
1070   my %subtotals     = map { $_ => 0 } @periods;
 
1071   my %totals        = map { $_ => 0 } @periods;
 
1073   foreach my $ref (@{ $form->{AG} }) {
 
1074     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
 
1075       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
 
1078     foreach my $key (@periods) {
 
1079       $subtotals{$key}  += $ref->{"$key"};
 
1080       $totals{$key}     += $ref->{"$key"};
 
1081       $ref->{"$key"}  = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
 
1086     foreach my $column (@columns) {
 
1088         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
 
1089         'align'  => $column_alignment{$column},
 
1090         'valign' => $column eq 'statement' ? 'center' : '',
 
1094     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
 
1096     if ($previous_ctid != $ref->{ctid}) {
 
1097       $row->{statement}->{raw_data} =
 
1098           $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
 
1099         . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
 
1100       $row->{ct}->{data} = $ref->{name};
 
1105     $previous_ctid = $ref->{ctid};
 
1107     $report->add_data($row);
 
1110   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
 
1112   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
 
1114   if ($form->{arap} eq 'ar') {
 
1115     my $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
 
1116     my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
 
1117                                                                                'PRINT_OPTIONS' => print_options(inline => 1), });
 
1118     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
 
1119                          'raw_bottom_info_text' => $raw_bottom_info_text);
 
1122   setup_rp_aging_action_bar(arap => $form->{arap});
 
1123   $report->generate_with_headers();
 
1125   $main::lxdebug->leave_sub();
 
1129   $main::lxdebug->enter_sub();
 
1131   $main::auth->assert('general_ledger');
 
1133   my $form     = $main::form;
 
1134   my %myconfig = %main::myconfig;
 
1135   my $locale   = $main::locale;
 
1137   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
 
1138     unless $form->{subject};
 
1140   RP->aging(\%myconfig, \%$form);
 
1143   my $email_form  = delete $form->{email_form};
 
1144   my %field_names = (to => 'email');
 
1146   $form->{ $field_names{$_} // $_ } = $email_form->{$_} for keys %{ $email_form };
 
1147   $form->{media} = 'email';
 
1150   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
 
1152   $main::lxdebug->leave_sub();
 
1156   $main::lxdebug->enter_sub();
 
1158   $main::auth->assert('general_ledger');
 
1160   my $form     = $main::form;
 
1161   my %myconfig = %main::myconfig;
 
1162   my $locale   = $main::locale;
 
1164   if ($form->{media} eq 'printer') {
 
1165     $form->error($locale->text('Select postscript or PDF!'))
 
1166       if ($form->{format} !~ /(postscript|pdf)/);
 
1170   for my $i (1 .. $form->{rowcount}) {
 
1171     if ($form->{"statement_$i"}) {
 
1172       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
 
1178   $form->error($locale->text('Nothing selected!')) unless $selected;
 
1180   if ($form->{media} eq 'printer') {
 
1181     $form->{"$form->{ct}_id"} = "";
 
1183     $form->{"statement_1"} = 1;
 
1186   RP->aging(\%myconfig, \%$form);
 
1190   $form->redirect($locale->text('Statements sent to printer!'))
 
1191     if ($form->{media} eq 'printer');
 
1193   $main::lxdebug->leave_sub();
 
1197   $main::lxdebug->enter_sub();
 
1199   $main::auth->assert('general_ledger');
 
1201   my $form     = $main::form;
 
1202   my %myconfig = %main::myconfig;
 
1203   my $locale   = $main::locale;
 
1205   my $defaults = SL::DB::Default->get;
 
1206   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
 
1207   $form->{templates} = $defaults->templates;
 
1209   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
 
1211   my $suffix = "html";
 
1212   my $attachment_suffix = "html";
 
1213   if ($form->{format} eq 'postscript') {
 
1214     $form->{postscript} = 1;
 
1216     $attachment_suffix = "ps";
 
1217   } elsif ($form->{format} eq 'pdf') {
 
1220     $attachment_suffix = "pdf";
 
1223   $form->{IN}  = "$form->{type}.$suffix";
 
1224   $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
 
1226   # Save $form->{email} because it will be overwritten.
 
1227   $form->{EMAIL_RECIPIENT} = $form->{email};
 
1231   while (@{ $form->{AG} }) {
 
1233     my $ref = shift @{ $form->{AG} };
 
1235     if ($ctid != $ref->{ctid}) {
 
1237       $ctid = $ref->{ctid};
 
1240       if ($form->{"statement_$i"}) {
 
1243           ("name", "street", "zipcode", "city", "country", "contact", "email",
 
1244            "$form->{ct}phone", "$form->{ct}fax");
 
1245         map { $form->{$_} = $ref->{$_} } @a;
 
1247         $form->{ $form->{ct} } = $form->{name};
 
1248         $form->{"$form->{ct}_id"} = $ref->{ctid};
 
1250         map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
 
1252         foreach my $item (qw(c0 c30 c60 c90)) {
 
1253           $form->{$item} = ();
 
1254           $form->{"${item}total"} = 0;
 
1257         &statement_details($ref);
 
1261           if (scalar(@{ $form->{AG} }) > 0) {
 
1263             # one or more left to go
 
1264             if ($ctid == $form->{AG}->[0]->{ctid}) {
 
1265               $ref = shift @{ $form->{AG} };
 
1266               &statement_details($ref);
 
1269               $ref = scalar(@{ $form->{AG} });
 
1275             # set initial ref to 0
 
1282           $form->{"${_}total"} =
 
1283             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
 
1284         } ('c0', 'c30', 'c60', 'c90', "");
 
1286         $form->{attachment_filename} =  $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
 
1287         $form->{attachment_filename} =~ s/\s+/_/g;
 
1289         $form->parse_template(\%myconfig);
 
1294   # saving the history
 
1295   if(!exists $form->{addition} && $form->{id} ne "") {
 
1296     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1297     $form->{addition} = "PRINTED";
 
1298     $form->{what_done} = $form->{type};
 
1299     $form->save_history;
 
1301   # /saving the history
 
1302   $main::lxdebug->leave_sub();
 
1305 sub statement_details {
 
1306   $main::lxdebug->enter_sub();
 
1308   $main::auth->assert('general_ledger');
 
1310   my $form     = $main::form;
 
1311   my %myconfig = %main::myconfig;
 
1312   my $locale   = $main::locale;
 
1316   push @{ $form->{invnumber} }, $ref->{invnumber};
 
1317   push @{ $form->{invdate} },   $ref->{transdate};
 
1318   push @{ $form->{duedate} },   $ref->{duedate};
 
1319   push @{ $form->{amount} },    $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
 
1320   push @{ $form->{open} },      $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
 
1322   foreach my $item (qw(c0 c30 c60 c90)) {
 
1323     if ($ref->{exchangerate} * 1) {
 
1324       # add only the open amount of the invoice to the aging, not the total amount
 
1325       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
 
1326       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
 
1327       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
 
1328       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
 
1330     $form->{"${item}total"} += $ref->{$item};
 
1331     $form->{total}          += $ref->{$item};
 
1332     push @{ $form->{$item} },
 
1333       $form->format_amount(\%myconfig, $ref->{$item}, 2);
 
1336   $main::lxdebug->leave_sub();
 
1339 sub generate_tax_report {
 
1340   $::lxdebug->enter_sub;
 
1341   $::auth->assert('report');
 
1343   RP->tax_report(\%::myconfig, $::form);
 
1345   my $descvar     = "$::form->{accno}_description";
 
1346   my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
 
1350   my $callback = build_std_url('action=generate_tax_report', $descvar,
 
1351     qw(fromdate todate db method accno department report title));
 
1353   my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
 
1356   for my $item (@columns, 'subtotal') {
 
1357     if ($::form->{"l_$item"} eq "Y") {
 
1358       $callback .= "&l_$item=Y";
 
1359       $href     .= "&l_$item=Y";
 
1363   for my $item (@columns) {
 
1364     if ($::form->{"l_$item"} eq "Y") {
 
1365       push @column_index, $item;
 
1370   if ($::form->{department}) {
 
1371     my ($department) = split /--/, $::form->{department};
 
1372     push @options, $::locale->text('Department') . " : $department";
 
1375   # if there are any dates
 
1376   if ($::form->{fromdate} || $::form->{todate}) {
 
1377     my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
 
1378     my $todate   = $::form->{todate}   ? $::locale->date(\%::myconfig, $::form->{todate}, 1)   : '';
 
1379     push @options, "$fromdate - $todate";
 
1381     push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
 
1384   my ($name, $invoice, $arap);
 
1385   if ($::form->{db} eq 'ar') {
 
1386     $name    = $::locale->text('Customer');
 
1390   if ($::form->{db} eq 'ap') {
 
1391     $name    = $::locale->text('Vendor');
 
1396   my %column_header = (
 
1397     id        => $::locale->text('ID'),
 
1398     invnumber => $::locale->text('Invoice'),
 
1399     transdate => $::locale->text('Date'),
 
1400     netamount => $::locale->text('Amount'),
 
1401     tax       => $::locale->text('Tax'),
 
1402     amount    => $::locale->text('Total'),
 
1406   my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
 
1408   $callback .= "&sort=$::form->{sort}";
 
1411   if (@{ $::form->{TR} }) {
 
1412     $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
 
1415   my ($totalnetamount, $totaltax, @data);
 
1416   for my $ref (@{ $::form->{TR} }) {
 
1418     my $module = ($ref->{invoice}) ? $invoice : $arap;
 
1420     if ($::form->{l_subtotal} eq 'Y') {
 
1421       if ($sameitem ne $ref->{ $::form->{sort} }) {
 
1424           netamount => $subtotalnetamount,
 
1425           tax       => $subtotaltax,
 
1426           amount    => $subtotal,
 
1428         $subtotalnetamount = 0;
 
1430         $sameitem          = $ref->{ $::form->{sort} };
 
1434     $subtotalnetamount += $ref->{netamount};
 
1435     $subtotaltax       += $ref->{tax};
 
1436     $totalnetamount    += $ref->{netamount};
 
1437     $totaltax          += $ref->{tax};
 
1438     $ref->{amount}      = $ref->{netamount} + $ref->{tax};
 
1440     push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
 
1441     $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
 
1442     $data[-1]{$_}{numeric}     = 1 for qw(netamount tax amount);
 
1445   if ($::form->{l_subtotal} eq 'Y') {
 
1448       netamount => $subtotalnetamount,
 
1449       tax       => $subtotaltax,
 
1450       amount    => $subtotal,
 
1456     netamount => $totalnetamount,
 
1458     amount    => $totalnetamount + $totaltax,
 
1462   print $::form->parse_html_template('rp/tax_report', {
 
1463     column_index  => \@column_index,
 
1464     column_header => \%column_header,
 
1465     column_sorted => \%column_sorted,
 
1468     options       => \@options,
 
1471   $::lxdebug->leave_sub;
 
1475   $main::lxdebug->enter_sub();
 
1477   $main::auth->assert('cash');
 
1479   my $form     = $main::form;
 
1480   my %myconfig = %main::myconfig;
 
1481   my $locale   = $main::locale;
 
1483   if ($form->{account}) {
 
1484     ($form->{paymentaccounts}) = split /--/, $form->{account};
 
1488   if ($form->{department}) {
 
1489     (my $department, $form->{department_id}) = split /--/, $form->{department};
 
1490     $option = $locale->text('Department') . " : $department";
 
1493   report_generator_set_default_sort('transdate', 1);
 
1495   RP->payments(\%myconfig, \%$form);
 
1497   my @hidden_variables = qw(account title department reference source memo fromdate todate
 
1498                             fx_transaction db prepayment paymentaccounts sort);
 
1500   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
 
1501   $form->{callback} = $href;
 
1503   my @columns     = qw(transdate invnumber name paid source memo);
 
1505     'name'      => { 'text' => $locale->text('Description'), },
 
1506     'invnumber' => { 'text' => $locale->text('Reference'), },
 
1507     'transdate' => { 'text' => $locale->text('Date'), },
 
1508     'paid'      => { 'text' => $locale->text('Amount'), },
 
1509     'source'    => { 'text' => $locale->text('Source'), },
 
1510     'memo'      => { 'text' => $locale->text('Memo'), },
 
1512   my %column_alignment = ('paid' => 'right');
 
1514   foreach my $name (grep { $_ ne 'paid' } @columns) {
 
1515     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
1516     $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
 
1520   if ($form->{fromdate}) {
 
1521     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
 
1523   if ($form->{todate}) {
 
1524     push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1527   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
1529   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
 
1531   $report->set_options('top_info_text'         => join("\n", @options),
 
1532                        'output_format'         => 'HTML',
 
1533                        'title'                 => $form->{title},
 
1534                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1535                        'std_column_visibility' => 1,
 
1537   $report->set_options_from_form();
 
1539   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1541   $report->set_columns(%column_defs);
 
1542   $report->set_column_order(@columns);
 
1544   $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
 
1546   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
1550   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
 
1551     next unless @{ $form->{ $ref->{id} } };
 
1553     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
 
1555     my $subtotal_paid = 0;
 
1557     foreach my $payment (@{ $form->{ $ref->{id} } }) {
 
1558       my $module = $payment->{module};
 
1559       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
 
1560       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
 
1562       $subtotal_paid += $payment->{paid};
 
1563       $total_paid    += $payment->{paid};
 
1565       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
 
1569       foreach my $column (@columns) {
 
1571           'data'  => $payment->{$column},
 
1572           'align' => $column_alignment{$column},
 
1576       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
 
1578       $report->add_data($row);
 
1581     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
 
1583       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
 
1585       'class' => 'listsubtotal',
 
1588     $report->add_data($row);
 
1591   $report->add_separator();
 
1593   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
 
1595     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
 
1597     'class' => 'listtotal',
 
1600   $report->add_data($row);
 
1602   $report->generate_with_headers();
 
1604   $main::lxdebug->leave_sub();
 
1608   $::lxdebug->enter_sub;
 
1610   my ($dont_print) = @_;
 
1612   $::form->{sendmode} = "attachment";
 
1613   $::form->{format} ||= $::myconfig{template_format} || "pdf";
 
1614   $::form->{copies} ||= $::myconfig{copies}          || 2;
 
1616   $::form->{PD}{ $::form->{type} }     = "selected";
 
1617   $::form->{DF}{ $::form->{format} }   = "selected";
 
1618   $::form->{OP}{ $::form->{media} }    = "selected";
 
1619   $::form->{SM}{ $::form->{sendmode} } = "selected";
 
1621   my $output = $::form->parse_html_template('rp/print_options', {
 
1622     is_email    => $::form->{media} eq 'email',
 
1625   print $output unless $dont_print;
 
1627   $::lxdebug->leave_sub;
 
1633   $main::lxdebug->enter_sub();
 
1635   $main::auth->assert('report');
 
1637   my $form     = $main::form;
 
1638   my %myconfig = %main::myconfig;
 
1639   my $locale   = $main::locale;
 
1641   $form->{padding} = "  ";
 
1642   $form->{bold}    = "<b>";
 
1643   $form->{endbold} = "</b>";
 
1644   $form->{br}      = "<br>";
 
1646   if ($form->{reporttype} eq "custom") {
 
1648     #forgotten the year --> thisyear
 
1649     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
1650       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
1656     if ($form->{duetyp} eq "13") {
 
1657       $form->{fromdate}        = "1.1.$form->{year}";
 
1658       $form->{todate}          = "31.12.$form->{year}";
 
1659       $form->{comparefromdate} = "1.01.$form->{year}";
 
1660       $form->{comparetodate}   = "31.12.$form->{year}";
 
1664     if ($form->{duetyp} eq "A") {
 
1665       $form->{fromdate}        = "1.1.$form->{year}";
 
1666       $form->{todate}          = "31.3.$form->{year}";
 
1667       $form->{comparefromdate} = "1.01.$form->{year}";
 
1668       $form->{comparetodate}   = "31.03.$form->{year}";
 
1670     if ($form->{duetyp} eq "B") {
 
1671       $form->{fromdate}        = "1.4.$form->{year}";
 
1672       $form->{todate}          = "30.6.$form->{year}";
 
1673       $form->{comparefromdate} = "1.01.$form->{year}";
 
1674       $form->{comparetodate}   = "30.06.$form->{year}";
 
1676     if ($form->{duetyp} eq "C") {
 
1677       $form->{fromdate}        = "1.7.$form->{year}";
 
1678       $form->{todate}          = "30.9.$form->{year}";
 
1679       $form->{comparefromdate} = "1.01.$form->{year}";
 
1680       $form->{comparetodate}   = "30.09.$form->{year}";
 
1682     if ($form->{duetyp} eq "D") {
 
1683       $form->{fromdate}        = "1.10.$form->{year}";
 
1684       $form->{todate}          = "31.12.$form->{year}";
 
1685       $form->{comparefromdate} = "1.01.$form->{year}";
 
1686       $form->{comparetodate}   = "31.12.$form->{year}";
 
1691       $form->{duetyp} eq "1" && do {
 
1692         $form->{fromdate}        = "1.1.$form->{year}";
 
1693         $form->{todate}          = "31.1.$form->{year}";
 
1694         $form->{comparefromdate} = "1.01.$form->{year}";
 
1695         $form->{comparetodate}   = "31.01.$form->{year}";
 
1698       $form->{duetyp} eq "2" && do {
 
1699         $form->{fromdate} = "1.2.$form->{year}";
 
1701         #this works from 1901 to 2099, 1900 and 2100 fail.
 
1702         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
1703         $form->{todate}          = "$leap.2.$form->{year}";
 
1704         $form->{comparefromdate} = "1.01.$form->{year}";
 
1705         $form->{comparetodate}   = "$leap.02.$form->{year}";
 
1708       $form->{duetyp} eq "3" && do {
 
1709         $form->{fromdate}        = "1.3.$form->{year}";
 
1710         $form->{todate}          = "31.3.$form->{year}";
 
1711         $form->{comparefromdate} = "1.01.$form->{year}";
 
1712         $form->{comparetodate}   = "31.03.$form->{year}";
 
1715       $form->{duetyp} eq "4" && do {
 
1716         $form->{fromdate}        = "1.4.$form->{year}";
 
1717         $form->{todate}          = "30.4.$form->{year}";
 
1718         $form->{comparefromdate} = "1.01.$form->{year}";
 
1719         $form->{comparetodate}   = "30.04.$form->{year}";
 
1722       $form->{duetyp} eq "5" && do {
 
1723         $form->{fromdate}        = "1.5.$form->{year}";
 
1724         $form->{todate}          = "31.5.$form->{year}";
 
1725         $form->{comparefromdate} = "1.01.$form->{year}";
 
1726         $form->{comparetodate}   = "31.05.$form->{year}";
 
1729       $form->{duetyp} eq "6" && do {
 
1730         $form->{fromdate}        = "1.6.$form->{year}";
 
1731         $form->{todate}          = "30.6.$form->{year}";
 
1732         $form->{comparefromdate} = "1.01.$form->{year}";
 
1733         $form->{comparetodate}   = "30.06.$form->{year}";
 
1736       $form->{duetyp} eq "7" && do {
 
1737         $form->{fromdate}        = "1.7.$form->{year}";
 
1738         $form->{todate}          = "31.7.$form->{year}";
 
1739         $form->{comparefromdate} = "1.01.$form->{year}";
 
1740         $form->{comparetodate}   = "31.07.$form->{year}";
 
1743       $form->{duetyp} eq "8" && do {
 
1744         $form->{fromdate}        = "1.8.$form->{year}";
 
1745         $form->{todate}          = "31.8.$form->{year}";
 
1746         $form->{comparefromdate} = "1.01.$form->{year}";
 
1747         $form->{comparetodate}   = "31.08.$form->{year}";
 
1750       $form->{duetyp} eq "9" && do {
 
1751         $form->{fromdate}        = "1.9.$form->{year}";
 
1752         $form->{todate}          = "30.9.$form->{year}";
 
1753         $form->{comparefromdate} = "1.01.$form->{year}";
 
1754         $form->{comparetodate}   = "30.09.$form->{year}";
 
1757       $form->{duetyp} eq "10" && do {
 
1758         $form->{fromdate}        = "1.10.$form->{year}";
 
1759         $form->{todate}          = "31.10.$form->{year}";
 
1760         $form->{comparefromdate} = "1.01.$form->{year}";
 
1761         $form->{comparetodate}   = "31.10.$form->{year}";
 
1764       $form->{duetyp} eq "11" && do {
 
1765         $form->{fromdate}        = "1.11.$form->{year}";
 
1766         $form->{todate}          = "30.11.$form->{year}";
 
1767         $form->{comparefromdate} = "1.01.$form->{year}";
 
1768         $form->{comparetodate}   = "30.11.$form->{year}";
 
1771       $form->{duetyp} eq "12" && do {
 
1772         $form->{fromdate}        = "1.12.$form->{year}";
 
1773         $form->{todate}          = "31.12.$form->{year}";
 
1774         $form->{comparefromdate} = "1.01.$form->{year}";
 
1775         $form->{comparetodate}   = "31.12.$form->{year}";
 
1779     hotfix_reformat_date();
 
1781     # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
 
1782     # ansonsten ist die prüfung in RP.pm
 
1783     # if (defined ($form->{fromdate|todate}=='..'))
 
1785     if ($form->{fromdate}){
 
1786       my $datetime = $locale->parse_date_to_object($form->{fromdate});
 
1787       $datetime->set( month      => 1,
 
1789       $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
 
1791     if ($form->{todate}){
 
1792       $form->{comparetodate}   = $form->{todate};
 
1796   RP->bwa(\%myconfig, \%$form);
 
1798   ($form->{department}) = split /--/, $form->{department};
 
1801     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
1802   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
1804   # if there are any dates construct a where
 
1805   if ($form->{fromdate} || $form->{todate}) {
 
1807     unless ($form->{todate}) {
 
1808       $form->{todate} = $form->current_date(\%myconfig);
 
1811     my %germandate = ("dateformat" => "dd.mm.yyyy");
 
1813     my $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
 
1814     my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
 
1816     my $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
 
1817     my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
 
1819     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
1821         $locale->text('for Period')
 
1822       . qq|\n$longfromdate |
 
1823       . $locale->text('bis')
 
1827   $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
 
1829   if ( $form->{method} eq 'cash' ) {
 
1830     $form->{accounting_method} = $locale->text('Cash accounting');
 
1831   } elsif ( $form->{method} eq 'accrual' ) {
 
1832     $form->{accounting_method} = $locale->text('Accrual accounting');
 
1834     $form->{accounting_method} = "";
 
1837   $form->{title} = $locale->text('BWA');
 
1839   $::request->layout->add_stylesheets('bwa.css');
 
1841   print $form->parse_html_template('rp/bwa');
 
1843   $main::lxdebug->leave_sub();
 
1846 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
 
1847 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
 
1848 sub hotfix_reformat_date {
 
1850   $main::lxdebug->enter_sub();
 
1852   my $form     = $main::form;
 
1853   my %myconfig = %main::myconfig;
 
1854   my $locale   = $main::locale;
 
1856   if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
 
1857     my $current_dateformat = $myconfig{dateformat};
 
1858     $myconfig{dateformat} = 'dd.mm.yyyy';
 
1859     $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
 
1860     $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
 
1861     $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
 
1862       unless (!defined ($form->{comparefromdate}));
 
1863     $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
 
1864       unless (!defined ($form->{comparetodate}));
 
1866     # Und wieder zurücksetzen
 
1867     $myconfig{dateformat} =  $current_dateformat; #'dd.mm.yyyy';
 
1868   } # Ende Hotifx Bug 1388
 
1870   $main::lxdebug->leave_sub();
 
1874 sub setup_rp_aging_action_bar {
 
1877   return unless $params{arap} eq 'ar';
 
1879   for my $bar ($::request->layout->get('actionbar')) {
 
1884           call   => [ 'kivi.SalesPurchase.show_print_dialog' ],
 
1885           checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
 
1889           call   => [ 'kivi.SalesPurchase.show_email_dialog', 'send_email' ],
 
1890           checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
 
1897 sub setup_rp_report_action_bar {
 
1900   for my $bar ($::request->layout->get('actionbar')) {
 
1904         submit    => [ '#form', { action => 'continue' } ],
 
1905         accesskey => 'enter',