1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
   9 # Copyright (c) 1998-2002
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  15 #  Contributors: Antonio Gallardo <agssa@ibw.com.ni>
 
  16 #                Benjamin Lee <benjaminlee@consultant.com>
 
  17 #                Philip Reetz <p.reetz@linet-services.de>
 
  20 # This program is free software; you can redistribute it and/or modify
 
  21 # it under the terms of the GNU General Public License as published by
 
  22 # the Free Software Foundation; either version 2 of the License, or
 
  23 # (at your option) any later version.
 
  25 # This program is distributed in the hope that it will be useful,
 
  26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  28 # GNU General Public License for more details.
 
  29 # You should have received a copy of the GNU General Public License
 
  30 # along with this program; if not, write to the Free Software
 
  31 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  32 #======================================================================
 
  34 # module for preparing Income Statement and Balance Sheet
 
  36 #======================================================================
 
  38 use POSIX qw(strftime);
 
  45 use SL::ReportGenerator;
 
  47 use List::MoreUtils qw(any);
 
  49 require "bin/mozilla/arap.pl";
 
  50 require "bin/mozilla/common.pl";
 
  51 require "bin/mozilla/reportgenerator.pl";
 
  53 # note: this file was particularly hard to strictify.
 
  54 # alot of the vars are passed carelessly between invocations
 
  55 # should there be any missing vars, declare them globally
 
  58 # this is for our long dates
 
  59 # $locale->text('January')
 
  60 # $locale->text('February')
 
  61 # $locale->text('March')
 
  62 # $locale->text('April')
 
  63 # $locale->text('May ')
 
  64 # $locale->text('June')
 
  65 # $locale->text('July')
 
  66 # $locale->text('August')
 
  67 # $locale->text('September')
 
  68 # $locale->text('October')
 
  69 # $locale->text('November')
 
  70 # $locale->text('December')
 
  72 # this is for our short month
 
  73 # $locale->text('Jan')
 
  74 # $locale->text('Feb')
 
  75 # $locale->text('Mar')
 
  76 # $locale->text('Apr')
 
  77 # $locale->text('May')
 
  78 # $locale->text('Jun')
 
  79 # $locale->text('Jul')
 
  80 # $locale->text('Aug')
 
  81 # $locale->text('Sep')
 
  82 # $locale->text('Oct')
 
  83 # $locale->text('Nov')
 
  84 # $locale->text('Dec')
 
  86 # $locale->text('Balance Sheet')
 
  87 # $locale->text('Income Statement')
 
  88 # $locale->text('Trial Balance')
 
  89 # $locale->text('AR Aging')
 
  90 # $locale->text('AP Aging')
 
  91 # $locale->text('Search AR Aging')
 
  92 # $locale->text('Search AP Aging')
 
  93 # $locale->text('Tax collected')
 
  94 # $locale->text('Tax paid')
 
  95 # $locale->text('Receipts')
 
  96 # $locale->text('Payments')
 
  97 # $locale->text('Project Transactions')
 
  98 # $locale->text('Business evaluation')
 
 100 # $form->parse_html_template('rp/html_report_susa')
 
 102 my $rp_access_map = {
 
 103   'projects'         => 'report',
 
 104   'ar_aging'         => 'general_ledger',
 
 105   'ap_aging'         => 'general_ledger',
 
 106   'receipts'         => 'cash',
 
 107   'payments'         => 'cash',
 
 108   'trial_balance'    => 'report',
 
 109   'income_statement' => 'report',
 
 111   'balance_sheet'    => 'report',
 
 114 sub check_rp_access {
 
 115   my $form     = $main::form;
 
 117   my $right   = $rp_access_map->{$form->{report}};
 
 118   $right    ||= 'DOES_NOT_EXIST';
 
 120   $main::auth->assert($right);
 
 124   $::lxdebug->enter_sub;
 
 129     balance_sheet        => $::locale->text('Balance Sheet'),
 
 130     income_statement     => $::locale->text('Income Statement'),
 
 131     trial_balance        => $::locale->text('Trial Balance'),
 
 132     ar_aging             => $::locale->text('Search AR Aging'),
 
 133     ap_aging             => $::locale->text('Search AP Aging'),
 
 134     tax_collected        => $::locale->text('Tax collected'),
 
 135     tax_paid             => $::locale->text('Tax paid'),
 
 136     receipts             => $::locale->text('Receipts'),
 
 137     payments             => $::locale->text('Payments'),
 
 138     projects             => $::locale->text('Project Transactions'),
 
 139     bwa                  => $::locale->text('Business evaluation'),
 
 142   $::form->{title} = $title{$::form->{report}};
 
 145   $::form->all_departments(\%::myconfig);
 
 146   if (@{ $::form->{all_departments} || [] }) {
 
 147     $::form->{selectdepartment} = "<option>\n";
 
 148     map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
 
 151   $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
 
 153   my $is_projects         = $::form->{report} eq "projects";
 
 154   my $is_income_statement = $::form->{report} eq "income_statement";
 
 155   my $is_bwa              = $::form->{report} eq "bwa";
 
 156   my $is_balance_sheet    = $::form->{report} eq "balance_sheet";
 
 157   my $is_trial_balance    = $::form->{report} eq "trial_balance";
 
 158   my $is_aging            = $::form->{report} =~ /^a[rp]_aging$/;
 
 159   my $is_payments         = $::form->{report} =~ /(receipts|payments)$/;
 
 161   my ($label, $nextsub, $vc);
 
 163     my $is_sales  = $::form->{report} eq 'ar_aging';
 
 164     $label        = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
 
 165     $::form->{vc} = $is_sales ? 'customer' : 'vendor';
 
 167     $nextsub = "generate_$::form->{report}";
 
 170     $::form->all_vc(\%::myconfig, $::form->{vc}, $is_sales ? "AR" : "AP");
 
 171     $vc .= "<option>$_->{name}--$_->{id}\n" for @{ $::form->{"all_$::form->{vc}"} };
 
 173         ? qq|<select name=$::form->{vc} class="initial_focus"><option>\n$vc</select>|
 
 174         : qq|<input name=$::form->{vc} size=35 class="initial_focus">|;
 
 177   my ($selection, $paymentaccounts);
 
 179     $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
 
 181     RP->paymentaccounts(\%::myconfig, $::form);
 
 183     $selection = "<option>\n";
 
 184     for my $ref (@{ $::form->{PR} }) {
 
 185       $paymentaccounts .= "$ref->{accno} ";
 
 186       $selection       .= "<option>$ref->{accno}--$ref->{description}\n";
 
 191   print $::form->parse_html_template('rp/report', {
 
 192     paymentaccounts     => $paymentaccounts,
 
 193     selection           => $selection,
 
 194     is_aging            => $is_aging,
 
 197     year                => DateTime->today->year,
 
 198     today               => DateTime->today,
 
 200     accrual             => $::instance_conf->get_accounting_method ne 'cash',
 
 201     cash                => $::instance_conf->get_accounting_method eq 'cash',
 
 202     is_payments         => $is_payments,
 
 203     is_trial_balance    => $is_trial_balance,
 
 204     is_balance_sheet    => $is_balance_sheet,
 
 206     is_income_statement => $is_income_statement,
 
 207     is_projects         => $is_projects,
 
 210   $::lxdebug->leave_sub;
 
 213 sub continue { call_sub($main::form->{"nextsub"}); }
 
 215 sub generate_income_statement {
 
 216   $main::lxdebug->enter_sub();
 
 218   $main::auth->assert('report');
 
 220   my $form     = $main::form;
 
 221   my %myconfig = %main::myconfig;
 
 222   my $locale   = $main::locale;
 
 224   $form->{padding} = "  ";
 
 225   $form->{bold}    = "<b>";
 
 226   $form->{endbold} = "</b>";
 
 227   $form->{br}      = "<br>";
 
 229   if ($form->{reporttype} eq "custom") {
 
 231     #forgotten the year --> thisyear
 
 232     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 233       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 239     if ($form->{duetyp} eq "13") {
 
 240       $form->{fromdate} = "1.1.$form->{year}";
 
 241       $form->{todate}   = "31.12.$form->{year}";
 
 245     if ($form->{duetyp} eq "A") {
 
 246       $form->{fromdate} = "1.1.$form->{year}";
 
 247       $form->{todate}   = "31.3.$form->{year}";
 
 249     if ($form->{duetyp} eq "B") {
 
 250       $form->{fromdate} = "1.4.$form->{year}";
 
 251       $form->{todate}   = "30.6.$form->{year}";
 
 253     if ($form->{duetyp} eq "C") {
 
 254       $form->{fromdate} = "1.7.$form->{year}";
 
 255       $form->{todate}   = "30.9.$form->{year}";
 
 257     if ($form->{duetyp} eq "D") {
 
 258       $form->{fromdate} = "1.10.$form->{year}";
 
 259       $form->{todate}   = "31.12.$form->{year}";
 
 264       $form->{duetyp} eq "1" && do {
 
 265         $form->{fromdate} = "1.1.$form->{year}";
 
 266         $form->{todate}   = "31.1.$form->{year}";
 
 269       $form->{duetyp} eq "2" && do {
 
 270         $form->{fromdate} = "1.2.$form->{year}";
 
 272         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 273         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 274         $form->{todate} = "$leap.2.$form->{year}";
 
 277       $form->{duetyp} eq "3" && do {
 
 278         $form->{fromdate} = "1.3.$form->{year}";
 
 279         $form->{todate}   = "31.3.$form->{year}";
 
 282       $form->{duetyp} eq "4" && do {
 
 283         $form->{fromdate} = "1.4.$form->{year}";
 
 284         $form->{todate}   = "30.4.$form->{year}";
 
 287       $form->{duetyp} eq "5" && do {
 
 288         $form->{fromdate} = "1.5.$form->{year}";
 
 289         $form->{todate}   = "31.5.$form->{year}";
 
 292       $form->{duetyp} eq "6" && do {
 
 293         $form->{fromdate} = "1.6.$form->{year}";
 
 294         $form->{todate}   = "30.6.$form->{year}";
 
 297       $form->{duetyp} eq "7" && do {
 
 298         $form->{fromdate} = "1.7.$form->{year}";
 
 299         $form->{todate}   = "31.7.$form->{year}";
 
 302       $form->{duetyp} eq "8" && do {
 
 303         $form->{fromdate} = "1.8.$form->{year}";
 
 304         $form->{todate}   = "31.8.$form->{year}";
 
 307       $form->{duetyp} eq "9" && do {
 
 308         $form->{fromdate} = "1.9.$form->{year}";
 
 309         $form->{todate}   = "30.9.$form->{year}";
 
 312       $form->{duetyp} eq "10" && do {
 
 313         $form->{fromdate} = "1.10.$form->{year}";
 
 314         $form->{todate}   = "31.10.$form->{year}";
 
 317       $form->{duetyp} eq "11" && do {
 
 318         $form->{fromdate} = "1.11.$form->{year}";
 
 319         $form->{todate}   = "30.11.$form->{year}";
 
 322       $form->{duetyp} eq "12" && do {
 
 323         $form->{fromdate} = "1.12.$form->{year}";
 
 324         $form->{todate}   = "31.12.$form->{year}";
 
 328     hotfix_reformat_date();
 
 329   } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
 
 331   RP->income_statement(\%myconfig, \%$form);
 
 333   ($form->{department}) = split /--/, $form->{department};
 
 336     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 337   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
 339   # if there are any dates construct a where
 
 340   if ($form->{fromdate} || $form->{todate}) {
 
 342     unless ($form->{todate}) {
 
 343       $form->{todate} = $form->current_date(\%myconfig);
 
 346     my $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
 
 347     my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
 
 349     my $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 350     my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
 
 352     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
 354         $locale->text('for Period')
 
 355       . qq|\n$longfromdate |
 
 356       . $locale->text('Bis')
 
 360   if ($form->{comparefromdate} || $form->{comparetodate}) {
 
 361     my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
 
 362     my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
 
 364     my $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
 
 365     my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
 
 367     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
 
 369         "\n$longcomparefromdate "
 
 370       . $locale->text('Bis')
 
 371       . qq| $longcomparetodate|;
 
 374   if ( $::instance_conf->get_profit_determination eq 'balance' ) {
 
 375     $form->{title} = $locale->text('Income Statement');
 
 376   } elsif ( $::instance_conf->get_profit_determination eq 'income' ) {
 
 377     $form->{title} = $locale->text('Net Income Statement');
 
 382   if ( $form->{method} eq 'cash' ) {
 
 383     $form->{accounting_method} = $locale->text('Cash accounting');
 
 384   } elsif ( $form->{method} eq 'accrual' ) {
 
 385     $form->{accounting_method} = $locale->text('Accrual accounting');
 
 387     $form->{accounting_method} = "";
 
 390   $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
 
 393   print $form->parse_html_template('rp/income_statement');
 
 395   $main::lxdebug->leave_sub();
 
 398 sub generate_balance_sheet {
 
 399   $::lxdebug->enter_sub;
 
 400   $::auth->assert('report');
 
 402   $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
 
 403   $::form->{padding}       = "  ";
 
 404   $::form->{bold}          = "<b>";
 
 405   $::form->{endbold}       = "</b>";
 
 406   $::form->{br}            = "<br>";
 
 408   my $data = RP->balance_sheet(\%::myconfig, $::form);
 
 410   $::form->{asofdate}    ||= $::form->current_date;
 
 411   $::form->{report_title}  = $::locale->text('Balance Sheet');
 
 412   $::form->{report_date} ||= $::form->current_date;
 
 414   ($::form->{department}) = split /--/, $::form->{department};
 
 416   # define Current Earnings account
 
 417   my $padding = $::form->{l_heading} ? $::form->{padding} : "";
 
 418   push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
 
 420   $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
 
 421   $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
 
 423 #  balance sheet isn't read from print templates anymore,
 
 424 #  instead use template in rp
 
 425 #  $::form->{IN} = "balance_sheet.html";
 
 428   print $::form->parse_html_template('rp/balance_sheet', $data);
 
 430   $::lxdebug->leave_sub;
 
 433 sub generate_projects {
 
 434   $main::lxdebug->enter_sub();
 
 436   $main::auth->assert('report');
 
 438   my $form     = $main::form;
 
 439   my %myconfig = %main::myconfig;
 
 440   my $locale   = $main::locale;
 
 442   my $project            = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
 
 443   $form->{projectnumber} = $project ? $project->projectnumber : '';
 
 445   $form->{nextsub} = "generate_projects";
 
 446   $form->{title}   = $locale->text('Project Transactions');
 
 447   RP->trial_balance(\%myconfig, \%$form);
 
 449   list_accounts('generate_projects');
 
 451   $main::lxdebug->leave_sub();
 
 457 # included links to display transactions for period entered
 
 458 # added headers and subtotals
 
 460 sub generate_trial_balance {
 
 461   $main::lxdebug->enter_sub();
 
 463   $main::auth->assert('report');
 
 465   my $form     = $main::form;
 
 466   my %myconfig = %main::myconfig;
 
 467   my $locale   = $main::locale;
 
 468   my $defaults = SL::DB::Default->get;
 
 470   if ($form->{reporttype} eq "custom") {
 
 472     #forgotten the year --> thisyear
 
 473     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 474       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 480     if ($form->{duetyp} eq "13") {
 
 481       $form->{fromdate} = "1.1.$form->{year}";
 
 482       $form->{todate}   = "31.12.$form->{year}";
 
 486     if ($form->{duetyp} eq "A") {
 
 487       $form->{fromdate} = "1.1.$form->{year}";
 
 488       $form->{todate}   = "31.3.$form->{year}";
 
 490     if ($form->{duetyp} eq "B") {
 
 491       $form->{fromdate} = "1.4.$form->{year}";
 
 492       $form->{todate}   = "30.6.$form->{year}";
 
 494     if ($form->{duetyp} eq "C") {
 
 495       $form->{fromdate} = "1.7.$form->{year}";
 
 496       $form->{todate}   = "30.9.$form->{year}";
 
 498     if ($form->{duetyp} eq "D") {
 
 499       $form->{fromdate} = "1.10.$form->{year}";
 
 500       $form->{todate}   = "31.12.$form->{year}";
 
 505       $form->{duetyp} eq "1" && do {
 
 506         $form->{fromdate} = "1.1.$form->{year}";
 
 507         $form->{todate}   = "31.1.$form->{year}";
 
 510       $form->{duetyp} eq "2" && do {
 
 511         $form->{fromdate} = "1.2.$form->{year}";
 
 513         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 514         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 515         $form->{todate} = "$leap.2.$form->{year}";
 
 518       $form->{duetyp} eq "3" && do {
 
 519         $form->{fromdate} = "1.3.$form->{year}";
 
 520         $form->{todate}   = "31.3.$form->{year}";
 
 523       $form->{duetyp} eq "4" && do {
 
 524         $form->{fromdate} = "1.4.$form->{year}";
 
 525         $form->{todate}   = "30.4.$form->{year}";
 
 528       $form->{duetyp} eq "5" && do {
 
 529         $form->{fromdate} = "1.5.$form->{year}";
 
 530         $form->{todate}   = "31.5.$form->{year}";
 
 533       $form->{duetyp} eq "6" && do {
 
 534         $form->{fromdate} = "1.6.$form->{year}";
 
 535         $form->{todate}   = "30.6.$form->{year}";
 
 538       $form->{duetyp} eq "7" && do {
 
 539         $form->{fromdate} = "1.7.$form->{year}";
 
 540         $form->{todate}   = "31.7.$form->{year}";
 
 543       $form->{duetyp} eq "8" && do {
 
 544         $form->{fromdate} = "1.8.$form->{year}";
 
 545         $form->{todate}   = "31.8.$form->{year}";
 
 548       $form->{duetyp} eq "9" && do {
 
 549         $form->{fromdate} = "1.9.$form->{year}";
 
 550         $form->{todate}   = "30.9.$form->{year}";
 
 553       $form->{duetyp} eq "10" && do {
 
 554         $form->{fromdate} = "1.10.$form->{year}";
 
 555         $form->{todate}   = "31.10.$form->{year}";
 
 558       $form->{duetyp} eq "11" && do {
 
 559         $form->{fromdate} = "1.11.$form->{year}";
 
 560         $form->{todate}   = "30.11.$form->{year}";
 
 563       $form->{duetyp} eq "12" && do {
 
 564         $form->{fromdate} = "1.12.$form->{year}";
 
 565         $form->{todate}   = "31.12.$form->{year}";
 
 569     hotfix_reformat_date();
 
 573   # get for each account initial balance, debits and credits
 
 574   RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
 
 577   $form->{rowcount} = scalar @{ $form->{TB} || [] };
 
 578   $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
 
 581     "accno",               "description",
 
 582     "last_transaction",    "soll_eb",
 
 585     "soll_kumuliert",      "haben_kumuliert",
 
 586     "soll_saldo",          "haben_saldo"
 
 590   my $attachment_basename = $locale->text('trial_balance');
 
 591   my $report              = SL::ReportGenerator->new(\%myconfig, $form);
 
 593   my @hidden_variables    = qw(fromdate todate year method);
 
 595   my $href                = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
 
 598     'accno'               => { 'text' => $locale->text('Account'), },
 
 599     'description'         => { 'text' => $locale->text('Description'), },
 
 600     'last_transaction'    => { 'text' => $locale->text('Last Transaction'), },
 
 601     'soll_eb'             => { 'text' => $locale->text('Debit Starting Balance'), },
 
 602     'haben_eb'            => { 'text' => $locale->text('Credit Starting Balance'), },
 
 603     'soll'                => { 'text' => $locale->text('Debit'), },
 
 604     'haben'               => { 'text' => $locale->text('Credit'), },
 
 605     'soll_kumuliert'      => { 'text' => $locale->text('Sum Debit'), },
 
 606     'haben_kumuliert'     => { 'text' => $locale->text('Sum Credit'), },
 
 607     'soll_saldo'          => { 'text' => $locale->text('Saldo Debit'), },
 
 608     'haben_saldo'         => { 'text' => $locale->text('Saldo Credit'), }
 
 613   my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 615   map { $column_defs{$_}->{visible} =  1 } @columns;
 
 617   $report->set_columns(%column_defs);
 
 618   $report->set_column_order(@columns);
 
 620   $report->set_export_options('generate_trial_balance', @hidden_variables);
 
 625   $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
 
 627   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
 
 628   push (@options, $form->{print_date});
 
 630   $form->{company} = $locale->text('Company') . " " . $defaults->company;
 
 631   push (@options, $form->{company});
 
 634   $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
 
 636   my @custom_headers = ([
 
 637     { text => $::locale->text('Account'),          rowspan => 2, },
 
 638     { text => $::locale->text('Description'),      rowspan => 2, },
 
 639     { text => $::locale->text('Last Transaction'), rowspan => 2, },
 
 640     { text => $::locale->text('Starting Balance'), colspan => 2, },
 
 641     { text => $::locale->text('Sum for')   . " $form->{template_fromto}", colspan => 2, },
 
 642     { text => $::locale->text('Sum per')   . " $form->{template_to}",     colspan => 2, },
 
 643     { text => $::locale->text('Saldo per') . " $form->{template_to}",     colspan => 2, },
 
 648     { text => $::locale->text('Assets'), },
 
 649     { text => $::locale->text('Equity'), },
 
 650     { text => $::locale->text('Debit'),  },
 
 651     { text => $::locale->text('Credit'), },
 
 652     { text => $::locale->text('Debit'),  },
 
 653     { text => $::locale->text('Credit'), },
 
 654     { text => $::locale->text('Debit'),  },
 
 655     { text => $::locale->text('Credit'), },
 
 658   $report->set_options('output_format'        => 'HTML',
 
 659                        'top_info_text'        => join("\n", @options),
 
 660                        'title'                => $form->{title},
 
 661                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
 662                        'html_template'        => 'rp/html_report_susa',
 
 663                        'pdf_template'         => 'rp/html_report_susa',
 
 665   $report->set_custom_headers(@custom_headers);
 
 666   $report->set_options_from_form();
 
 667   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 669   # add sort and escape callback, this one we use for the add sub
 
 670   $form->{callback} = $href .= "&sort=$form->{sort}";
 
 672   # escape callback for href
 
 673   my $callback = $form->escape($href);
 
 675   my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 677   my %totals    = map { $_ => 0 } @subtotal_columns;
 
 679   my $edit_url = build_std_url('action=edit', 'type', 'vc');
 
 682   foreach my $accno (@{ $form->{TB} || [] }) {
 
 684     $accno->{soll} = $accno->{debit};
 
 685     $accno->{haben} = $accno->{credit};
 
 686     map { $totals{$_}    += $accno->{$_} } @subtotal_columns;
 
 688     map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
 
 689       qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 693     foreach my $column (@columns) {
 
 695         'data'  => $accno->{$column},
 
 696         'align' => $column_alignment{$column},
 
 700     $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}));
 
 702     my $row_set = [ $row ];
 
 705     $report->add_data($row_set);
 
 710   $report->add_separator();
 
 712   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
 714   $report->generate_with_headers();
 
 716   $main::lxdebug->leave_sub();
 
 720 sub create_subtotal_row {
 
 721   $main::lxdebug->enter_sub();
 
 723   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
 725   my $form     = $main::form;
 
 726   my %myconfig = %main::myconfig;
 
 727   my $locale   = $main::locale;
 
 729   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
 731   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
 733   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
 
 735   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
 737   $main::lxdebug->leave_sub();
 
 742 sub create_list_accounts_subtotal_row {
 
 743   $main::lxdebug->enter_sub();
 
 745   my ($subtotals, $columns, $fields, $class) = @_;
 
 747   my $form     = $main::form;
 
 748   my %myconfig = %main::myconfig;
 
 749   my $locale   = $main::locale;
 
 751   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 753   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
 
 755   $main::lxdebug->leave_sub();
 
 761   $main::lxdebug->enter_sub();
 
 765   my $form     = $main::form;
 
 766   my %myconfig = %main::myconfig;
 
 767   my $locale   = $main::locale;
 
 770   if ($form->{department}) {
 
 771     my ($department) = split /--/, $form->{department};
 
 772     push @options, $locale->text('Department') . " : $department";
 
 774   if ($form->{projectnumber}) {
 
 775     push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
 
 778   # if there are any dates
 
 779   if ($form->{fromdate} || $form->{todate}) {
 
 780     my ($fromdate, $todate);
 
 782     if ($form->{fromdate}) {
 
 783       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 785     if ($form->{todate}) {
 
 786       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
 
 789     push @options, "$fromdate - $todate";
 
 792     push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 795   my @columns     = qw(accno description begbalance debit credit endbalance);
 
 797     'accno'       => { 'text' => $locale->text('Account'), },
 
 798     'description' => { 'text' => $locale->text('Description'), },
 
 799     'debit'       => { 'text' => $locale->text('Debit'), },
 
 800     'credit'      => { 'text' => $locale->text('Credit'), },
 
 801     'begbalance'  => { 'text' => $locale->text('Balance'), },
 
 802     'endbalance'  => { 'text' => $locale->text('Balance'), },
 
 804   my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
 
 806   my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
 
 808   $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
 
 810   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 812   $report->set_options('top_info_text'         => join("\n", @options),
 
 813                        'output_format'         => 'HTML',
 
 814                        'title'                 => $form->{title},
 
 815                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
 
 816                        'std_column_visibility' => 1,
 
 818   $report->set_options_from_form();
 
 819   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 821   $report->set_columns(%column_defs);
 
 822   $report->set_column_order(@columns);
 
 824   $report->set_export_options($action, @hidden_variables);
 
 826   my @totals_columns = qw(credit debit begbalance endbalance);
 
 827   my %subtotals      = map { $_ => 0 } @totals_columns;
 
 828   my %totals         = map { $_ => 0 } @totals_columns;
 
 829   my $found_heading  = 0;
 
 830   my @tb             = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
 
 832   # sort the whole thing by account numbers and display
 
 833   foreach my $idx (0 .. scalar(@tb) - 1) {
 
 835     my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
 
 837     my $ml   = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
 
 839     my $row  = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
 
 841     if ($ref->{charttype} eq 'H') {
 
 842       next unless ($form->{l_heading});
 
 844       %subtotals                   = map { $_ => 0 } @totals_columns;
 
 846       $row->{description}->{class} = 'listheading';
 
 847       $row->{description}->{data}  = $ref->{description};
 
 849       $report->add_data($row);
 
 854     foreach (qw(debit credit)) {
 
 855       $subtotals{$_} += $ref->{$_};
 
 856       $totals{$_}    += $ref->{$_};
 
 859     $subtotals{begbalance} += $ref->{balance} * $ml;
 
 860     $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
 
 862     map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
 
 863     map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
 
 865     $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
 
 866     $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
 
 868     $report->add_data($row);
 
 870     if ($form->{l_heading} && $found_heading &&
 
 871         (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
 
 872       $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
 
 876   $report->add_separator();
 
 878   $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
 
 880   $report->generate_with_headers();
 
 882   $main::lxdebug->leave_sub();
 
 885 sub generate_ar_aging {
 
 886   $main::lxdebug->enter_sub();
 
 888   $main::auth->assert('general_ledger');
 
 890   my $form     = $main::form;
 
 891   my %myconfig = %main::myconfig;
 
 892   my $locale   = $main::locale;
 
 895   ($form->{customer}) = split(/--/, $form->{customer});
 
 897   $form->{ct}   = "customer";
 
 898   $form->{arap} = "ar";
 
 900   $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
 
 902   RP->aging(\%myconfig, \%$form);
 
 905   $main::lxdebug->leave_sub();
 
 908 sub generate_ap_aging {
 
 909   $main::lxdebug->enter_sub();
 
 911   $main::auth->assert('general_ledger');
 
 913   my $form     = $main::form;
 
 914   my %myconfig = %main::myconfig;
 
 915   my $locale   = $main::locale;
 
 918   ($form->{vendor}) = split(/--/, $form->{vendor});
 
 920   $form->{ct}   = "vendor";
 
 921   $form->{arap} = "ap";
 
 923   $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
 
 925   RP->aging(\%myconfig, \%$form);
 
 928   $main::lxdebug->leave_sub();
 
 931 sub create_aging_subtotal_row {
 
 932   $main::lxdebug->enter_sub();
 
 934   my ($subtotals, $columns, $periods, $class) = @_;
 
 936   my $form     = $main::form;
 
 937   my %myconfig = %main::myconfig;
 
 938   my $locale   = $main::locale;
 
 940   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 942   foreach (@{ $periods }) {
 
 943     $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
 
 944     $subtotals->{$_}      = 0;
 
 947   $main::lxdebug->leave_sub();
 
 953   $main::lxdebug->enter_sub();
 
 955   $main::auth->assert('general_ledger');
 
 957   my $form     = $main::form;
 
 958   my %myconfig = %main::myconfig;
 
 959   my $locale   = $main::locale;
 
 960   my $cgi      = $::request->{cgi};
 
 962   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 964   my @columns = qw(statement ct invnumber transdate duedate amount open);
 
 967     'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
 
 968     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
 
 969     'invnumber' => { 'text' => $locale->text('Invoice'), },
 
 970     'transdate' => { 'text' => $locale->text('Date'), },
 
 971     'duedate'   => { 'text' => $locale->text('Due'), },
 
 972     'amount'    => { 'text' => $locale->text('Amount'), },
 
 973     'open'      => { 'text' => $locale->text('Open'), },
 
 976   my %column_alignment = ('statement' => 'center',
 
 977                           map { $_ => 'right' } qw(open amount));
 
 979   $report->set_options('std_column_visibility' => 1);
 
 980   $report->set_columns(%column_defs);
 
 981   $report->set_column_order(@columns);
 
 983   my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype);
 
 984   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
 
 987   my $attachment_basename;
 
 989   if ($form->{department}) {
 
 990     my ($department) = split /--/, $form->{department};
 
 991     push @options, $locale->text('Department') . " : $department";
 
 992     $form->{callback} .= "&department=" . E($department);
 
 995   if (($form->{arap} eq 'ar') && $form->{customer}) {
 
 996     push @options, $form->{customer};
 
 997     $attachment_basename = $locale->text('ar_aging_list');
 
 998     $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
 
1001   if (($form->{arap} eq 'ap') && $form->{vendor}) {
 
1002     push @options, $form->{vendor};
 
1003     $attachment_basename = $locale->text('ap_aging_list');
 
1004     $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
 
1007   if ($form->{fromdate}) {
 
1008     push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1010     push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1013   $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
 
1015   $report->set_options('top_info_text'        => join("\n", @options),
 
1016                        'output_format'        => 'HTML',
 
1017                        'title'                => $form->{title},
 
1018                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1020   $report->set_options_from_form();
 
1021   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1023   my $previous_ctid = 0;
 
1025   my @periods       = qw(open amount);
 
1026   my %subtotals     = map { $_ => 0 } @periods;
 
1027   my %totals        = map { $_ => 0 } @periods;
 
1029   foreach my $ref (@{ $form->{AG} }) {
 
1030     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
 
1031       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
 
1034     foreach my $key (@periods) {
 
1035       $subtotals{$key}  += $ref->{"$key"};
 
1036       $totals{$key}     += $ref->{"$key"};
 
1037       $ref->{"$key"}  = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
 
1042     foreach my $column (@columns) {
 
1044         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
 
1045         'align'  => $column_alignment{$column},
 
1046         'valign' => $column eq 'statement' ? 'center' : '',
 
1050     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
 
1052     if ($previous_ctid != $ref->{ctid}) {
 
1053       $row->{statement}->{raw_data} =
 
1054           $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
 
1055         . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
 
1056       $row->{ct}->{data} = $ref->{name};
 
1061     $previous_ctid = $ref->{ctid};
 
1063     $report->add_data($row);
 
1066   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
 
1068   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
 
1070   if ($form->{arap} eq 'ar') {
 
1071     my $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
 
1072     my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
 
1073                                                                                'PRINT_OPTIONS' => print_options(inline => 1), });
 
1074     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
 
1075                          'raw_bottom_info_text' => $raw_bottom_info_text);
 
1078   $report->generate_with_headers();
 
1080   $main::lxdebug->leave_sub();
 
1084   $main::lxdebug->enter_sub();
 
1086   my $form     = $main::form;
 
1087   my %myconfig = %main::myconfig;
 
1088   my $locale   = $main::locale;
 
1090   RP->aging(\%myconfig, \%$form);
 
1092   map { $_->{checked} = "checked" } @{ $form->{AG} };
 
1096   $main::lxdebug->leave_sub();
 
1100   $::lxdebug->enter_sub;
 
1101   $::auth->assert('general_ledger');
 
1103   # get name and email addresses
 
1105   for my $i (1 .. $::form->{rowcount}) {
 
1106     next unless $::form->{"statement_$i"};
 
1107     $::form->{"$::form->{ct}_id"} = $::form->{"$::form->{ct}_id_$i"};
 
1108     RP->get_customer(\%::myconfig, $::form);
 
1113   $::form->error($::locale->text('Nothing selected!')) unless $selected;
 
1115   $::form->{media} = "email";
 
1117   # save all other variables
 
1119   for my $key (keys %$::form) {
 
1120     next if any { $key eq $_ } qw(login password action email cc bcc subject message type sendmode format header);
 
1121     next unless '' eq ref $::form->{$key};
 
1122     push @hidden_values, $key;
 
1126   print $::form->parse_html_template('rp/e_mail', {
 
1127     show_bcc      => $::auth->assert('email_bcc', 'may fail'),
 
1128     print_options => print_options(inline => 1),
 
1129     hidden_values => \@hidden_values,
 
1132   $::lxdebug->leave_sub;
 
1136   $main::lxdebug->enter_sub();
 
1138   $main::auth->assert('general_ledger');
 
1140   my $form     = $main::form;
 
1141   my %myconfig = %main::myconfig;
 
1142   my $locale   = $main::locale;
 
1144   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
 
1145     unless $form->{subject};
 
1147   RP->aging(\%myconfig, \%$form);
 
1149   $form->{"statement_1"} = 1;
 
1151   $form->{media} = 'email';
 
1154   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
 
1156   $main::lxdebug->leave_sub();
 
1160   $main::lxdebug->enter_sub();
 
1162   $main::auth->assert('general_ledger');
 
1164   my $form     = $main::form;
 
1165   my %myconfig = %main::myconfig;
 
1166   my $locale   = $main::locale;
 
1168   if ($form->{media} eq 'printer') {
 
1169     $form->error($locale->text('Select postscript or PDF!'))
 
1170       if ($form->{format} !~ /(postscript|pdf)/);
 
1174   for my $i (1 .. $form->{rowcount}) {
 
1175     if ($form->{"statement_$i"}) {
 
1176       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
 
1182   $form->error($locale->text('Nothing selected!')) unless $selected;
 
1184   if ($form->{media} eq 'printer') {
 
1185     $form->{"$form->{ct}_id"} = "";
 
1187     $form->{"statement_1"} = 1;
 
1190   RP->aging(\%myconfig, \%$form);
 
1194   $form->redirect($locale->text('Statements sent to printer!'))
 
1195     if ($form->{media} eq 'printer');
 
1197   $main::lxdebug->leave_sub();
 
1201   $main::lxdebug->enter_sub();
 
1203   $main::auth->assert('general_ledger');
 
1205   my $form     = $main::form;
 
1206   my %myconfig = %main::myconfig;
 
1207   my $locale   = $main::locale;
 
1209   my $defaults = SL::DB::Default->get;
 
1210   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
 
1211   $form->{templates} = $defaults->templates;
 
1213   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
 
1215   my $suffix = "html";
 
1216   my $attachment_suffix = "html";
 
1217   if ($form->{format} eq 'postscript') {
 
1218     $form->{postscript} = 1;
 
1220     $attachment_suffix = "ps";
 
1221   } elsif ($form->{format} eq 'pdf') {
 
1224     $attachment_suffix = "pdf";
 
1227   $form->{IN}  = "$form->{type}.$suffix";
 
1228   $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
 
1230   # Save $form->{email} because it will be overwritten.
 
1231   $form->{EMAIL_RECIPIENT} = $form->{email};
 
1235   while (@{ $form->{AG} }) {
 
1237     my $ref = shift @{ $form->{AG} };
 
1239     if ($ctid != $ref->{ctid}) {
 
1241       $ctid = $ref->{ctid};
 
1244       if ($form->{"statement_$i"}) {
 
1247           ("name", "street", "zipcode", "city", "country", "contact", "email",
 
1248            "$form->{ct}phone", "$form->{ct}fax");
 
1249         map { $form->{$_} = $ref->{$_} } @a;
 
1251         $form->{ $form->{ct} } = $form->{name};
 
1252         $form->{"$form->{ct}_id"} = $ref->{ctid};
 
1254         map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
 
1256         foreach my $item (qw(c0 c30 c60 c90)) {
 
1257           $form->{$item} = ();
 
1258           $form->{"${item}total"} = 0;
 
1261         &statement_details($ref);
 
1265           if (scalar(@{ $form->{AG} }) > 0) {
 
1267             # one or more left to go
 
1268             if ($ctid == $form->{AG}->[0]->{ctid}) {
 
1269               $ref = shift @{ $form->{AG} };
 
1270               &statement_details($ref);
 
1273               $ref = scalar(@{ $form->{AG} });
 
1279             # set initial ref to 0
 
1286           $form->{"${_}total"} =
 
1287             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
 
1288         } ('c0', 'c30', 'c60', 'c90', "");
 
1290         $form->{attachment_filename} =  $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
 
1291         $form->{attachment_filename} =~ s/\s+/_/g;
 
1293         $form->parse_template(\%myconfig);
 
1298   # saving the history
 
1299   if(!exists $form->{addition} && $form->{id} ne "") {
 
1300     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1301     $form->{addition} = "PRINTED";
 
1302     $form->{what_done} = $form->{type};
 
1303     $form->save_history;
 
1305   # /saving the history
 
1306   $main::lxdebug->leave_sub();
 
1309 sub statement_details {
 
1310   $main::lxdebug->enter_sub();
 
1312   $main::auth->assert('general_ledger');
 
1314   my $form     = $main::form;
 
1315   my %myconfig = %main::myconfig;
 
1316   my $locale   = $main::locale;
 
1320   push @{ $form->{invnumber} }, $ref->{invnumber};
 
1321   push @{ $form->{invdate} },   $ref->{transdate};
 
1322   push @{ $form->{duedate} },   $ref->{duedate};
 
1323   push @{ $form->{amount} },    $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
 
1324   push @{ $form->{open} },      $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
 
1326   foreach my $item (qw(c0 c30 c60 c90)) {
 
1327     if ($ref->{exchangerate} * 1) {
 
1328       # add only the open amount of the invoice to the aging, not the total amount
 
1329       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
 
1330       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
 
1331       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
 
1332       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
 
1334     $form->{"${item}total"} += $ref->{$item};
 
1335     $form->{total}          += $ref->{$item};
 
1336     push @{ $form->{$item} },
 
1337       $form->format_amount(\%myconfig, $ref->{$item}, 2);
 
1340   $main::lxdebug->leave_sub();
 
1343 sub generate_tax_report {
 
1344   $::lxdebug->enter_sub;
 
1345   $::auth->assert('report');
 
1347   RP->tax_report(\%::myconfig, $::form);
 
1349   my $descvar     = "$::form->{accno}_description";
 
1350   my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
 
1354   my $callback = build_std_url('action=generate_tax_report', $descvar,
 
1355     qw(fromdate todate db method accno department report title));
 
1357   my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
 
1360   for my $item (@columns, 'subtotal') {
 
1361     if ($::form->{"l_$item"} eq "Y") {
 
1362       $callback .= "&l_$item=Y";
 
1363       $href     .= "&l_$item=Y";
 
1367   for my $item (@columns) {
 
1368     if ($::form->{"l_$item"} eq "Y") {
 
1369       push @column_index, $item;
 
1374   if ($::form->{department}) {
 
1375     my ($department) = split /--/, $::form->{department};
 
1376     push @options, $::locale->text('Department') . " : $department";
 
1379   # if there are any dates
 
1380   if ($::form->{fromdate} || $::form->{todate}) {
 
1381     my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
 
1382     my $todate   = $::form->{todate}   ? $::locale->date(\%::myconfig, $::form->{todate}, 1)   : '';
 
1383     push @options, "$fromdate - $todate";
 
1385     push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
 
1388   my ($name, $invoice, $arap);
 
1389   if ($::form->{db} eq 'ar') {
 
1390     $name    = $::locale->text('Customer');
 
1394   if ($::form->{db} eq 'ap') {
 
1395     $name    = $::locale->text('Vendor');
 
1400   my %column_header = (
 
1401     id        => $::locale->text('ID'),
 
1402     invnumber => $::locale->text('Invoice'),
 
1403     transdate => $::locale->text('Date'),
 
1404     netamount => $::locale->text('Amount'),
 
1405     tax       => $::locale->text('Tax'),
 
1406     amount    => $::locale->text('Total'),
 
1410   my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
 
1412   $callback .= "&sort=$::form->{sort}";
 
1415   if (@{ $::form->{TR} }) {
 
1416     $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
 
1419   my ($totalnetamount, $totaltax, @data);
 
1420   for my $ref (@{ $::form->{TR} }) {
 
1422     my $module = ($ref->{invoice}) ? $invoice : $arap;
 
1424     if ($::form->{l_subtotal} eq 'Y') {
 
1425       if ($sameitem ne $ref->{ $::form->{sort} }) {
 
1428           netamount => $subtotalnetamount,
 
1429           tax       => $subtotaltax,
 
1430           amount    => $subtotal,
 
1432         $subtotalnetamount = 0;
 
1434         $sameitem          = $ref->{ $::form->{sort} };
 
1438     $subtotalnetamount += $ref->{netamount};
 
1439     $subtotaltax       += $ref->{tax};
 
1440     $totalnetamount    += $ref->{netamount};
 
1441     $totaltax          += $ref->{tax};
 
1442     $ref->{amount}      = $ref->{netamount} + $ref->{tax};
 
1444     push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
 
1445     $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
 
1446     $data[-1]{$_}{numeric}     = 1 for qw(netamount tax amount);
 
1449   if ($::form->{l_subtotal} eq 'Y') {
 
1452       netamount => $subtotalnetamount,
 
1453       tax       => $subtotaltax,
 
1454       amount    => $subtotal,
 
1460     netamount => $totalnetamount,
 
1462     amount    => $totalnetamount + $totaltax,
 
1466   print $::form->parse_html_template('rp/tax_report', {
 
1467     column_index  => \@column_index,
 
1468     column_header => \%column_header,
 
1469     column_sorted => \%column_sorted,
 
1472     options       => \@options,
 
1475   $::lxdebug->leave_sub;
 
1479   $main::lxdebug->enter_sub();
 
1481   $main::auth->assert('cash');
 
1483   my $form     = $main::form;
 
1484   my %myconfig = %main::myconfig;
 
1485   my $locale   = $main::locale;
 
1487   if ($form->{account}) {
 
1488     ($form->{paymentaccounts}) = split /--/, $form->{account};
 
1492   if ($form->{department}) {
 
1493     (my $department, $form->{department_id}) = split /--/, $form->{department};
 
1494     $option = $locale->text('Department') . " : $department";
 
1497   report_generator_set_default_sort('transdate', 1);
 
1499   RP->payments(\%myconfig, \%$form);
 
1501   my @hidden_variables = qw(account title department reference source memo fromdate todate
 
1502                             fx_transaction db prepayment paymentaccounts sort);
 
1504   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
 
1505   $form->{callback} = $href;
 
1507   my @columns     = qw(transdate invnumber name paid source memo);
 
1509     'name'      => { 'text' => $locale->text('Description'), },
 
1510     'invnumber' => { 'text' => $locale->text('Reference'), },
 
1511     'transdate' => { 'text' => $locale->text('Date'), },
 
1512     'paid'      => { 'text' => $locale->text('Amount'), },
 
1513     'source'    => { 'text' => $locale->text('Source'), },
 
1514     'memo'      => { 'text' => $locale->text('Memo'), },
 
1516   my %column_alignment = ('paid' => 'right');
 
1518   foreach my $name (grep { $_ ne 'paid' } @columns) {
 
1519     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
1520     $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
 
1524   if ($form->{fromdate}) {
 
1525     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
 
1527   if ($form->{todate}) {
 
1528     push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1531   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
1533   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
 
1535   $report->set_options('top_info_text'         => join("\n", @options),
 
1536                        'output_format'         => 'HTML',
 
1537                        'title'                 => $form->{title},
 
1538                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1539                        'std_column_visibility' => 1,
 
1541   $report->set_options_from_form();
 
1543   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1545   $report->set_columns(%column_defs);
 
1546   $report->set_column_order(@columns);
 
1548   $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
 
1550   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
1554   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
 
1555     next unless @{ $form->{ $ref->{id} } };
 
1557     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
 
1559     my $subtotal_paid = 0;
 
1561     foreach my $payment (@{ $form->{ $ref->{id} } }) {
 
1562       my $module = $payment->{module};
 
1563       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
 
1564       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
 
1566       $subtotal_paid += $payment->{paid};
 
1567       $total_paid    += $payment->{paid};
 
1569       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
 
1573       foreach my $column (@columns) {
 
1575           'data'  => $payment->{$column},
 
1576           'align' => $column_alignment{$column},
 
1580       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
 
1582       $report->add_data($row);
 
1585     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
 
1587       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
 
1589       'class' => 'listsubtotal',
 
1592     $report->add_data($row);
 
1595   $report->add_separator();
 
1597   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
 
1599     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
 
1601     'class' => 'listtotal',
 
1604   $report->add_data($row);
 
1606   $report->generate_with_headers();
 
1608   $main::lxdebug->leave_sub();
 
1612   $::lxdebug->enter_sub;
 
1614   my ($dont_print) = @_;
 
1616   $::form->{sendmode} = "attachment";
 
1617   $::form->{format} ||= $::myconfig{template_format} || "pdf";
 
1618   $::form->{copies} ||= $::myconfig{copies}          || 2;
 
1620   $::form->{PD}{ $::form->{type} }     = "selected";
 
1621   $::form->{DF}{ $::form->{format} }   = "selected";
 
1622   $::form->{OP}{ $::form->{media} }    = "selected";
 
1623   $::form->{SM}{ $::form->{sendmode} } = "selected";
 
1625   my $output = $::form->parse_html_template('rp/print_options', {
 
1626     got_printer => $::myconfig{printer},
 
1627     show_latex  => $::lx_office_conf{print_templates}->{latex},
 
1628     is_email    => $::form->{media} eq 'email',
 
1631   print $output unless $dont_print;
 
1633   $::lxdebug->leave_sub;
 
1639   $main::lxdebug->enter_sub();
 
1641   $main::auth->assert('report');
 
1643   my $form     = $main::form;
 
1644   my %myconfig = %main::myconfig;
 
1645   my $locale   = $main::locale;
 
1647   $form->{padding} = "  ";
 
1648   $form->{bold}    = "<b>";
 
1649   $form->{endbold} = "</b>";
 
1650   $form->{br}      = "<br>";
 
1652   if ($form->{reporttype} eq "custom") {
 
1654     #forgotten the year --> thisyear
 
1655     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
1656       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
1662     if ($form->{duetyp} eq "13") {
 
1663       $form->{fromdate}        = "1.1.$form->{year}";
 
1664       $form->{todate}          = "31.12.$form->{year}";
 
1665       $form->{comparefromdate} = "1.01.$form->{year}";
 
1666       $form->{comparetodate}   = "31.12.$form->{year}";
 
1670     if ($form->{duetyp} eq "A") {
 
1671       $form->{fromdate}        = "1.1.$form->{year}";
 
1672       $form->{todate}          = "31.3.$form->{year}";
 
1673       $form->{comparefromdate} = "1.01.$form->{year}";
 
1674       $form->{comparetodate}   = "31.03.$form->{year}";
 
1676     if ($form->{duetyp} eq "B") {
 
1677       $form->{fromdate}        = "1.4.$form->{year}";
 
1678       $form->{todate}          = "30.6.$form->{year}";
 
1679       $form->{comparefromdate} = "1.01.$form->{year}";
 
1680       $form->{comparetodate}   = "30.06.$form->{year}";
 
1682     if ($form->{duetyp} eq "C") {
 
1683       $form->{fromdate}        = "1.7.$form->{year}";
 
1684       $form->{todate}          = "30.9.$form->{year}";
 
1685       $form->{comparefromdate} = "1.01.$form->{year}";
 
1686       $form->{comparetodate}   = "30.09.$form->{year}";
 
1688     if ($form->{duetyp} eq "D") {
 
1689       $form->{fromdate}        = "1.10.$form->{year}";
 
1690       $form->{todate}          = "31.12.$form->{year}";
 
1691       $form->{comparefromdate} = "1.01.$form->{year}";
 
1692       $form->{comparetodate}   = "31.12.$form->{year}";
 
1697       $form->{duetyp} eq "1" && do {
 
1698         $form->{fromdate}        = "1.1.$form->{year}";
 
1699         $form->{todate}          = "31.1.$form->{year}";
 
1700         $form->{comparefromdate} = "1.01.$form->{year}";
 
1701         $form->{comparetodate}   = "31.01.$form->{year}";
 
1704       $form->{duetyp} eq "2" && do {
 
1705         $form->{fromdate} = "1.2.$form->{year}";
 
1707         #this works from 1901 to 2099, 1900 and 2100 fail.
 
1708         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
1709         $form->{todate}          = "$leap.2.$form->{year}";
 
1710         $form->{comparefromdate} = "1.01.$form->{year}";
 
1711         $form->{comparetodate}   = "$leap.02.$form->{year}";
 
1714       $form->{duetyp} eq "3" && do {
 
1715         $form->{fromdate}        = "1.3.$form->{year}";
 
1716         $form->{todate}          = "31.3.$form->{year}";
 
1717         $form->{comparefromdate} = "1.01.$form->{year}";
 
1718         $form->{comparetodate}   = "31.03.$form->{year}";
 
1721       $form->{duetyp} eq "4" && do {
 
1722         $form->{fromdate}        = "1.4.$form->{year}";
 
1723         $form->{todate}          = "30.4.$form->{year}";
 
1724         $form->{comparefromdate} = "1.01.$form->{year}";
 
1725         $form->{comparetodate}   = "30.04.$form->{year}";
 
1728       $form->{duetyp} eq "5" && do {
 
1729         $form->{fromdate}        = "1.5.$form->{year}";
 
1730         $form->{todate}          = "31.5.$form->{year}";
 
1731         $form->{comparefromdate} = "1.01.$form->{year}";
 
1732         $form->{comparetodate}   = "31.05.$form->{year}";
 
1735       $form->{duetyp} eq "6" && do {
 
1736         $form->{fromdate}        = "1.6.$form->{year}";
 
1737         $form->{todate}          = "30.6.$form->{year}";
 
1738         $form->{comparefromdate} = "1.01.$form->{year}";
 
1739         $form->{comparetodate}   = "30.06.$form->{year}";
 
1742       $form->{duetyp} eq "7" && do {
 
1743         $form->{fromdate}        = "1.7.$form->{year}";
 
1744         $form->{todate}          = "31.7.$form->{year}";
 
1745         $form->{comparefromdate} = "1.01.$form->{year}";
 
1746         $form->{comparetodate}   = "31.07.$form->{year}";
 
1749       $form->{duetyp} eq "8" && do {
 
1750         $form->{fromdate}        = "1.8.$form->{year}";
 
1751         $form->{todate}          = "31.8.$form->{year}";
 
1752         $form->{comparefromdate} = "1.01.$form->{year}";
 
1753         $form->{comparetodate}   = "31.08.$form->{year}";
 
1756       $form->{duetyp} eq "9" && do {
 
1757         $form->{fromdate}        = "1.9.$form->{year}";
 
1758         $form->{todate}          = "30.9.$form->{year}";
 
1759         $form->{comparefromdate} = "1.01.$form->{year}";
 
1760         $form->{comparetodate}   = "30.09.$form->{year}";
 
1763       $form->{duetyp} eq "10" && do {
 
1764         $form->{fromdate}        = "1.10.$form->{year}";
 
1765         $form->{todate}          = "31.10.$form->{year}";
 
1766         $form->{comparefromdate} = "1.01.$form->{year}";
 
1767         $form->{comparetodate}   = "31.10.$form->{year}";
 
1770       $form->{duetyp} eq "11" && do {
 
1771         $form->{fromdate}        = "1.11.$form->{year}";
 
1772         $form->{todate}          = "30.11.$form->{year}";
 
1773         $form->{comparefromdate} = "1.01.$form->{year}";
 
1774         $form->{comparetodate}   = "30.11.$form->{year}";
 
1777       $form->{duetyp} eq "12" && do {
 
1778         $form->{fromdate}        = "1.12.$form->{year}";
 
1779         $form->{todate}          = "31.12.$form->{year}";
 
1780         $form->{comparefromdate} = "1.01.$form->{year}";
 
1781         $form->{comparetodate}   = "31.12.$form->{year}";
 
1785     hotfix_reformat_date();
 
1787     # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
 
1788     # ansonsten ist die prüfung in RP.pm
 
1789     # if (defined ($form->{fromdate|todate}=='..'))
 
1791     if ($form->{fromdate}){
 
1792       my ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
 
1793       my $datetime = $locale->parse_date_to_object(\%myconfig, $form->{fromdate});
 
1794       $datetime->set( month      => 1,
 
1796       $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
 
1798     if ($form->{todate}){
 
1799       $form->{comparetodate}   = $form->{todate};
 
1803   RP->bwa(\%myconfig, \%$form);
 
1805   ($form->{department}) = split /--/, $form->{department};
 
1808     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
1809   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
1811   # if there are any dates construct a where
 
1812   if ($form->{fromdate} || $form->{todate}) {
 
1814     unless ($form->{todate}) {
 
1815       $form->{todate} = $form->current_date(\%myconfig);
 
1818     my %germandate = ("dateformat" => "dd.mm.yyyy");
 
1820     my $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
 
1821     my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
 
1823     my $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
 
1824     my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
 
1826     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
1828         $locale->text('for Period')
 
1829       . qq|\n$longfromdate |
 
1830       . $locale->text('bis')
 
1834   $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
 
1836   if ( $form->{method} eq 'cash' ) {
 
1837     $form->{accounting_method} = $locale->text('Cash accounting');
 
1838   } elsif ( $form->{method} eq 'accrual' ) {
 
1839     $form->{accounting_method} = $locale->text('Accrual accounting');
 
1841     $form->{accounting_method} = "";
 
1844   $form->{title} = $locale->text('BWA');
 
1847   print $form->parse_html_template('rp/bwa');
 
1849   $main::lxdebug->leave_sub();
 
1852 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
 
1853 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
 
1854 sub hotfix_reformat_date {
 
1856   $main::lxdebug->enter_sub();
 
1858   my $form     = $main::form;
 
1859   my %myconfig = %main::myconfig;
 
1860   my $locale   = $main::locale;
 
1862   if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
 
1863     my $current_dateformat = $myconfig{dateformat};
 
1864     $myconfig{dateformat} = 'dd.mm.yyyy';
 
1865     $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
 
1866     $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
 
1867     $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
 
1868       unless (!defined ($form->{comparefromdate}));
 
1869     $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
 
1870       unless (!defined ($form->{comparetodate}));
 
1872     # Und wieder zurücksetzen
 
1873     $myconfig{dateformat} =  $current_dateformat; #'dd.mm.yyyy';
 
1874   } # Ende Hotifx Bug 1388
 
1876   $main::lxdebug->leave_sub();