Merge branch 'test' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / bin / mozilla / rp.pl
index dc1b153..630a2b5 100644 (file)
@@ -28,7 +28,8 @@
 # GNU General Public License for more details.
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1335, USA.
 #======================================================================
 #
 # module for preparing Income Statement and Balance Sheet
 
 use POSIX qw(strftime);
 
-use SL::PE;
+use SL::DB::Default;
+use SL::DB::Project;
+use SL::DB::Customer;
 use SL::RP;
 use SL::Iconv;
+use SL::Locale::String qw(t8);
+use SL::Presenter::Tag;
 use SL::ReportGenerator;
 use Data::Dumper;
+use List::MoreUtils qw(any);
 
-require "bin/mozilla/arap.pl";
 require "bin/mozilla/common.pl";
 require "bin/mozilla/reportgenerator.pl";
 
@@ -92,22 +97,22 @@ use strict;
 # $locale->text('Receipts')
 # $locale->text('Payments')
 # $locale->text('Project Transactions')
-# $locale->text('Non-taxable Sales')
-# $locale->text('Non-taxable Purchases')
 # $locale->text('Business evaluation')
+# $locale->text('Final Invoice, please use mark as paid manually')
 
 # $form->parse_html_template('rp/html_report_susa')
 
 my $rp_access_map = {
-  'projects'         => 'report',
-  'ar_aging'         => 'general_ledger',
-  'ap_aging'         => 'general_ledger',
-  'receipts'         => 'cash',
-  'payments'         => 'cash',
-  'trial_balance'    => 'report',
-  'income_statement' => 'report',
-  'bwa'              => 'report',
-  'balance_sheet'    => 'report',
+  'projects'           => 'report',
+  'ar_aging'           => 'general_ledger',
+  'ap_aging'           => 'general_ledger',
+  'receipts'           => 'cash',
+  'payments'           => 'cash',
+  'trial_balance'      => 'report',
+  'income_statement'   => 'report',
+  'erfolgsrechnung'    => 'report',
+  'bwa'                => 'report',
+  'balance_sheet'      => 'report',
 };
 
 sub check_rp_access {
@@ -120,838 +125,103 @@ sub check_rp_access {
 }
 
 sub report {
-  $main::lxdebug->enter_sub();
+  $::lxdebug->enter_sub;
 
   check_rp_access();
 
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
   my %title = (
-            'balance_sheet'        => 'Balance Sheet',
-            'income_statement'     => 'Income Statement',
-            'trial_balance'        => 'Trial Balance',
-            'ar_aging'             => 'Search AR Aging',
-            'ap_aging'             => 'Search AP Aging',
-            'tax_collected'        => 'Tax collected',
-            'tax_paid'             => 'Tax paid',
-            'nontaxable_sales'     => 'Non-taxable Sales',
-            'nontaxable_purchases' => 'Non-taxable Purchases',
-            'receipts'             => 'Receipts',
-            'payments'             => 'Payments',
-            'projects'             => 'Project Transactions',
-            'bwa'                  => 'Business evaluation',
+    balance_sheet        => $::locale->text('Balance Sheet'),
+    income_statement     => $::locale->text('Income Statement'),
+    erfolgsrechnung      => $::locale->text('Erfolgsrechnung'),
+    trial_balance        => $::locale->text('Trial Balance'),
+    ar_aging             => $::locale->text('Search AR Aging'),
+    ap_aging             => $::locale->text('Search AP Aging'),
+    tax_collected        => $::locale->text('Tax collected'),
+    tax_paid             => $::locale->text('Tax paid'),
+    receipts             => $::locale->text('Receipts'),
+    payments             => $::locale->text('Payments'),
+    projects             => $::locale->text('Project Transactions'),
+    bwa                  => $::locale->text('Business evaluation'),
   );
 
-  $form->{title} = $locale->text($title{ $form->{report} });
-
-  my $accrual =  $::instance_conf->get_accounting_method eq 'cash' ? ""        : "checked";
-  my $cash    =  $::instance_conf->get_accounting_method eq 'cash' ? "checked" : "";
-
-  my $year = (localtime)[5] + 1900;
+  $::form->{title} = $title{$::form->{report}};
+  $::request->{layout}->add_javascripts('kivi.CustomerVendor.js');
+  $::request->{layout}->add_javascripts('autocomplete_project.js');
+  $::form->{fromdate} = DateTime->today->truncate(to => 'year')->to_kivitendo;
+  $::form->{todate} = DateTime->today->truncate(to => 'year')->add(years => 1)->add(days => -1)->to_kivitendo;
 
   # get departments
-  $form->all_departments(\%myconfig);
-  if (@{ $form->{all_departments} || [] }) {
-    $form->{selectdepartment} = "<option>\n";
-    map { $form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $form->{all_departments} || [] };
-  }
-
-  my $department;
-  $department = qq|
-        <tr>
-          <th align=right nowrap>| . $locale->text('Department') . qq|</th>
-          <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
-        </tr>
-| if $form->{selectdepartment};
-
-  $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
-                                   "all" => 1 });
-
-  my %project_labels = ();
-  my @project_values = ("");
-  foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
-    push(@project_values, $item->{"id"});
-    $project_labels{$item->{"id"}} = $item->{"projectnumber"};
-  }
-
-  my $projectnumber =
-    NTI($::request->{cgi}->popup_menu('-name' => "project_id",
-                               '-values' => \@project_values,
-                               '-labels' => \%project_labels));
-
-  # use JavaScript Calendar or not
-  $form->{jsscript} = 1;
-  my $jsscript = "";
-  my ( $name_1, $id_1, $value_1, $trigger_1, $name_2, $id_2, $value_2, $trigger_2, );
-  if ($form->{report} eq "balance_sheet") {
-    $name_1    = "asofdate";
-    $id_1      = "asofdate";
-    $value_1   = "$form->{asofdate}";
-    $trigger_1 = "trigger1";
-    $name_2    = "compareasofdate";
-    $id_2      = "compareasofdate";
-    $value_2   = "$form->{compareasofdate}";
-    $trigger_2 = "trigger2";
-  } elsif ($form->{report} =~ /(receipts|payments)$/) {
-    $name_1    = "fromdate";
-    $id_1      = "fromdate";
-    $value_1   = "$form->{fromdate}";
-    $trigger_1 = "trigger1";
-    $name_2    = "todate";
-    $id_2      = "todate";
-    $value_2   = "";
-    $trigger_2 = "trigger2";
-  } elsif (($form->{report} eq "ar_aging") || ($form->{report} eq "ap_aging")) {
-    $name_1    = "fromdate";
-    $id_1      = "fromdate";
-    $value_1   = "$form->{fromdate}";
-    $trigger_1 = "trigger1";
-    $name_2    = "todate";
-    $id_2      = "todate";
-    $value_2   = "";
-    $trigger_2 = "trigger2";
-
-  } else {
-    $name_1    = "fromdate";
-    $id_1      = "fromdate";
-    $value_1   = "$form->{fromdate}";
-    $trigger_1 = "trigger1";
-    $name_2    = "todate";
-    $id_2      = "todate";
-    $value_2   = "";
-    $trigger_2 = "trigger2";
-  }
-
-  my ($button1, $button1_2, $button2, $button2_2);
-  my $checked;
-
-  # with JavaScript Calendar
-  if ($form->{jsscript}) {
-    if ($name_1 eq "") {
-      $button1   = qq| <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
-      $button1_2 = qq| <input type=button name=$name_2 id="$trigger_2" value=| . $locale->text('button') . qq|>|;
-
-      #write Trigger
-      $jsscript = Form->write_trigger(\%myconfig, "1", "$name_2", "BR", "$trigger_2");
-    } else {
-      $button1   = qq| <input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" value="$value_1" onBlur=\"check_right_date_format(this)\">|;
-      $button1_2 = qq| <input type=button name=$name_1 id="$trigger_1" value=| . $locale->text('button') . qq|>|;
-      $button2   = qq| <input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
-      $button2_2 = qq| <input type=button name=$name_2 id="$trigger_2" value=| . $locale->text('button') . qq|> |;
-
-      #write Trigger
-      $jsscript = Form->write_trigger(\%myconfig, "2", "$name_1", "BR", "$trigger_1", "$name_2", "BL", "$trigger_2");
-    }
-  } else {
-
-    # without JavaScript Calendar
-    if ($name_1 eq "") {
-      $button1 = qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
-    } else {
-      $button1 = qq|<input name=$name_1 id=$id_1 size=11 title="$myconfig{dateformat}" value=$value_1 onBlur=\"check_right_date_format(this)\">|;
-      $button2 = qq|<input name=$name_2 id=$id_2 size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">|;
-    }
-  }
-  $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
-  $form->header;
-  my $onload = qq|focus()|;
-  $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
-  $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
-  print qq|
-<body onLoad="$onload">
-
-<form method=post action=$form->{script}>
-
-<input type=hidden name=title value="$form->{title}">
-
-<table width=100% border="0">
-  <tr>
-    <th class=listtop>$form->{title}</th>
-  </tr>
-  <tr height="5"></tr>
-  <tr>
-    <td>
-      <table border="0">
-      $department
-|;
-
-  if ($form->{report} eq "projects") {
-    print qq|
-        <tr>
-          <th align=right nowrap>| . $locale->text('Project') . qq|</th>
-          <td colspan=5><input name=projectnumber size=25</td>
-        </tr>
-        <input type=hidden name=nextsub value=generate_projects>
-        <tr>
-          <th align=right>| . $locale->text('From') . qq|</th>
-          <td>$button1</td>
-          <td>$button1_2</td>
-          <th align=right>| . $locale->text('Bis') . qq|</th>
-          <td>$button2</td>
-          <td>$button2_2</td>
-        </tr>
-      </table>
-    </td>
-  </tr>
-  <tr>
-    <td>
-      <table>
-        <tr>
-          <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
-          <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;| . $locale->text('Heading') . qq|
-          <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;| . $locale->text('Subtotal') . qq|</td>
-        </tr>
-
-$jsscript
-|;
-  }
-
-  if ($form->{report} eq "income_statement") {
-    print qq|
-        <tr>
-          <th align=right nowrap>| . $locale->text('Project') . qq|</th>
-          <td colspan=3>$projectnumber</td>
-        </tr>
-        <input type=hidden name=nextsub value=generate_income_statement>
-</table>
-<table>
-        <tr>
-          <th align=left><input name=reporttype class=radio type=radio value="custom" checked> | . $locale->text('Customized Report') . qq|</th>
-        </tr>
-        <tr>
-          <th colspan=1>| . $locale->text('Year') . qq|</th>
-          <td><input name=year size=11 title="| . $locale->text('YYYY') . qq|" value="$year"></td>
-        </tr>
-|;
-
-    print qq|
-        <tr>
-                <td align=right> <b> | . $locale->text('Yearly') . qq|</b> </td>
-                <th align=left>| . $locale->text('Quarterly') . qq|</th>
-                <th align=left colspan=3>| . $locale->text('Monthly') . qq|</th>
-        </tr>
-        <tr>
-                <td align=right>&nbsp; <input name=duetyp class=radio type=radio value="13" "checked"></td>
-                <td><input name=duetyp class=radio type=radio value="A" $checked >&nbsp;1. | . $locale->text('Quarter') . qq|</td>
-|;
-    $checked = "";
-    print qq|
-                <td><input name=duetyp class=radio type=radio value="1" $checked >&nbsp;| . $locale->text('January') . qq|</td>
-|;
-    $checked = "";
-    print qq|
-                <td><input name=duetyp class=radio type=radio value="5" $checked >&nbsp;| . $locale->text('May') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="9" $checked >&nbsp;| . $locale->text('September') . qq|</td>
-
-        </tr>
-        <tr>
-                <td align= right>&nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="B" $checked>&nbsp;2. | . $locale->text('Quarter') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="2" $checked >&nbsp;| . $locale->text('February') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="6" $checked >&nbsp;| . $locale->text('June') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="10" $checked >&nbsp;| . $locale->text('October') . qq|</td>
-        </tr>
-        <tr>
-                <td> &nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="C" $checked>&nbsp;3. | . $locale->text('Quarter') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="3" $checked >&nbsp;| . $locale->text('March') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="7" $checked >&nbsp;| . $locale->text('July') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="11" $checked >&nbsp;| . $locale->text('November') . qq|</td>
-
-        </tr>
-        <tr>
-                <td> &nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="D" $checked>&nbsp;4. | . $locale->text('Quarter') . qq|&nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="4" $checked >&nbsp;| . $locale->text('April') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="8" $checked >&nbsp;| . $locale->text('August') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="12" $checked >&nbsp;| . $locale->text('December') . qq|</td>
-
-        </tr>
-        <tr>
-                   <td colspan=5><hr size=3 noshade></td>
-        </tr>
-        <tr>
-          <th align=left><input name=reporttype class=radio type=radio value="free" $checked> | . $locale->text('Free report period') . qq|</th>
-          <td align=left colspan=4>| . $locale->text('From') . qq|&nbsp;
-              $button1
-              $button1_2&nbsp;
-              | . $locale->text('Bis') . qq|
-              $button2
-              $button2_2&nbsp;
-          </td>
-        </tr>
-        <tr>
-                   <td colspan=5><hr size=3 noshade></td>
-        </tr>
-        <tr>
-          <th align=leftt>| . $locale->text('Method') . qq|</th>
-          <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>| . $locale->text('Accrual') . qq|
-          &nbsp;<input name=method class=radio type=radio value=cash $cash>| . $locale->text('EUR') . qq|</td>
-        </tr>
-
-$jsscript
-|;
-  }
-
-  if ($form->{report} eq "bwa") {
-    print qq|
-        <tr>
-          <th align=right nowrap>| . $locale->text('Project') . qq|</th>
-          <td colspan=3>$projectnumber</td>
-        </tr>
-        <input type=hidden name=nextsub value=generate_bwa>
-</table>
-<table>
-        <tr>
-          <th align=left><input name=reporttype class=radio type=radio value="custom" checked> | . $locale->text('Customized Report') . qq|</th>
-        </tr>
-        <tr>
-          <th colspan=1>| . $locale->text('Year') . qq|</th>
-          <td><input name=year size=11 title="| . $locale->text('YYYY') . qq|" value="$year"></td>
-        </tr>
-|;
-
-    print qq|
-        <tr>
-                <td align=right> <b> | . $locale->text('Yearly') . qq|</b> </td>
-                <th align=left>| . $locale->text('Quarterly') . qq|</th>
-                <th align=left colspan=3>| . $locale->text('Monthly') . qq|</th>
-        </tr>
-        <tr>
-                <td align=right>&nbsp; <input name=duetyp class=radio type=radio value="13"
-$checked></td>
-                <td><input name=duetyp class=radio type=radio value="A" $checked >&nbsp;1. | . $locale->text('Quarter') . qq|</td>
-|;
-    $checked = "checked";
-    print qq|
-                <td><input name=duetyp class=radio type=radio value="1" $checked >&nbsp;| . $locale->text('January') . qq|</td>
-|;
-    $checked = "";
-    print qq|
-                <td><input name=duetyp class=radio type=radio value="5" $checked >&nbsp;| . $locale->text('May') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="9" $checked >&nbsp;| . $locale->text('September') . qq|</td>
-
-        </tr>
-        <tr>
-                <td align= right>&nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="B" $checked>&nbsp;2. | . $locale->text('Quarter') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="2" $checked >&nbsp;| . $locale->text('February') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="6" $checked >&nbsp;| . $locale->text('June') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="10" $checked >&nbsp;| . $locale->text('October') . qq|</td>
-        </tr>
-        <tr>
-                <td> &nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="C" $checked>&nbsp;3. | . $locale->text('Quarter') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="3" $checked >&nbsp;| . $locale->text('March') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="7" $checked >&nbsp;| . $locale->text('July') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="11" $checked >&nbsp;| . $locale->text('November') . qq|</td>
-
-        </tr>
-        <tr>
-                <td> &nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="D" $checked>&nbsp;4. | . $locale->text('Quarter') . qq|&nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="4" $checked >&nbsp;| . $locale->text('April') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="8" $checked >&nbsp;| . $locale->text('August') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="12" $checked >&nbsp;| . $locale->text('December') . qq|</td>
-
-        </tr>
-        <tr>
-                   <td colspan=5><hr size=3 noshade></td>
-        </tr>
-        <tr>
-          <th align=left><input name=reporttype class=radio type=radio value="free" $checked> | . $locale->text('Free report period') . qq|</th>
-          <td align=left colspan=4>| . $locale->text('From') . qq|&nbsp;
-              $button1
-              $button1_2&nbsp;
-              | . $locale->text('Bis') . qq|&nbsp;
-              $button2
-              $button2_2
-          </td>
-        </tr>
-        <tr>
-                   <td colspan=5><hr size=3 noshade></td>
-        </tr>
-        <tr>
-          <th align=leftt>| . $locale->text('Method') . qq|</th>
-          <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>| . $locale->text('Accrual') . qq|
-          &nbsp;<input name=method class=radio type=radio value=cash $cash>| . $locale->text('EUR') . qq|</td>
-        </tr>
-        <tr>
-         <th align=right colspan=4>| . $locale->text('Decimalplaces') . qq|</th>
-             <td><input name=decimalplaces size=3 value="2"></td>
-         </tr>
-
-$jsscript
-|;
-  }
-
-  if ($form->{report} eq "balance_sheet") {
-    print qq|
-        <input type=hidden name=nextsub value=generate_balance_sheet>
-        <tr>
-          <th align=right>| . $locale->text('as at') . qq|</th>
-          <td>
-            $button1
-            $button1_2
-          </td>
-          <th align=right nowrap>| . $locale->text('Compare to') . qq|</th>
-          <td>
-          $button2
-          $button2_2
-          </td>
-        </tr>
-        <tr>
-          <th align=right>| . $locale->text('Decimalplaces') . qq|</th>
-          <td><input name=decimalplaces size=3 value="2"></td>
-        </tr>
-      </table>
-    </td>
-  </tr>
-  <tr>
-    <td>
-      <table>
-        <tr>
-          <th align=right>| . $locale->text('Method') . qq|</th>
-          <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>| . $locale->text('Accrual') . qq|
-          &nbsp;<input name=method class=radio type=radio value=cash $cash>| . $locale->text('EUR') . qq|</td>
-        </tr>
-
-        <tr>
-          <th align=right nowrap>| . $locale->text('Include in Report') . qq|</th>
-          <td><input name=l_heading class=checkbox type=checkbox value=Y>&nbsp;| . $locale->text('Heading') . qq|
-          <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;| . $locale->text('Subtotal') . qq|
-          <input name=l_accno class=checkbox type=checkbox value=Y>&nbsp;| . $locale->text('Account Number') . qq|</td>
-        </tr>
-
-$jsscript
-|;
-  }
-
-  if ($form->{report} eq "trial_balance") {
-    print qq|
-        <tr>
-          <th align=right nowrap>| . $locale->text('Project') . qq|</th>
-          <td colspan=3>$projectnumber</td>
-        </tr>
-        <input type=hidden name=nextsub value=generate_trial_balance>
-</table>
-<table>
-        <tr>
-          <th align=left><input name=reporttype class=radio type=radio value="custom" checked> | . $locale->text('Customized Report') . qq|</th>
-        </tr>
-        <tr>
-          <th colspan=1>| . $locale->text('Year') . qq|</th>
-          <td><input name=year size=11 title="| . $locale->text('YYYY') . qq|" value="$year"></td>
-        </tr>
-|;
-
-    print qq|
-        <tr>
-                <td align=right> <b> | . $locale->text('Yearly') . qq|</b> </td>
-                <th align=left>| . $locale->text('Quarterly') . qq|</th>
-                <th align=left colspan=3>| . $locale->text('Monthly') . qq|</th>
-        </tr>
-        <tr>
-                <td align=right>&nbsp; <input name=duetyp class=radio type=radio value="13" $checked></td>
-                <td><input name=duetyp class=radio type=radio value="A" $checked >&nbsp;1. | . $locale->text('Quarter') . qq|</td>
-|;
-    $checked = "checked";
-    print qq|
-                <td><input name=duetyp class=radio type=radio value="1" $checked >&nbsp;| . $locale->text('January') . qq|</td>
-|;
-    $checked = "";
-    print qq|
-                <td><input name=duetyp class=radio type=radio value="5" $checked >&nbsp;| . $locale->text('May') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="9" $checked >&nbsp;| . $locale->text('September') . qq|</td>
-
-        </tr>
-        <tr>
-                <td align= right>&nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="B" $checked>&nbsp;2. | . $locale->text('Quarter') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="2" $checked >&nbsp;| . $locale->text('February') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="6" $checked >&nbsp;| . $locale->text('June') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="10" $checked >&nbsp;| . $locale->text('October') . qq|</td>
-        </tr>
-        <tr>
-                <td> &nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="C" $checked>&nbsp;3. | . $locale->text('Quarter') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="3" $checked >&nbsp;| . $locale->text('March') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="7" $checked >&nbsp;| . $locale->text('July') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="11" $checked >&nbsp;| . $locale->text('November') . qq|</td>
-
-        </tr>
-        <tr>
-                <td> &nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="D" $checked>&nbsp;4. | . $locale->text('Quarter') . qq|&nbsp;</td>
-                <td><input name=duetyp class=radio type=radio value="4" $checked >&nbsp;| . $locale->text('April') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="8" $checked >&nbsp;| . $locale->text('August') . qq|</td>
-                <td><input name=duetyp class=radio type=radio value="12" $checked >&nbsp;| . $locale->text('December') . qq|</td>
-
-        </tr>
-        <tr>
-                   <td colspan=5><hr size=3 noshade></td>
-        </tr>
-        <tr>
-          <th align=left><input name=reporttype class=radio type=radio value="free" $checked> | . $locale->text('Free report period') . qq|</th>
-          <td align=left colspan=4>| . $locale->text('From') . qq|&nbsp;
-              $button1
-              $button1_2&nbsp;
-              | . $locale->text('Bis') . qq|&nbsp;
-              $button2
-              $button2_2
-          </td>
-        </tr>
-        <tr>
-                   <td colspan=5><hr size=3 noshade></td>
-        </tr>
-        <tr>
-          <th align=leftt>| . $locale->text('Method') . qq|</th>
-          <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>| . $locale->text('Accrual') . qq|
-          &nbsp;<input name=method class=radio type=radio value=cash $cash>| . $locale->text('EUR') . qq|</td>
-        </tr>
-       <tr>
-         <th align=right colspan=4>| . $locale->text('All Accounts') . qq|</th>
-             <td><input name=all_accounts type=checkbox value=1></td>
-         </tr>
-        <tr>
-         <th align=right colspan=4>| . $locale->text('Decimalplaces') . qq|</th>
-             <td><input name=decimalplaces size=3 value="2"></td>
-         </tr>
-
-$jsscript
-|;
+  $::form->all_departments(\%::myconfig);
+  if (@{ $::form->{all_departments} || [] }) {
+    $::form->{selectdepartment} = "<option>\n";
+    map { $::form->{selectdepartment} .= "<option>$_->{description}--$_->{id}\n" } @{ $::form->{all_departments} || [] };
   }
 
-  if ($form->{report} =~ /^tax_/) {
-    $form->{db} = ($form->{report} =~ /_collected/) ? "ar" : "ap";
+  $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 });
 
-    RP->get_taxaccounts(\%myconfig, \%$form);
+  my $is_projects            = $::form->{report} eq "projects";
+  my $is_income_statement    = $::form->{report} eq "income_statement";
+  my $is_erfolgsrechnung     = $::form->{report} eq "erfolgsrechnung";
+  my $is_bwa                 = $::form->{report} eq "bwa";
+  my $is_balance_sheet       = $::form->{report} eq "balance_sheet";
+  my $is_trial_balance       = $::form->{report} eq "trial_balance";
+  my $is_aging               = $::form->{report} =~ /^a[rp]_aging$/;
+  my $is_payments            = $::form->{report} =~ /(receipts|payments)$/;
+  my $format                 = 'html';
 
-    print qq|
-        <input type=hidden name=nextsub value=generate_tax_report>
-        <tr>
-          <th align=right>| . $locale->text('From') . qq|</th>
-          <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
-          <th align=right>| . $locale->text('Bis') . qq|</th>
-          <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
-        </tr>
-        <tr>
-          <th align=right>| . $locale->text('Report for') . qq|</th>
-          <td colspan=3>
-|;
-
-    $checked = "checked";
-    foreach my $ref (@{ $form->{taxaccounts} }) {
-
-      print
-        qq|<input name=accno class=radio type=radio value=$ref->{accno} $checked>&nbsp;$ref->{description}
-
-    <input name="$ref->{accno}_description" type=hidden value="$ref->{description}">
-    <input name="$ref->{accno}_rate" type=hidden value="$ref->{rate}">|;
-
-      $checked = "";
-
-    }
-
-    print qq|
-  <input type=hidden name=db value=$form->{db}>
-  <input type=hidden name=sort value=transdate>
-
-          </td>
-        </tr>
-        <tr>
-          <th align=right>| . $locale->text('Method') . qq|</th>
-          <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>| . $locale->text('Accrual') . qq|
-          &nbsp;<input name=method class=radio type=radio value=cash $cash>| . $locale->text('EUR') . qq|</td>
-        </tr>
-      </table>
-    </td>
-  </tr>
-  <tr>
-    <td>
-      <table>
-        <tr>
-          <th align=right>| . $locale->text('Include in Report') . qq|</th>
-          <td>
-            <table>
-              <tr>
-                <td><input name="l_id" class=checkbox type=checkbox value=Y></td>
-                <td>| . $locale->text('ID') . qq|</td>
-                <td><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
-                <td>| . $locale->text('Invoice') . qq|</td>
-                <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
-                <td>| . $locale->text('Date') . qq|</td>
-              </tr>
-              <tr>
-                <td><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
-                <td>|;
-
-    if ($form->{db} eq 'ar') {
-      print $locale->text('Customer');
-    }
-    if ($form->{db} eq 'ap') {
-      print $locale->text('Vendor');
-    }
-
-    print qq|</td>
-                <td><input name="l_netamount" class=checkbox type=checkbox value=Y checked></td>
-                <td>| . $locale->text('Amount') . qq|</td>
-                <td><input name="l_tax" class=checkbox type=checkbox value=Y checked></td>
-                <td>| . $locale->text('Tax') . qq|</td>
-                <td><input name="l_amount" class=checkbox type=checkbox value=Y></td>
-                <td>| . $locale->text('Total') . qq|</td>
-              </tr>
-              <tr>
-                <td><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
-                <td>| . $locale->text('Subtotal') . qq|</td>
-              </tr>
-            </table>
-          </td>
-        </tr>
-|;
-
-  }
-
-  if ($form->{report} =~ /^nontaxable_/) {
-    $form->{db} = ($form->{report} =~ /_sales/) ? "ar" : "ap";
-
-    print qq|
-        <input type=hidden name=nextsub value=generate_tax_report>
-
-        <input type=hidden name=db value=$form->{db}>
-        <input type=hidden name=sort value=transdate>
-        <input type=hidden name=report value=$form->{report}>
-
-        <tr>
-          <th align=right>| . $locale->text('From') . qq|</th>
-          <td><input name=fromdate size=11 title="$myconfig{dateformat}" value=$form->{fromdate}></td>
-          <th align=right>| . $locale->text('Bis') . qq|</th>
-          <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
-        </tr>
-        <tr>
-          <th align=right>| . $locale->text('Method') . qq|</th>
-          <td colspan=3><input name=method class=radio type=radio value=accrual $accrual>|
-      . $locale->text('Accrual') . qq|
-          &nbsp;<input name=method class=radio type=radio value=cash $cash>|
-      . $locale->text('EUR') . qq|</td>
-        </tr>
-        <tr>
-          <th align=right>| . $locale->text('Include in Report') . qq|</th>
-          <td colspan=3>
-            <table>
-              <tr>
-                <td><input name="l_id" class=checkbox type=checkbox value=Y></td>
-                <td>| . $locale->text('ID') . qq|</td>
-                <td><input name="l_invnumber" class=checkbox type=checkbox value=Y checked></td>
-                <td>| . $locale->text('Invoice') . qq|</td>
-                <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
-                <td>| . $locale->text('Date') . qq|</td>
-              </tr>
-              <tr>
-                <td><input name="l_name" class=checkbox type=checkbox value=Y checked></td>
-                <td>|;
-
-    if ($form->{db} eq 'ar') {
-      print $locale->text('Customer');
-    }
-    if ($form->{db} eq 'ap') {
-      print $locale->text('Vendor');
-    }
-
-    print qq|</td>
-                <td><input name="l_netamount" class=checkbox type=checkbox value=Y checked></td>
-                <td>| . $locale->text('Amount') . qq|</td>
-                <td><input name="l_amount" class=checkbox type=checkbox value=Y></td>
-                <td>| . $locale->text('Total') . qq|</td>
-              </tr>
-              <tr>
-                <td><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
-                <td>| . $locale->text('Subtotal') . qq|</td>
-              </tr>
-            </table>
-          </td>
-        </tr>
-|;
+  my ($label, $nextsub, $vc);
+  if ($is_aging) {
+    my $is_sales  = $::form->{report} eq 'ar_aging';
+    $label        = $is_sales ? $::locale->text('Customer') : $::locale->text('Vendor');
+    $::form->{vc} = $is_sales ? 'customer' : 'vendor';
 
-  }
+    $nextsub = "generate_$::form->{report}";
 
-  my ($label, $nextsub, $vc);
-  if (($form->{report} eq "ar_aging") || ($form->{report} eq "ap_aging")) {
-    if ($form->{report} eq 'ar_aging') {
-      $label = $locale->text('Customer');
-      $form->{vc} = 'customer';
-    } else {
-      $label = $locale->text('Vendor');
-      $form->{vc} = 'vendor';
-    }
+    $vc = qq|<input name=$::form->{vc} size=35 class="initial_focus">|;
 
-    $nextsub = "generate_$form->{report}";
-
-    # setup vc selection
-    $form->all_vc(\%myconfig, $form->{vc},
-                  ($form->{vc} eq 'customer') ? "AR" : "AP");
-
-    map { $vc .= "<option>$_->{name}--$_->{id}\n" }
-      @{ $form->{"all_$form->{vc}"} };
-
-    $vc =
-      ($vc)
-      ? qq|<select name=$form->{vc}><option>\n$vc</select>|
-      : qq|<input name=$form->{vc} size=35>|;
-
-    print qq|
-        <tr>
-          <th align=right>| . $locale->text($label) . qq|</th>
-          <td>$vc</td>
-        </tr>
-        <tr>
-          <td>| . $locale->text('Review of Aging list') . qq|</td>
-          <td><select name="review_of_aging_list">
-              <option></option>
-              <option>0-30</option>
-              <option>30-60</option>
-              <option>60-90</option>
-              <option>90-120</option>
-              <option>> 120</option>
-              </select>
-          </td>
-        </tr>
-        <tr>
-          <td align=left colspan=4>| . $locale->text('From') . qq|&nbsp;
-              $button1
-              $button1_2&nbsp;
-              | . $locale->text('Bis') . qq|&nbsp;
-              $button2
-              $button2_2
-          </td>
-        </tr>
-        <input type=hidden name=type value=statement>
-        <input type=hidden name=format value=html>
-        <input type=hidden name=media value=screen>
-
-        <input type=hidden name=nextsub value=$nextsub>
-        <input type=hidden name=action value=$nextsub>
-
-$jsscript
-|;
+    $format = 'pdf';
   }
 
-  # above action can be removed if there is more than one input field
-
   my ($selection, $paymentaccounts);
-  if ($form->{report} =~ /(receipts|payments)$/) {
-    $form->{db} = ($form->{report} =~ /payments$/) ? "ap" : "ar";
+  if ($is_payments) {
+    $::form->{db} = $::form->{report} =~ /payments$/ ? "ap" : "ar";
 
-    RP->paymentaccounts(\%myconfig, \%$form);
+    RP->paymentaccounts(\%::myconfig, $::form);
 
     $selection = "<option>\n";
-    foreach my $ref (@{ $form->{PR} }) {
+    for my $ref (@{ $::form->{PR} }) {
       $paymentaccounts .= "$ref->{accno} ";
       $selection       .= "<option>$ref->{accno}--$ref->{description}\n";
     }
-
-    chop $paymentaccounts;
-
-    print qq|
-        <input type=hidden name=nextsub value=list_payments>
-        <tr>
-          <th align=right nowrap>| . $locale->text('Account') . qq|</th>
-          <td colspan=3><select name=account>$selection</select>
-            <input type=hidden name=paymentaccounts value="$paymentaccounts">
-          </td>
-        </tr>
-        <tr>
-          <th align=right>| . $locale->text('Reference') . qq|</th>
-          <td colspan=3><input name=reference></td>
-        </tr>
-        <tr>
-          <th align=right nowrap>| . $locale->text('Source') . qq|</th>
-          <td colspan=3><input name=source></td>
-        </tr>
-        <tr>
-          <th align=right nowrap>| . $locale->text('Memo') . qq|</th>
-          <td colspan=3><input name=memo size=30></td>
-        </tr>
-        <tr>
-          <th align=right>| . $locale->text('From') . qq|</th>
-          <td>
-            $button1
-            $button1_2
-          </td>
-          <th align=right>| . $locale->text('Bis') . qq|</th>
-          <td>
-            $button2
-            $button2_2
-          </td>
-        </tr>
-        <tr>
-          <td align=right><input type=checkbox style=checkbox name=fx_transaction value=1 checked></td>
-          <th align=left colspan=3>| . $locale->text('Include Exchangerate Difference') . qq|</td>
-        </tr>
-
-$jsscript
-
-          <input type=hidden name=db value=$form->{db}>
-          <input type=hidden name=sort value=transdate>
-|;
-
   }
 
-  print qq|
-
-      </table>
-    </td>
-  </tr>
-  <tr>
-    <td><hr size=3 noshade></td>
-  </tr>
-</table>
-
-<br>
-<input type=submit class=submit name=action value="| . $locale->text('Continue') . qq|">
+  setup_rp_report_action_bar();
 
-</form>
-
-</body>
-</html>
-|;
+  $::form->header;
+  print $::form->parse_html_template('rp/report', {
+    paymentaccounts        => $paymentaccounts,
+    selection              => $selection,
+    is_aging               => $is_aging,
+    vc                     => $vc,
+    label                  => $label,
+    year                   => DateTime->today->year,
+    today                  => DateTime->today,
+    nextsub                => $nextsub,
+    is_payments            => $is_payments,
+    is_trial_balance       => $is_trial_balance,
+    is_balance_sheet       => $is_balance_sheet,
+    is_bwa                 => $is_bwa,
+    is_income_statement    => $is_income_statement,
+    is_erfolgsrechnung     => $is_erfolgsrechnung,
+    is_projects            => $is_projects,
+    format                 => $format,
+  });
 
-  $main::lxdebug->leave_sub();
+  $::lxdebug->leave_sub;
 }
 
 sub continue { call_sub($main::form->{"nextsub"}); }
 
-sub get_project {
-  $main::lxdebug->enter_sub();
-
-  $main::auth->assert('report');
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-  my $nextsub = shift;
-
-  $form->{project_id} = $form->{project_id_1};
-  if ($form->{projectnumber} && !$form->{project_id}) {
-    $form->{rowcount} = 1;
-
-    # call this instead of update
-    $form->{update}          = $nextsub;
-    $form->{projectnumber_1} = $form->{projectnumber};
-
-    delete $form->{sort};
-    check_project('generate_projects');
-
-    # if there is one only, assign id
-    $form->{project_id} = $form->{project_id_1};
-  }
-
-  $main::lxdebug->leave_sub();
-}
-
 sub generate_income_statement {
   $main::lxdebug->enter_sub();
 
@@ -1111,19 +381,49 @@ sub generate_income_statement {
       . qq| $longcomparetodate|;
   }
 
-  # setup variables for the form
-  my @a = qw(company address businessnumber);
-  map { $form->{$_} = $myconfig{$_} } @a;
+  if ( $::instance_conf->get_profit_determination eq 'balance' ) {
+    $form->{title} = $locale->text('Income Statement');
+  } elsif ( $::instance_conf->get_profit_determination eq 'income' ) {
+    $form->{title} = $locale->text('Net Income Statement');
+  } else {
+    $form->{title} = "";
+  };
 
-  $form->{templates} = $myconfig{templates};
+  if ( $form->{method} eq 'cash' ) {
+    $form->{accounting_method} = $locale->text('Cash accounting');
+  } elsif ( $form->{method} eq 'accrual' ) {
+    $form->{accounting_method} = $locale->text('Accrual accounting');
+  } else {
+    $form->{accounting_method} = "";
+  };
 
-  $form->{IN} = "income_statement.html";
+  $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
 
-  $form->parse_template;
+  $form->header;
+  print $form->parse_html_template('rp/income_statement');
 
   $main::lxdebug->leave_sub();
 }
 
+sub generate_erfolgsrechnung {
+  $::lxdebug->enter_sub;
+  $::auth->assert('report');
+
+  $::form->{decimalplaces} = $::form->{decimalplaces} * 1 || 2;
+  $::form->{padding}       = "&emsp;";
+  $::form->{bold}          = "<b>";
+  $::form->{endbold}       = "</b>";
+  $::form->{br}            = "<br>";
+
+  my $data = RP->erfolgsrechnung(\%::myconfig, $::form);
+
+  $::form->header();
+  print $::form->parse_html_template('rp/erfolgsrechnung', $data);
+
+  $::lxdebug->leave_sub;
+}
+
+
 sub generate_balance_sheet {
   $::lxdebug->enter_sub;
   $::auth->assert('report');
@@ -1136,8 +436,9 @@ sub generate_balance_sheet {
 
   my $data = RP->balance_sheet(\%::myconfig, $::form);
 
-  $::form->{asofdate} ||= $::form->current_date;
-  $::form->{period}     = $::locale->date(\%::myconfig, $::form->current_date, 1);
+  $::form->{asofdate}    ||= $::form->current_date;
+  $::form->{report_title}  = $::locale->text('Balance Sheet');
+  $::form->{report_date} ||= $::form->current_date;
 
   ($::form->{department}) = split /--/, $::form->{department};
 
@@ -1148,13 +449,10 @@ sub generate_balance_sheet {
   $::form->{this_period} = $::locale->date(\%::myconfig, $::form->{asofdate}, 0);
   $::form->{last_period} = $::locale->date(\%::myconfig, $::form->{compareasofdate}, 0);
 
+#  balance sheet isn't read from print templates anymore,
+#  instead use template in rp
 #  $::form->{IN} = "balance_sheet.html";
 
-  # setup company variables for the form
-  map { $::form->{$_} = $::myconfig{$_} } qw(company address businessnumber nativecurr);
-
-  $::form->{templates} = $::myconfig{templates};
-
   $::form->header;
   print $::form->parse_html_template('rp/balance_sheet', $data);
 
@@ -1170,8 +468,20 @@ sub generate_projects {
   my %myconfig = %main::myconfig;
   my $locale   = $main::locale;
 
-  &get_project("generate_projects");
-  $form->{projectnumber} = $form->{projectnumber_1};
+  my $project            = $form->{project_id} ? SL::DB::Project->new(id => $form->{project_id})->load : undef;
+  $form->{projectnumber} = $project ? $project->projectnumber : '';
+
+  # make sure todate and fromdate always have a value, even if the date fields
+  # were left empty or the inputs weren't valid dates/couldn't be parsed
+
+  $project = SL::DB::Project->new() unless $project;  # dummy object for dbh
+  unless ($::locale->parse_date_to_object($::form->{fromdate})) {
+    ($form->{fromdate}) = $project->db->dbh->selectrow_array('select min(transdate) from acc_trans');
+  };
+
+  unless ($::locale->parse_date_to_object($::form->{todate})) {
+    ($form->{todate})   = $project->db->dbh->selectrow_array('select max(transdate) from acc_trans');
+  };
 
   $form->{nextsub} = "generate_projects";
   $form->{title}   = $locale->text('Project Transactions');
@@ -1196,6 +506,7 @@ sub generate_trial_balance {
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
   my $locale   = $main::locale;
+  my $defaults = SL::DB::Default->get;
 
   if ($form->{reporttype} eq "custom") {
 
@@ -1320,7 +631,7 @@ sub generate_trial_balance {
   my $attachment_basename = $locale->text('trial_balance');
   my $report              = SL::ReportGenerator->new(\%myconfig, $form);
 
-  my @hidden_variables    = qw(fromdate todate year cash);
+  my @hidden_variables    = qw(fromdate todate year method department_id all_accounts);
 
   my $href                = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
 
@@ -1352,17 +663,44 @@ sub generate_trial_balance {
   my @options;
 
 
-  $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . "&nbsp; - &nbsp;" . $locale->date(\%myconfig, $form->{todate}, 0);
+  $form->{template_fromto} = $locale->date(\%myconfig, $form->{fromdate}, 0) . " - " . $locale->date(\%myconfig, $form->{todate}, 0);
 
   $form->{print_date} = $locale->text('Create Date') . " " . $locale->date(\%myconfig, $form->current_date(\%myconfig), 0);
   push (@options, $form->{print_date});
 
-  $form->{company} = $locale->text('Company') . " " . $myconfig{company};
+  $form->{company} = $locale->text('Company') . " " . $defaults->company;
   push (@options, $form->{company});
 
+  if ($::form->{customer_id}) {
+    my $customer = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id});
+    push @options, $::locale->text('Customer') . ' ' . $customer->displayable_name;
+  }
+
 
   $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
 
+  my @custom_headers = ([
+    { text => $::locale->text('Account'),          rowspan => 2, },
+    { text => $::locale->text('Description'),      rowspan => 2, },
+    { text => $::locale->text('Last Transaction'), rowspan => 2, },
+    { text => $::locale->text('Starting Balance'), colspan => 2, },
+    { text => $::locale->text('Sum for')   . " $form->{template_fromto}", colspan => 2, },
+    { text => $::locale->text('Sum per')   . " $form->{template_to}",     colspan => 2, },
+    { text => $::locale->text('Saldo per') . " $form->{template_to}",     colspan => 2, },
+  ], [
+    { text => '', },
+    { text => '', },
+    { text => '', },
+    { text => $::locale->text('Assets'), },
+    { text => $::locale->text('Equity'), },
+    { text => $::locale->text('Debit'),  },
+    { text => $::locale->text('Credit'), },
+    { text => $::locale->text('Debit'),  },
+    { text => $::locale->text('Credit'), },
+    { text => $::locale->text('Debit'),  },
+    { text => $::locale->text('Credit'), },
+  ]);
+
   $report->set_options('output_format'        => 'HTML',
                        'top_info_text'        => join("\n", @options),
                        'title'                => $form->{title},
@@ -1370,6 +708,7 @@ sub generate_trial_balance {
                        'html_template'        => 'rp/html_report_susa',
                        'pdf_template'         => 'rp/html_report_susa',
     );
+  $report->set_custom_headers(@custom_headers);
   $report->set_options_from_form();
   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
@@ -1592,7 +931,7 @@ sub list_accounts {
 sub generate_ar_aging {
   $main::lxdebug->enter_sub();
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('general_ledger | ar_transactions');
 
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
@@ -1615,7 +954,7 @@ sub generate_ar_aging {
 sub generate_ap_aging {
   $main::lxdebug->enter_sub();
 
-  $main::auth->assert('general_ledger');
+  $main::auth->assert('general_ledger | ap_transactions');
 
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
@@ -1668,26 +1007,27 @@ sub aging {
 
   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
-  my @columns = qw(statement ct invnumber transdate duedate amount open);
-
+  my @columns = qw(statement ct invnumber transdate duedate amount open datepaid current_open type);
   my %column_defs = (
-    'statement' => { 'text' => '', 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, },
+    'statement' => { raw_header_data => SL::Presenter::Tag::checkbox_tag("checkall", checkall => '[name^=statement_]'), 'visible' => $form->{ct} eq 'customer' ? 'HTML' : 0, align => "center" },
     'ct'        => { 'text' => $form->{ct} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
     'invnumber' => { 'text' => $locale->text('Invoice'), },
     'transdate' => { 'text' => $locale->text('Date'), },
     'duedate'   => { 'text' => $locale->text('Due'), },
     'amount'    => { 'text' => $locale->text('Amount'), },
     'open'      => { 'text' => $locale->text('Open'), },
+    'datepaid'  => { 'text' => $locale->text('Date of Last Payment'), visible => ($form->{reporttype} eq 'custom') },
+    'current_open' => { 'text' => $locale->text('Open Amount at Last Payment Date'), visible => ($form->{reporttype} eq 'custom') },
+    'type'      => { 'text' => $locale->text('Note'), },
   );
 
   my %column_alignment = ('statement' => 'center',
-                          map { $_ => 'right' } qw(open amount));
+                          map { $_ => 'right' } qw(open amount current_open datepaid));
 
   $report->set_options('std_column_visibility' => 1);
   $report->set_columns(%column_defs);
   $report->set_column_order(@columns);
-
-  my @hidden_variables = qw(todate customer vendor arap title ct);
+  my @hidden_variables = qw(todate customer vendor arap title ct fordate reporttype department fromdate);
   $report->set_export_options('generate_' . ($form->{arap} eq 'ar' ? 'ar' : 'ap') . '_aging', @hidden_variables);
 
   my @options;
@@ -1711,10 +1051,20 @@ sub aging {
     $form->{title} = sprintf($locale->text('Ap aging on %s'), $form->{todate});
   }
 
-  if ($form->{fromdate}) {
-    push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .$locale->date(\%myconfig, $form->{fromdate}, 1) . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
+  $form->{callback} .= "&reporttype=" . E($form->{reporttype});
+  if ($form->{reporttype} eq 'free') {
+    if ($form->{fromdate}) {
+      push @options, $locale->text('for Period') . " " . $locale->text('From') . " " .
+      $locale->date(\%myconfig, $form->{fromdate}, 1) . " "                          .
+      $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
+    } else {
+      push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " .
+      $locale->date(\%myconfig, $form->{todate}, 1);
+    }
+  } elsif ($form->{reporttype} eq 'custom') {
+    push @options, $locale->text('Reference day') . " " . $locale->date(\%myconfig, $form->{fordate}, 1);
   } else {
-    push @options, $locale->text('for Period') . " " . $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{todate}, 1);
+    die "Unknown reporttype for aging";
   }
 
   $attachment_basename = $form->{ct} eq 'customer' ? $locale->text('ar_aging_list') : $locale->text('ap_aging_list');
@@ -1729,7 +1079,7 @@ sub aging {
 
   my $previous_ctid = 0;
   my $row_idx       = 0;
-  my @periods       = qw(open amount);
+  my @periods       = qw(open amount current_open);
   my %subtotals     = map { $_ => 0 } @periods;
   my %totals        = map { $_ => 0 } @periods;
 
@@ -1755,6 +1105,12 @@ sub aging {
     }
 
     $row->{invnumber}->{link} =  build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
+    if ($row->{type}->{data} eq 'final_invoice') {
+      $row->{type}->{data} = $locale->text('Final Invoice, please use mark as paid manually');
+      $row->{type}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'callback', 'id=' . E($ref->{id}));
+    } else {
+      $row->{type}->{data} = '';
+    }
 
     if ($previous_ctid != $ref->{ctid}) {
       $row->{statement}->{raw_data} =
@@ -1782,140 +1138,12 @@ sub aging {
                          'raw_bottom_info_text' => $raw_bottom_info_text);
   }
 
+  setup_rp_aging_action_bar(arap => $form->{arap});
   $report->generate_with_headers();
 
   $main::lxdebug->leave_sub();
 }
 
-sub select_all {
-  $main::lxdebug->enter_sub();
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-  RP->aging(\%myconfig, \%$form);
-
-  map { $_->{checked} = "checked" } @{ $form->{AG} };
-
-  &aging;
-
-  $main::lxdebug->leave_sub();
-}
-
-sub e_mail {
-  $main::lxdebug->enter_sub();
-
-  $main::auth->assert('general_ledger');
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-  # get name and email addresses
-  my $selected = 0;
-  for my $i (1 .. $form->{rowcount}) {
-    if ($form->{"statement_$i"}) {
-      $form->{"$form->{ct}_id"} = $form->{"$form->{ct}_id_$i"};
-      RP->get_customer(\%myconfig, \%$form);
-      $selected = 1;
-      last;
-    }
-  }
-
-  $form->error($locale->text('Nothing selected!')) unless $selected;
-
-  my $bcc = '';
-  if ($myconfig{role} eq 'admin') {
-    $bcc = qq|
-          <th align=right nowrap=true>| . $locale->text('Bcc') . qq|</th>
-          <td><input name=bcc size=30 value="$form->{bcc}"></td>
-|;
-  }
-
-  my $title = $locale->text('E-mail Statement to') . " $form->{$form->{ct}}";
-
-  $form->{media} = "email";
-
-  $form->header;
-
-  print qq|
-<body>
-
-<form method=post action=$form->{script}>
-
-<table width=100%>
-  <tr class=listtop>
-    <th>$title</th>
-  </tr>
-  <tr height="5"></tr>
-  <tr>
-    <td>
-      <table width=100%>
-        <tr>
-          <th align=right nowrap>| . $locale->text('E-mail') . qq|</th>
-          <td><input name=email size=30 value="$form->{email}"></td>
-          <th align=right nowrap>| . $locale->text('Cc') . qq|</th>
-          <td><input name=cc size=30 value="$form->{cc}"></td>
-        </tr>
-        <tr>
-          <th align=right nowrap>| . $locale->text('Subject') . qq|</th>
-          <td><input name=subject size=30 value="$form->{subject}"></td>
-          $bcc
-        </tr>
-      </table>
-    </td>
-  </tr>
-  <tr>
-    <td>
-      <table width=100%>
-        <tr>
-          <th align=left nowrap>| . $locale->text('Message') . qq|</th>
-        </tr>
-        <tr>
-          <td><textarea name=message rows=15 cols=60 wrap=soft>$form->{message}</textarea></td>
-        </tr>
-      </table>
-    </td>
-  </tr>
-  <tr>
-    <td>
-|;
-
-  &print_options;
-
-  map { delete $form->{$_} }
-    qw(action email cc bcc subject message type sendmode format header);
-
-  # save all other variables
-  foreach my $key (keys %$form) {
-    next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
-    $form->{$key} =~ s/\"/&quot;/g;
-    print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
-  }
-
-  print qq|
-    </td>
-  </tr>
-  <tr>
-    <td><hr size=3 noshade></td>
-  </tr>
-</table>
-
-<input type=hidden name=nextsub value=send_email>
-
-<br>
-<input name=action class=submit type=submit value="|
-    . $locale->text('Continue') . qq|">
-</form>
-
-</body>
-</html>
-|;
-
-  $main::lxdebug->leave_sub();
-}
-
 sub send_email {
   $main::lxdebug->enter_sub();
 
@@ -1930,8 +1158,11 @@ sub send_email {
 
   RP->aging(\%myconfig, \%$form);
 
-  $form->{"statement_1"} = 1;
 
+  my $email_form  = delete $form->{email_form};
+  my %field_names = (to => 'email');
+
+  $form->{ $field_names{$_} // $_ } = $email_form->{$_} for keys %{ $email_form };
   $form->{media} = 'email';
   print_form();
 
@@ -1990,9 +1221,11 @@ sub print_form {
   my %myconfig = %main::myconfig;
   my $locale   = $main::locale;
 
-  $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
+  my $defaults = SL::DB::Default->get;
+  $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
+  $form->{templates} = $defaults->templates;
 
-  $form->{templates} = "$myconfig{templates}";
+  $form->{statementdate} = $locale->date(\%myconfig, $form->{todate}, 1);
 
   my $suffix = "html";
   my $attachment_suffix = "html";
@@ -2123,259 +1356,138 @@ sub statement_details {
 }
 
 sub generate_tax_report {
-  $main::lxdebug->enter_sub();
-
-  $main::auth->assert('report');
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
+  $::lxdebug->enter_sub;
+  $::auth->assert('report');
 
-  RP->tax_report(\%myconfig, \%$form);
+  RP->tax_report(\%::myconfig, $::form);
 
-  my $descvar     = "$form->{accno}_description";
-  my $description = $form->escape($form->{$descvar});
-  my $ratevar     = "$form->{accno}_rate";
+  my $descvar     = "$::form->{accno}_description";
   my ($subtotalnetamount, $subtotaltax, $subtotal) = (0, 0, 0);
 
-  my $department = $form->escape($form->{department});
-
   # construct href
-  my $href =
-    "$form->{script}?&action=generate_tax_report&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&accno=$form->{accno}&$descvar=$description&department=$department&report=$form->{report}";
-
-  # construct callback
-  $description = $form->escape($form->{$descvar},   1);
-  $department  = $form->escape($form->{department}, 1);
-  my $callback    =
-    "$form->{script}?&action=generate_tax_report&fromdate=$form->{fromdate}&todate=$form->{todate}&db=$form->{db}&method=$form->{method}&accno=$form->{accno}&$descvar=$description&department=$department&report=$form->{report}";
+  my $href     =
+  my $callback = build_std_url('action=generate_tax_report', $descvar,
+    qw(fromdate todate db method accno department report title));
 
-  my $title = $form->escape($form->{title});
-  $href .= "&title=$title";
-  $title = $form->escape($form->{title}, 1);
-  $callback .= "&title=$title";
-
-  $form->{title} = qq|$form->{title} $form->{"$form->{accno}_description"} |;
-
-  my @columns =
-    $form->sort_columns(qw(id transdate invnumber name netamount tax amount));
+  my @columns = $::form->sort_columns(qw(id transdate invnumber name netamount tax amount));
   my @column_index;
 
-  foreach my $item (@columns) {
-    if ($form->{"l_$item"} eq "Y") {
-      push @column_index, $item;
-
-      # add column to href and callback
+  for my $item (@columns, 'subtotal') {
+    if ($::form->{"l_$item"} eq "Y") {
       $callback .= "&l_$item=Y";
       $href     .= "&l_$item=Y";
     }
   }
 
-  if ($form->{l_subtotal} eq 'Y') {
-    $callback .= "&l_subtotal=Y";
-    $href     .= "&l_subtotal=Y";
+  for my $item (@columns) {
+    if ($::form->{"l_$item"} eq "Y") {
+      push @column_index, $item;
+    }
   }
 
-  my $option;
-  if ($form->{department}) {
-    ($department) = split /--/, $form->{department};
-    $option = $locale->text('Department') . " : $department";
+  my @options;
+  if ($::form->{department}) {
+    my ($department) = split /--/, $::form->{department};
+    push @options, $::locale->text('Department') . " : $department";
   }
 
-  my ($fromdate, $todate);
   # if there are any dates
-  if ($form->{fromdate} || $form->{todate}) {
-    if ($form->{fromdate}) {
-      $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
-    }
-    if ($form->{todate}) {
-      $todate = $locale->date(\%myconfig, $form->{todate}, 1);
-    }
-
-    $form->{period} = "$fromdate - $todate";
+  if ($::form->{fromdate} || $::form->{todate}) {
+    my $fromdate = $::form->{fromdate} ? $::locale->date(\%::myconfig, $::form->{fromdate}, 1) : '';
+    my $todate   = $::form->{todate}   ? $::locale->date(\%::myconfig, $::form->{todate}, 1)   : '';
+    push @options, "$fromdate - $todate";
   } else {
-    $form->{period} =
-      $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
+    push @options, $::locale->date(\%::myconfig, $::form->current_date, 1);
   }
 
   my ($name, $invoice, $arap);
-  if ($form->{db} eq 'ar') {
-    $name    = $locale->text('Customer');
+  if ($::form->{db} eq 'ar') {
+    $name    = $::locale->text('Customer');
     $invoice = 'is.pl';
     $arap    = 'ar.pl';
   }
-  if ($form->{db} eq 'ap') {
-    $name    = $locale->text('Vendor');
+  if ($::form->{db} eq 'ap') {
+    $name    = $::locale->text('Vendor');
     $invoice = 'ir.pl';
     $arap    = 'ap.pl';
   }
 
-  $option .= "<br>" if $option;
-  $option .= "$form->{period}";
-
-  my %column_header;
-  $column_header{id}        = qq|<th><a class=listheading href=$href&sort=id>| . $locale->text('ID') . qq|</th>|;
-  $column_header{invnumber} = qq|<th><a class=listheading href=$href&sort=invnumber>| . $locale->text('Invoice') . qq|</th>|;
-  $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>| . $locale->text('Date') . qq|</th>|;
-  $column_header{netamount} = qq|<th class=listheading>| . $locale->text('Amount') . qq|</th>|;
-  $column_header{tax}       = qq|<th class=listheading>| . $locale->text('Tax') . qq|</th>|;
-  $column_header{amount}    = qq|<th class=listheading>| . $locale->text('Total') . qq|</th>|;
-
-  $column_header{name}      = qq|<th><a class=listheading href=$href&sort=name>$name</th>|;
-
-  $form->header;
-
-  print qq|
-<body>
-
-<table width=100%>
-  <tr>
-    <th class=listtop>$form->{title}</th>
-  </tr>
-  <tr height="5"></tr>
-  <tr>
-    <td>$option</td>
-  </tr>
-  <tr>
-    <td>
-      <table width=100%>
-        <tr class=listheading>
-|;
-
-  map { print "$column_header{$_}\n" } @column_index;
+  my %column_header = (
+    id        => $::locale->text('ID'),
+    invnumber => $::locale->text('Invoice'),
+    transdate => $::locale->text('Date'),
+    netamount => $::locale->text('Amount'),
+    tax       => $::locale->text('Tax'),
+    amount    => $::locale->text('Total'),
+    name      => $name,
+  );
 
-  print qq|
-        </tr>
-|;
+  my %column_sorted = map { $_ => 1 } qw(id invnumber transdate);
 
-  # add sort and escape callback
-  $callback = $form->escape($callback . "&sort=$form->{sort}");
+  $callback .= "&sort=$::form->{sort}";
 
   my $sameitem;
-  if (@{ $form->{TR} }) {
-    $sameitem = $form->{TR}->[0]->{ $form->{sort} };
+  if (@{ $::form->{TR} }) {
+    $sameitem = $::form->{TR}->[0]->{ $::form->{sort} };
   }
 
-  my ($totalnetamount, $totaltax);
-  my ($i);
-  foreach my $ref (@{ $form->{TR} }) {
+  my ($totalnetamount, $totaltax, @data);
+  for my $ref (@{ $::form->{TR} }) {
 
     my $module = ($ref->{invoice}) ? $invoice : $arap;
 
-    if ($form->{l_subtotal} eq 'Y') {
-      if ($sameitem ne $ref->{ $form->{sort} }) {
-        tax_subtotal(\@column_index, \$subtotalnetamount, \$subtotaltax, \$subtotal);
-        $sameitem = $ref->{ $form->{sort} };
+    if ($::form->{l_subtotal} eq 'Y') {
+      if ($sameitem ne $ref->{ $::form->{sort} }) {
+        push @data, {
+          subtotal  => 1,
+          netamount => $subtotalnetamount,
+          tax       => $subtotaltax,
+          amount    => $subtotal,
+        };
+        $subtotalnetamount = 0;
+        $subtotaltax       = 0;
+        $sameitem          = $ref->{ $::form->{sort} };
       }
     }
 
-    $totalnetamount += $ref->{netamount};
-    $totaltax       += $ref->{tax};
-    $ref->{amount} = $ref->{netamount} + $ref->{tax};
-
     $subtotalnetamount += $ref->{netamount};
     $subtotaltax       += $ref->{tax};
+    $totalnetamount    += $ref->{netamount};
+    $totaltax          += $ref->{tax};
+    $ref->{amount}      = $ref->{netamount} + $ref->{tax};
 
-    map {
-      $ref->{$_} = $form->format_amount(\%myconfig, $ref->{$_}, 2, "&nbsp;");
-    } qw(netamount tax amount);
-
-    my %column_data;
-    $column_data{id}        = qq|<td>$ref->{id}</td>|;
-    $column_data{invnumber} =
-      qq|<td><a href=$module?action=edit&id=$ref->{id}&callback=$callback>$ref->{invnumber}</a></td>|;
-    $column_data{transdate} = qq|<td>$ref->{transdate}</td>|;
-    $column_data{name}      = qq|<td>$ref->{name}&nbsp;</td>|;
-
-    map { $column_data{$_} = qq|<td align=right>$ref->{$_}</td>| }
-      qw(netamount tax amount);
-
-    $i++;
-    $i %= 2;
-    print qq|
-        <tr class=listrow$i>
-|;
-
-    map { print "$column_data{$_}\n" } @column_index;
-
-    print qq|
-        </tr>
-|;
-
+    push @data, { map { $_ => { data => $ref->{$_} } } keys %$ref };
+    $data[-1]{invnumber}{link} = "$module?action=edit&id=$ref->{id}&callback=$callback";
+    $data[-1]{$_}{numeric}     = 1 for qw(netamount tax amount);
   }
 
-  if ($form->{l_subtotal} eq 'Y') {
-    tax_subtotal(\@column_index, \$subtotalnetamount, \$subtotaltax, \$subtotal);
+  if ($::form->{l_subtotal} eq 'Y') {
+    push @data, {
+      subtotal  => 1,
+      netamount => $subtotalnetamount,
+      tax       => $subtotaltax,
+      amount    => $subtotal,
+    };
   }
 
-  my %column_data;
-  map { $column_data{$_} = qq|<th>&nbsp;</th>| } @column_index;
-
-  print qq|
-        </tr>
-        <tr class=listtotal>
-|;
-
-  my $total          = $form->format_amount(\%myconfig, $totalnetamount + $totaltax, 2, "&nbsp;");
-  $totalnetamount = $form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;");
-  $totaltax       = $form->format_amount(\%myconfig, $totaltax, 2, "&nbsp;");
-
-  $column_data{netamount} = qq|<th class=listtotal align=right>$totalnetamount</th>|;
-  $column_data{tax}    = qq|<th class=listtotal align=right>$totaltax</th>|;
-  $column_data{amount} = qq|<th class=listtotal align=right>$total</th>|;
-
-  map { print "$column_data{$_}\n" } @column_index;
-
-  print qq|
-        </tr>
-      </table>
-    </td>
-  </tr>
-  <tr>
-    <td><hr size=3 noshade></td>
-  </tr>
-</table>
-
-</body>
-</html>
-|;
-
-  $main::lxdebug->leave_sub();
-}
-
-sub tax_subtotal {
-  $main::lxdebug->enter_sub();
-
-  my ($column_index, $subtotalnetamount, $subtotaltax, $subtotal) = @_;
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-  my %column_data;
-  map { $column_data{$_} = "<td>&nbsp;</td>" } @{ $column_index };
-
-  $$subtotalnetamount = $form->format_amount(\%myconfig, $$subtotalnetamount, 2, "&nbsp;");
-  $$subtotaltax       = $form->format_amount(\%myconfig, $$subtotaltax, 2, "&nbsp;");
-  $$subtotal          = $form->format_amount(\%myconfig, $$subtotalnetamount + $$subtotaltax, 2, "&nbsp;");
-
-  $column_data{netamount} = "<th class=listsubtotal align=right>$$subtotalnetamount</th>";
-  $column_data{tax}       = "<th class=listsubtotal align=right>$$subtotaltax</th>";
-  $column_data{amount}    = "<th class=listsubtotal align=right>$$subtotal</th>";
-
-  $$subtotalnetamount = 0;
-  $$subtotaltax       = 0;
-
-  print qq|
-        <tr class=listsubtotal>
-|;
-  map { print "\n$column_data{$_}" } @{ $column_index };
+  push @data, {
+    total     => 1,
+    netamount => $totalnetamount,
+    tax       => $totaltax,
+    amount    => $totalnetamount + $totaltax,
+  };
 
-  print qq|
-        </tr>
-|;
+  $::form->header;
+  print $::form->parse_html_template('rp/tax_report', {
+    column_index  => \@column_index,
+    column_header => \%column_header,
+    column_sorted => \%column_sorted,
+    sort_base     => $href,
+    DATA          => \@data,
+    options       => \@options,
+  });
 
-  $main::lxdebug->leave_sub();
+  $::lxdebug->leave_sub;
 }
 
 sub list_payments {
@@ -2512,79 +1624,26 @@ sub list_payments {
 }
 
 sub print_options {
-  $main::lxdebug->enter_sub();
+  $::lxdebug->enter_sub;
 
   my ($dont_print) = @_;
 
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-  $form->{sendmode} = "attachment";
-
-  $form->{"format"} =
-    $form->{"format"} ? $form->{"format"} :
-    $myconfig{"template_format"} ? $myconfig{"template_format"} :
-    "pdf";
-
-  $form->{"copies"} =
-    $form->{"copies"} ? $form->{"copies"} :
-    $myconfig{"copies"} ? $myconfig{"copies"} :
-    2;
-
-  $form->{PD}{ $form->{type} }     = "selected";
-  $form->{DF}{ $form->{format} }   = "selected";
-  $form->{OP}{ $form->{media} }    = "selected";
-  $form->{SM}{ $form->{sendmode} } = "selected";
-
-  my ($media);
-  my $type = qq|
-            <option value=statement $form->{PD}{statement}>| . $locale->text('Statement');
-
-  if ($form->{media} eq 'email') {
-    $media = qq|
-            <option value=attachment $form->{SM}{attachment}>| . $locale->text('Attachment') . qq|
-            <option value=inline $form->{SM}{inline}>| . $locale->text('In-line');
-  } else {
-    $media = qq|
-            <option value=screen $form->{OP}{screen}>| . $locale->text('Screen');
-    if ($myconfig{printer} && $::lx_office_conf{print_templates}->{latex}) {
-      $media .= qq|
-            <option value=printer $form->{OP}{printer}>| . $locale->text('Printer');
-    }
-  }
-
-  my $format;
-  if ($::lx_office_conf{print_templates}->{latex}) {
-    $format .= qq|
-            <option value=html $form->{DF}{html}>| . $locale->text('HTML')
-      . qq| <option value=pdf $form->{DF}{pdf}>| . $locale->text('PDF')
-      . qq| <option value=postscript $form->{DF}{postscript}>| . $locale->text('Postscript');
-  }
+  $::form->{sendmode} = "attachment";
+  $::form->{format} ||= $::myconfig{template_format} || "pdf";
+  $::form->{copies} ||= $::myconfig{copies}          || 2;
 
-  my $output = qq|
-<table>
-  <tr>
-    <td><select name=type>$type</select></td>
-    <td><select name=format>$format</select></td>
-    <td><select name=media>$media</select></td>
-|;
-
-  if ($myconfig{printer} && $::lx_office_conf{print_templates}->{latex} && $form->{media} ne 'email') {
-    $output .= qq|
-      <td>| . $locale->text('Copies') . qq|
-      <input name=copies size=2 value=$form->{copies}></td>
-|;
-  }
+  $::form->{PD}{ $::form->{type} }     = "selected";
+  $::form->{DF}{ $::form->{format} }   = "selected";
+  $::form->{OP}{ $::form->{media} }    = "selected";
+  $::form->{SM}{ $::form->{sendmode} } = "selected";
 
-  $output .= qq|
-  </tr>
-</table>
-|;
+  my $output = $::form->parse_html_template('rp/print_options', {
+    is_email    => $::form->{media} eq 'email',
+  });
 
   print $output unless $dont_print;
 
-  $main::lxdebug->leave_sub();
+  $::lxdebug->leave_sub;
 
   return $output;
 }
@@ -2743,8 +1802,7 @@ sub generate_bwa {
     # if (defined ($form->{fromdate|todate}=='..'))
     # immer wahr
     if ($form->{fromdate}){
-      my ($yy, $mm, $dd) = $locale->parse_date(\%myconfig, $form->{fromdate});
-      my $datetime = $locale->parse_date_to_object(\%myconfig, $form->{fromdate});
+      my $datetime = $locale->parse_date_to_object($form->{fromdate});
       $datetime->set( month      => 1,
                       day        => 1);
       $form->{comparefromdate} = $locale->format_date(\%::myconfig, $datetime);
@@ -2785,14 +1843,21 @@ sub generate_bwa {
       . qq| $longtodate|;
   }
 
-  # setup variables for the form
-  my @a = qw(company address businessnumber);
-  map { $form->{$_} = $myconfig{$_} } @a;
-  $form->{templates} = $myconfig{templates};
+  $form->{report_date} = $locale->text('Report date') . ": " . $form->current_date;
 
-  $form->{IN} = "bwa.html";
+  if ( $form->{method} eq 'cash' ) {
+    $form->{accounting_method} = $locale->text('Cash accounting');
+  } elsif ( $form->{method} eq 'accrual' ) {
+    $form->{accounting_method} = $locale->text('Accrual accounting');
+  } else {
+    $form->{accounting_method} = "";
+  };
 
-  $form->parse_template;
+  $form->{title} = $locale->text('BWA');
+
+  $::request->layout->add_stylesheets('bwa.css');
+  $form->header;
+  print $form->parse_html_template('rp/bwa');
 
   $main::lxdebug->leave_sub();
 }
@@ -2824,4 +1889,42 @@ sub hotfix_reformat_date {
   $main::lxdebug->leave_sub();
 
 }
+
+sub setup_rp_aging_action_bar {
+  my %params = @_;
+
+  return unless $params{arap} eq 'ar';
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      combobox => [
+        action => [
+          t8('Print'),
+          call   => [ 'kivi.SalesPurchase.show_print_dialog' ],
+          checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
+        ],
+        action => [
+          t8('E Mail'),
+          call   => [ 'kivi.SalesPurchase.show_email_dialog', 'send_email' ],
+          checks => [ [ 'kivi.check_if_entries_selected', '[name^=statement_]' ] ],
+        ],
+      ],
+    );
+  }
+}
+
+sub setup_rp_report_action_bar {
+  my %params = @_;
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      action => [
+        t8('Continue'),
+        submit    => [ '#form', { action => 'continue' } ],
+        accesskey => 'enter',
+      ],
+    );
+  }
+}
+
 1;