+ my %sort_columns = (
+ 'id' => [ qw(id) ],
+ 'transdate' => [ qw(transdate id) ],
+ 'gldate' => [ qw(gldate id) ],
+ 'reference' => [ qw(lower_reference id) ],
+ 'description' => [ qw(lower_description id) ],
+ 'accno' => [ qw(accno transdate id) ],
+ 'department' => [ qw(department transdate id) ],
+ 'transaction_description' => [ qw(lower_transaction_description 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', },
+ 'transaction_description' => { 'gl' => 'g.transaction_description', 'arap' => 'a.transaction_description', },
+ );
+
+ # sortdir = sort direction (ascending or descending)
+ my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
+ my $sortkey = $sort_columns{$form->{sort}} ? $form->{sort} : $form->{datesort}; # default used to be 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);