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);
 
  46 use SL::ReportGenerator;
 
  48 use List::MoreUtils qw(any);
 
  50 require "bin/mozilla/arap.pl";
 
  51 require "bin/mozilla/common.pl";
 
  52 require "bin/mozilla/reportgenerator.pl";
 
  54 # note: this file was particularly hard to strictify.
 
  55 # alot of the vars are passed carelessly between invocations
 
  56 # should there be any missing vars, declare them globally
 
  59 # this is for our long dates
 
  60 # $locale->text('January')
 
  61 # $locale->text('February')
 
  62 # $locale->text('March')
 
  63 # $locale->text('April')
 
  64 # $locale->text('May ')
 
  65 # $locale->text('June')
 
  66 # $locale->text('July')
 
  67 # $locale->text('August')
 
  68 # $locale->text('September')
 
  69 # $locale->text('October')
 
  70 # $locale->text('November')
 
  71 # $locale->text('December')
 
  73 # this is for our short month
 
  74 # $locale->text('Jan')
 
  75 # $locale->text('Feb')
 
  76 # $locale->text('Mar')
 
  77 # $locale->text('Apr')
 
  78 # $locale->text('May')
 
  79 # $locale->text('Jun')
 
  80 # $locale->text('Jul')
 
  81 # $locale->text('Aug')
 
  82 # $locale->text('Sep')
 
  83 # $locale->text('Oct')
 
  84 # $locale->text('Nov')
 
  85 # $locale->text('Dec')
 
  87 # $locale->text('Balance Sheet')
 
  88 # $locale->text('Income Statement')
 
  89 # $locale->text('Trial Balance')
 
  90 # $locale->text('AR Aging')
 
  91 # $locale->text('AP Aging')
 
  92 # $locale->text('Search AR Aging')
 
  93 # $locale->text('Search AP Aging')
 
  94 # $locale->text('Tax collected')
 
  95 # $locale->text('Tax paid')
 
  96 # $locale->text('Receipts')
 
  97 # $locale->text('Payments')
 
  98 # $locale->text('Project Transactions')
 
  99 # $locale->text('Business evaluation')
 
 101 # $form->parse_html_template('rp/html_report_susa')
 
 103 my $rp_access_map = {
 
 104   'projects'           => 'report',
 
 105   'ar_aging'           => 'general_ledger',
 
 106   'ap_aging'           => 'general_ledger',
 
 107   'receipts'           => 'cash',
 
 108   'payments'           => 'cash',
 
 109   'trial_balance'      => 'report',
 
 110   'income_statement'   => 'report',
 
 111   'erfolgsrechnung'    => 'report',
 
 113   'balance_sheet'      => 'report',
 
 116 sub check_rp_access {
 
 117   my $form     = $main::form;
 
 119   my $right   = $rp_access_map->{$form->{report}};
 
 120   $right    ||= 'DOES_NOT_EXIST';
 
 122   $main::auth->assert($right);
 
 126   $::lxdebug->enter_sub;
 
 131     balance_sheet        => $::locale->text('Balance Sheet'),
 
 132     income_statement     => $::locale->text('Income Statement'),
 
 133     erfolgsrechnung      => $::locale->text('Erfolgsrechnung'),
 
 134     trial_balance        => $::locale->text('Trial Balance'),
 
 135     ar_aging             => $::locale->text('Search AR Aging'),
 
 136     ap_aging             => $::locale->text('Search AP Aging'),
 
 137     tax_collected        => $::locale->text('Tax collected'),
 
 138     tax_paid             => $::locale->text('Tax paid'),
 
 139     receipts             => $::locale->text('Receipts'),
 
 140     payments             => $::locale->text('Payments'),
 
 141     projects             => $::locale->text('Project Transactions'),
 
 142     bwa                  => $::locale->text('Business evaluation'),
 
 145   $::form->{title} = $title{$::form->{report}};
 
 146   $::request->{layout}->add_javascripts('autocomplete_customer.js');
 
 147   $::form->{fromdate} = DateTime->today->truncate(to => 'year')->to_kivitendo;
 
 148   $::form->{todate} = DateTime->today->truncate(to => 'year')->add(years => 1)->add(days => -1)->to_kivitendo;
 
 151   $::form->all_departments(\%::myconfig);
 
 152   if (@{ $::form->{all_departments} || [] }) {
 
 153     $::form->{selectdepartment} = "<option>\n";
 
 154     map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
 
 157   $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
 
 159   my $is_projects            = $::form->{report} eq "projects";
 
 160   my $is_income_statement    = $::form->{report} eq "income_statement";
 
 161   my $is_erfolgsrechnung     = $::form->{report} eq "erfolgsrechnung";
 
 162   my $is_bwa                 = $::form->{report} eq "bwa";
 
 163   my $is_balance_sheet       = $::form->{report} eq "balance_sheet";
 
 164   my $is_trial_balance       = $::form->{report} eq "trial_balance";
 
 165   my $is_aging               = $::form->{report} =~ /^a[rp]_aging$/;
 
 166   my $is_payments            = $::form->{report} =~ /(receipts|payments)$/;
 
 168   my ($label, $nextsub, $vc);
 
 170     my $is_sales  = $::form->{report} eq 'ar_aging';
 
 171     $label        = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
 
 172     $::form->{vc} = $is_sales ? 'customer' : 'vendor';
 
 174     $nextsub = "generate_$::form->{report}";
 
 177     $::form->all_vc(\%::myconfig, $::form->{vc}, $is_sales ? "AR" : "AP");
 
 178     $vc .= "<option>$_->{name}--$_->{id}\n" for @{ $::form->{"all_$::form->{vc}"} };
 
 180         ? qq|<select name=$::form->{vc} class="initial_focus"><option>\n$vc</select>|
 
 181         : 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";
 
 198   print $::form->parse_html_template('rp/report', {
 
 199     paymentaccounts        => $paymentaccounts,
 
 200     selection              => $selection,
 
 201     is_aging               => $is_aging,
 
 204     year                   => DateTime->today->year,
 
 205     today                  => DateTime->today,
 
 207     is_payments            => $is_payments,
 
 208     is_trial_balance       => $is_trial_balance,
 
 209     is_balance_sheet       => $is_balance_sheet,
 
 211     is_income_statement    => $is_income_statement,
 
 212     is_erfolgsrechnung     => $is_erfolgsrechnung,
 
 213     is_projects            => $is_projects,
 
 216   $::lxdebug->leave_sub;
 
 219 sub continue { call_sub($main::form->{"nextsub"}); }
 
 221 sub generate_income_statement {
 
 222   $main::lxdebug->enter_sub();
 
 224   $main::auth->assert('report');
 
 226   my $form     = $main::form;
 
 227   my %myconfig = %main::myconfig;
 
 228   my $locale   = $main::locale;
 
 230   $form->{padding} = "  ";
 
 231   $form->{bold}    = "<b>";
 
 232   $form->{endbold} = "</b>";
 
 233   $form->{br}      = "<br>";
 
 235   if ($form->{reporttype} eq "custom") {
 
 237     #forgotten the year --> thisyear
 
 238     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 239       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 245     if ($form->{duetyp} eq "13") {
 
 246       $form->{fromdate} = "1.1.$form->{year}";
 
 247       $form->{todate}   = "31.12.$form->{year}";
 
 251     if ($form->{duetyp} eq "A") {
 
 252       $form->{fromdate} = "1.1.$form->{year}";
 
 253       $form->{todate}   = "31.3.$form->{year}";
 
 255     if ($form->{duetyp} eq "B") {
 
 256       $form->{fromdate} = "1.4.$form->{year}";
 
 257       $form->{todate}   = "30.6.$form->{year}";
 
 259     if ($form->{duetyp} eq "C") {
 
 260       $form->{fromdate} = "1.7.$form->{year}";
 
 261       $form->{todate}   = "30.9.$form->{year}";
 
 263     if ($form->{duetyp} eq "D") {
 
 264       $form->{fromdate} = "1.10.$form->{year}";
 
 265       $form->{todate}   = "31.12.$form->{year}";
 
 270       $form->{duetyp} eq "1" && do {
 
 271         $form->{fromdate} = "1.1.$form->{year}";
 
 272         $form->{todate}   = "31.1.$form->{year}";
 
 275       $form->{duetyp} eq "2" && do {
 
 276         $form->{fromdate} = "1.2.$form->{year}";
 
 278         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 279         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 280         $form->{todate} = "$leap.2.$form->{year}";
 
 283       $form->{duetyp} eq "3" && do {
 
 284         $form->{fromdate} = "1.3.$form->{year}";
 
 285         $form->{todate}   = "31.3.$form->{year}";
 
 288       $form->{duetyp} eq "4" && do {
 
 289         $form->{fromdate} = "1.4.$form->{year}";
 
 290         $form->{todate}   = "30.4.$form->{year}";
 
 293       $form->{duetyp} eq "5" && do {
 
 294         $form->{fromdate} = "1.5.$form->{year}";
 
 295         $form->{todate}   = "31.5.$form->{year}";
 
 298       $form->{duetyp} eq "6" && do {
 
 299         $form->{fromdate} = "1.6.$form->{year}";
 
 300         $form->{todate}   = "30.6.$form->{year}";
 
 303       $form->{duetyp} eq "7" && do {
 
 304         $form->{fromdate} = "1.7.$form->{year}";
 
 305         $form->{todate}   = "31.7.$form->{year}";
 
 308       $form->{duetyp} eq "8" && do {
 
 309         $form->{fromdate} = "1.8.$form->{year}";
 
 310         $form->{todate}   = "31.8.$form->{year}";
 
 313       $form->{duetyp} eq "9" && do {
 
 314         $form->{fromdate} = "1.9.$form->{year}";
 
 315         $form->{todate}   = "30.9.$form->{year}";
 
 318       $form->{duetyp} eq "10" && do {
 
 319         $form->{fromdate} = "1.10.$form->{year}";
 
 320         $form->{todate}   = "31.10.$form->{year}";
 
 323       $form->{duetyp} eq "11" && do {
 
 324         $form->{fromdate} = "1.11.$form->{year}";
 
 325         $form->{todate}   = "30.11.$form->{year}";
 
 328       $form->{duetyp} eq "12" && do {
 
 329         $form->{fromdate} = "1.12.$form->{year}";
 
 330         $form->{todate}   = "31.12.$form->{year}";
 
 334     hotfix_reformat_date();
 
 335   } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
 
 337   RP->income_statement(\%myconfig, \%$form);
 
 339   ($form->{department}) = split /--/, $form->{department};
 
 342     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 343   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
 345   # if there are any dates construct a where
 
 346   if ($form->{fromdate} || $form->{todate}) {
 
 348     unless ($form->{todate}) {
 
 349       $form->{todate} = $form->current_date(\%myconfig);
 
 352     my $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
 
 353     my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
 
 355     my $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 356     my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
 
 358     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
 360         $locale->text('for Period')
 
 361       . qq|\n$longfromdate |
 
 362       . $locale->text('Bis')
 
 366   if ($form->{comparefromdate} || $form->{comparetodate}) {
 
 367     my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
 
 368     my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
 
 370     my $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
 
 371     my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
 
 373     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
 
 375         "\n$longcomparefromdate "
 
 376       . $locale->text('Bis')
 
 377       . qq| $longcomparetodate|;
 
 380   if ( $::instance_conf->get_profit_determination eq 'balance' ) {
 
 381     $form->{title} = $locale->text('Income Statement');
 
 382   } elsif ( $::instance_conf->get_profit_determination eq 'income' ) {
 
 383     $form->{title} = $locale->text('Net Income Statement');
 
 388   if ( $form->{method} eq 'cash' ) {
 
 389     $form->{accounting_method} = $locale->text('Cash accounting');
 
 390   } elsif ( $form->{method} eq 'accrual' ) {
 
 391     $form->{accounting_method} = $locale->text('Accrual accounting');
 
 393     $form->{accounting_method} = "";
 
 396   $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
 
 399   print $form->parse_html_template('rp/income_statement');
 
 401   $main::lxdebug->leave_sub();
 
 404 sub generate_erfolgsrechnung {
 
 405   $::lxdebug->enter_sub;
 
 406   $::auth->assert('report');
 
 408   $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
 
 409   $::form->{padding}       = " ";
 
 410   $::form->{bold}          = "<b>";
 
 411   $::form->{endbold}       = "</b>";
 
 412   $::form->{br}            = "<br>";
 
 414   my $data = RP->erfolgsrechnung(\%::myconfig, $::form);
 
 417   print $::form->parse_html_template('rp/erfolgsrechnung', $data);
 
 419   $::lxdebug->leave_sub;
 
 423 sub generate_balance_sheet {
 
 424   $::lxdebug->enter_sub;
 
 425   $::auth->assert('report');
 
 427   $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
 
 428   $::form->{padding}       = "  ";
 
 429   $::form->{bold}          = "<b>";
 
 430   $::form->{endbold}       = "</b>";
 
 431   $::form->{br}            = "<br>";
 
 433   my $data = RP->balance_sheet(\%::myconfig, $::form);
 
 435   $::form->{asofdate}    ||= $::form->current_date;
 
 436   $::form->{report_title}  = $::locale->text('Balance Sheet');
 
 437   $::form->{report_date} ||= $::form->current_date;
 
 439   ($::form->{department}) = split /--/, $::form->{department};
 
 441   # define Current Earnings account
 
 442   my $padding = $::form->{l_heading} ? $::form->{padding} : "";
 
 443   push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
 
 445   $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
 
 446   $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
 
 448 #  balance sheet isn't read from print templates anymore,
 
 449 #  instead use template in rp
 
 450 #  $::form->{IN} = "balance_sheet.html";
 
 453   print $::form->parse_html_template('rp/balance_sheet', $data);
 
 455   $::lxdebug->leave_sub;
 
 458 sub generate_projects {
 
 459   $main::lxdebug->enter_sub();
 
 461   $main::auth->assert('report');
 
 463   my $form     = $main::form;
 
 464   my %myconfig = %main::myconfig;
 
 465   my $locale   = $main::locale;
 
 467   my $project            = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
 
 468   $form->{projectnumber} = $project ? $project->projectnumber : '';
 
 470   $form->{nextsub} = "generate_projects";
 
 471   $form->{title}   = $locale->text('Project Transactions');
 
 472   RP->trial_balance(\%myconfig, \%$form);
 
 474   list_accounts('generate_projects');
 
 476   $main::lxdebug->leave_sub();
 
 482 # included links to display transactions for period entered
 
 483 # added headers and subtotals
 
 485 sub generate_trial_balance {
 
 486   $main::lxdebug->enter_sub();
 
 488   $main::auth->assert('report');
 
 490   my $form     = $main::form;
 
 491   my %myconfig = %main::myconfig;
 
 492   my $locale   = $main::locale;
 
 493   my $defaults = SL::DB::Default->get;
 
 495   if ($form->{reporttype} eq "custom") {
 
 497     #forgotten the year --> thisyear
 
 498     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 499       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 505     if ($form->{duetyp} eq "13") {
 
 506       $form->{fromdate} = "1.1.$form->{year}";
 
 507       $form->{todate}   = "31.12.$form->{year}";
 
 511     if ($form->{duetyp} eq "A") {
 
 512       $form->{fromdate} = "1.1.$form->{year}";
 
 513       $form->{todate}   = "31.3.$form->{year}";
 
 515     if ($form->{duetyp} eq "B") {
 
 516       $form->{fromdate} = "1.4.$form->{year}";
 
 517       $form->{todate}   = "30.6.$form->{year}";
 
 519     if ($form->{duetyp} eq "C") {
 
 520       $form->{fromdate} = "1.7.$form->{year}";
 
 521       $form->{todate}   = "30.9.$form->{year}";
 
 523     if ($form->{duetyp} eq "D") {
 
 524       $form->{fromdate} = "1.10.$form->{year}";
 
 525       $form->{todate}   = "31.12.$form->{year}";
 
 530       $form->{duetyp} eq "1" && do {
 
 531         $form->{fromdate} = "1.1.$form->{year}";
 
 532         $form->{todate}   = "31.1.$form->{year}";
 
 535       $form->{duetyp} eq "2" && do {
 
 536         $form->{fromdate} = "1.2.$form->{year}";
 
 538         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 539         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 540         $form->{todate} = "$leap.2.$form->{year}";
 
 543       $form->{duetyp} eq "3" && do {
 
 544         $form->{fromdate} = "1.3.$form->{year}";
 
 545         $form->{todate}   = "31.3.$form->{year}";
 
 548       $form->{duetyp} eq "4" && do {
 
 549         $form->{fromdate} = "1.4.$form->{year}";
 
 550         $form->{todate}   = "30.4.$form->{year}";
 
 553       $form->{duetyp} eq "5" && do {
 
 554         $form->{fromdate} = "1.5.$form->{year}";
 
 555         $form->{todate}   = "31.5.$form->{year}";
 
 558       $form->{duetyp} eq "6" && do {
 
 559         $form->{fromdate} = "1.6.$form->{year}";
 
 560         $form->{todate}   = "30.6.$form->{year}";
 
 563       $form->{duetyp} eq "7" && do {
 
 564         $form->{fromdate} = "1.7.$form->{year}";
 
 565         $form->{todate}   = "31.7.$form->{year}";
 
 568       $form->{duetyp} eq "8" && do {
 
 569         $form->{fromdate} = "1.8.$form->{year}";
 
 570         $form->{todate}   = "31.8.$form->{year}";
 
 573       $form->{duetyp} eq "9" && do {
 
 574         $form->{fromdate} = "1.9.$form->{year}";
 
 575         $form->{todate}   = "30.9.$form->{year}";
 
 578       $form->{duetyp} eq "10" && do {
 
 579         $form->{fromdate} = "1.10.$form->{year}";
 
 580         $form->{todate}   = "31.10.$form->{year}";
 
 583       $form->{duetyp} eq "11" && do {
 
 584         $form->{fromdate} = "1.11.$form->{year}";
 
 585         $form->{todate}   = "30.11.$form->{year}";
 
 588       $form->{duetyp} eq "12" && do {
 
 589         $form->{fromdate} = "1.12.$form->{year}";
 
 590         $form->{todate}   = "31.12.$form->{year}";
 
 594     hotfix_reformat_date();
 
 598   # get for each account initial balance, debits and credits
 
 599   RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
 
 602   $form->{rowcount} = scalar @{ $form->{TB} || [] };
 
 603   $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
 
 606     "accno",               "description",
 
 607     "last_transaction",    "soll_eb",
 
 610     "soll_kumuliert",      "haben_kumuliert",
 
 611     "soll_saldo",          "haben_saldo"
 
 615   my $attachment_basename = $locale->text('trial_balance');
 
 616   my $report              = SL::ReportGenerator->new(\%myconfig, $form);
 
 618   my @hidden_variables    = qw(fromdate todate year method);
 
 620   my $href                = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
 
 623     'accno'               => { 'text' => $locale->text('Account'), },
 
 624     'description'         => { 'text' => $locale->text('Description'), },
 
 625     'last_transaction'    => { 'text' => $locale->text('Last Transaction'), },
 
 626     'soll_eb'             => { 'text' => $locale->text('Debit Starting Balance'), },
 
 627     'haben_eb'            => { 'text' => $locale->text('Credit Starting Balance'), },
 
 628     'soll'                => { 'text' => $locale->text('Debit'), },
 
 629     'haben'               => { 'text' => $locale->text('Credit'), },
 
 630     'soll_kumuliert'      => { 'text' => $locale->text('Sum Debit'), },
 
 631     'haben_kumuliert'     => { 'text' => $locale->text('Sum Credit'), },
 
 632     'soll_saldo'          => { 'text' => $locale->text('Saldo Debit'), },
 
 633     'haben_saldo'         => { 'text' => $locale->text('Saldo Credit'), }
 
 638   my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 640   map { $column_defs{$_}->{visible} =  1 } @columns;
 
 642   $report->set_columns(%column_defs);
 
 643   $report->set_column_order(@columns);
 
 645   $report->set_export_options('generate_trial_balance', @hidden_variables);
 
 650   $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
 
 652   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
 
 653   push (@options, $form->{print_date});
 
 655   $form->{company} = $locale->text('Company') . " " . $defaults->company;
 
 656   push (@options, $form->{company});
 
 658   if ($::form->{customer_id}) {
 
 659     my $customer = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id});
 
 660     push @options, $::locale->text('Customer') . ' ' . $customer->displayable_name;
 
 664   $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
 
 666   my @custom_headers = ([
 
 667     { text => $::locale->text('Account'),          rowspan => 2, },
 
 668     { text => $::locale->text('Description'),      rowspan => 2, },
 
 669     { text => $::locale->text('Last Transaction'), rowspan => 2, },
 
 670     { text => $::locale->text('Starting Balance'), colspan => 2, },
 
 671     { text => $::locale->text('Sum for')   . " $form->{template_fromto}", colspan => 2, },
 
 672     { text => $::locale->text('Sum per')   . " $form->{template_to}",     colspan => 2, },
 
 673     { text => $::locale->text('Saldo per') . " $form->{template_to}",     colspan => 2, },
 
 678     { text => $::locale->text('Assets'), },
 
 679     { text => $::locale->text('Equity'), },
 
 680     { text => $::locale->text('Debit'),  },
 
 681     { text => $::locale->text('Credit'), },
 
 682     { text => $::locale->text('Debit'),  },
 
 683     { text => $::locale->text('Credit'), },
 
 684     { text => $::locale->text('Debit'),  },
 
 685     { text => $::locale->text('Credit'), },
 
 688   $report->set_options('output_format'        => 'HTML',
 
 689                        'top_info_text'        => join("\n", @options),
 
 690                        'title'                => $form->{title},
 
 691                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
 692                        'html_template'        => 'rp/html_report_susa',
 
 693                        'pdf_template'         => 'rp/html_report_susa',
 
 695   $report->set_custom_headers(@custom_headers);
 
 696   $report->set_options_from_form();
 
 697   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 699   # add sort and escape callback, this one we use for the add sub
 
 700   $form->{callback} = $href .= "&sort=$form->{sort}";
 
 702   # escape callback for href
 
 703   my $callback = $form->escape($href);
 
 705   my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 707   my %totals    = map { $_ => 0 } @subtotal_columns;
 
 709   my $edit_url = build_std_url('action=edit', 'type', 'vc');
 
 712   foreach my $accno (@{ $form->{TB} || [] }) {
 
 714     $accno->{soll} = $accno->{debit};
 
 715     $accno->{haben} = $accno->{credit};
 
 716     map { $totals{$_}    += $accno->{$_} } @subtotal_columns;
 
 718     map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
 
 719       qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 723     foreach my $column (@columns) {
 
 725         'data'  => $accno->{$column},
 
 726         'align' => $column_alignment{$column},
 
 730     $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}));
 
 732     my $row_set = [ $row ];
 
 735     $report->add_data($row_set);
 
 740   $report->add_separator();
 
 742   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
 744   $report->generate_with_headers();
 
 746   $main::lxdebug->leave_sub();
 
 750 sub create_subtotal_row {
 
 751   $main::lxdebug->enter_sub();
 
 753   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
 755   my $form     = $main::form;
 
 756   my %myconfig = %main::myconfig;
 
 757   my $locale   = $main::locale;
 
 759   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
 761   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
 763   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
 
 765   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
 767   $main::lxdebug->leave_sub();
 
 772 sub create_list_accounts_subtotal_row {
 
 773   $main::lxdebug->enter_sub();
 
 775   my ($subtotals, $columns, $fields, $class) = @_;
 
 777   my $form     = $main::form;
 
 778   my %myconfig = %main::myconfig;
 
 779   my $locale   = $main::locale;
 
 781   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 783   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
 
 785   $main::lxdebug->leave_sub();
 
 791   $main::lxdebug->enter_sub();
 
 795   my $form     = $main::form;
 
 796   my %myconfig = %main::myconfig;
 
 797   my $locale   = $main::locale;
 
 800   if ($form->{department}) {
 
 801     my ($department) = split /--/, $form->{department};
 
 802     push @options, $locale->text('Department') . " : $department";
 
 804   if ($form->{projectnumber}) {
 
 805     push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
 
 808   # if there are any dates
 
 809   if ($form->{fromdate} || $form->{todate}) {
 
 810     my ($fromdate, $todate);
 
 812     if ($form->{fromdate}) {
 
 813       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 815     if ($form->{todate}) {
 
 816       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
 
 819     push @options, "$fromdate - $todate";
 
 822     push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 825   my @columns     = qw(accno description begbalance debit credit endbalance);
 
 827     'accno'       => { 'text' => $locale->text('Account'), },
 
 828     'description' => { 'text' => $locale->text('Description'), },
 
 829     'debit'       => { 'text' => $locale->text('Debit'), },
 
 830     'credit'      => { 'text' => $locale->text('Credit'), },
 
 831     'begbalance'  => { 'text' => $locale->text('Balance'), },
 
 832     'endbalance'  => { 'text' => $locale->text('Balance'), },
 
 834   my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
 
 836   my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
 
 838   $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
 
 840   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 842   $report->set_options('top_info_text'         => join("\n", @options),
 
 843                        'output_format'         => 'HTML',
 
 844                        'title'                 => $form->{title},
 
 845                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
 
 846                        'std_column_visibility' => 1,
 
 848   $report->set_options_from_form();
 
 849   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 851   $report->set_columns(%column_defs);
 
 852   $report->set_column_order(@columns);
 
 854   $report->set_export_options($action, @hidden_variables);
 
 856   my @totals_columns = qw(credit debit begbalance endbalance);
 
 857   my %subtotals      = map { $_ => 0 } @totals_columns;
 
 858   my %totals         = map { $_ => 0 } @totals_columns;
 
 859   my $found_heading  = 0;
 
 860   my @tb             = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
 
 862   # sort the whole thing by account numbers and display
 
 863   foreach my $idx (0 .. scalar(@tb) - 1) {
 
 865     my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
 
 867     my $ml   = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
 
 869     my $row  = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
 
 871     if ($ref->{charttype} eq 'H') {
 
 872       next unless ($form->{l_heading});
 
 874       %subtotals                   = map { $_ => 0 } @totals_columns;
 
 876       $row->{description}->{class} = 'listheading';
 
 877       $row->{description}->{data}  = $ref->{description};
 
 879       $report->add_data($row);
 
 884     foreach (qw(debit credit)) {
 
 885       $subtotals{$_} += $ref->{$_};
 
 886       $totals{$_}    += $ref->{$_};
 
 889     $subtotals{begbalance} += $ref->{balance} * $ml;
 
 890     $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
 
 892     map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
 
 893     map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
 
 895     $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
 
 896     $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
 
 898     $report->add_data($row);
 
 900     if ($form->{l_heading} && $found_heading &&
 
 901         (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
 
 902       $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
 
 906   $report->add_separator();
 
 908   $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
 
 910   $report->generate_with_headers();
 
 912   $main::lxdebug->leave_sub();
 
 915 sub generate_ar_aging {
 
 916   $main::lxdebug->enter_sub();
 
 918   $main::auth->assert('general_ledger');
 
 920   my $form     = $main::form;
 
 921   my %myconfig = %main::myconfig;
 
 922   my $locale   = $main::locale;
 
 925   ($form->{customer}) = split(/--/, $form->{customer});
 
 927   $form->{ct}   = "customer";
 
 928   $form->{arap} = "ar";
 
 930   $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
 
 932   RP->aging(\%myconfig, \%$form);
 
 935   $main::lxdebug->leave_sub();
 
 938 sub generate_ap_aging {
 
 939   $main::lxdebug->enter_sub();
 
 941   $main::auth->assert('general_ledger');
 
 943   my $form     = $main::form;
 
 944   my %myconfig = %main::myconfig;
 
 945   my $locale   = $main::locale;
 
 948   ($form->{vendor}) = split(/--/, $form->{vendor});
 
 950   $form->{ct}   = "vendor";
 
 951   $form->{arap} = "ap";
 
 953   $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
 
 955   RP->aging(\%myconfig, \%$form);
 
 958   $main::lxdebug->leave_sub();
 
 961 sub create_aging_subtotal_row {
 
 962   $main::lxdebug->enter_sub();
 
 964   my ($subtotals, $columns, $periods, $class) = @_;
 
 966   my $form     = $main::form;
 
 967   my %myconfig = %main::myconfig;
 
 968   my $locale   = $main::locale;
 
 970   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 972   foreach (@{ $periods }) {
 
 973     $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
 
 974     $subtotals->{$_}      = 0;
 
 977   $main::lxdebug->leave_sub();
 
 983   $main::lxdebug->enter_sub();
 
 985   $main::auth->assert('general_ledger');
 
 987   my $form     = $main::form;
 
 988   my %myconfig = %main::myconfig;
 
 989   my $locale   = $main::locale;
 
 990   my $cgi      = $::request->{cgi};
 
 992   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 994   my @columns = qw(statement ct invnumber transdate duedate amount open);
 
 997     'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
 
 998     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
 
 999     'invnumber' => { 'text' => $locale->text('Invoice'), },
 
1000     'transdate' => { 'text' => $locale->text('Date'), },
 
1001     'duedate'   => { 'text' => $locale->text('Due'), },
 
1002     'amount'    => { 'text' => $locale->text('Amount'), },
 
1003     'open'      => { 'text' => $locale->text('Open'), },
 
1006   my %column_alignment = ('statement' => 'center',
 
1007                           map { $_ => 'right' } qw(open amount));
 
1009   $report->set_options('std_column_visibility' => 1);
 
1010   $report->set_columns(%column_defs);
 
1011   $report->set_column_order(@columns);
 
1013   my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype department);
 
1014   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
 
1017   my $attachment_basename;
 
1019   if ($form->{department}) {
 
1020     my ($department) = split /--/, $form->{department};
 
1021     push @options, $locale->text('Department') . " : $department";
 
1022     $form->{callback} .= "&department=" . E($department);
 
1025   if (($form->{arap} eq 'ar') && $form->{customer}) {
 
1026     push @options, $form->{customer};
 
1027     $attachment_basename = $locale->text('ar_aging_list');
 
1028     $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
 
1031   if (($form->{arap} eq 'ap') && $form->{vendor}) {
 
1032     push @options, $form->{vendor};
 
1033     $attachment_basename = $locale->text('ap_aging_list');
 
1034     $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
 
1037   if ($form->{fromdate}) {
 
1038     push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1040     push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1043   $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
 
1045   $report->set_options('top_info_text'        => join("\n", @options),
 
1046                        'output_format'        => 'HTML',
 
1047                        'title'                => $form->{title},
 
1048                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1050   $report->set_options_from_form();
 
1051   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1053   my $previous_ctid = 0;
 
1055   my @periods       = qw(open amount);
 
1056   my %subtotals     = map { $_ => 0 } @periods;
 
1057   my %totals        = map { $_ => 0 } @periods;
 
1059   foreach my $ref (@{ $form->{AG} }) {
 
1060     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
 
1061       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
 
1064     foreach my $key (@periods) {
 
1065       $subtotals{$key}  += $ref->{"$key"};
 
1066       $totals{$key}     += $ref->{"$key"};
 
1067       $ref->{"$key"}  = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
 
1072     foreach my $column (@columns) {
 
1074         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
 
1075         'align'  => $column_alignment{$column},
 
1076         'valign' => $column eq 'statement' ? 'center' : '',
 
1080     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
 
1082     if ($previous_ctid != $ref->{ctid}) {
 
1083       $row->{statement}->{raw_data} =
 
1084           $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
 
1085         . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
 
1086       $row->{ct}->{data} = $ref->{name};
 
1091     $previous_ctid = $ref->{ctid};
 
1093     $report->add_data($row);
 
1096   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
 
1098   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
 
1100   if ($form->{arap} eq 'ar') {
 
1101     my $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
 
1102     my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
 
1103                                                                                'PRINT_OPTIONS' => print_options(inline => 1), });
 
1104     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
 
1105                          'raw_bottom_info_text' => $raw_bottom_info_text);
 
1108   $report->generate_with_headers();
 
1110   $main::lxdebug->leave_sub();
 
1114   $main::lxdebug->enter_sub();
 
1116   my $form     = $main::form;
 
1117   my %myconfig = %main::myconfig;
 
1118   my $locale   = $main::locale;
 
1120   RP->aging(\%myconfig, \%$form);
 
1122   map { $_->{checked} = "checked" } @{ $form->{AG} };
 
1126   $main::lxdebug->leave_sub();
 
1130   $::lxdebug->enter_sub;
 
1131   $::auth->assert('general_ledger');
 
1133   # get name and email addresses
 
1135   for my $i (1 .. $::form->{rowcount}) {
 
1136     next unless $::form->{"statement_$i"};
 
1137     $::form->{"$::form->{ct}_id"} = $::form->{"$::form->{ct}_id_$i"};
 
1138     RP->get_customer(\%::myconfig, $::form);
 
1143   $::form->error($::locale->text('Nothing selected!')) unless $selected;
 
1145   $::form->{media} = "email";
 
1147   # save all other variables
 
1149   for my $key (keys %$::form) {
 
1150     next if any { $key eq $_ } qw(login password action email cc bcc subject message type sendmode format header);
 
1151     next unless '' eq ref $::form->{$key};
 
1152     push @hidden_values, $key;
 
1156   print $::form->parse_html_template('rp/e_mail', {
 
1157     print_options => print_options(inline => 1),
 
1158     hidden_values => \@hidden_values,
 
1161   $::lxdebug->leave_sub;
 
1165   $main::lxdebug->enter_sub();
 
1167   $main::auth->assert('general_ledger');
 
1169   my $form     = $main::form;
 
1170   my %myconfig = %main::myconfig;
 
1171   my $locale   = $main::locale;
 
1173   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
 
1174     unless $form->{subject};
 
1176   RP->aging(\%myconfig, \%$form);
 
1178   $form->{"statement_1"} = 1;
 
1180   $form->{media} = 'email';
 
1183   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
 
1185   $main::lxdebug->leave_sub();
 
1189   $main::lxdebug->enter_sub();
 
1191   $main::auth->assert('general_ledger');
 
1193   my $form     = $main::form;
 
1194   my %myconfig = %main::myconfig;
 
1195   my $locale   = $main::locale;
 
1197   if ($form->{media} eq 'printer') {
 
1198     $form->error($locale->text('Select postscript or PDF!'))
 
1199       if ($form->{format} !~ /(postscript|pdf)/);
 
1203   for my $i (1 .. $form->{rowcount}) {
 
1204     if ($form->{"statement_$i"}) {
 
1205       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
 
1211   $form->error($locale->text('Nothing selected!')) unless $selected;
 
1213   if ($form->{media} eq 'printer') {
 
1214     $form->{"$form->{ct}_id"} = "";
 
1216     $form->{"statement_1"} = 1;
 
1219   RP->aging(\%myconfig, \%$form);
 
1223   $form->redirect($locale->text('Statements sent to printer!'))
 
1224     if ($form->{media} eq 'printer');
 
1226   $main::lxdebug->leave_sub();
 
1230   $main::lxdebug->enter_sub();
 
1232   $main::auth->assert('general_ledger');
 
1234   my $form     = $main::form;
 
1235   my %myconfig = %main::myconfig;
 
1236   my $locale   = $main::locale;
 
1238   my $defaults = SL::DB::Default->get;
 
1239   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
 
1240   $form->{templates} = $defaults->templates;
 
1242   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
 
1244   my $suffix = "html";
 
1245   my $attachment_suffix = "html";
 
1246   if ($form->{format} eq 'postscript') {
 
1247     $form->{postscript} = 1;
 
1249     $attachment_suffix = "ps";
 
1250   } elsif ($form->{format} eq 'pdf') {
 
1253     $attachment_suffix = "pdf";
 
1256   $form->{IN}  = "$form->{type}.$suffix";
 
1257   $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
 
1259   # Save $form->{email} because it will be overwritten.
 
1260   $form->{EMAIL_RECIPIENT} = $form->{email};
 
1264   while (@{ $form->{AG} }) {
 
1266     my $ref = shift @{ $form->{AG} };
 
1268     if ($ctid != $ref->{ctid}) {
 
1270       $ctid = $ref->{ctid};
 
1273       if ($form->{"statement_$i"}) {
 
1276           ("name", "street", "zipcode", "city", "country", "contact", "email",
 
1277            "$form->{ct}phone", "$form->{ct}fax");
 
1278         map { $form->{$_} = $ref->{$_} } @a;
 
1280         $form->{ $form->{ct} } = $form->{name};
 
1281         $form->{"$form->{ct}_id"} = $ref->{ctid};
 
1283         map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
 
1285         foreach my $item (qw(c0 c30 c60 c90)) {
 
1286           $form->{$item} = ();
 
1287           $form->{"${item}total"} = 0;
 
1290         &statement_details($ref);
 
1294           if (scalar(@{ $form->{AG} }) > 0) {
 
1296             # one or more left to go
 
1297             if ($ctid == $form->{AG}->[0]->{ctid}) {
 
1298               $ref = shift @{ $form->{AG} };
 
1299               &statement_details($ref);
 
1302               $ref = scalar(@{ $form->{AG} });
 
1308             # set initial ref to 0
 
1315           $form->{"${_}total"} =
 
1316             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
 
1317         } ('c0', 'c30', 'c60', 'c90', "");
 
1319         $form->{attachment_filename} =  $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
 
1320         $form->{attachment_filename} =~ s/\s+/_/g;
 
1322         $form->parse_template(\%myconfig);
 
1327   # saving the history
 
1328   if(!exists $form->{addition} && $form->{id} ne "") {
 
1329     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1330     $form->{addition} = "PRINTED";
 
1331     $form->{what_done} = $form->{type};
 
1332     $form->save_history;
 
1334   # /saving the history
 
1335   $main::lxdebug->leave_sub();
 
1338 sub statement_details {
 
1339   $main::lxdebug->enter_sub();
 
1341   $main::auth->assert('general_ledger');
 
1343   my $form     = $main::form;
 
1344   my %myconfig = %main::myconfig;
 
1345   my $locale   = $main::locale;
 
1349   push @{ $form->{invnumber} }, $ref->{invnumber};
 
1350   push @{ $form->{invdate} },   $ref->{transdate};
 
1351   push @{ $form->{duedate} },   $ref->{duedate};
 
1352   push @{ $form->{amount} },    $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
 
1353   push @{ $form->{open} },      $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
 
1355   foreach my $item (qw(c0 c30 c60 c90)) {
 
1356     if ($ref->{exchangerate} * 1) {
 
1357       # add only the open amount of the invoice to the aging, not the total amount
 
1358       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
 
1359       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
 
1360       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
 
1361       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
 
1363     $form->{"${item}total"} += $ref->{$item};
 
1364     $form->{total}          += $ref->{$item};
 
1365     push @{ $form->{$item} },
 
1366       $form->format_amount(\%myconfig, $ref->{$item}, 2);
 
1369   $main::lxdebug->leave_sub();
 
1372 sub generate_tax_report {
 
1373   $::lxdebug->enter_sub;
 
1374   $::auth->assert('report');
 
1376   RP->tax_report(\%::myconfig, $::form);
 
1378   my $descvar     = "$::form->{accno}_description";
 
1379   my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
 
1383   my $callback = build_std_url('action=generate_tax_report', $descvar,
 
1384     qw(fromdate todate db method accno department report title));
 
1386   my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
 
1389   for my $item (@columns, 'subtotal') {
 
1390     if ($::form->{"l_$item"} eq "Y") {
 
1391       $callback .= "&l_$item=Y";
 
1392       $href     .= "&l_$item=Y";
 
1396   for my $item (@columns) {
 
1397     if ($::form->{"l_$item"} eq "Y") {
 
1398       push @column_index, $item;
 
1403   if ($::form->{department}) {
 
1404     my ($department) = split /--/, $::form->{department};
 
1405     push @options, $::locale->text('Department') . " : $department";
 
1408   # if there are any dates
 
1409   if ($::form->{fromdate} || $::form->{todate}) {
 
1410     my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
 
1411     my $todate   = $::form->{todate}   ? $::locale->date(\%::myconfig, $::form->{todate}, 1)   : '';
 
1412     push @options, "$fromdate - $todate";
 
1414     push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
 
1417   my ($name, $invoice, $arap);
 
1418   if ($::form->{db} eq 'ar') {
 
1419     $name    = $::locale->text('Customer');
 
1423   if ($::form->{db} eq 'ap') {
 
1424     $name    = $::locale->text('Vendor');
 
1429   my %column_header = (
 
1430     id        => $::locale->text('ID'),
 
1431     invnumber => $::locale->text('Invoice'),
 
1432     transdate => $::locale->text('Date'),
 
1433     netamount => $::locale->text('Amount'),
 
1434     tax       => $::locale->text('Tax'),
 
1435     amount    => $::locale->text('Total'),
 
1439   my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
 
1441   $callback .= "&sort=$::form->{sort}";
 
1444   if (@{ $::form->{TR} }) {
 
1445     $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
 
1448   my ($totalnetamount, $totaltax, @data);
 
1449   for my $ref (@{ $::form->{TR} }) {
 
1451     my $module = ($ref->{invoice}) ? $invoice : $arap;
 
1453     if ($::form->{l_subtotal} eq 'Y') {
 
1454       if ($sameitem ne $ref->{ $::form->{sort} }) {
 
1457           netamount => $subtotalnetamount,
 
1458           tax       => $subtotaltax,
 
1459           amount    => $subtotal,
 
1461         $subtotalnetamount = 0;
 
1463         $sameitem          = $ref->{ $::form->{sort} };
 
1467     $subtotalnetamount += $ref->{netamount};
 
1468     $subtotaltax       += $ref->{tax};
 
1469     $totalnetamount    += $ref->{netamount};
 
1470     $totaltax          += $ref->{tax};
 
1471     $ref->{amount}      = $ref->{netamount} + $ref->{tax};
 
1473     push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
 
1474     $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
 
1475     $data[-1]{$_}{numeric}     = 1 for qw(netamount tax amount);
 
1478   if ($::form->{l_subtotal} eq 'Y') {
 
1481       netamount => $subtotalnetamount,
 
1482       tax       => $subtotaltax,
 
1483       amount    => $subtotal,
 
1489     netamount => $totalnetamount,
 
1491     amount    => $totalnetamount + $totaltax,
 
1495   print $::form->parse_html_template('rp/tax_report', {
 
1496     column_index  => \@column_index,
 
1497     column_header => \%column_header,
 
1498     column_sorted => \%column_sorted,
 
1501     options       => \@options,
 
1504   $::lxdebug->leave_sub;
 
1508   $main::lxdebug->enter_sub();
 
1510   $main::auth->assert('cash');
 
1512   my $form     = $main::form;
 
1513   my %myconfig = %main::myconfig;
 
1514   my $locale   = $main::locale;
 
1516   if ($form->{account}) {
 
1517     ($form->{paymentaccounts}) = split /--/, $form->{account};
 
1521   if ($form->{department}) {
 
1522     (my $department, $form->{department_id}) = split /--/, $form->{department};
 
1523     $option = $locale->text('Department') . " : $department";
 
1526   report_generator_set_default_sort('transdate', 1);
 
1528   RP->payments(\%myconfig, \%$form);
 
1530   my @hidden_variables = qw(account title department reference source memo fromdate todate
 
1531                             fx_transaction db prepayment paymentaccounts sort);
 
1533   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
 
1534   $form->{callback} = $href;
 
1536   my @columns     = qw(transdate invnumber name paid source memo);
 
1538     'name'      => { 'text' => $locale->text('Description'), },
 
1539     'invnumber' => { 'text' => $locale->text('Reference'), },
 
1540     'transdate' => { 'text' => $locale->text('Date'), },
 
1541     'paid'      => { 'text' => $locale->text('Amount'), },
 
1542     'source'    => { 'text' => $locale->text('Source'), },
 
1543     'memo'      => { 'text' => $locale->text('Memo'), },
 
1545   my %column_alignment = ('paid' => 'right');
 
1547   foreach my $name (grep { $_ ne 'paid' } @columns) {
 
1548     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
1549     $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
 
1553   if ($form->{fromdate}) {
 
1554     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
 
1556   if ($form->{todate}) {
 
1557     push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1560   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
1562   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
 
1564   $report->set_options('top_info_text'         => join("\n", @options),
 
1565                        'output_format'         => 'HTML',
 
1566                        'title'                 => $form->{title},
 
1567                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1568                        'std_column_visibility' => 1,
 
1570   $report->set_options_from_form();
 
1572   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1574   $report->set_columns(%column_defs);
 
1575   $report->set_column_order(@columns);
 
1577   $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
 
1579   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
1583   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
 
1584     next unless @{ $form->{ $ref->{id} } };
 
1586     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
 
1588     my $subtotal_paid = 0;
 
1590     foreach my $payment (@{ $form->{ $ref->{id} } }) {
 
1591       my $module = $payment->{module};
 
1592       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
 
1593       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
 
1595       $subtotal_paid += $payment->{paid};
 
1596       $total_paid    += $payment->{paid};
 
1598       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
 
1602       foreach my $column (@columns) {
 
1604           'data'  => $payment->{$column},
 
1605           'align' => $column_alignment{$column},
 
1609       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
 
1611       $report->add_data($row);
 
1614     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
 
1616       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
 
1618       'class' => 'listsubtotal',
 
1621     $report->add_data($row);
 
1624   $report->add_separator();
 
1626   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
 
1628     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
 
1630     'class' => 'listtotal',
 
1633   $report->add_data($row);
 
1635   $report->generate_with_headers();
 
1637   $main::lxdebug->leave_sub();
 
1641   $::lxdebug->enter_sub;
 
1643   my ($dont_print) = @_;
 
1645   $::form->{sendmode} = "attachment";
 
1646   $::form->{format} ||= $::myconfig{template_format} || "pdf";
 
1647   $::form->{copies} ||= $::myconfig{copies}          || 2;
 
1649   $::form->{PD}{ $::form->{type} }     = "selected";
 
1650   $::form->{DF}{ $::form->{format} }   = "selected";
 
1651   $::form->{OP}{ $::form->{media} }    = "selected";
 
1652   $::form->{SM}{ $::form->{sendmode} } = "selected";
 
1654   my $output = $::form->parse_html_template('rp/print_options', {
 
1655     is_email    => $::form->{media} eq 'email',
 
1658   print $output unless $dont_print;
 
1660   $::lxdebug->leave_sub;
 
1666   $main::lxdebug->enter_sub();
 
1668   $main::auth->assert('report');
 
1670   my $form     = $main::form;
 
1671   my %myconfig = %main::myconfig;
 
1672   my $locale   = $main::locale;
 
1674   $form->{padding} = "  ";
 
1675   $form->{bold}    = "<b>";
 
1676   $form->{endbold} = "</b>";
 
1677   $form->{br}      = "<br>";
 
1679   if ($form->{reporttype} eq "custom") {
 
1681     #forgotten the year --> thisyear
 
1682     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
1683       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
1689     if ($form->{duetyp} eq "13") {
 
1690       $form->{fromdate}        = "1.1.$form->{year}";
 
1691       $form->{todate}          = "31.12.$form->{year}";
 
1692       $form->{comparefromdate} = "1.01.$form->{year}";
 
1693       $form->{comparetodate}   = "31.12.$form->{year}";
 
1697     if ($form->{duetyp} eq "A") {
 
1698       $form->{fromdate}        = "1.1.$form->{year}";
 
1699       $form->{todate}          = "31.3.$form->{year}";
 
1700       $form->{comparefromdate} = "1.01.$form->{year}";
 
1701       $form->{comparetodate}   = "31.03.$form->{year}";
 
1703     if ($form->{duetyp} eq "B") {
 
1704       $form->{fromdate}        = "1.4.$form->{year}";
 
1705       $form->{todate}          = "30.6.$form->{year}";
 
1706       $form->{comparefromdate} = "1.01.$form->{year}";
 
1707       $form->{comparetodate}   = "30.06.$form->{year}";
 
1709     if ($form->{duetyp} eq "C") {
 
1710       $form->{fromdate}        = "1.7.$form->{year}";
 
1711       $form->{todate}          = "30.9.$form->{year}";
 
1712       $form->{comparefromdate} = "1.01.$form->{year}";
 
1713       $form->{comparetodate}   = "30.09.$form->{year}";
 
1715     if ($form->{duetyp} eq "D") {
 
1716       $form->{fromdate}        = "1.10.$form->{year}";
 
1717       $form->{todate}          = "31.12.$form->{year}";
 
1718       $form->{comparefromdate} = "1.01.$form->{year}";
 
1719       $form->{comparetodate}   = "31.12.$form->{year}";
 
1724       $form->{duetyp} eq "1" && do {
 
1725         $form->{fromdate}        = "1.1.$form->{year}";
 
1726         $form->{todate}          = "31.1.$form->{year}";
 
1727         $form->{comparefromdate} = "1.01.$form->{year}";
 
1728         $form->{comparetodate}   = "31.01.$form->{year}";
 
1731       $form->{duetyp} eq "2" && do {
 
1732         $form->{fromdate} = "1.2.$form->{year}";
 
1734         #this works from 1901 to 2099, 1900 and 2100 fail.
 
1735         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
1736         $form->{todate}          = "$leap.2.$form->{year}";
 
1737         $form->{comparefromdate} = "1.01.$form->{year}";
 
1738         $form->{comparetodate}   = "$leap.02.$form->{year}";
 
1741       $form->{duetyp} eq "3" && do {
 
1742         $form->{fromdate}        = "1.3.$form->{year}";
 
1743         $form->{todate}          = "31.3.$form->{year}";
 
1744         $form->{comparefromdate} = "1.01.$form->{year}";
 
1745         $form->{comparetodate}   = "31.03.$form->{year}";
 
1748       $form->{duetyp} eq "4" && do {
 
1749         $form->{fromdate}        = "1.4.$form->{year}";
 
1750         $form->{todate}          = "30.4.$form->{year}";
 
1751         $form->{comparefromdate} = "1.01.$form->{year}";
 
1752         $form->{comparetodate}   = "30.04.$form->{year}";
 
1755       $form->{duetyp} eq "5" && do {
 
1756         $form->{fromdate}        = "1.5.$form->{year}";
 
1757         $form->{todate}          = "31.5.$form->{year}";
 
1758         $form->{comparefromdate} = "1.01.$form->{year}";
 
1759         $form->{comparetodate}   = "31.05.$form->{year}";
 
1762       $form->{duetyp} eq "6" && do {
 
1763         $form->{fromdate}        = "1.6.$form->{year}";
 
1764         $form->{todate}          = "30.6.$form->{year}";
 
1765         $form->{comparefromdate} = "1.01.$form->{year}";
 
1766         $form->{comparetodate}   = "30.06.$form->{year}";
 
1769       $form->{duetyp} eq "7" && do {
 
1770         $form->{fromdate}        = "1.7.$form->{year}";
 
1771         $form->{todate}          = "31.7.$form->{year}";
 
1772         $form->{comparefromdate} = "1.01.$form->{year}";
 
1773         $form->{comparetodate}   = "31.07.$form->{year}";
 
1776       $form->{duetyp} eq "8" && do {
 
1777         $form->{fromdate}        = "1.8.$form->{year}";
 
1778         $form->{todate}          = "31.8.$form->{year}";
 
1779         $form->{comparefromdate} = "1.01.$form->{year}";
 
1780         $form->{comparetodate}   = "31.08.$form->{year}";
 
1783       $form->{duetyp} eq "9" && do {
 
1784         $form->{fromdate}        = "1.9.$form->{year}";
 
1785         $form->{todate}          = "30.9.$form->{year}";
 
1786         $form->{comparefromdate} = "1.01.$form->{year}";
 
1787         $form->{comparetodate}   = "30.09.$form->{year}";
 
1790       $form->{duetyp} eq "10" && do {
 
1791         $form->{fromdate}        = "1.10.$form->{year}";
 
1792         $form->{todate}          = "31.10.$form->{year}";
 
1793         $form->{comparefromdate} = "1.01.$form->{year}";
 
1794         $form->{comparetodate}   = "31.10.$form->{year}";
 
1797       $form->{duetyp} eq "11" && do {
 
1798         $form->{fromdate}        = "1.11.$form->{year}";
 
1799         $form->{todate}          = "30.11.$form->{year}";
 
1800         $form->{comparefromdate} = "1.01.$form->{year}";
 
1801         $form->{comparetodate}   = "30.11.$form->{year}";
 
1804       $form->{duetyp} eq "12" && do {
 
1805         $form->{fromdate}        = "1.12.$form->{year}";
 
1806         $form->{todate}          = "31.12.$form->{year}";
 
1807         $form->{comparefromdate} = "1.01.$form->{year}";
 
1808         $form->{comparetodate}   = "31.12.$form->{year}";
 
1812     hotfix_reformat_date();
 
1814     # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
 
1815     # ansonsten ist die prüfung in RP.pm
 
1816     # if (defined ($form->{fromdate|todate}=='..'))
 
1818     if ($form->{fromdate}){
 
1819       my $datetime = $locale->parse_date_to_object($form->{fromdate});
 
1820       $datetime->set( month      => 1,
 
1822       $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
 
1824     if ($form->{todate}){
 
1825       $form->{comparetodate}   = $form->{todate};
 
1829   RP->bwa(\%myconfig, \%$form);
 
1831   ($form->{department}) = split /--/, $form->{department};
 
1834     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
1835   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
1837   # if there are any dates construct a where
 
1838   if ($form->{fromdate} || $form->{todate}) {
 
1840     unless ($form->{todate}) {
 
1841       $form->{todate} = $form->current_date(\%myconfig);
 
1844     my %germandate = ("dateformat" => "dd.mm.yyyy");
 
1846     my $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
 
1847     my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
 
1849     my $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
 
1850     my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
 
1852     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
1854         $locale->text('for Period')
 
1855       . qq|\n$longfromdate |
 
1856       . $locale->text('bis')
 
1860   $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
 
1862   if ( $form->{method} eq 'cash' ) {
 
1863     $form->{accounting_method} = $locale->text('Cash accounting');
 
1864   } elsif ( $form->{method} eq 'accrual' ) {
 
1865     $form->{accounting_method} = $locale->text('Accrual accounting');
 
1867     $form->{accounting_method} = "";
 
1870   $form->{title} = $locale->text('BWA');
 
1872   $::request->layout->add_stylesheets('bwa.css');
 
1874   print $form->parse_html_template('rp/bwa');
 
1876   $main::lxdebug->leave_sub();
 
1879 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
 
1880 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
 
1881 sub hotfix_reformat_date {
 
1883   $main::lxdebug->enter_sub();
 
1885   my $form     = $main::form;
 
1886   my %myconfig = %main::myconfig;
 
1887   my $locale   = $main::locale;
 
1889   if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
 
1890     my $current_dateformat = $myconfig{dateformat};
 
1891     $myconfig{dateformat} = 'dd.mm.yyyy';
 
1892     $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
 
1893     $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
 
1894     $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
 
1895       unless (!defined ($form->{comparefromdate}));
 
1896     $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
 
1897       unless (!defined ($form->{comparetodate}));
 
1899     # Und wieder zurücksetzen
 
1900     $myconfig{dateformat} =  $current_dateformat; #'dd.mm.yyyy';
 
1901   } # Ende Hotifx Bug 1388
 
1903   $main::lxdebug->leave_sub();