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   my $defaults = SL::DB::Default->get;
 
 225   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
 
 226   $form->{templates} = $defaults->templates;
 
 228   $form->{padding} = "  ";
 
 229   $form->{bold}    = "<b>";
 
 230   $form->{endbold} = "</b>";
 
 231   $form->{br}      = "<br>";
 
 233   if ($form->{reporttype} eq "custom") {
 
 235     #forgotten the year --> thisyear
 
 236     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 237       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 243     if ($form->{duetyp} eq "13") {
 
 244       $form->{fromdate} = "1.1.$form->{year}";
 
 245       $form->{todate}   = "31.12.$form->{year}";
 
 249     if ($form->{duetyp} eq "A") {
 
 250       $form->{fromdate} = "1.1.$form->{year}";
 
 251       $form->{todate}   = "31.3.$form->{year}";
 
 253     if ($form->{duetyp} eq "B") {
 
 254       $form->{fromdate} = "1.4.$form->{year}";
 
 255       $form->{todate}   = "30.6.$form->{year}";
 
 257     if ($form->{duetyp} eq "C") {
 
 258       $form->{fromdate} = "1.7.$form->{year}";
 
 259       $form->{todate}   = "30.9.$form->{year}";
 
 261     if ($form->{duetyp} eq "D") {
 
 262       $form->{fromdate} = "1.10.$form->{year}";
 
 263       $form->{todate}   = "31.12.$form->{year}";
 
 268       $form->{duetyp} eq "1" && do {
 
 269         $form->{fromdate} = "1.1.$form->{year}";
 
 270         $form->{todate}   = "31.1.$form->{year}";
 
 273       $form->{duetyp} eq "2" && do {
 
 274         $form->{fromdate} = "1.2.$form->{year}";
 
 276         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 277         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 278         $form->{todate} = "$leap.2.$form->{year}";
 
 281       $form->{duetyp} eq "3" && do {
 
 282         $form->{fromdate} = "1.3.$form->{year}";
 
 283         $form->{todate}   = "31.3.$form->{year}";
 
 286       $form->{duetyp} eq "4" && do {
 
 287         $form->{fromdate} = "1.4.$form->{year}";
 
 288         $form->{todate}   = "30.4.$form->{year}";
 
 291       $form->{duetyp} eq "5" && do {
 
 292         $form->{fromdate} = "1.5.$form->{year}";
 
 293         $form->{todate}   = "31.5.$form->{year}";
 
 296       $form->{duetyp} eq "6" && do {
 
 297         $form->{fromdate} = "1.6.$form->{year}";
 
 298         $form->{todate}   = "30.6.$form->{year}";
 
 301       $form->{duetyp} eq "7" && do {
 
 302         $form->{fromdate} = "1.7.$form->{year}";
 
 303         $form->{todate}   = "31.7.$form->{year}";
 
 306       $form->{duetyp} eq "8" && do {
 
 307         $form->{fromdate} = "1.8.$form->{year}";
 
 308         $form->{todate}   = "31.8.$form->{year}";
 
 311       $form->{duetyp} eq "9" && do {
 
 312         $form->{fromdate} = "1.9.$form->{year}";
 
 313         $form->{todate}   = "30.9.$form->{year}";
 
 316       $form->{duetyp} eq "10" && do {
 
 317         $form->{fromdate} = "1.10.$form->{year}";
 
 318         $form->{todate}   = "31.10.$form->{year}";
 
 321       $form->{duetyp} eq "11" && do {
 
 322         $form->{fromdate} = "1.11.$form->{year}";
 
 323         $form->{todate}   = "30.11.$form->{year}";
 
 326       $form->{duetyp} eq "12" && do {
 
 327         $form->{fromdate} = "1.12.$form->{year}";
 
 328         $form->{todate}   = "31.12.$form->{year}";
 
 332     hotfix_reformat_date();
 
 333   } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
 
 335   RP->income_statement(\%myconfig, \%$form);
 
 337   ($form->{department}) = split /--/, $form->{department};
 
 340     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 341   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
 343   # if there are any dates construct a where
 
 344   if ($form->{fromdate} || $form->{todate}) {
 
 346     unless ($form->{todate}) {
 
 347       $form->{todate} = $form->current_date(\%myconfig);
 
 350     my $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
 
 351     my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
 
 353     my $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 354     my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
 
 356     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
 358         $locale->text('for Period')
 
 359       . qq|\n$longfromdate |
 
 360       . $locale->text('Bis')
 
 364   if ($form->{comparefromdate} || $form->{comparetodate}) {
 
 365     my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
 
 366     my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
 
 368     my $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
 
 369     my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
 
 371     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
 
 373         "\n$longcomparefromdate "
 
 374       . $locale->text('Bis')
 
 375       . qq| $longcomparetodate|;
 
 378   $form->{IN} = "income_statement.html";
 
 380   $form->parse_template;
 
 382   $main::lxdebug->leave_sub();
 
 385 sub generate_balance_sheet {
 
 386   $::lxdebug->enter_sub;
 
 387   $::auth->assert('report');
 
 389   my $defaults = SL::DB::Default->get;
 
 390   $::form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
 
 391   $::form->{templates}     = $defaults->templates;
 
 392   $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
 
 393   $::form->{padding}       = "  ";
 
 394   $::form->{bold}          = "<b>";
 
 395   $::form->{endbold}       = "</b>";
 
 396   $::form->{br}            = "<br>";
 
 398   my $data = RP->balance_sheet(\%::myconfig, $::form);
 
 400   $::form->{asofdate} ||= $::form->current_date;
 
 401   $::form->{period}     = $::locale->date(\%::myconfig, $::form->current_date, 1);
 
 403   ($::form->{department}) = split /--/, $::form->{department};
 
 405   # define Current Earnings account
 
 406   my $padding = $::form->{l_heading} ? $::form->{padding} : "";
 
 407   push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
 
 409   $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
 
 410   $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
 
 412 #  $::form->{IN} = "balance_sheet.html";
 
 415   print $::form->parse_html_template('rp/balance_sheet', $data);
 
 417   $::lxdebug->leave_sub;
 
 420 sub generate_projects {
 
 421   $main::lxdebug->enter_sub();
 
 423   $main::auth->assert('report');
 
 425   my $form     = $main::form;
 
 426   my %myconfig = %main::myconfig;
 
 427   my $locale   = $main::locale;
 
 429   my $project            = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
 
 430   $form->{projectnumber} = $project ? $project->projectnumber : '';
 
 432   $form->{nextsub} = "generate_projects";
 
 433   $form->{title}   = $locale->text('Project Transactions');
 
 434   RP->trial_balance(\%myconfig, \%$form);
 
 436   list_accounts('generate_projects');
 
 438   $main::lxdebug->leave_sub();
 
 444 # included links to display transactions for period entered
 
 445 # added headers and subtotals
 
 447 sub generate_trial_balance {
 
 448   $main::lxdebug->enter_sub();
 
 450   $main::auth->assert('report');
 
 452   my $form     = $main::form;
 
 453   my %myconfig = %main::myconfig;
 
 454   my $locale   = $main::locale;
 
 455   my $defaults = SL::DB::Default->get;
 
 457   if ($form->{reporttype} eq "custom") {
 
 459     #forgotten the year --> thisyear
 
 460     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 461       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 467     if ($form->{duetyp} eq "13") {
 
 468       $form->{fromdate} = "1.1.$form->{year}";
 
 469       $form->{todate}   = "31.12.$form->{year}";
 
 473     if ($form->{duetyp} eq "A") {
 
 474       $form->{fromdate} = "1.1.$form->{year}";
 
 475       $form->{todate}   = "31.3.$form->{year}";
 
 477     if ($form->{duetyp} eq "B") {
 
 478       $form->{fromdate} = "1.4.$form->{year}";
 
 479       $form->{todate}   = "30.6.$form->{year}";
 
 481     if ($form->{duetyp} eq "C") {
 
 482       $form->{fromdate} = "1.7.$form->{year}";
 
 483       $form->{todate}   = "30.9.$form->{year}";
 
 485     if ($form->{duetyp} eq "D") {
 
 486       $form->{fromdate} = "1.10.$form->{year}";
 
 487       $form->{todate}   = "31.12.$form->{year}";
 
 492       $form->{duetyp} eq "1" && do {
 
 493         $form->{fromdate} = "1.1.$form->{year}";
 
 494         $form->{todate}   = "31.1.$form->{year}";
 
 497       $form->{duetyp} eq "2" && do {
 
 498         $form->{fromdate} = "1.2.$form->{year}";
 
 500         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 501         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 502         $form->{todate} = "$leap.2.$form->{year}";
 
 505       $form->{duetyp} eq "3" && do {
 
 506         $form->{fromdate} = "1.3.$form->{year}";
 
 507         $form->{todate}   = "31.3.$form->{year}";
 
 510       $form->{duetyp} eq "4" && do {
 
 511         $form->{fromdate} = "1.4.$form->{year}";
 
 512         $form->{todate}   = "30.4.$form->{year}";
 
 515       $form->{duetyp} eq "5" && do {
 
 516         $form->{fromdate} = "1.5.$form->{year}";
 
 517         $form->{todate}   = "31.5.$form->{year}";
 
 520       $form->{duetyp} eq "6" && do {
 
 521         $form->{fromdate} = "1.6.$form->{year}";
 
 522         $form->{todate}   = "30.6.$form->{year}";
 
 525       $form->{duetyp} eq "7" && do {
 
 526         $form->{fromdate} = "1.7.$form->{year}";
 
 527         $form->{todate}   = "31.7.$form->{year}";
 
 530       $form->{duetyp} eq "8" && do {
 
 531         $form->{fromdate} = "1.8.$form->{year}";
 
 532         $form->{todate}   = "31.8.$form->{year}";
 
 535       $form->{duetyp} eq "9" && do {
 
 536         $form->{fromdate} = "1.9.$form->{year}";
 
 537         $form->{todate}   = "30.9.$form->{year}";
 
 540       $form->{duetyp} eq "10" && do {
 
 541         $form->{fromdate} = "1.10.$form->{year}";
 
 542         $form->{todate}   = "31.10.$form->{year}";
 
 545       $form->{duetyp} eq "11" && do {
 
 546         $form->{fromdate} = "1.11.$form->{year}";
 
 547         $form->{todate}   = "30.11.$form->{year}";
 
 550       $form->{duetyp} eq "12" && do {
 
 551         $form->{fromdate} = "1.12.$form->{year}";
 
 552         $form->{todate}   = "31.12.$form->{year}";
 
 556     hotfix_reformat_date();
 
 560   # get for each account initial balance, debits and credits
 
 561   RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
 
 564   $form->{rowcount} = scalar @{ $form->{TB} || [] };
 
 565   $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
 
 568     "accno",               "description",
 
 569     "last_transaction",    "soll_eb",
 
 572     "soll_kumuliert",      "haben_kumuliert",
 
 573     "soll_saldo",          "haben_saldo"
 
 577   my $attachment_basename = $locale->text('trial_balance');
 
 578   my $report              = SL::ReportGenerator->new(\%myconfig, $form);
 
 580   my @hidden_variables    = qw(fromdate todate year cash);
 
 582   my $href                = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
 
 585     'accno'               => { 'text' => $locale->text('Account'), },
 
 586     'description'         => { 'text' => $locale->text('Description'), },
 
 587     'last_transaction'    => { 'text' => $locale->text('Last Transaction'), },
 
 588     'soll_eb'             => { 'text' => $locale->text('Debit Starting Balance'), },
 
 589     'haben_eb'            => { 'text' => $locale->text('Credit Starting Balance'), },
 
 590     'soll'                => { 'text' => $locale->text('Debit'), },
 
 591     'haben'               => { 'text' => $locale->text('Credit'), },
 
 592     'soll_kumuliert'      => { 'text' => $locale->text('Sum Debit'), },
 
 593     'haben_kumuliert'     => { 'text' => $locale->text('Sum Credit'), },
 
 594     'soll_saldo'          => { 'text' => $locale->text('Saldo Debit'), },
 
 595     'haben_saldo'         => { 'text' => $locale->text('Saldo Credit'), }
 
 600   my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 602   map { $column_defs{$_}->{visible} =  1 } @columns;
 
 604   $report->set_columns(%column_defs);
 
 605   $report->set_column_order(@columns);
 
 607   $report->set_export_options('generate_trial_balance', @hidden_variables);
 
 612   $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
 
 614   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
 
 615   push (@options, $form->{print_date});
 
 617   $form->{company} = $locale->text('Company') . " " . $defaults->company;
 
 618   push (@options, $form->{company});
 
 621   $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
 
 623   my @custom_headers = ([
 
 624     { text => $::locale->text('Account'),          rowspan => 2, },
 
 625     { text => $::locale->text('Description'),      rowspan => 2, },
 
 626     { text => $::locale->text('Last Transaction'), rowspan => 2, },
 
 627     { text => $::locale->text('Starting Balance'), colspan => 2, },
 
 628     { text => $::locale->text('Sum for')   . " $form->{template_fromto}", colspan => 2, },
 
 629     { text => $::locale->text('Sum per')   . " $form->{template_to}",     colspan => 2, },
 
 630     { text => $::locale->text('Saldo per') . " $form->{template_to}",     colspan => 2, },
 
 635     { text => $::locale->text('Assets'), },
 
 636     { text => $::locale->text('Equity'), },
 
 637     { text => $::locale->text('Debit'),  },
 
 638     { text => $::locale->text('Credit'), },
 
 639     { text => $::locale->text('Debit'),  },
 
 640     { text => $::locale->text('Credit'), },
 
 641     { text => $::locale->text('Debit'),  },
 
 642     { text => $::locale->text('Credit'), },
 
 645   $report->set_options('output_format'        => 'HTML',
 
 646                        'top_info_text'        => join("\n", @options),
 
 647                        'title'                => $form->{title},
 
 648                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
 649                        'html_template'        => 'rp/html_report_susa',
 
 650                        'pdf_template'         => 'rp/html_report_susa',
 
 652   $report->set_custom_headers(@custom_headers);
 
 653   $report->set_options_from_form();
 
 654   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 656   # add sort and escape callback, this one we use for the add sub
 
 657   $form->{callback} = $href .= "&sort=$form->{sort}";
 
 659   # escape callback for href
 
 660   my $callback = $form->escape($href);
 
 662   my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 664   my %totals    = map { $_ => 0 } @subtotal_columns;
 
 666   my $edit_url = build_std_url('action=edit', 'type', 'vc');
 
 669   foreach my $accno (@{ $form->{TB} || [] }) {
 
 671     $accno->{soll} = $accno->{debit};
 
 672     $accno->{haben} = $accno->{credit};
 
 673     map { $totals{$_}    += $accno->{$_} } @subtotal_columns;
 
 675     map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
 
 676       qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 680     foreach my $column (@columns) {
 
 682         'data'  => $accno->{$column},
 
 683         'align' => $column_alignment{$column},
 
 687     $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}));
 
 689     my $row_set = [ $row ];
 
 692     $report->add_data($row_set);
 
 697   $report->add_separator();
 
 699   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
 701   $report->generate_with_headers();
 
 703   $main::lxdebug->leave_sub();
 
 707 sub create_subtotal_row {
 
 708   $main::lxdebug->enter_sub();
 
 710   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
 712   my $form     = $main::form;
 
 713   my %myconfig = %main::myconfig;
 
 714   my $locale   = $main::locale;
 
 716   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
 718   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
 720   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
 
 722   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
 724   $main::lxdebug->leave_sub();
 
 729 sub create_list_accounts_subtotal_row {
 
 730   $main::lxdebug->enter_sub();
 
 732   my ($subtotals, $columns, $fields, $class) = @_;
 
 734   my $form     = $main::form;
 
 735   my %myconfig = %main::myconfig;
 
 736   my $locale   = $main::locale;
 
 738   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 740   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
 
 742   $main::lxdebug->leave_sub();
 
 748   $main::lxdebug->enter_sub();
 
 752   my $form     = $main::form;
 
 753   my %myconfig = %main::myconfig;
 
 754   my $locale   = $main::locale;
 
 757   if ($form->{department}) {
 
 758     my ($department) = split /--/, $form->{department};
 
 759     push @options, $locale->text('Department') . " : $department";
 
 761   if ($form->{projectnumber}) {
 
 762     push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
 
 765   # if there are any dates
 
 766   if ($form->{fromdate} || $form->{todate}) {
 
 767     my ($fromdate, $todate);
 
 769     if ($form->{fromdate}) {
 
 770       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 772     if ($form->{todate}) {
 
 773       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
 
 776     push @options, "$fromdate - $todate";
 
 779     push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 782   my @columns     = qw(accno description begbalance debit credit endbalance);
 
 784     'accno'       => { 'text' => $locale->text('Account'), },
 
 785     'description' => { 'text' => $locale->text('Description'), },
 
 786     'debit'       => { 'text' => $locale->text('Debit'), },
 
 787     'credit'      => { 'text' => $locale->text('Credit'), },
 
 788     'begbalance'  => { 'text' => $locale->text('Balance'), },
 
 789     'endbalance'  => { 'text' => $locale->text('Balance'), },
 
 791   my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
 
 793   my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
 
 795   $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
 
 797   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 799   $report->set_options('top_info_text'         => join("\n", @options),
 
 800                        'output_format'         => 'HTML',
 
 801                        'title'                 => $form->{title},
 
 802                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
 
 803                        'std_column_visibility' => 1,
 
 805   $report->set_options_from_form();
 
 806   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 808   $report->set_columns(%column_defs);
 
 809   $report->set_column_order(@columns);
 
 811   $report->set_export_options($action, @hidden_variables);
 
 813   my @totals_columns = qw(credit debit begbalance endbalance);
 
 814   my %subtotals      = map { $_ => 0 } @totals_columns;
 
 815   my %totals         = map { $_ => 0 } @totals_columns;
 
 816   my $found_heading  = 0;
 
 817   my @tb             = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
 
 819   # sort the whole thing by account numbers and display
 
 820   foreach my $idx (0 .. scalar(@tb) - 1) {
 
 822     my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
 
 824     my $ml   = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
 
 826     my $row  = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
 
 828     if ($ref->{charttype} eq 'H') {
 
 829       next unless ($form->{l_heading});
 
 831       %subtotals                   = map { $_ => 0 } @totals_columns;
 
 833       $row->{description}->{class} = 'listheading';
 
 834       $row->{description}->{data}  = $ref->{description};
 
 836       $report->add_data($row);
 
 841     foreach (qw(debit credit)) {
 
 842       $subtotals{$_} += $ref->{$_};
 
 843       $totals{$_}    += $ref->{$_};
 
 846     $subtotals{begbalance} += $ref->{balance} * $ml;
 
 847     $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
 
 849     map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
 
 850     map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
 
 852     $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
 
 853     $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
 
 855     $report->add_data($row);
 
 857     if ($form->{l_heading} && $found_heading &&
 
 858         (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
 
 859       $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
 
 863   $report->add_separator();
 
 865   $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
 
 867   $report->generate_with_headers();
 
 869   $main::lxdebug->leave_sub();
 
 872 sub generate_ar_aging {
 
 873   $main::lxdebug->enter_sub();
 
 875   $main::auth->assert('general_ledger');
 
 877   my $form     = $main::form;
 
 878   my %myconfig = %main::myconfig;
 
 879   my $locale   = $main::locale;
 
 882   ($form->{customer}) = split(/--/, $form->{customer});
 
 884   $form->{ct}   = "customer";
 
 885   $form->{arap} = "ar";
 
 887   $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
 
 889   RP->aging(\%myconfig, \%$form);
 
 892   $main::lxdebug->leave_sub();
 
 895 sub generate_ap_aging {
 
 896   $main::lxdebug->enter_sub();
 
 898   $main::auth->assert('general_ledger');
 
 900   my $form     = $main::form;
 
 901   my %myconfig = %main::myconfig;
 
 902   my $locale   = $main::locale;
 
 905   ($form->{vendor}) = split(/--/, $form->{vendor});
 
 907   $form->{ct}   = "vendor";
 
 908   $form->{arap} = "ap";
 
 910   $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
 
 912   RP->aging(\%myconfig, \%$form);
 
 915   $main::lxdebug->leave_sub();
 
 918 sub create_aging_subtotal_row {
 
 919   $main::lxdebug->enter_sub();
 
 921   my ($subtotals, $columns, $periods, $class) = @_;
 
 923   my $form     = $main::form;
 
 924   my %myconfig = %main::myconfig;
 
 925   my $locale   = $main::locale;
 
 927   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 929   foreach (@{ $periods }) {
 
 930     $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
 
 931     $subtotals->{$_}      = 0;
 
 934   $main::lxdebug->leave_sub();
 
 940   $main::lxdebug->enter_sub();
 
 942   $main::auth->assert('general_ledger');
 
 944   my $form     = $main::form;
 
 945   my %myconfig = %main::myconfig;
 
 946   my $locale   = $main::locale;
 
 947   my $cgi      = $::request->{cgi};
 
 949   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 951   my @columns = qw(statement ct invnumber transdate duedate amount open);
 
 954     'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
 
 955     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
 
 956     'invnumber' => { 'text' => $locale->text('Invoice'), },
 
 957     'transdate' => { 'text' => $locale->text('Date'), },
 
 958     'duedate'   => { 'text' => $locale->text('Due'), },
 
 959     'amount'    => { 'text' => $locale->text('Amount'), },
 
 960     'open'      => { 'text' => $locale->text('Open'), },
 
 963   my %column_alignment = ('statement' => 'center',
 
 964                           map { $_ => 'right' } qw(open amount));
 
 966   $report->set_options('std_column_visibility' => 1);
 
 967   $report->set_columns(%column_defs);
 
 968   $report->set_column_order(@columns);
 
 970   my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype);
 
 971   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
 
 974   my $attachment_basename;
 
 976   if ($form->{department}) {
 
 977     my ($department) = split /--/, $form->{department};
 
 978     push @options, $locale->text('Department') . " : $department";
 
 979     $form->{callback} .= "&department=" . E($department);
 
 982   if (($form->{arap} eq 'ar') && $form->{customer}) {
 
 983     push @options, $form->{customer};
 
 984     $attachment_basename = $locale->text('ar_aging_list');
 
 985     $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
 
 988   if (($form->{arap} eq 'ap') && $form->{vendor}) {
 
 989     push @options, $form->{vendor};
 
 990     $attachment_basename = $locale->text('ap_aging_list');
 
 991     $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
 
 994   if ($form->{fromdate}) {
 
 995     push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
 997     push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1000   $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
 
1002   $report->set_options('top_info_text'        => join("\n", @options),
 
1003                        'output_format'        => 'HTML',
 
1004                        'title'                => $form->{title},
 
1005                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1007   $report->set_options_from_form();
 
1008   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1010   my $previous_ctid = 0;
 
1012   my @periods       = qw(open amount);
 
1013   my %subtotals     = map { $_ => 0 } @periods;
 
1014   my %totals        = map { $_ => 0 } @periods;
 
1016   foreach my $ref (@{ $form->{AG} }) {
 
1017     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
 
1018       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
 
1021     foreach my $key (@periods) {
 
1022       $subtotals{$key}  += $ref->{"$key"};
 
1023       $totals{$key}     += $ref->{"$key"};
 
1024       $ref->{"$key"}  = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
 
1029     foreach my $column (@columns) {
 
1031         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
 
1032         'align'  => $column_alignment{$column},
 
1033         'valign' => $column eq 'statement' ? 'center' : '',
 
1037     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
 
1039     if ($previous_ctid != $ref->{ctid}) {
 
1040       $row->{statement}->{raw_data} =
 
1041           $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
 
1042         . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
 
1043       $row->{ct}->{data} = $ref->{name};
 
1048     $previous_ctid = $ref->{ctid};
 
1050     $report->add_data($row);
 
1053   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
 
1055   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
 
1057   if ($form->{arap} eq 'ar') {
 
1058     my $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
 
1059     my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
 
1060                                                                                'PRINT_OPTIONS' => print_options(inline => 1), });
 
1061     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
 
1062                          'raw_bottom_info_text' => $raw_bottom_info_text);
 
1065   $report->generate_with_headers();
 
1067   $main::lxdebug->leave_sub();
 
1071   $main::lxdebug->enter_sub();
 
1073   my $form     = $main::form;
 
1074   my %myconfig = %main::myconfig;
 
1075   my $locale   = $main::locale;
 
1077   RP->aging(\%myconfig, \%$form);
 
1079   map { $_->{checked} = "checked" } @{ $form->{AG} };
 
1083   $main::lxdebug->leave_sub();
 
1087   $::lxdebug->enter_sub;
 
1088   $::auth->assert('general_ledger');
 
1090   # get name and email addresses
 
1092   for my $i (1 .. $::form->{rowcount}) {
 
1093     next unless $::form->{"statement_$i"};
 
1094     $::form->{"$::form->{ct}_id"} = $::form->{"$::form->{ct}_id_$i"};
 
1095     RP->get_customer(\%::myconfig, $::form);
 
1100   $::form->error($::locale->text('Nothing selected!')) unless $selected;
 
1102   $::form->{media} = "email";
 
1104   # save all other variables
 
1106   for my $key (keys %$::form) {
 
1107     next if any { $key eq $_ } qw(login password action email cc bcc subject message type sendmode format header);
 
1108     next unless '' eq ref $::form->{$key};
 
1109     push @hidden_values, $key;
 
1113   print $::form->parse_html_template('rp/e_mail', {
 
1114     show_bcc      => $::auth->assert('email_bcc', 'may fail'),
 
1115     print_options => print_options(inline => 1),
 
1116     hidden_values => \@hidden_values,
 
1119   $::lxdebug->leave_sub;
 
1123   $main::lxdebug->enter_sub();
 
1125   $main::auth->assert('general_ledger');
 
1127   my $form     = $main::form;
 
1128   my %myconfig = %main::myconfig;
 
1129   my $locale   = $main::locale;
 
1131   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
 
1132     unless $form->{subject};
 
1134   RP->aging(\%myconfig, \%$form);
 
1136   $form->{"statement_1"} = 1;
 
1138   $form->{media} = 'email';
 
1141   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
 
1143   $main::lxdebug->leave_sub();
 
1147   $main::lxdebug->enter_sub();
 
1149   $main::auth->assert('general_ledger');
 
1151   my $form     = $main::form;
 
1152   my %myconfig = %main::myconfig;
 
1153   my $locale   = $main::locale;
 
1155   if ($form->{media} eq 'printer') {
 
1156     $form->error($locale->text('Select postscript or PDF!'))
 
1157       if ($form->{format} !~ /(postscript|pdf)/);
 
1161   for my $i (1 .. $form->{rowcount}) {
 
1162     if ($form->{"statement_$i"}) {
 
1163       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
 
1169   $form->error($locale->text('Nothing selected!')) unless $selected;
 
1171   if ($form->{media} eq 'printer') {
 
1172     $form->{"$form->{ct}_id"} = "";
 
1174     $form->{"statement_1"} = 1;
 
1177   RP->aging(\%myconfig, \%$form);
 
1181   $form->redirect($locale->text('Statements sent to printer!'))
 
1182     if ($form->{media} eq 'printer');
 
1184   $main::lxdebug->leave_sub();
 
1188   $main::lxdebug->enter_sub();
 
1190   $main::auth->assert('general_ledger');
 
1192   my $form     = $main::form;
 
1193   my %myconfig = %main::myconfig;
 
1194   my $locale   = $main::locale;
 
1196   my $defaults = SL::DB::Default->get;
 
1197   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
 
1198   $form->{templates} = $defaults->templates;
 
1200   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
 
1202   my $suffix = "html";
 
1203   my $attachment_suffix = "html";
 
1204   if ($form->{format} eq 'postscript') {
 
1205     $form->{postscript} = 1;
 
1207     $attachment_suffix = "ps";
 
1208   } elsif ($form->{format} eq 'pdf') {
 
1211     $attachment_suffix = "pdf";
 
1214   $form->{IN}  = "$form->{type}.$suffix";
 
1215   $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
 
1217   # Save $form->{email} because it will be overwritten.
 
1218   $form->{EMAIL_RECIPIENT} = $form->{email};
 
1222   while (@{ $form->{AG} }) {
 
1224     my $ref = shift @{ $form->{AG} };
 
1226     if ($ctid != $ref->{ctid}) {
 
1228       $ctid = $ref->{ctid};
 
1231       if ($form->{"statement_$i"}) {
 
1234           ("name", "street", "zipcode", "city", "country", "contact", "email",
 
1235            "$form->{ct}phone", "$form->{ct}fax");
 
1236         map { $form->{$_} = $ref->{$_} } @a;
 
1238         $form->{ $form->{ct} } = $form->{name};
 
1239         $form->{"$form->{ct}_id"} = $ref->{ctid};
 
1241         map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
 
1243         foreach my $item (qw(c0 c30 c60 c90)) {
 
1244           $form->{$item} = ();
 
1245           $form->{"${item}total"} = 0;
 
1248         &statement_details($ref);
 
1252           if (scalar(@{ $form->{AG} }) > 0) {
 
1254             # one or more left to go
 
1255             if ($ctid == $form->{AG}->[0]->{ctid}) {
 
1256               $ref = shift @{ $form->{AG} };
 
1257               &statement_details($ref);
 
1260               $ref = scalar(@{ $form->{AG} });
 
1266             # set initial ref to 0
 
1273           $form->{"${_}total"} =
 
1274             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
 
1275         } ('c0', 'c30', 'c60', 'c90', "");
 
1277         $form->{attachment_filename} =  $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
 
1278         $form->{attachment_filename} =~ s/\s+/_/g;
 
1280         $form->parse_template(\%myconfig);
 
1285   # saving the history
 
1286   if(!exists $form->{addition} && $form->{id} ne "") {
 
1287     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1288     $form->{addition} = "PRINTED";
 
1289     $form->{what_done} = $form->{type};
 
1290     $form->save_history;
 
1292   # /saving the history
 
1293   $main::lxdebug->leave_sub();
 
1296 sub statement_details {
 
1297   $main::lxdebug->enter_sub();
 
1299   $main::auth->assert('general_ledger');
 
1301   my $form     = $main::form;
 
1302   my %myconfig = %main::myconfig;
 
1303   my $locale   = $main::locale;
 
1307   push @{ $form->{invnumber} }, $ref->{invnumber};
 
1308   push @{ $form->{invdate} },   $ref->{transdate};
 
1309   push @{ $form->{duedate} },   $ref->{duedate};
 
1310   push @{ $form->{amount} },    $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
 
1311   push @{ $form->{open} },      $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
 
1313   foreach my $item (qw(c0 c30 c60 c90)) {
 
1314     if ($ref->{exchangerate} * 1) {
 
1315       # add only the open amount of the invoice to the aging, not the total amount
 
1316       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
 
1317       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
 
1318       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
 
1319       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
 
1321     $form->{"${item}total"} += $ref->{$item};
 
1322     $form->{total}          += $ref->{$item};
 
1323     push @{ $form->{$item} },
 
1324       $form->format_amount(\%myconfig, $ref->{$item}, 2);
 
1327   $main::lxdebug->leave_sub();
 
1330 sub generate_tax_report {
 
1331   $::lxdebug->enter_sub;
 
1332   $::auth->assert('report');
 
1334   RP->tax_report(\%::myconfig, $::form);
 
1336   my $descvar     = "$::form->{accno}_description";
 
1337   my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
 
1341   my $callback = build_std_url('action=generate_tax_report', $descvar,
 
1342     qw(fromdate todate db method accno department report title));
 
1344   my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
 
1347   for my $item (@columns, 'subtotal') {
 
1348     if ($::form->{"l_$item"} eq "Y") {
 
1349       $callback .= "&l_$item=Y";
 
1350       $href     .= "&l_$item=Y";
 
1354   for my $item (@columns) {
 
1355     if ($::form->{"l_$item"} eq "Y") {
 
1356       push @column_index, $item;
 
1361   if ($::form->{department}) {
 
1362     my ($department) = split /--/, $::form->{department};
 
1363     push @options, $::locale->text('Department') . " : $department";
 
1366   # if there are any dates
 
1367   if ($::form->{fromdate} || $::form->{todate}) {
 
1368     my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
 
1369     my $todate   = $::form->{todate}   ? $::locale->date(\%::myconfig, $::form->{todate}, 1)   : '';
 
1370     push @options, "$fromdate - $todate";
 
1372     push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
 
1375   my ($name, $invoice, $arap);
 
1376   if ($::form->{db} eq 'ar') {
 
1377     $name    = $::locale->text('Customer');
 
1381   if ($::form->{db} eq 'ap') {
 
1382     $name    = $::locale->text('Vendor');
 
1387   my %column_header = (
 
1388     id        => $::locale->text('ID'),
 
1389     invnumber => $::locale->text('Invoice'),
 
1390     transdate => $::locale->text('Date'),
 
1391     netamount => $::locale->text('Amount'),
 
1392     tax       => $::locale->text('Tax'),
 
1393     amount    => $::locale->text('Total'),
 
1397   my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
 
1399   $callback .= "&sort=$::form->{sort}";
 
1402   if (@{ $::form->{TR} }) {
 
1403     $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
 
1406   my ($totalnetamount, $totaltax, @data);
 
1407   for my $ref (@{ $::form->{TR} }) {
 
1409     my $module = ($ref->{invoice}) ? $invoice : $arap;
 
1411     if ($::form->{l_subtotal} eq 'Y') {
 
1412       if ($sameitem ne $ref->{ $::form->{sort} }) {
 
1415           netamount => $subtotalnetamount,
 
1416           tax       => $subtotaltax,
 
1417           amount    => $subtotal,
 
1419         $subtotalnetamount = 0;
 
1421         $sameitem          = $ref->{ $::form->{sort} };
 
1425     $subtotalnetamount += $ref->{netamount};
 
1426     $subtotaltax       += $ref->{tax};
 
1427     $totalnetamount    += $ref->{netamount};
 
1428     $totaltax          += $ref->{tax};
 
1429     $ref->{amount}      = $ref->{netamount} + $ref->{tax};
 
1431     push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
 
1432     $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
 
1433     $data[-1]{$_}{numeric}     = 1 for qw(netamount tax amount);
 
1436   if ($::form->{l_subtotal} eq 'Y') {
 
1439       netamount => $subtotalnetamount,
 
1440       tax       => $subtotaltax,
 
1441       amount    => $subtotal,
 
1447     netamount => $totalnetamount,
 
1449     amount    => $totalnetamount + $totaltax,
 
1453   print $::form->parse_html_template('rp/tax_report', {
 
1454     column_index  => \@column_index,
 
1455     column_header => \%column_header,
 
1456     column_sorted => \%column_sorted,
 
1459     options       => \@options,
 
1462   $::lxdebug->leave_sub;
 
1466   $main::lxdebug->enter_sub();
 
1468   $main::auth->assert('cash');
 
1470   my $form     = $main::form;
 
1471   my %myconfig = %main::myconfig;
 
1472   my $locale   = $main::locale;
 
1474   if ($form->{account}) {
 
1475     ($form->{paymentaccounts}) = split /--/, $form->{account};
 
1479   if ($form->{department}) {
 
1480     (my $department, $form->{department_id}) = split /--/, $form->{department};
 
1481     $option = $locale->text('Department') . " : $department";
 
1484   report_generator_set_default_sort('transdate', 1);
 
1486   RP->payments(\%myconfig, \%$form);
 
1488   my @hidden_variables = qw(account title department reference source memo fromdate todate
 
1489                             fx_transaction db prepayment paymentaccounts sort);
 
1491   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
 
1492   $form->{callback} = $href;
 
1494   my @columns     = qw(transdate invnumber name paid source memo);
 
1496     'name'      => { 'text' => $locale->text('Description'), },
 
1497     'invnumber' => { 'text' => $locale->text('Reference'), },
 
1498     'transdate' => { 'text' => $locale->text('Date'), },
 
1499     'paid'      => { 'text' => $locale->text('Amount'), },
 
1500     'source'    => { 'text' => $locale->text('Source'), },
 
1501     'memo'      => { 'text' => $locale->text('Memo'), },
 
1503   my %column_alignment = ('paid' => 'right');
 
1505   foreach my $name (grep { $_ ne 'paid' } @columns) {
 
1506     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
1507     $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
 
1511   if ($form->{fromdate}) {
 
1512     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
 
1514   if ($form->{todate}) {
 
1515     push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1518   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
1520   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
 
1522   $report->set_options('top_info_text'         => join("\n", @options),
 
1523                        'output_format'         => 'HTML',
 
1524                        'title'                 => $form->{title},
 
1525                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1526                        'std_column_visibility' => 1,
 
1528   $report->set_options_from_form();
 
1530   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1532   $report->set_columns(%column_defs);
 
1533   $report->set_column_order(@columns);
 
1535   $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
 
1537   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
1541   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
 
1542     next unless @{ $form->{ $ref->{id} } };
 
1544     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
 
1546     my $subtotal_paid = 0;
 
1548     foreach my $payment (@{ $form->{ $ref->{id} } }) {
 
1549       my $module = $payment->{module};
 
1550       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
 
1551       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
 
1553       $subtotal_paid += $payment->{paid};
 
1554       $total_paid    += $payment->{paid};
 
1556       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
 
1560       foreach my $column (@columns) {
 
1562           'data'  => $payment->{$column},
 
1563           'align' => $column_alignment{$column},
 
1567       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
 
1569       $report->add_data($row);
 
1572     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
 
1574       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
 
1576       'class' => 'listsubtotal',
 
1579     $report->add_data($row);
 
1582   $report->add_separator();
 
1584   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
 
1586     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
 
1588     'class' => 'listtotal',
 
1591   $report->add_data($row);
 
1593   $report->generate_with_headers();
 
1595   $main::lxdebug->leave_sub();
 
1599   $::lxdebug->enter_sub;
 
1601   my ($dont_print) = @_;
 
1603   $::form->{sendmode} = "attachment";
 
1604   $::form->{format} ||= $::myconfig{template_format} || "pdf";
 
1605   $::form->{copies} ||= $::myconfig{copies}          || 2;
 
1607   $::form->{PD}{ $::form->{type} }     = "selected";
 
1608   $::form->{DF}{ $::form->{format} }   = "selected";
 
1609   $::form->{OP}{ $::form->{media} }    = "selected";
 
1610   $::form->{SM}{ $::form->{sendmode} } = "selected";
 
1612   my $output = $::form->parse_html_template('rp/print_options', {
 
1613     got_printer => $::myconfig{printer},
 
1614     show_latex  => $::lx_office_conf{print_templates}->{latex},
 
1615     is_email    => $::form->{media} eq 'email',
 
1618   print $output unless $dont_print;
 
1620   $::lxdebug->leave_sub;
 
1626   $main::lxdebug->enter_sub();
 
1628   $main::auth->assert('report');
 
1630   my $form     = $main::form;
 
1631   my %myconfig = %main::myconfig;
 
1632   my $locale   = $main::locale;
 
1634   my $defaults = SL::DB::Default->get;
 
1635   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
 
1636   $form->{templates} = $defaults->templates;
 
1638   $form->{padding} = "  ";
 
1639   $form->{bold}    = "<b>";
 
1640   $form->{endbold} = "</b>";
 
1641   $form->{br}      = "<br>";
 
1643   if ($form->{reporttype} eq "custom") {
 
1645     #forgotten the year --> thisyear
 
1646     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
1647       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
1653     if ($form->{duetyp} eq "13") {
 
1654       $form->{fromdate}        = "1.1.$form->{year}";
 
1655       $form->{todate}          = "31.12.$form->{year}";
 
1656       $form->{comparefromdate} = "1.01.$form->{year}";
 
1657       $form->{comparetodate}   = "31.12.$form->{year}";
 
1661     if ($form->{duetyp} eq "A") {
 
1662       $form->{fromdate}        = "1.1.$form->{year}";
 
1663       $form->{todate}          = "31.3.$form->{year}";
 
1664       $form->{comparefromdate} = "1.01.$form->{year}";
 
1665       $form->{comparetodate}   = "31.03.$form->{year}";
 
1667     if ($form->{duetyp} eq "B") {
 
1668       $form->{fromdate}        = "1.4.$form->{year}";
 
1669       $form->{todate}          = "30.6.$form->{year}";
 
1670       $form->{comparefromdate} = "1.01.$form->{year}";
 
1671       $form->{comparetodate}   = "30.06.$form->{year}";
 
1673     if ($form->{duetyp} eq "C") {
 
1674       $form->{fromdate}        = "1.7.$form->{year}";
 
1675       $form->{todate}          = "30.9.$form->{year}";
 
1676       $form->{comparefromdate} = "1.01.$form->{year}";
 
1677       $form->{comparetodate}   = "30.09.$form->{year}";
 
1679     if ($form->{duetyp} eq "D") {
 
1680       $form->{fromdate}        = "1.10.$form->{year}";
 
1681       $form->{todate}          = "31.12.$form->{year}";
 
1682       $form->{comparefromdate} = "1.01.$form->{year}";
 
1683       $form->{comparetodate}   = "31.12.$form->{year}";
 
1688       $form->{duetyp} eq "1" && do {
 
1689         $form->{fromdate}        = "1.1.$form->{year}";
 
1690         $form->{todate}          = "31.1.$form->{year}";
 
1691         $form->{comparefromdate} = "1.01.$form->{year}";
 
1692         $form->{comparetodate}   = "31.01.$form->{year}";
 
1695       $form->{duetyp} eq "2" && do {
 
1696         $form->{fromdate} = "1.2.$form->{year}";
 
1698         #this works from 1901 to 2099, 1900 and 2100 fail.
 
1699         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
1700         $form->{todate}          = "$leap.2.$form->{year}";
 
1701         $form->{comparefromdate} = "1.01.$form->{year}";
 
1702         $form->{comparetodate}   = "$leap.02.$form->{year}";
 
1705       $form->{duetyp} eq "3" && do {
 
1706         $form->{fromdate}        = "1.3.$form->{year}";
 
1707         $form->{todate}          = "31.3.$form->{year}";
 
1708         $form->{comparefromdate} = "1.01.$form->{year}";
 
1709         $form->{comparetodate}   = "31.03.$form->{year}";
 
1712       $form->{duetyp} eq "4" && do {
 
1713         $form->{fromdate}        = "1.4.$form->{year}";
 
1714         $form->{todate}          = "30.4.$form->{year}";
 
1715         $form->{comparefromdate} = "1.01.$form->{year}";
 
1716         $form->{comparetodate}   = "30.04.$form->{year}";
 
1719       $form->{duetyp} eq "5" && do {
 
1720         $form->{fromdate}        = "1.5.$form->{year}";
 
1721         $form->{todate}          = "31.5.$form->{year}";
 
1722         $form->{comparefromdate} = "1.01.$form->{year}";
 
1723         $form->{comparetodate}   = "31.05.$form->{year}";
 
1726       $form->{duetyp} eq "6" && do {
 
1727         $form->{fromdate}        = "1.6.$form->{year}";
 
1728         $form->{todate}          = "30.6.$form->{year}";
 
1729         $form->{comparefromdate} = "1.01.$form->{year}";
 
1730         $form->{comparetodate}   = "30.06.$form->{year}";
 
1733       $form->{duetyp} eq "7" && do {
 
1734         $form->{fromdate}        = "1.7.$form->{year}";
 
1735         $form->{todate}          = "31.7.$form->{year}";
 
1736         $form->{comparefromdate} = "1.01.$form->{year}";
 
1737         $form->{comparetodate}   = "31.07.$form->{year}";
 
1740       $form->{duetyp} eq "8" && do {
 
1741         $form->{fromdate}        = "1.8.$form->{year}";
 
1742         $form->{todate}          = "31.8.$form->{year}";
 
1743         $form->{comparefromdate} = "1.01.$form->{year}";
 
1744         $form->{comparetodate}   = "31.08.$form->{year}";
 
1747       $form->{duetyp} eq "9" && do {
 
1748         $form->{fromdate}        = "1.9.$form->{year}";
 
1749         $form->{todate}          = "30.9.$form->{year}";
 
1750         $form->{comparefromdate} = "1.01.$form->{year}";
 
1751         $form->{comparetodate}   = "30.09.$form->{year}";
 
1754       $form->{duetyp} eq "10" && do {
 
1755         $form->{fromdate}        = "1.10.$form->{year}";
 
1756         $form->{todate}          = "31.10.$form->{year}";
 
1757         $form->{comparefromdate} = "1.01.$form->{year}";
 
1758         $form->{comparetodate}   = "31.10.$form->{year}";
 
1761       $form->{duetyp} eq "11" && do {
 
1762         $form->{fromdate}        = "1.11.$form->{year}";
 
1763         $form->{todate}          = "30.11.$form->{year}";
 
1764         $form->{comparefromdate} = "1.01.$form->{year}";
 
1765         $form->{comparetodate}   = "30.11.$form->{year}";
 
1768       $form->{duetyp} eq "12" && do {
 
1769         $form->{fromdate}        = "1.12.$form->{year}";
 
1770         $form->{todate}          = "31.12.$form->{year}";
 
1771         $form->{comparefromdate} = "1.01.$form->{year}";
 
1772         $form->{comparetodate}   = "31.12.$form->{year}";
 
1776     hotfix_reformat_date();
 
1778     # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
 
1779     # ansonsten ist die prüfung in RP.pm
 
1780     # if (defined ($form->{fromdate|todate}=='..'))
 
1782     if ($form->{fromdate}){
 
1783       my ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
 
1784       my $datetime = $locale->parse_date_to_object(\%myconfig, $form->{fromdate});
 
1785       $datetime->set( month      => 1,
 
1787       $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
 
1789     if ($form->{todate}){
 
1790       $form->{comparetodate}   = $form->{todate};
 
1794   RP->bwa(\%myconfig, \%$form);
 
1796   ($form->{department}) = split /--/, $form->{department};
 
1799     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
1800   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
1802   # if there are any dates construct a where
 
1803   if ($form->{fromdate} || $form->{todate}) {
 
1805     unless ($form->{todate}) {
 
1806       $form->{todate} = $form->current_date(\%myconfig);
 
1809     my %germandate = ("dateformat" => "dd.mm.yyyy");
 
1811     my $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
 
1812     my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
 
1814     my $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
 
1815     my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
 
1817     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
1819         $locale->text('for Period')
 
1820       . qq|\n$longfromdate |
 
1821       . $locale->text('bis')
 
1825   $form->{IN} = "bwa.html";
 
1827   $form->parse_template;
 
1829   $main::lxdebug->leave_sub();
 
1832 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
 
1833 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
 
1834 sub hotfix_reformat_date {
 
1836   $main::lxdebug->enter_sub();
 
1838   my $form     = $main::form;
 
1839   my %myconfig = %main::myconfig;
 
1840   my $locale   = $main::locale;
 
1842   if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
 
1843     my $current_dateformat = $myconfig{dateformat};
 
1844     $myconfig{dateformat} = 'dd.mm.yyyy';
 
1845     $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
 
1846     $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
 
1847     $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
 
1848       unless (!defined ($form->{comparefromdate}));
 
1849     $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
 
1850       unless (!defined ($form->{comparetodate}));
 
1852     # Und wieder zurücksetzen
 
1853     $myconfig{dateformat} =  $current_dateformat; #'dd.mm.yyyy';
 
1854   } # Ende Hotifx Bug 1388
 
1856   $main::lxdebug->leave_sub();