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);
 
  43 use SL::ReportGenerator;
 
  46 require "bin/mozilla/arap.pl";
 
  47 require "bin/mozilla/common.pl";
 
  48 require "bin/mozilla/reportgenerator.pl";
 
  50 # note: this file was particularly hard to strictify.
 
  51 # alot of the vars are passed carelessly between invocations
 
  52 # should there be any missing vars, declare them globally
 
  55 # this is for our long dates
 
  56 # $locale->text('January')
 
  57 # $locale->text('February')
 
  58 # $locale->text('March')
 
  59 # $locale->text('April')
 
  60 # $locale->text('May ')
 
  61 # $locale->text('June')
 
  62 # $locale->text('July')
 
  63 # $locale->text('August')
 
  64 # $locale->text('September')
 
  65 # $locale->text('October')
 
  66 # $locale->text('November')
 
  67 # $locale->text('December')
 
  69 # this is for our short month
 
  70 # $locale->text('Jan')
 
  71 # $locale->text('Feb')
 
  72 # $locale->text('Mar')
 
  73 # $locale->text('Apr')
 
  74 # $locale->text('May')
 
  75 # $locale->text('Jun')
 
  76 # $locale->text('Jul')
 
  77 # $locale->text('Aug')
 
  78 # $locale->text('Sep')
 
  79 # $locale->text('Oct')
 
  80 # $locale->text('Nov')
 
  81 # $locale->text('Dec')
 
  83 # $locale->text('Balance Sheet')
 
  84 # $locale->text('Income Statement')
 
  85 # $locale->text('Trial Balance')
 
  86 # $locale->text('AR Aging')
 
  87 # $locale->text('AP Aging')
 
  88 # $locale->text('Search AR Aging')
 
  89 # $locale->text('Search AP Aging')
 
  90 # $locale->text('Tax collected')
 
  91 # $locale->text('Tax paid')
 
  92 # $locale->text('Receipts')
 
  93 # $locale->text('Payments')
 
  94 # $locale->text('Project Transactions')
 
  95 # $locale->text('Non-taxable Sales')
 
  96 # $locale->text('Non-taxable Purchases')
 
  97 # $locale->text('Business evaluation')
 
  99 # $form->parse_html_template('rp/html_report_susa')
 
 101 my $rp_access_map = {
 
 102   'projects'         => 'report',
 
 103   'ar_aging'         => 'general_ledger',
 
 104   'ap_aging'         => 'general_ledger',
 
 105   'receipts'         => 'cash',
 
 106   'payments'         => 'cash',
 
 107   'trial_balance'    => 'report',
 
 108   'income_statement' => 'report',
 
 110   'balance_sheet'    => 'report',
 
 113 sub check_rp_access {
 
 114   my $form     = $main::form;
 
 116   my $right   = $rp_access_map->{$form->{report}};
 
 117   $right    ||= 'DOES_NOT_EXIST';
 
 119   $main::auth->assert($right);
 
 123   $::lxdebug->enter_sub;
 
 128     balance_sheet        => $::locale->text('Balance Sheet'),
 
 129     income_statement     => $::locale->text('Income Statement'),
 
 130     trial_balance        => $::locale->text('Trial Balance'),
 
 131     ar_aging             => $::locale->text('Search AR Aging'),
 
 132     ap_aging             => $::locale->text('Search AP Aging'),
 
 133     tax_collected        => $::locale->text('Tax collected'),
 
 134     tax_paid             => $::locale->text('Tax paid'),
 
 135     nontaxable_sales     => $::locale->text('Non-taxable Sales'),
 
 136     nontaxable_purchases => $::locale->text('Non-taxable Purchases'),
 
 137     receipts             => $::locale->text('Receipts'),
 
 138     payments             => $::locale->text('Payments'),
 
 139     projects             => $::locale->text('Project Transactions'),
 
 140     bwa                  => $::locale->text('Business evaluation'),
 
 143   $::form->{title} = $title{$::form->{report}};
 
 146   $::form->all_departments(\%::myconfig);
 
 147   if (@{ $::form->{all_departments} || [] }) {
 
 148     $::form->{selectdepartment} = "<option>\n";
 
 149     map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
 
 152   $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
 
 154   my $is_projects         = $::form->{report} eq "projects";
 
 155   my $is_income_statement = $::form->{report} eq "income_statement";
 
 156   my $is_bwa              = $::form->{report} eq "bwa";
 
 157   my $is_balance_sheet    = $::form->{report} eq "balance_sheet";
 
 158   my $is_trial_balance    = $::form->{report} eq "trial_balance";
 
 159   my $is_aging            = $::form->{report} =~ /^a[rp]_aging$/;
 
 160   my $is_payments         = $::form->{report} =~ /(receipts|payments)$/;
 
 162   my ($label, $nextsub, $vc);
 
 164     my $is_sales  = $::form->{report} eq 'ar_aging';
 
 165     $label        = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
 
 166     $::form->{vc} = $is_sales ? 'customer' : 'vendor';
 
 168     $nextsub = "generate_$::form->{report}";
 
 171     $::form->all_vc(\%::myconfig, $::form->{vc}, $is_sales ? "AR" : "AP");
 
 172     $vc .= "<option>$_->{name}--$_->{id}\n" for @{ $::form->{"all_$::form->{vc}"} };
 
 174         ? qq|<select name=$::form->{vc}><option>\n$vc</select>|
 
 175         : qq|<input name=$::form->{vc} size=35>|;
 
 178   my ($selection, $paymentaccounts);
 
 180     $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
 
 182     RP->paymentaccounts(\%::myconfig, $::form);
 
 184     $selection = "<option>\n";
 
 185     for my $ref (@{ $::form->{PR} }) {
 
 186       $paymentaccounts .= "$ref->{accno} ";
 
 187       $selection       .= "<option>$ref->{accno}--$ref->{description}\n";
 
 192   print $::form->parse_html_template('rp/report', {
 
 193     paymentaccounts     => $paymentaccounts,
 
 194     selection           => $selection,
 
 195     is_aging            => $is_aging,
 
 198     year                => DateTime->today->year,
 
 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"}); }
 
 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;
 
 226   $form->{project_id} = $form->{project_id_1};
 
 227   if ($form->{projectnumber} && !$form->{project_id}) {
 
 228     $form->{rowcount} = 1;
 
 230     # call this instead of update
 
 231     $form->{update}          = $nextsub;
 
 232     $form->{projectnumber_1} = $form->{projectnumber};
 
 234     delete $form->{sort};
 
 235     check_project('generate_projects');
 
 237     # if there is one only, assign id
 
 238     $form->{project_id} = $form->{project_id_1};
 
 241   $main::lxdebug->leave_sub();
 
 244 sub generate_income_statement {
 
 245   $main::lxdebug->enter_sub();
 
 247   $main::auth->assert('report');
 
 249   my $form     = $main::form;
 
 250   my %myconfig = %main::myconfig;
 
 251   my $locale   = $main::locale;
 
 253   $form->{padding} = "  ";
 
 254   $form->{bold}    = "<b>";
 
 255   $form->{endbold} = "</b>";
 
 256   $form->{br}      = "<br>";
 
 258   if ($form->{reporttype} eq "custom") {
 
 260     #forgotten the year --> thisyear
 
 261     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 262       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 268     if ($form->{duetyp} eq "13") {
 
 269       $form->{fromdate} = "1.1.$form->{year}";
 
 270       $form->{todate}   = "31.12.$form->{year}";
 
 274     if ($form->{duetyp} eq "A") {
 
 275       $form->{fromdate} = "1.1.$form->{year}";
 
 276       $form->{todate}   = "31.3.$form->{year}";
 
 278     if ($form->{duetyp} eq "B") {
 
 279       $form->{fromdate} = "1.4.$form->{year}";
 
 280       $form->{todate}   = "30.6.$form->{year}";
 
 282     if ($form->{duetyp} eq "C") {
 
 283       $form->{fromdate} = "1.7.$form->{year}";
 
 284       $form->{todate}   = "30.9.$form->{year}";
 
 286     if ($form->{duetyp} eq "D") {
 
 287       $form->{fromdate} = "1.10.$form->{year}";
 
 288       $form->{todate}   = "31.12.$form->{year}";
 
 293       $form->{duetyp} eq "1" && do {
 
 294         $form->{fromdate} = "1.1.$form->{year}";
 
 295         $form->{todate}   = "31.1.$form->{year}";
 
 298       $form->{duetyp} eq "2" && do {
 
 299         $form->{fromdate} = "1.2.$form->{year}";
 
 301         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 302         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 303         $form->{todate} = "$leap.2.$form->{year}";
 
 306       $form->{duetyp} eq "3" && do {
 
 307         $form->{fromdate} = "1.3.$form->{year}";
 
 308         $form->{todate}   = "31.3.$form->{year}";
 
 311       $form->{duetyp} eq "4" && do {
 
 312         $form->{fromdate} = "1.4.$form->{year}";
 
 313         $form->{todate}   = "30.4.$form->{year}";
 
 316       $form->{duetyp} eq "5" && do {
 
 317         $form->{fromdate} = "1.5.$form->{year}";
 
 318         $form->{todate}   = "31.5.$form->{year}";
 
 321       $form->{duetyp} eq "6" && do {
 
 322         $form->{fromdate} = "1.6.$form->{year}";
 
 323         $form->{todate}   = "30.6.$form->{year}";
 
 326       $form->{duetyp} eq "7" && do {
 
 327         $form->{fromdate} = "1.7.$form->{year}";
 
 328         $form->{todate}   = "31.7.$form->{year}";
 
 331       $form->{duetyp} eq "8" && do {
 
 332         $form->{fromdate} = "1.8.$form->{year}";
 
 333         $form->{todate}   = "31.8.$form->{year}";
 
 336       $form->{duetyp} eq "9" && do {
 
 337         $form->{fromdate} = "1.9.$form->{year}";
 
 338         $form->{todate}   = "30.9.$form->{year}";
 
 341       $form->{duetyp} eq "10" && do {
 
 342         $form->{fromdate} = "1.10.$form->{year}";
 
 343         $form->{todate}   = "31.10.$form->{year}";
 
 346       $form->{duetyp} eq "11" && do {
 
 347         $form->{fromdate} = "1.11.$form->{year}";
 
 348         $form->{todate}   = "30.11.$form->{year}";
 
 351       $form->{duetyp} eq "12" && do {
 
 352         $form->{fromdate} = "1.12.$form->{year}";
 
 353         $form->{todate}   = "31.12.$form->{year}";
 
 357     hotfix_reformat_date();
 
 358   } # Ende Bericht für vorgewählten Zeitraum (warum auch immer die Prüfung (custom eq true) ist ...
 
 360   RP->income_statement(\%myconfig, \%$form);
 
 362   ($form->{department}) = split /--/, $form->{department};
 
 365     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 366   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
 368   # if there are any dates construct a where
 
 369   if ($form->{fromdate} || $form->{todate}) {
 
 371     unless ($form->{todate}) {
 
 372       $form->{todate} = $form->current_date(\%myconfig);
 
 375     my $longtodate  = $locale->date(\%myconfig, $form->{todate}, 1);
 
 376     my $shorttodate = $locale->date(\%myconfig, $form->{todate}, 0);
 
 378     my $longfromdate  = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 379     my $shortfromdate = $locale->date(\%myconfig, $form->{fromdate}, 0);
 
 381     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
 383         $locale->text('for Period')
 
 384       . qq|\n$longfromdate |
 
 385       . $locale->text('Bis')
 
 389   if ($form->{comparefromdate} || $form->{comparetodate}) {
 
 390     my $longcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 1);
 
 391     my $shortcomparefromdate = $locale->date(\%myconfig, $form->{comparefromdate}, 0);
 
 393     my $longcomparetodate  = $locale->date(\%myconfig, $form->{comparetodate}, 1);
 
 394     my $shortcomparetodate = $locale->date(\%myconfig, $form->{comparetodate}, 0);
 
 396     $form->{last_period} = "$shortcomparefromdate\n$shortcomparetodate";
 
 398         "\n$longcomparefromdate "
 
 399       . $locale->text('Bis')
 
 400       . qq| $longcomparetodate|;
 
 403   # setup variables for the form
 
 404   my @a = qw(company address businessnumber);
 
 405   map { $form->{$_} = $myconfig{$_} } @a;
 
 407   $form->{templates} = $myconfig{templates};
 
 409   $form->{IN} = "income_statement.html";
 
 411   $form->parse_template;
 
 413   $main::lxdebug->leave_sub();
 
 416 sub generate_balance_sheet {
 
 417   $::lxdebug->enter_sub;
 
 418   $::auth->assert('report');
 
 420   $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
 
 421   $::form->{padding}       = "  ";
 
 422   $::form->{bold}          = "<b>";
 
 423   $::form->{endbold}       = "</b>";
 
 424   $::form->{br}            = "<br>";
 
 426   my $data = RP->balance_sheet(\%::myconfig, $::form);
 
 428   $::form->{asofdate} ||= $::form->current_date;
 
 429   $::form->{period}     = $::locale->date(\%::myconfig, $::form->current_date, 1);
 
 431   ($::form->{department}) = split /--/, $::form->{department};
 
 433   # define Current Earnings account
 
 434   my $padding = $::form->{l_heading} ? $::form->{padding} : "";
 
 435   push @{ $::form->{equity_account} }, $padding . $::locale->text('Current Earnings');
 
 437   $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
 
 438   $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
 
 440 #  $::form->{IN} = "balance_sheet.html";
 
 442   # setup company variables for the form
 
 443   map { $::form->{$_} = $::myconfig{$_} } qw(company address businessnumber nativecurr);
 
 445   $::form->{templates} = $::myconfig{templates};
 
 448   print $::form->parse_html_template('rp/balance_sheet', $data);
 
 450   $::lxdebug->leave_sub;
 
 453 sub generate_projects {
 
 454   $main::lxdebug->enter_sub();
 
 456   $main::auth->assert('report');
 
 458   my $form     = $main::form;
 
 459   my %myconfig = %main::myconfig;
 
 460   my $locale   = $main::locale;
 
 462   &get_project("generate_projects");
 
 463   $form->{projectnumber} = $form->{projectnumber_1};
 
 465   $form->{nextsub} = "generate_projects";
 
 466   $form->{title}   = $locale->text('Project Transactions');
 
 467   RP->trial_balance(\%myconfig, \%$form);
 
 469   list_accounts('generate_projects');
 
 471   $main::lxdebug->leave_sub();
 
 477 # included links to display transactions for period entered
 
 478 # added headers and subtotals
 
 480 sub generate_trial_balance {
 
 481   $main::lxdebug->enter_sub();
 
 483   $main::auth->assert('report');
 
 485   my $form     = $main::form;
 
 486   my %myconfig = %main::myconfig;
 
 487   my $locale   = $main::locale;
 
 489   if ($form->{reporttype} eq "custom") {
 
 491     #forgotten the year --> thisyear
 
 492     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
 493       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
 499     if ($form->{duetyp} eq "13") {
 
 500       $form->{fromdate} = "1.1.$form->{year}";
 
 501       $form->{todate}   = "31.12.$form->{year}";
 
 505     if ($form->{duetyp} eq "A") {
 
 506       $form->{fromdate} = "1.1.$form->{year}";
 
 507       $form->{todate}   = "31.3.$form->{year}";
 
 509     if ($form->{duetyp} eq "B") {
 
 510       $form->{fromdate} = "1.4.$form->{year}";
 
 511       $form->{todate}   = "30.6.$form->{year}";
 
 513     if ($form->{duetyp} eq "C") {
 
 514       $form->{fromdate} = "1.7.$form->{year}";
 
 515       $form->{todate}   = "30.9.$form->{year}";
 
 517     if ($form->{duetyp} eq "D") {
 
 518       $form->{fromdate} = "1.10.$form->{year}";
 
 519       $form->{todate}   = "31.12.$form->{year}";
 
 524       $form->{duetyp} eq "1" && do {
 
 525         $form->{fromdate} = "1.1.$form->{year}";
 
 526         $form->{todate}   = "31.1.$form->{year}";
 
 529       $form->{duetyp} eq "2" && do {
 
 530         $form->{fromdate} = "1.2.$form->{year}";
 
 532         #this works from 1901 to 2099, 1900 and 2100 fail.
 
 533         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
 534         $form->{todate} = "$leap.2.$form->{year}";
 
 537       $form->{duetyp} eq "3" && do {
 
 538         $form->{fromdate} = "1.3.$form->{year}";
 
 539         $form->{todate}   = "31.3.$form->{year}";
 
 542       $form->{duetyp} eq "4" && do {
 
 543         $form->{fromdate} = "1.4.$form->{year}";
 
 544         $form->{todate}   = "30.4.$form->{year}";
 
 547       $form->{duetyp} eq "5" && do {
 
 548         $form->{fromdate} = "1.5.$form->{year}";
 
 549         $form->{todate}   = "31.5.$form->{year}";
 
 552       $form->{duetyp} eq "6" && do {
 
 553         $form->{fromdate} = "1.6.$form->{year}";
 
 554         $form->{todate}   = "30.6.$form->{year}";
 
 557       $form->{duetyp} eq "7" && do {
 
 558         $form->{fromdate} = "1.7.$form->{year}";
 
 559         $form->{todate}   = "31.7.$form->{year}";
 
 562       $form->{duetyp} eq "8" && do {
 
 563         $form->{fromdate} = "1.8.$form->{year}";
 
 564         $form->{todate}   = "31.8.$form->{year}";
 
 567       $form->{duetyp} eq "9" && do {
 
 568         $form->{fromdate} = "1.9.$form->{year}";
 
 569         $form->{todate}   = "30.9.$form->{year}";
 
 572       $form->{duetyp} eq "10" && do {
 
 573         $form->{fromdate} = "1.10.$form->{year}";
 
 574         $form->{todate}   = "31.10.$form->{year}";
 
 577       $form->{duetyp} eq "11" && do {
 
 578         $form->{fromdate} = "1.11.$form->{year}";
 
 579         $form->{todate}   = "30.11.$form->{year}";
 
 582       $form->{duetyp} eq "12" && do {
 
 583         $form->{fromdate} = "1.12.$form->{year}";
 
 584         $form->{todate}   = "31.12.$form->{year}";
 
 588     hotfix_reformat_date();
 
 592   # get for each account initial balance, debits and credits
 
 593   RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
 
 596   $form->{rowcount} = scalar @{ $form->{TB} || [] };
 
 597   $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
 
 600     "accno",               "description",
 
 601     "last_transaction",    "soll_eb",
 
 604     "soll_kumuliert",      "haben_kumuliert",
 
 605     "soll_saldo",          "haben_saldo"
 
 609   my $attachment_basename = $locale->text('trial_balance');
 
 610   my $report              = SL::ReportGenerator->new(\%myconfig, $form);
 
 612   my @hidden_variables    = qw(fromdate todate year cash);
 
 614   my $href                = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
 
 617     'accno'               => { 'text' => $locale->text('Account'), },
 
 618     'description'         => { 'text' => $locale->text('Description'), },
 
 619     'last_transaction'    => { 'text' => $locale->text('Last Transaction'), },
 
 620     'soll_eb'             => { 'text' => $locale->text('Debit Starting Balance'), },
 
 621     'haben_eb'            => { 'text' => $locale->text('Credit Starting Balance'), },
 
 622     'soll'                => { 'text' => $locale->text('Debit'), },
 
 623     'haben'               => { 'text' => $locale->text('Credit'), },
 
 624     'soll_kumuliert'      => { 'text' => $locale->text('Sum Debit'), },
 
 625     'haben_kumuliert'     => { 'text' => $locale->text('Sum Credit'), },
 
 626     'soll_saldo'          => { 'text' => $locale->text('Saldo Debit'), },
 
 627     'haben_saldo'         => { 'text' => $locale->text('Saldo Credit'), }
 
 632   my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 634   map { $column_defs{$_}->{visible} =  1 } @columns;
 
 636   $report->set_columns(%column_defs);
 
 637   $report->set_column_order(@columns);
 
 639   $report->set_export_options('generate_trial_balance', @hidden_variables);
 
 644   $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
 
 646   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
 
 647   push (@options, $form->{print_date});
 
 649   $form->{company} = $locale->text('Company') . " " . $myconfig{company};
 
 650   push (@options, $form->{company});
 
 653   $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
 
 655   my @custom_headers = ([
 
 656     { text => $::locale->text('Account'),          rowspan => 2, },
 
 657     { text => $::locale->text('Description'),      rowspan => 2, },
 
 658     { text => $::locale->text('Last Transaction'), rowspan => 2, },
 
 659     { text => $::locale->text('Starting Balance'), colspan => 2, },
 
 660     { text => $::locale->text('Sum for')   . " $form->{template_fromto}", colspan => 2, },
 
 661     { text => $::locale->text('Sum per')   . " $form->{template_to}",     colspan => 2, },
 
 662     { text => $::locale->text('Saldo per') . " $form->{template_to}",     colspan => 2, },
 
 667     { text => $::locale->text('Assets'), },
 
 668     { text => $::locale->text('Equity'), },
 
 669     { text => $::locale->text('Debit'),  },
 
 670     { text => $::locale->text('Credit'), },
 
 671     { text => $::locale->text('Debit'),  },
 
 672     { text => $::locale->text('Credit'), },
 
 673     { text => $::locale->text('Debit'),  },
 
 674     { text => $::locale->text('Credit'), },
 
 677   $report->set_options('output_format'        => 'HTML',
 
 678                        'top_info_text'        => join("\n", @options),
 
 679                        'title'                => $form->{title},
 
 680                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
 681                        'html_template'        => 'rp/html_report_susa',
 
 682                        'pdf_template'         => 'rp/html_report_susa',
 
 684   $report->set_custom_headers(@custom_headers);
 
 685   $report->set_options_from_form();
 
 686   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 688   # add sort and escape callback, this one we use for the add sub
 
 689   $form->{callback} = $href .= "&sort=$form->{sort}";
 
 691   # escape callback for href
 
 692   my $callback = $form->escape($href);
 
 694   my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 696   my %totals    = map { $_ => 0 } @subtotal_columns;
 
 698   my $edit_url = build_std_url('action=edit', 'type', 'vc');
 
 701   foreach my $accno (@{ $form->{TB} || [] }) {
 
 703     $accno->{soll} = $accno->{debit};
 
 704     $accno->{haben} = $accno->{credit};
 
 705     map { $totals{$_}    += $accno->{$_} } @subtotal_columns;
 
 707     map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
 
 708       qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
 
 712     foreach my $column (@columns) {
 
 714         'data'  => $accno->{$column},
 
 715         'align' => $column_alignment{$column},
 
 719     $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}));
 
 721     my $row_set = [ $row ];
 
 724     $report->add_data($row_set);
 
 729   $report->add_separator();
 
 731   $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
 
 733   $report->generate_with_headers();
 
 735   $main::lxdebug->leave_sub();
 
 739 sub create_subtotal_row {
 
 740   $main::lxdebug->enter_sub();
 
 742   my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
 
 744   my $form     = $main::form;
 
 745   my %myconfig = %main::myconfig;
 
 746   my $locale   = $main::locale;
 
 748   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
 
 750   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
 
 752   $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
 
 754   map { $totals->{$_} = 0 } @{ $subtotal_columns };
 
 756   $main::lxdebug->leave_sub();
 
 761 sub create_list_accounts_subtotal_row {
 
 762   $main::lxdebug->enter_sub();
 
 764   my ($subtotals, $columns, $fields, $class) = @_;
 
 766   my $form     = $main::form;
 
 767   my %myconfig = %main::myconfig;
 
 768   my $locale   = $main::locale;
 
 770   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 772   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
 
 774   $main::lxdebug->leave_sub();
 
 780   $main::lxdebug->enter_sub();
 
 784   my $form     = $main::form;
 
 785   my %myconfig = %main::myconfig;
 
 786   my $locale   = $main::locale;
 
 789   if ($form->{department}) {
 
 790     my ($department) = split /--/, $form->{department};
 
 791     push @options, $locale->text('Department') . " : $department";
 
 793   if ($form->{projectnumber}) {
 
 794     push @options, $locale->text('Project Number') . " : $form->{projectnumber}";
 
 797   # if there are any dates
 
 798   if ($form->{fromdate} || $form->{todate}) {
 
 799     my ($fromdate, $todate);
 
 801     if ($form->{fromdate}) {
 
 802       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
 
 804     if ($form->{todate}) {
 
 805       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
 
 808     push @options, "$fromdate - $todate";
 
 811     push @options, $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
 814   my @columns     = qw(accno description begbalance debit credit endbalance);
 
 816     'accno'       => { 'text' => $locale->text('Account'), },
 
 817     'description' => { 'text' => $locale->text('Description'), },
 
 818     'debit'       => { 'text' => $locale->text('Debit'), },
 
 819     'credit'      => { 'text' => $locale->text('Credit'), },
 
 820     'begbalance'  => { 'text' => $locale->text('Balance'), },
 
 821     'endbalance'  => { 'text' => $locale->text('Balance'), },
 
 823   my %column_alignment = map { $_ => 'right' } qw(debit credit begbalance endbalance);
 
 825   my @hidden_variables = qw(fromdate todate department l_heading l_subtotal all_accounts sort accounttype eur projectnumber project_id title nextsub);
 
 827   $form->{callback} = build_std_url("action=$action", grep { $form->{$_} } @hidden_variables);
 
 829   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 831   $report->set_options('top_info_text'         => join("\n", @options),
 
 832                        'output_format'         => 'HTML',
 
 833                        'title'                 => $form->{title},
 
 834                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
 
 835                        'std_column_visibility' => 1,
 
 837   $report->set_options_from_form();
 
 838   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 840   $report->set_columns(%column_defs);
 
 841   $report->set_column_order(@columns);
 
 843   $report->set_export_options($action, @hidden_variables);
 
 845   my @totals_columns = qw(credit debit begbalance endbalance);
 
 846   my %subtotals      = map { $_ => 0 } @totals_columns;
 
 847   my %totals         = map { $_ => 0 } @totals_columns;
 
 848   my $found_heading  = 0;
 
 849   my @tb             = sort { $a->{accno} cmp $b->{accno} } @{ $form->{TB} || [] };
 
 851   # sort the whole thing by account numbers and display
 
 852   foreach my $idx (0 .. scalar(@tb) - 1) {
 
 854     my $href = build_std_url('script=ca.pl', 'action=list_transactions', 'accno=' . E($ref->{accno}), 'description=' . E($ref->{description}), @hidden_variables);
 
 856     my $ml   = ($ref->{category} =~ /(A|C|E)/) ? -1 : 1;
 
 858     my $row  = { map { $_ => { 'align' => $column_alignment{$_} } } @columns };
 
 860     if ($ref->{charttype} eq 'H') {
 
 861       next unless ($form->{l_heading});
 
 863       %subtotals                   = map { $_ => 0 } @totals_columns;
 
 865       $row->{description}->{class} = 'listheading';
 
 866       $row->{description}->{data}  = $ref->{description};
 
 868       $report->add_data($row);
 
 873     foreach (qw(debit credit)) {
 
 874       $subtotals{$_} += $ref->{$_};
 
 875       $totals{$_}    += $ref->{$_};
 
 878     $subtotals{begbalance} += $ref->{balance} * $ml;
 
 879     $subtotals{endbalance} += ($ref->{balance} + $ref->{amount}) * $ml;
 
 881     map { $row->{$_}->{data} = $ref->{$_} } qw(accno description);
 
 882     map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $ref->{$_}, 2) if ($ref->{$_} != 0) } qw(credit debit);
 
 884     $row->{begbalance}->{data} = $form->format_amount(\%myconfig, $ref->{balance} * $ml, 2);
 
 885     $row->{endbalance}->{data} = $form->format_amount(\%myconfig, ($ref->{balance} + $ref->{amount}) * $ml, 2);
 
 887     $report->add_data($row);
 
 889     if ($form->{l_heading} && $found_heading &&
 
 890         (($idx == scalar(@tb) - 1) || ('H' eq $tb[$idx + 1]->{charttype}))) {
 
 891       $report->add_data(create_list_accounts_subtotal_row(\%subtotals, \@columns, \@totals_columns, 'listsubtotal'));
 
 895   $report->add_separator();
 
 897   $report->add_data(create_list_accounts_subtotal_row(\%totals, \@columns, [ qw(debit credit) ], 'listtotal'));
 
 899   $report->generate_with_headers();
 
 901   $main::lxdebug->leave_sub();
 
 904 sub generate_ar_aging {
 
 905   $main::lxdebug->enter_sub();
 
 907   $main::auth->assert('general_ledger');
 
 909   my $form     = $main::form;
 
 910   my %myconfig = %main::myconfig;
 
 911   my $locale   = $main::locale;
 
 914   ($form->{customer}) = split(/--/, $form->{customer});
 
 916   $form->{ct}   = "customer";
 
 917   $form->{arap} = "ar";
 
 919   $form->{callback} = build_std_url('action=generate_ar_aging', qw(todate customer title));
 
 921   RP->aging(\%myconfig, \%$form);
 
 924   $main::lxdebug->leave_sub();
 
 927 sub generate_ap_aging {
 
 928   $main::lxdebug->enter_sub();
 
 930   $main::auth->assert('general_ledger');
 
 932   my $form     = $main::form;
 
 933   my %myconfig = %main::myconfig;
 
 934   my $locale   = $main::locale;
 
 937   ($form->{vendor}) = split(/--/, $form->{vendor});
 
 939   $form->{ct}   = "vendor";
 
 940   $form->{arap} = "ap";
 
 942   $form->{callback} = build_std_url('action=generate_ap_aging', qw(todate vendor title));
 
 944   RP->aging(\%myconfig, \%$form);
 
 947   $main::lxdebug->leave_sub();
 
 950 sub create_aging_subtotal_row {
 
 951   $main::lxdebug->enter_sub();
 
 953   my ($subtotals, $columns, $periods, $class) = @_;
 
 955   my $form     = $main::form;
 
 956   my %myconfig = %main::myconfig;
 
 957   my $locale   = $main::locale;
 
 959   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
 
 961   foreach (@{ $periods }) {
 
 962     $row->{"$_"}->{data} = $subtotals->{$_} != 0 ? $form->format_amount(\%myconfig, $subtotals->{$_}, 2) : '';
 
 963     $subtotals->{$_}      = 0;
 
 966   $main::lxdebug->leave_sub();
 
 972   $main::lxdebug->enter_sub();
 
 974   $main::auth->assert('general_ledger');
 
 976   my $form     = $main::form;
 
 977   my %myconfig = %main::myconfig;
 
 978   my $locale   = $main::locale;
 
 979   my $cgi      = $::request->{cgi};
 
 981   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 983   my @columns = qw(statement ct invnumber transdate duedate amount open);
 
 986     'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
 
 987     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
 
 988     'invnumber' => { 'text' => $locale->text('Invoice'), },
 
 989     'transdate' => { 'text' => $locale->text('Date'), },
 
 990     'duedate'   => { 'text' => $locale->text('Due'), },
 
 991     'amount'    => { 'text' => $locale->text('Amount'), },
 
 992     'open'      => { 'text' => $locale->text('Open'), },
 
 995   my %column_alignment = ('statement' => 'center',
 
 996                           map { $_ => 'right' } qw(open amount));
 
 998   $report->set_options('std_column_visibility' => 1);
 
 999   $report->set_columns(%column_defs);
 
1000   $report->set_column_order(@columns);
 
1002   my @hidden_variables = qw(todate customer vendor arap title ct);
 
1003   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
 
1006   my $attachment_basename;
 
1008   if ($form->{department}) {
 
1009     my ($department) = split /--/, $form->{department};
 
1010     push @options, $locale->text('Department') . " : $department";
 
1011     $form->{callback} .= "&department=" . E($department);
 
1014   if (($form->{arap} eq 'ar') && $form->{customer}) {
 
1015     push @options, $form->{customer};
 
1016     $attachment_basename = $locale->text('ar_aging_list');
 
1017     $form->{title} = sprintf($locale->text('Ar aging on %s'), $form->{todate});
 
1020   if (($form->{arap} eq 'ap') && $form->{vendor}) {
 
1021     push @options, $form->{vendor};
 
1022     $attachment_basename = $locale->text('ap_aging_list');
 
1023     $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
 
1026   if ($form->{fromdate}) {
 
1027     push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1029     push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1032   $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
 
1034   $report->set_options('top_info_text'        => join("\n", @options),
 
1035                        'output_format'        => 'HTML',
 
1036                        'title'                => $form->{title},
 
1037                        'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1039   $report->set_options_from_form();
 
1040   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1042   my $previous_ctid = 0;
 
1044   my @periods       = qw(open amount);
 
1045   my %subtotals     = map { $_ => 0 } @periods;
 
1046   my %totals        = map { $_ => 0 } @periods;
 
1048   foreach my $ref (@{ $form->{AG} }) {
 
1049     if ($row_idx && ($previous_ctid != $ref->{ctid})) {
 
1050       $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal'));
 
1053     foreach my $key (@periods) {
 
1054       $subtotals{$key}  += $ref->{"$key"};
 
1055       $totals{$key}     += $ref->{"$key"};
 
1056       $ref->{"$key"}  = $ref->{"$key"} != 0 ? $form->format_amount(\%myconfig, $ref->{"$key"}, 2) : '';
 
1061     foreach my $column (@columns) {
 
1063         'data'   => (($column eq 'ct') || ($column eq 'statement')) ? '' : $ref->{$column},
 
1064         'align'  => $column_alignment{$column},
 
1065         'valign' => $column eq 'statement' ? 'center' : '',
 
1069     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
 
1071     if ($previous_ctid != $ref->{ctid}) {
 
1072       $row->{statement}->{raw_data} =
 
1073           $cgi->hidden('-name' => "customer_id_" . ($row_idx + 1), '-value' => $ref->{ctid})
 
1074         . $cgi->checkbox('-name' => "statement_" . ($row_idx + 1), '-value' => 1, '-label' => '', 'checked' => $ref->{checked});
 
1075       $row->{ct}->{data} = $ref->{name};
 
1080     $previous_ctid = $ref->{ctid};
 
1082     $report->add_data($row);
 
1085   $report->add_data(create_aging_subtotal_row(\%subtotals, \@columns, \@periods, 'listsubtotal')) if ($row_idx);
 
1087   $report->add_data(create_aging_subtotal_row(\%totals, \@columns, \@periods, 'listtotal'));
 
1089   if ($form->{arap} eq 'ar') {
 
1090     my $raw_top_info_text    = $form->parse_html_template('rp/aging_ar_top');
 
1091     my $raw_bottom_info_text = $form->parse_html_template('rp/aging_ar_bottom', { 'row_idx' => $row_idx,
 
1092                                                                                'PRINT_OPTIONS' => print_options(inline => 1), });
 
1093     $report->set_options('raw_top_info_text'    => $raw_top_info_text,
 
1094                          'raw_bottom_info_text' => $raw_bottom_info_text);
 
1097   $report->generate_with_headers();
 
1099   $main::lxdebug->leave_sub();
 
1103   $main::lxdebug->enter_sub();
 
1105   my $form     = $main::form;
 
1106   my %myconfig = %main::myconfig;
 
1107   my $locale   = $main::locale;
 
1109   RP->aging(\%myconfig, \%$form);
 
1111   map { $_->{checked} = "checked" } @{ $form->{AG} };
 
1115   $main::lxdebug->leave_sub();
 
1119   $::lxdebug->enter_sub;
 
1120   $::auth->assert('general_ledger');
 
1122   # get name and email addresses
 
1124   for my $i (1 .. $::form->{rowcount}) {
 
1125     next unless $::form->{"statement_$i"};
 
1126     $::form->{"$::form->{ct}_id"} = $::form->{"$::form->{ct}_id_$i"};
 
1127     RP->get_customer(\%::myconfig, $::form);
 
1132   $::form->error($::locale->text('Nothing selected!')) unless $selected;
 
1134   $::form->{media} = "email";
 
1136   # save all other variables
 
1138   for my $key (keys %$::form) {
 
1139     next if any { $key eq $_ } qw(login password action email cc bcc subject message type sendmode format header);
 
1140     next unless '' eq ref $::form->{$key};
 
1141     push @hidden_values, $key;
 
1145   print $::form->parse_html_template('rp/e_mail', {
 
1146     show_bcc      => $::auth->assert('email_bcc', 'may fail'),
 
1147     print_options => print_options(inline => 1),
 
1148     hidden_values => \@hidden_values,
 
1151   $::lxdebug->leave_sub;
 
1155   $main::lxdebug->enter_sub();
 
1157   $main::auth->assert('general_ledger');
 
1159   my $form     = $main::form;
 
1160   my %myconfig = %main::myconfig;
 
1161   my $locale   = $main::locale;
 
1163   $form->{subject} = $locale->text('Statement') . qq| - $form->{todate}|
 
1164     unless $form->{subject};
 
1166   RP->aging(\%myconfig, \%$form);
 
1168   $form->{"statement_1"} = 1;
 
1170   $form->{media} = 'email';
 
1173   $form->redirect($locale->text('Statement sent to') . " $form->{$form->{ct}}");
 
1175   $main::lxdebug->leave_sub();
 
1179   $main::lxdebug->enter_sub();
 
1181   $main::auth->assert('general_ledger');
 
1183   my $form     = $main::form;
 
1184   my %myconfig = %main::myconfig;
 
1185   my $locale   = $main::locale;
 
1187   if ($form->{media} eq 'printer') {
 
1188     $form->error($locale->text('Select postscript or PDF!'))
 
1189       if ($form->{format} !~ /(postscript|pdf)/);
 
1193   for my $i (1 .. $form->{rowcount}) {
 
1194     if ($form->{"statement_$i"}) {
 
1195       $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
 
1201   $form->error($locale->text('Nothing selected!')) unless $selected;
 
1203   if ($form->{media} eq 'printer') {
 
1204     $form->{"$form->{ct}_id"} = "";
 
1206     $form->{"statement_1"} = 1;
 
1209   RP->aging(\%myconfig, \%$form);
 
1213   $form->redirect($locale->text('Statements sent to printer!'))
 
1214     if ($form->{media} eq 'printer');
 
1216   $main::lxdebug->leave_sub();
 
1220   $main::lxdebug->enter_sub();
 
1222   $main::auth->assert('general_ledger');
 
1224   my $form     = $main::form;
 
1225   my %myconfig = %main::myconfig;
 
1226   my $locale   = $main::locale;
 
1228   $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
 
1230   $form->{templates} = "$myconfig{templates}";
 
1232   my $suffix = "html";
 
1233   my $attachment_suffix = "html";
 
1234   if ($form->{format} eq 'postscript') {
 
1235     $form->{postscript} = 1;
 
1237     $attachment_suffix = "ps";
 
1238   } elsif ($form->{format} eq 'pdf') {
 
1241     $attachment_suffix = "pdf";
 
1244   $form->{IN}  = "$form->{type}.$suffix";
 
1245   $form->{OUT} = $form->{media} eq 'printer' ? "| $myconfig{printer}" : "";
 
1247   # Save $form->{email} because it will be overwritten.
 
1248   $form->{EMAIL_RECIPIENT} = $form->{email};
 
1252   while (@{ $form->{AG} }) {
 
1254     my $ref = shift @{ $form->{AG} };
 
1256     if ($ctid != $ref->{ctid}) {
 
1258       $ctid = $ref->{ctid};
 
1261       if ($form->{"statement_$i"}) {
 
1264           ("name", "street", "zipcode", "city", "country", "contact", "email",
 
1265            "$form->{ct}phone", "$form->{ct}fax");
 
1266         map { $form->{$_} = $ref->{$_} } @a;
 
1268         $form->{ $form->{ct} } = $form->{name};
 
1269         $form->{"$form->{ct}_id"} = $ref->{ctid};
 
1271         map { $form->{$_} = () } qw(invnumber invdate duedate amount open);
 
1273         foreach my $item (qw(c0 c30 c60 c90)) {
 
1274           $form->{$item} = ();
 
1275           $form->{"${item}total"} = 0;
 
1278         &statement_details($ref);
 
1282           if (scalar(@{ $form->{AG} }) > 0) {
 
1284             # one or more left to go
 
1285             if ($ctid == $form->{AG}->[0]->{ctid}) {
 
1286               $ref = shift @{ $form->{AG} };
 
1287               &statement_details($ref);
 
1290               $ref = scalar(@{ $form->{AG} });
 
1296             # set initial ref to 0
 
1303           $form->{"${_}total"} =
 
1304             $form->format_amount(\%myconfig, $form->{"${_}total"}, 2)
 
1305         } ('c0', 'c30', 'c60', 'c90', "");
 
1307         $form->{attachment_filename} =  $locale->quote_special_chars('filenames', $locale->text("Statement") . "_$form->{todate}.$attachment_suffix");
 
1308         $form->{attachment_filename} =~ s/\s+/_/g;
 
1310         $form->parse_template(\%myconfig);
 
1315   # saving the history
 
1316   if(!exists $form->{addition} && $form->{id} ne "") {
 
1317     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
 
1318     $form->{addition} = "PRINTED";
 
1319     $form->{what_done} = $form->{type};
 
1320     $form->save_history;
 
1322   # /saving the history
 
1323   $main::lxdebug->leave_sub();
 
1326 sub statement_details {
 
1327   $main::lxdebug->enter_sub();
 
1329   $main::auth->assert('general_ledger');
 
1331   my $form     = $main::form;
 
1332   my %myconfig = %main::myconfig;
 
1333   my $locale   = $main::locale;
 
1337   push @{ $form->{invnumber} }, $ref->{invnumber};
 
1338   push @{ $form->{invdate} },   $ref->{transdate};
 
1339   push @{ $form->{duedate} },   $ref->{duedate};
 
1340   push @{ $form->{amount} },    $form->format_amount(\%myconfig, $ref->{amount} / $ref->{exchangerate}, 2);
 
1341   push @{ $form->{open} },      $form->format_amount(\%myconfig, $ref->{open} / $ref->{exchangerate}, 2);
 
1343   foreach my $item (qw(c0 c30 c60 c90)) {
 
1344     if ($ref->{exchangerate} * 1) {
 
1345       # add only the open amount of the invoice to the aging, not the total amount
 
1346       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} < 30 and $item eq 'c0';
 
1347       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 30 and $ref->{overduedays} < 60 and $item eq 'c30';
 
1348       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 60 and $ref->{overduedays} < 90 and $item eq 'c60';
 
1349       $ref->{"${item}"} = $form->round_amount($ref->{open} / $ref->{exchangerate}, 2) if $ref->{overduedays} >= 90 and $item eq 'c90';
 
1351     $form->{"${item}total"} += $ref->{$item};
 
1352     $form->{total}          += $ref->{$item};
 
1353     push @{ $form->{$item} },
 
1354       $form->format_amount(\%myconfig, $ref->{$item}, 2);
 
1357   $main::lxdebug->leave_sub();
 
1360 sub generate_tax_report {
 
1361   $::lxdebug->enter_sub;
 
1362   $::auth->assert('report');
 
1364   RP->tax_report(\%::myconfig, $::form);
 
1366   my $descvar     = "$::form->{accno}_description";
 
1367   my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
 
1371   my $callback = build_std_url('action=generate_tax_report', $descvar,
 
1372     qw(fromdate todate db method accno department report title));
 
1374   my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
 
1377   for my $item (@columns, 'subtotal') {
 
1378     if ($::form->{"l_$item"} eq "Y") {
 
1379       $callback .= "&l_$item=Y";
 
1380       $href     .= "&l_$item=Y";
 
1384   for my $item (@columns) {
 
1385     if ($::form->{"l_$item"} eq "Y") {
 
1386       push @column_index, $item;
 
1391   if ($::form->{department}) {
 
1392     my ($department) = split /--/, $::form->{department};
 
1393     push @options, $::locale->text('Department') . " : $department";
 
1396   # if there are any dates
 
1397   if ($::form->{fromdate} || $::form->{todate}) {
 
1398     my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
 
1399     my $todate   = $::form->{todate}   ? $::locale->date(\%::myconfig, $::form->{todate}, 1)   : '';
 
1400     push @options, "$fromdate - $todate";
 
1402     push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
 
1405   my ($name, $invoice, $arap);
 
1406   if ($::form->{db} eq 'ar') {
 
1407     $name    = $::locale->text('Customer');
 
1411   if ($::form->{db} eq 'ap') {
 
1412     $name    = $::locale->text('Vendor');
 
1417   my %column_header = (
 
1418     id        => $::locale->text('ID'),
 
1419     invnumber => $::locale->text('Invoice'),
 
1420     transdate => $::locale->text('Date'),
 
1421     netamount => $::locale->text('Amount'),
 
1422     tax       => $::locale->text('Tax'),
 
1423     amount    => $::locale->text('Total'),
 
1427   my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
 
1429   $callback .= "&sort=$::form->{sort}";
 
1432   if (@{ $::form->{TR} }) {
 
1433     $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
 
1436   my ($totalnetamount, $totaltax, @data);
 
1437   for my $ref (@{ $::form->{TR} }) {
 
1439     my $module = ($ref->{invoice}) ? $invoice : $arap;
 
1441     if ($::form->{l_subtotal} eq 'Y') {
 
1442       if ($sameitem ne $ref->{ $::form->{sort} }) {
 
1445           netamount => $subtotalnetamount,
 
1446           tax       => $subtotaltax,
 
1447           amount    => $subtotal,
 
1449         $subtotalnetamount = 0;
 
1451         $sameitem          = $ref->{ $::form->{sort} };
 
1455     $subtotalnetamount += $ref->{netamount};
 
1456     $subtotaltax       += $ref->{tax};
 
1457     $totalnetamount    += $ref->{netamount};
 
1458     $totaltax          += $ref->{tax};
 
1459     $ref->{amount}      = $ref->{netamount} + $ref->{tax};
 
1461     push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
 
1462     $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
 
1463     $data[-1]{$_}{numeric}     = 1 for qw(netamount tax amount);
 
1466   if ($::form->{l_subtotal} eq 'Y') {
 
1469       netamount => $subtotalnetamount,
 
1470       tax       => $subtotaltax,
 
1471       amount    => $subtotal,
 
1477     netamount => $totalnetamount,
 
1479     amount    => $totalnetamount + $totaltax,
 
1483   print $::form->parse_html_template('rp/tax_report', {
 
1484     column_index  => \@column_index,
 
1485     column_header => \%column_header,
 
1486     column_sorted => \%column_sorted,
 
1489     options       => \@options,
 
1492   $::lxdebug->leave_sub;
 
1496   $main::lxdebug->enter_sub();
 
1498   $main::auth->assert('cash');
 
1500   my $form     = $main::form;
 
1501   my %myconfig = %main::myconfig;
 
1502   my $locale   = $main::locale;
 
1504   if ($form->{account}) {
 
1505     ($form->{paymentaccounts}) = split /--/, $form->{account};
 
1509   if ($form->{department}) {
 
1510     (my $department, $form->{department_id}) = split /--/, $form->{department};
 
1511     $option = $locale->text('Department') . " : $department";
 
1514   report_generator_set_default_sort('transdate', 1);
 
1516   RP->payments(\%myconfig, \%$form);
 
1518   my @hidden_variables = qw(account title department reference source memo fromdate todate
 
1519                             fx_transaction db prepayment paymentaccounts sort);
 
1521   my $href = build_std_url('action=list_payments', grep { $form->{$_} } @hidden_variables);
 
1522   $form->{callback} = $href;
 
1524   my @columns     = qw(transdate invnumber name paid source memo);
 
1526     'name'      => { 'text' => $locale->text('Description'), },
 
1527     'invnumber' => { 'text' => $locale->text('Reference'), },
 
1528     'transdate' => { 'text' => $locale->text('Date'), },
 
1529     'paid'      => { 'text' => $locale->text('Amount'), },
 
1530     'source'    => { 'text' => $locale->text('Source'), },
 
1531     'memo'      => { 'text' => $locale->text('Memo'), },
 
1533   my %column_alignment = ('paid' => 'right');
 
1535   foreach my $name (grep { $_ ne 'paid' } @columns) {
 
1536     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
 
1537     $column_defs{$name}->{link} = $href . "&sort=${name}&sortdir=$sortdir";
 
1541   if ($form->{fromdate}) {
 
1542     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{fromdate}, 1);
 
1544   if ($form->{todate}) {
 
1545     push @options, $locale->text('bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
 
1548   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
1550   my $attachment_basename = $form->{db} eq 'ar' ? $locale->text('list_of_receipts') : $locale->text('list_of_payments');
 
1552   $report->set_options('top_info_text'         => join("\n", @options),
 
1553                        'output_format'         => 'HTML',
 
1554                        'title'                 => $form->{title},
 
1555                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
1556                        'std_column_visibility' => 1,
 
1558   $report->set_options_from_form();
 
1560   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
1562   $report->set_columns(%column_defs);
 
1563   $report->set_column_order(@columns);
 
1565   $report->set_export_options('list_payments', @hidden_variables, qw(sort sortdir));
 
1567   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
1571   foreach my $ref (sort { $a->{accno} cmp $b->{accno} } @{ $form->{PR} }) {
 
1572     next unless @{ $form->{ $ref->{id} } };
 
1574     $report->add_control({ 'type' => 'colspan_data', 'data' => "$ref->{accno}--$ref->{description}" });
 
1576     my $subtotal_paid = 0;
 
1578     foreach my $payment (@{ $form->{ $ref->{id} } }) {
 
1579       my $module = $payment->{module};
 
1580       $module = 'is' if ($payment->{invoice} && $payment->{module} eq 'ar');
 
1581       $module = 'ir' if ($payment->{invoice} && $payment->{module} eq 'ap');
 
1583       $subtotal_paid += $payment->{paid};
 
1584       $total_paid    += $payment->{paid};
 
1586       $payment->{paid} = $form->format_amount(\%myconfig, $payment->{paid}, 2);
 
1590       foreach my $column (@columns) {
 
1592           'data'  => $payment->{$column},
 
1593           'align' => $column_alignment{$column},
 
1597       $row->{invnumber}->{link} = build_std_url("script=${module}.pl", 'action=edit', 'id=' . E($payment->{id}), 'callback');
 
1599       $report->add_data($row);
 
1602     my $row = { map { $_ => { 'class' => 'listsubtotal' } } @columns };
 
1604       'data'  => $form->format_amount(\%myconfig, $subtotal_paid, 2),
 
1606       'class' => 'listsubtotal',
 
1609     $report->add_data($row);
 
1612   $report->add_separator();
 
1614   my $row = { map { $_ => { 'class' => 'listtotal' } } @columns };
 
1616     'data'  => $form->format_amount(\%myconfig, $total_paid, 2),
 
1618     'class' => 'listtotal',
 
1621   $report->add_data($row);
 
1623   $report->generate_with_headers();
 
1625   $main::lxdebug->leave_sub();
 
1629   $::lxdebug->enter_sub;
 
1631   my ($dont_print) = @_;
 
1633   $::form->{sendmode} = "attachment";
 
1634   $::form->{format} ||= $::myconfig{template_format} || "pdf";
 
1635   $::form->{copies} ||= $::myconfig{copies}          || 2;
 
1637   $::form->{PD}{ $::form->{type} }     = "selected";
 
1638   $::form->{DF}{ $::form->{format} }   = "selected";
 
1639   $::form->{OP}{ $::form->{media} }    = "selected";
 
1640   $::form->{SM}{ $::form->{sendmode} } = "selected";
 
1642   my $output = $::form->parse_html_template('rp/print_options', {
 
1643     got_printer => $::myconfig{printer},
 
1644     show_latex  => $::lx_office_conf{print_templates}->{latex},
 
1645     is_email    => $::form->{media} eq 'email',
 
1648   print $output unless $dont_print;
 
1650   $::lxdebug->leave_sub;
 
1656   $main::lxdebug->enter_sub();
 
1658   $main::auth->assert('report');
 
1660   my $form     = $main::form;
 
1661   my %myconfig = %main::myconfig;
 
1662   my $locale   = $main::locale;
 
1664   $form->{padding} = "  ";
 
1665   $form->{bold}    = "<b>";
 
1666   $form->{endbold} = "</b>";
 
1667   $form->{br}      = "<br>";
 
1669   if ($form->{reporttype} eq "custom") {
 
1671     #forgotten the year --> thisyear
 
1672     if ($form->{year} !~ m/^\d\d\d\d$/) {
 
1673       $locale->date(\%myconfig, $form->current_date(\%myconfig), 0) =~
 
1679     if ($form->{duetyp} eq "13") {
 
1680       $form->{fromdate}        = "1.1.$form->{year}";
 
1681       $form->{todate}          = "31.12.$form->{year}";
 
1682       $form->{comparefromdate} = "1.01.$form->{year}";
 
1683       $form->{comparetodate}   = "31.12.$form->{year}";
 
1687     if ($form->{duetyp} eq "A") {
 
1688       $form->{fromdate}        = "1.1.$form->{year}";
 
1689       $form->{todate}          = "31.3.$form->{year}";
 
1690       $form->{comparefromdate} = "1.01.$form->{year}";
 
1691       $form->{comparetodate}   = "31.03.$form->{year}";
 
1693     if ($form->{duetyp} eq "B") {
 
1694       $form->{fromdate}        = "1.4.$form->{year}";
 
1695       $form->{todate}          = "30.6.$form->{year}";
 
1696       $form->{comparefromdate} = "1.01.$form->{year}";
 
1697       $form->{comparetodate}   = "30.06.$form->{year}";
 
1699     if ($form->{duetyp} eq "C") {
 
1700       $form->{fromdate}        = "1.7.$form->{year}";
 
1701       $form->{todate}          = "30.9.$form->{year}";
 
1702       $form->{comparefromdate} = "1.01.$form->{year}";
 
1703       $form->{comparetodate}   = "30.09.$form->{year}";
 
1705     if ($form->{duetyp} eq "D") {
 
1706       $form->{fromdate}        = "1.10.$form->{year}";
 
1707       $form->{todate}          = "31.12.$form->{year}";
 
1708       $form->{comparefromdate} = "1.01.$form->{year}";
 
1709       $form->{comparetodate}   = "31.12.$form->{year}";
 
1714       $form->{duetyp} eq "1" && do {
 
1715         $form->{fromdate}        = "1.1.$form->{year}";
 
1716         $form->{todate}          = "31.1.$form->{year}";
 
1717         $form->{comparefromdate} = "1.01.$form->{year}";
 
1718         $form->{comparetodate}   = "31.01.$form->{year}";
 
1721       $form->{duetyp} eq "2" && do {
 
1722         $form->{fromdate} = "1.2.$form->{year}";
 
1724         #this works from 1901 to 2099, 1900 and 2100 fail.
 
1725         my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
 
1726         $form->{todate}          = "$leap.2.$form->{year}";
 
1727         $form->{comparefromdate} = "1.01.$form->{year}";
 
1728         $form->{comparetodate}   = "$leap.02.$form->{year}";
 
1731       $form->{duetyp} eq "3" && do {
 
1732         $form->{fromdate}        = "1.3.$form->{year}";
 
1733         $form->{todate}          = "31.3.$form->{year}";
 
1734         $form->{comparefromdate} = "1.01.$form->{year}";
 
1735         $form->{comparetodate}   = "31.03.$form->{year}";
 
1738       $form->{duetyp} eq "4" && do {
 
1739         $form->{fromdate}        = "1.4.$form->{year}";
 
1740         $form->{todate}          = "30.4.$form->{year}";
 
1741         $form->{comparefromdate} = "1.01.$form->{year}";
 
1742         $form->{comparetodate}   = "30.04.$form->{year}";
 
1745       $form->{duetyp} eq "5" && do {
 
1746         $form->{fromdate}        = "1.5.$form->{year}";
 
1747         $form->{todate}          = "31.5.$form->{year}";
 
1748         $form->{comparefromdate} = "1.01.$form->{year}";
 
1749         $form->{comparetodate}   = "31.05.$form->{year}";
 
1752       $form->{duetyp} eq "6" && do {
 
1753         $form->{fromdate}        = "1.6.$form->{year}";
 
1754         $form->{todate}          = "30.6.$form->{year}";
 
1755         $form->{comparefromdate} = "1.01.$form->{year}";
 
1756         $form->{comparetodate}   = "30.06.$form->{year}";
 
1759       $form->{duetyp} eq "7" && do {
 
1760         $form->{fromdate}        = "1.7.$form->{year}";
 
1761         $form->{todate}          = "31.7.$form->{year}";
 
1762         $form->{comparefromdate} = "1.01.$form->{year}";
 
1763         $form->{comparetodate}   = "31.07.$form->{year}";
 
1766       $form->{duetyp} eq "8" && do {
 
1767         $form->{fromdate}        = "1.8.$form->{year}";
 
1768         $form->{todate}          = "31.8.$form->{year}";
 
1769         $form->{comparefromdate} = "1.01.$form->{year}";
 
1770         $form->{comparetodate}   = "31.08.$form->{year}";
 
1773       $form->{duetyp} eq "9" && do {
 
1774         $form->{fromdate}        = "1.9.$form->{year}";
 
1775         $form->{todate}          = "30.9.$form->{year}";
 
1776         $form->{comparefromdate} = "1.01.$form->{year}";
 
1777         $form->{comparetodate}   = "30.09.$form->{year}";
 
1780       $form->{duetyp} eq "10" && do {
 
1781         $form->{fromdate}        = "1.10.$form->{year}";
 
1782         $form->{todate}          = "31.10.$form->{year}";
 
1783         $form->{comparefromdate} = "1.01.$form->{year}";
 
1784         $form->{comparetodate}   = "31.10.$form->{year}";
 
1787       $form->{duetyp} eq "11" && do {
 
1788         $form->{fromdate}        = "1.11.$form->{year}";
 
1789         $form->{todate}          = "30.11.$form->{year}";
 
1790         $form->{comparefromdate} = "1.01.$form->{year}";
 
1791         $form->{comparetodate}   = "30.11.$form->{year}";
 
1794       $form->{duetyp} eq "12" && do {
 
1795         $form->{fromdate}        = "1.12.$form->{year}";
 
1796         $form->{todate}          = "31.12.$form->{year}";
 
1797         $form->{comparefromdate} = "1.01.$form->{year}";
 
1798         $form->{comparetodate}   = "31.12.$form->{year}";
 
1802     hotfix_reformat_date();
 
1804     # die konvertierungen nur dann durchführen, wenn auch daten gesetzt sind.
 
1805     # ansonsten ist die prüfung in RP.pm
 
1806     # if (defined ($form->{fromdate|todate}=='..'))
 
1808     if ($form->{fromdate}){
 
1809       my ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
 
1810       my $datetime = $locale->parse_date_to_object(\%myconfig, $form->{fromdate});
 
1811       $datetime->set( month      => 1,
 
1813       $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
 
1815     if ($form->{todate}){
 
1816       $form->{comparetodate}   = $form->{todate};
 
1820   RP->bwa(\%myconfig, \%$form);
 
1822   ($form->{department}) = split /--/, $form->{department};
 
1825     $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
 
1826   $form->{todate} = $form->current_date(\%myconfig) unless $form->{todate};
 
1828   # if there are any dates construct a where
 
1829   if ($form->{fromdate} || $form->{todate}) {
 
1831     unless ($form->{todate}) {
 
1832       $form->{todate} = $form->current_date(\%myconfig);
 
1835     my %germandate = ("dateformat" => "dd.mm.yyyy");
 
1837     my $longtodate  = $locale->date(\%germandate, $form->{todate}, 1);
 
1838     my $shorttodate = $locale->date(\%germandate, $form->{todate}, 0);
 
1840     my $longfromdate  = $locale->date(\%germandate, $form->{fromdate}, 1);
 
1841     my $shortfromdate = $locale->date(\%germandate, $form->{fromdate}, 0);
 
1843     $form->{this_period} = "$shortfromdate\n$shorttodate";
 
1845         $locale->text('for Period')
 
1846       . qq|\n$longfromdate |
 
1847       . $locale->text('bis')
 
1851   # setup variables for the form
 
1852   my @a = qw(company address businessnumber);
 
1853   map { $form->{$_} = $myconfig{$_} } @a;
 
1854   $form->{templates} = $myconfig{templates};
 
1856   $form->{IN} = "bwa.html";
 
1858   $form->parse_template;
 
1860   $main::lxdebug->leave_sub();
 
1863 # Hotfix, um das Datumsformat, die unten hart auf deutsches Datumsformat eingestellt
 
1864 # sind, entsprechend mit anderem Formaten (z.B. iso-kodiert) zum Laufen zu bringen (S.a.: Bug 1388)
 
1865 sub hotfix_reformat_date {
 
1867   $main::lxdebug->enter_sub();
 
1869   my $form     = $main::form;
 
1870   my %myconfig = %main::myconfig;
 
1871   my $locale   = $main::locale;
 
1873   if ($myconfig{dateformat} ne 'dd.mm.yyyy'){
 
1874     my $current_dateformat = $myconfig{dateformat};
 
1875     $myconfig{dateformat} = 'dd.mm.yyyy';
 
1876     $form->{fromdate} = $main::locale->reformat_date(\%myconfig, $form->{fromdate}, $current_dateformat);
 
1877     $form->{todate} = $main::locale->reformat_date(\%myconfig, $form->{todate}, $current_dateformat);
 
1878     $form->{comparefromdate} = $main::locale->reformat_date(\%myconfig, $form->{comparefromdate}, $current_dateformat)
 
1879       unless (!defined ($form->{comparefromdate}));
 
1880     $form->{comparetodate} = $main::locale->reformat_date(\%myconfig, $form->{comparetodate}, $current_dateformat)
 
1881       unless (!defined ($form->{comparetodate}));
 
1883     # Und wieder zurücksetzen
 
1884     $myconfig{dateformat} =  $current_dateformat; #'dd.mm.yyyy';
 
1885   } # Ende Hotifx Bug 1388
 
1887   $main::lxdebug->leave_sub();