+ #Quater reports
+ if ($form->{duetyp} eq "A") {
+ $form->{fromdate} = "1.1.$form->{year}";
+ $form->{todate} = "31.3.$form->{year}";
+ }
+ if ($form->{duetyp} eq "B") {
+ $form->{fromdate} = "1.4.$form->{year}";
+ $form->{todate} = "30.6.$form->{year}";
+ }
+ if ($form->{duetyp} eq "C") {
+ $form->{fromdate} = "1.7.$form->{year}";
+ $form->{todate} = "30.9.$form->{year}";
+ }
+ if ($form->{duetyp} eq "D") {
+ $form->{fromdate} = "1.10.$form->{year}";
+ $form->{todate} = "31.12.$form->{year}";
+ }
+
+ #Monthly reports
+ SWITCH: {
+ $form->{duetyp} eq "1" && do {
+ $form->{fromdate} = "1.1.$form->{year}";
+ $form->{todate} = "31.1.$form->{year}";
+ last SWITCH;
+ };
+ $form->{duetyp} eq "2" && do {
+ $form->{fromdate} = "1.2.$form->{year}";
+
+ #this works from 1901 to 2099, 1900 and 2100 fail.
+ my $leap = ($form->{year} % 4 == 0) ? "29" : "28";
+ $form->{todate} = "$leap.2.$form->{year}";
+ last SWITCH;
+ };
+ $form->{duetyp} eq "3" && do {
+ $form->{fromdate} = "1.3.$form->{year}";
+ $form->{todate} = "31.3.$form->{year}";
+ last SWITCH;
+ };
+ $form->{duetyp} eq "4" && do {
+ $form->{fromdate} = "1.4.$form->{year}";
+ $form->{todate} = "30.4.$form->{year}";
+ last SWITCH;
+ };
+ $form->{duetyp} eq "5" && do {
+ $form->{fromdate} = "1.5.$form->{year}";
+ $form->{todate} = "31.5.$form->{year}";
+ last SWITCH;
+ };
+ $form->{duetyp} eq "6" && do {
+ $form->{fromdate} = "1.6.$form->{year}";
+ $form->{todate} = "30.6.$form->{year}";
+ last SWITCH;
+ };
+ $form->{duetyp} eq "7" && do {
+ $form->{fromdate} = "1.7.$form->{year}";
+ $form->{todate} = "31.7.$form->{year}";
+ last SWITCH;
+ };
+ $form->{duetyp} eq "8" && do {
+ $form->{fromdate} = "1.8.$form->{year}";
+ $form->{todate} = "31.8.$form->{year}";
+ last SWITCH;
+ };
+ $form->{duetyp} eq "9" && do {
+ $form->{fromdate} = "1.9.$form->{year}";
+ $form->{todate} = "30.9.$form->{year}";
+ last SWITCH;
+ };
+ $form->{duetyp} eq "10" && do {
+ $form->{fromdate} = "1.10.$form->{year}";
+ $form->{todate} = "31.10.$form->{year}";
+ last SWITCH;
+ };
+ $form->{duetyp} eq "11" && do {
+ $form->{fromdate} = "1.11.$form->{year}";
+ $form->{todate} = "30.11.$form->{year}";
+ last SWITCH;
+ };
+ $form->{duetyp} eq "12" && do {
+ $form->{fromdate} = "1.12.$form->{year}";
+ $form->{todate} = "31.12.$form->{year}";
+ last SWITCH;
+ };
+ }
+ hotfix_reformat_date();
+ }
+
+
+ # get for each account initial balance, debits and credits
+ RP->trial_balance(\%myconfig, \%$form, 'beginning_balances' => 1);
+
+
+ $form->{rowcount} = scalar @{ $form->{TB} || [] };
+ $form->{title} = sprintf($locale->text('Trial balance between %s and %s'), $form->{fromdate}, $form->{todate});
+
+ my @columns = (
+ "accno", "description",
+ "last_transaction", "soll_eb",
+ "haben_eb",
+ "soll", "haben",
+ "soll_kumuliert", "haben_kumuliert",
+ "soll_saldo", "haben_saldo"
+ );
+
+
+ my $attachment_basename = $locale->text('trial_balance');
+ my $report = SL::ReportGenerator->new(\%myconfig, $form);
+
+ my @hidden_variables = qw(fromdate todate year cash);
+
+ my $href = build_std_url('action=generate_trial_balance', grep { $form->{$_} } @hidden_variables);
+
+ my %column_defs = (
+ 'accno' => { 'text' => $locale->text('Account'), },
+ 'description' => { 'text' => $locale->text('Description'), },
+ 'last_transaction' => { 'text' => $locale->text('Last Transaction'), },
+ 'soll_eb' => { 'text' => $locale->text('Debit Starting Balance'), },
+ 'haben_eb' => { 'text' => $locale->text('Credit Starting Balance'), },
+ 'soll' => { 'text' => $locale->text('Debit'), },
+ 'haben' => { 'text' => $locale->text('Credit'), },
+ 'soll_kumuliert' => { 'text' => $locale->text('Sum Debit'), },
+ 'haben_kumuliert' => { 'text' => $locale->text('Sum Credit'), },
+ 'soll_saldo' => { 'text' => $locale->text('Saldo Debit'), },
+ 'haben_saldo' => { 'text' => $locale->text('Saldo Credit'), }
+ );
+
+
+
+ my %column_alignment = map { $_ => 'right' } qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
+
+ map { $column_defs{$_}->{visible} = 1 } @columns;
+
+ $report->set_columns(%column_defs);
+ $report->set_column_order(@columns);
+
+ $report->set_export_options('generate_trial_balance', @hidden_variables);
+
+ my @options;
+
+
+ $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};
+ push (@options, $form->{company});
+
+
+ $form->{template_to} = $locale->date(\%myconfig, $form->{todate}, 0);
+
+ $report->set_options('output_format' => 'HTML',
+ 'top_info_text' => join("\n", @options),
+ 'title' => $form->{title},
+ 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
+ 'html_template' => 'rp/html_report_susa',
+ 'pdf_template' => 'rp/html_report_susa',
+ );
+ $report->set_options_from_form();
+ $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
+
+ # add sort and escape callback, this one we use for the add sub
+ $form->{callback} = $href .= "&sort=$form->{sort}";
+
+ # escape callback for href
+ my $callback = $form->escape($href);
+
+ my @subtotal_columns = qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
+
+ my %totals = map { $_ => 0 } @subtotal_columns;
+
+ my $edit_url = build_std_url('action=edit', 'type', 'vc');
+
+ my $idx;
+ foreach my $accno (@{ $form->{TB} || [] }) {
+
+ $accno->{soll} = $accno->{debit};
+ $accno->{haben} = $accno->{credit};
+ map { $totals{$_} += $accno->{$_} } @subtotal_columns;
+
+ map { $accno->{$_} = $accno->{$_} == 0 ? '' : $form->format_amount(\%myconfig, $accno->{$_}, 2) }
+ qw(soll_eb haben_eb soll haben soll_kumuliert haben_kumuliert soll_saldo haben_saldo);
+
+ my $row = { };
+
+ foreach my $column (@columns) {
+ $row->{$column} = {
+ 'data' => $accno->{$column},
+ 'align' => $column_alignment{$column},
+ };
+ }
+
+ $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}));
+
+ my $row_set = [ $row ];
+
+
+ $report->add_data($row_set);
+
+ $idx++;
+ }
+
+ $report->add_separator();
+
+ $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
+
+ $report->generate_with_headers();
+
+ $main::lxdebug->leave_sub();
+
+}
+
+sub create_subtotal_row {
+ $main::lxdebug->enter_sub();
+
+ my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
+
+ my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
+
+ map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
+
+ $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
+
+ map { $totals->{$_} = 0 } @{ $subtotal_columns };
+
+ $main::lxdebug->leave_sub();
+
+ return $row;
+}
+
+sub create_list_accounts_subtotal_row {
+ $main::lxdebug->enter_sub();
+
+ my ($subtotals, $columns, $fields, $class) = @_;
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
+
+ my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => 'right' } } @{ $columns } };
+
+ map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $subtotals->{$_}, 2) } @{ $fields };
+
+ $main::lxdebug->leave_sub();
+
+ return $row;
+}
+
+sub list_accounts {
+ $main::lxdebug->enter_sub();
+
+ my ($action) = @_;
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $locale = $main::locale;
+
+ my @options;