my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
- my $sortorder;
-
- if ($form->{sort}) {
- $form->{sort} =~ s/[^a-zA-Z_]//g;
- $sortorder = $form->{sort} . ",";
+ my %sort_columns = (
+ 'id' => [ qw(id) ],
+ 'transdate' => [ qw(transdate id) ],
+ 'reference' => [ qw(lower_reference id) ],
+ 'source' => [ qw(lower_source id) ],
+ 'description' => [ qw(lower_description id) ],
+ 'accno' => [ qw(accno transdate id) ],
+ );
+ my %lowered_columns = (
+ 'reference' => { 'gl' => 'g.reference', 'arap' => 'a.invnumber', },
+ 'source' => { 'gl' => 'ac.source', 'arap' => 'ac.source', },
+ 'description' => { 'gl' => 'g.description', 'arap' => 'ct.name', },
+ );
+
+ my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
+ my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
+ my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
+
+ my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
+ foreach my $spec (@{ $sort_columns{$sortkey} }) {
+ next if ($spec !~ m/^lower_(.*)$/);
+
+ my $column = $1;
+ map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
}
my $query =
g.description, ac.transdate, ac.source, ac.trans_id,
ac.amount, c.accno, g.notes, t.chart_id, ac.oid
$project_columns
+ $columns_for_sorting{gl}
FROM gl g, acc_trans ac $project_join, chart c
LEFT JOIN tax t ON (t.chart_id = c.id)
WHERE $glwhere
ct.name, ac.transdate, ac.source, ac.trans_id,
ac.amount, c.accno, a.notes, t.chart_id, ac.oid
$project_columns
+ $columns_for_sorting{arap}
FROM ar a, acc_trans ac $project_join, customer ct, chart c
LEFT JOIN tax t ON (t.chart_id=c.id)
WHERE $arwhere
ct.name, ac.transdate, ac.source, ac.trans_id,
ac.amount, c.accno, a.notes, t.chart_id, ac.oid
$project_columns
+ $columns_for_sorting{arap}
FROM ap a, acc_trans ac $project_join, vendor ct, chart c
LEFT JOIN tax t ON (t.chart_id=c.id)
WHERE $apwhere
AND (a.vendor_id = ct.id)
AND (a.id = ac.trans_id)
- ORDER BY $sortorder transdate, trans_id, acoid, taxkey DESC|;
+ ORDER BY $sortorder, acoid $sortdir|;
my @values = (@glvalues, @arvalues, @apvalues);
$auth->assert('general_ledger');
- $form->{sort} ||= "transdate";
+ report_generator_set_default_sort('transdate', 1);
GL->all_transactions(\%myconfig, \%$form);
}
- my $callback = build_std_url('action=generate_report', @hidden_variables);
+ my $callback = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
$form->{l_credit_accno} = 'Y';
$form->{l_debit_accno} = 'Y';
'projectnumbers' => { 'text' => $locale->text('Project Numbers'), },
);
- map { $column_defs{$_}->{link} = $callback . "&sort=${_}" } qw(id transdate reference source description);
- map { $column_defs{$_}->{link} = $callback . "&sort=accno" } qw(debit_accno credit_accno debit_tax_accno credit_tax_accno debit_tax credit_tax);
+ foreach my $name (qw(id transdate reference source description debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
+ my $sortname = $name =~ m/accno/ ? 'accno' : $name;
+ my $sortdir = $sortname eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
+ $column_defs{$name}->{link} = $callback . "&sort=$sortname&sortdir=$sortdir";
+ }
+
map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
map { $column_defs{$_}->{visible} = 0 } qw(debit_accno credit_accno debit_tax_accno credit_tax_accno) if $form->{accno};
my %column_alignment;
- map { $column_alignment{$_} = 'right' } qw(balance id debit credit debit_tax credit_tax);
- map { $column_alignment{$_} = 'center' } qw(transdate reference description source notes debit_accno credit_accno debit_tax_accno credit_tax_accno);
+ map { $column_alignment{$_} = 'right' } qw(balance id debit credit debit_tax credit_tax);
+ map { $column_alignment{$_} = 'center' } qw(transdate reference description source notes debit_accno credit_accno debit_tax_accno credit_tax_accno);
+ map { $column_defs{$_}->{align} = $column_alignment{$_} } keys %column_alignment;
my $report = SL::ReportGenerator->new(\%myconfig, $form);
$report->set_export_options('generate_report', @hidden_variables);
- $report->set_sort_indicator($form->{sort}, 1);
+ $report->set_sort_indicator($form->{sort} eq 'accno' ? 'debit_accno' : $form->{sort}, $form->{sortdir});
$report->set_options('top_info_text' => join("\n", @options),
'output_format' => 'HTML',
$report->set_options_from_form();
# add sort to callback
- $form->{callback} = "$callback&sort=" . E($form->{sort});
+ $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
$form->{balance} *= $ml;