sort by clicking on headers, RT#6465
authorivan <ivan>
Thu, 11 Nov 2010 01:44:43 +0000 (01:44 +0000)
committerivan <ivan>
Thu, 11 Nov 2010 01:44:43 +0000 (01:44 +0000)
34 files changed:
httemplate/browse/access_group.html
httemplate/browse/access_user.html
httemplate/browse/agent_type.cgi
httemplate/browse/cust_attachment.html
httemplate/browse/cust_category.html
httemplate/browse/cust_class.html
httemplate/browse/discount.html
httemplate/browse/part_pkg_report_option.html
httemplate/browse/pkg_category.html
httemplate/browse/pkg_class.html
httemplate/browse/rate.cgi
httemplate/browse/svc_acct_pop.cgi
httemplate/browse/usage_class.html
httemplate/search/cust_bill.html
httemplate/search/cust_bill_event.cgi
httemplate/search/cust_bill_pay.html
httemplate/search/cust_bill_pkg.cgi
httemplate/search/cust_bill_pkg_discount.html
httemplate/search/cust_credit.html
httemplate/search/cust_credit_bill.html
httemplate/search/cust_credit_refund.html
httemplate/search/cust_event.html
httemplate/search/cust_pkg_svc.html
httemplate/search/cust_svc.html
httemplate/search/elements/cust_pay_or_refund.html
httemplate/search/elements/search-html.html
httemplate/search/elements/search.html
httemplate/search/pay_batch.cgi
httemplate/search/queue.html
httemplate/search/svc_domain.cgi
httemplate/search/svc_external.cgi
httemplate/search/svc_forward.cgi
httemplate/search/svc_phone.cgi
httemplate/search/svc_www.cgi

index aa9097f..2df0ba0 100644 (file)
@@ -5,7 +5,7 @@
                  'name'        => 'employee groups',
                  'query'       => { 'table'     => 'access_group',
                                     'hashref'   => {},
-                                    'extra_sql' => 'ORDER BY groupname', #??
+                                    'order_by' => 'ORDER BY groupname', #??
                                   },
                  'count_query' => $count_query,
                  'header'      => [ '#',
index 3162e3a..6a1fea1 100644 (file)
@@ -7,7 +7,7 @@
                  'disabled_statuspos' => 2,
                  'query'              => { 'table'     => 'access_user',
                                            'hashref'   => {},
-                                           'extra_sql' => 'ORDER BY last, first'
+                                           'order_by' => 'ORDER BY last, first'
                                          },
                  'count_query'        => $count_query,
                  'header'             => \@header,
index f07a651..1959302 100755 (executable)
@@ -5,7 +5,7 @@
                  'name'        => 'agent types',
                  'query'       => { 'table'     => 'agent_type',
                                     'hashref'   => {},
-                                    'extra_sql' => 'ORDER BY typenum', # 'ORDER BY atype',
+                                    'order_by' => 'ORDER BY typenum', # 'ORDER BY atype',
                                   },
                  'count_query' => $count_query,
                  'header'      => [ '#',
index e19259d..9d62e56 100755 (executable)
@@ -13,7 +13,7 @@
                     , 
                  'query'       => { 'table'     => 'cust_attachment',
                                     'hashref'   => $hashref,
-                                    'extra_sql' => 'ORDER BY '.$orderby,
+                                    'order_by' => 'ORDER BY '.$orderby,
                                   },
                  'count_query' => $count_query,
                  'header' => [ selflink('#',orderby => 'attachnum'),
index 4468d2f..09168ba 100644 (file)
@@ -6,7 +6,7 @@
                  'disabled_statuspos' => 2,
                  'query'       => { 'table'     => 'cust_category',
                                     'hashref'   => {},
-                                    'extra_sql' => 'ORDER BY categorynum',
+                                    'order_by' => 'ORDER BY categorynum',
                                   },
                  'count_query' => $count_query,
                  'header'      => [ '#', 'Category' ],
index da303cf..d7c6228 100644 (file)
@@ -6,7 +6,7 @@
                  'disabled_statuspos' => 2,
                  'query'       => { 'table'     => 'cust_class',
                                     'hashref'   => {},
-                                    'extra_sql' => 'ORDER BY classnum',
+                                    'order_by' => 'ORDER BY classnum',
                                   },
                  'count_query' => $count_query,
                  'header'      => $header,
index f7ef0b3..13dc1e6 100644 (file)
@@ -7,14 +7,13 @@
                  'query'       => { 'table' => 'discount', },
                  'count_query' => 'SELECT COUNT(*) FROM discount',
                  'disableable' => 1,
-                 'disabled_statuspos' => 2,
-                 'header'      => [ '#', 'Name', 'Discount', ],
-                 'fields'      => [ 'discountnum',
-                                    'name',
+                 'disabled_statuspos' => 1,
+                 'header'      => [ 'Name', 'Discount', ],
+                 'fields'      => [ 'name',
                                     'description',
                                   ],
                  'links'       => [ $link,
-                                    $link,
+                                    '',
                                   ],
              )
 %>
index 9745b13..00f2e83 100644 (file)
@@ -6,7 +6,7 @@
                  'disabled_statuspos' => 2,
                  'query'       => { 'table'     => 'part_pkg_report_option',
                                     'hashref'   => {},
-                                    'extra_sql' => 'ORDER BY name',
+                                    'order_by' => 'ORDER BY name',
                                   },
                  'count_query' => 'SELECT COUNT(*) FROM part_pkg_report_option',
                  'header'      => [ '#', 'Class' ],
index 16da230..2c181a3 100644 (file)
@@ -6,7 +6,7 @@
                  'disabled_statuspos' => 3,
                  'query'       => { 'table'     => 'pkg_category',
                                     'hashref'   => {},
-                                    'extra_sql' => 'ORDER BY categorynum',
+                                    'order_by' => 'ORDER BY categorynum',
                                   },
                  'count_query' => $count_query,
                  'header'      => [ '#', 'Category', 'Weight', 'Condense' ],
index 7097c86..97b0621 100644 (file)
@@ -6,7 +6,7 @@
                  'disabled_statuspos' => 2,
                  'query'       => { 'table'     => 'pkg_class',
                                     'hashref'   => {},
-                                    'extra_sql' => 'ORDER BY classnum',
+                                    'order_by' => 'ORDER BY classnum',
                                   },
                  'count_query' => $count_query,
                  'header'      => $header,
index 428158a..c6cfec0 100644 (file)
@@ -9,7 +9,7 @@
               'name'        => 'rate plans',
               'query'       => { 'table'     => 'rate',
                                  'hashref'   => {},
-                                 'extra_sql' => 'ORDER BY ratenum',
+                                 'order_by' => 'ORDER BY ratenum',
                                },
               'count_query' => $count_query,
               'header'      => [ '#',       'Rate plan', 'Rates'    ],
index 501d362..e71a8a7 100755 (executable)
@@ -46,7 +46,7 @@ my $query = { 'select'    => '*,
                              ',
               'table'     => 'svc_acct_pop',
               #'hashref'   => { 'disabled' => '' },
-              'extra_sql' => 'ORDER BY state, city, ac, exch, loc',
+              'order_by' => 'ORDER BY state, city, ac, exch, loc',
             };
 
 my $count_query = "SELECT COUNT(*) FROM svc_acct_pop"; # WHERE DISABLED IS NULL OR DISABLED = ''";
index 75223e0..7e55dba 100644 (file)
@@ -6,7 +6,7 @@
                  'disabled_statuspos' => 2,
                  'query'       => { 'table'     => 'usage_class',
                                     'hashref'   => {},
-                                    'extra_sql' => 'ORDER BY classnum',
+                                    'order_by' => 'ORDER BY classnum',
                                   },
                  'count_query' => 'SELECT COUNT(*) FROM usage_class',
                  'header'      => [ '#',
index cf6ce49..4e40fb0 100755 (executable)
                    sub { time2str('%b %d %Y', shift->_date ) },
                    \&FS::UI::Web::cust_fields,
                  ],
+                 'sort_fields' => [
+                   'COALESCE( agent_invid, invnum )',
+                   FS::cust_bill->owed_sql,
+                   FS::cust_bill->net_sql,
+                   'charged',
+                   '_date',
+                 ],
                  'align' => 'rrrrl'.FS::UI::Web::cust_aligns(),
                  'links' => [
                    $link,
index 16c9acd..90c8913 100644 (file)
@@ -113,7 +113,8 @@ my $sql_query = {
                     FS::UI::Web::cust_sql_fields(),
                   ),
   'hashref'   => {}, 
-  'extra_sql' => "$where ORDER BY _date ASC",
+  'extra_sql' => $where,
+  'order_by'  => 'ORDER BY _date ASC',
   'addl_from' => $join,
 };
 
index 4272d86..1fc8ffd 100644 (file)
                    sub { shift->cust_pay_otaker },
                    \&FS::UI::Web::cust_fields,
                 ],
+                'sort_fields' => [
+                  'amount',
+                  'invnum',
+                  'cust_bill_charged',
+                  'cust_bill_date',
+                  '',
+                  'cust_pay_paid',
+                  'cust_pay_date',
+                  '', 
+                ],
                 'align' => 'rrrrlrrl'.FS::UI::Web::cust_aligns(),
                 'links' => [
                              '',
index f2a5ccd..8b6b0ce 100644 (file)
                    sub { time2str('%b %d %Y', shift->_date ) },
                    \&FS::UI::Web::cust_fields,
                  ],
+                 'sort_fields' => [
+                   'setup', #broken in $unearned case i guess
+                   ( $unearned ? ('', '') : () ),
+                   ( $use_usage eq 'recurring' ? 'recur - usage' :
+                     $use_usage eq 'usage'     ? 'usage'
+                                               : 'recur'
+                   ),
+                   ( $undearned ? ('sdate', 'edate') : () ),
+                   'invnum',
+                   '_date',
+                 ],
                  'links'       => [
                    #'',
                    '',
index b472366..bb8038a 100644 (file)
                    sub { time2str('%b %d %Y', shift->_date ) },
                    \&FS::UI::Web::cust_fields,
                  ],
+                 'sort_fields' => [
+                   '',
+                   'amount',
+                   'months',
+                   'pkg',
+                   'invnum',
+                   '_date',
+                 ],
                  'links'       => [
                    #'',
                    '', #link to customer discount???
index a3b22b1..43f112f 100755 (executable)
@@ -7,6 +7,7 @@
                  #'redirect'    => $link,
                  'header'      => \@header,
                  'fields'      => \@fields,
+                 'sort_fields' => \@sort_fields,
                  'align' => $align,
                  'links' => \@links,
                  'color' => \@color,
@@ -32,12 +33,13 @@ my $clink = sub {
     : '';
 };
 
-my (@header, @fields, $align, @links, @color, @style);
+my (@header, @fields, @sort_fields, $align, @links, @color, @style);
 $align = '';
 
 #amount
 push @header, 'Amount';
 push @fields, sub { $money_char .sprintf('%.2f', shift->amount) };
+push @sort_fields, 'amount';
 $align .= 'r';
 push @links, '';
 push @color, '';
@@ -47,6 +49,7 @@ push @style, '';
 if ($unapplied) {
   push @header, 'Unapplied';
   push @fields, sub { $money_char .sprintf('%.2f', shift->unapplied_amount) };
+  push @sort_fields, '';
   $align .= 'r';
   push @links, '';
   push @color, '';
@@ -63,6 +66,7 @@ push @fields, sub { time2str('%b %d %Y', shift->_date ) },
               'reason',
               \&FS::UI::Web::cust_fields,
               ;
+push @sort_fields, '_date', 'otaker', 'reason';
 $align .= 'rll'.FS::UI::Web::cust_aligns(),
 push @links,  '',
               '',
index 818e603..7f9eb78 100644 (file)
                    sub { shift->cust_credit->reason },
                    \&FS::UI::Web::cust_fields,
                 ],
+                'sort_fields' => [
+                  'amount',
+                  'invnum',
+                  'cust_credit_amount', #?
+                  '', #'otaker' #this is usernum now
+                  '',
+                ],
                 'align' => 'rrrll'.FS::UI::Web::cust_aligns(),
                 'links' => [
                              '',
index d9abe2e..fd87aa5 100644 (file)
                    sub { shift->cust_refund->otaker },
                    \&FS::UI::Web::cust_fields,
                 ],
+                'sort_fields' => [
+                  'amount',
+                  '', #cust_credit_amount?  cust_credit_date? prolly split field
+                  '', #_date?  cust_refund_refund?  also split
+                ],
                 'align' => 'rrrl'.FS::UI::Web::cust_aligns(),
                 'links' => [
                              '',
index a0429e4..503f252 100644 (file)
@@ -188,7 +188,8 @@ my $sql_query = {
                     FS::UI::Web::cust_sql_fields(),
                   ),
   'hashref'   => {}, 
-  'extra_sql' => "$where ORDER BY _date ASC",
+  'extra_sql' => $where,
+  'order_by'  => 'ORDER BY _date ASC',
   'addl_from' => $join,
 };
 
index 4f27d66..9c5b32f 100644 (file)
@@ -78,7 +78,8 @@ my $sql_query = {
   'table'      => 'cust_svc',
   'addl_from'  => $addl_from,
   'hashref'    => {},
-  'extra_sql'  => "$extra_sql $orderby",
+  'extra_sql'  => $extra_sql,
+  'order_by'   => $orderby,
 };
 
 #warn Dumper($sql_query)."\n";
index 2c17561..a2cdc8a 100644 (file)
@@ -110,7 +110,8 @@ my $sql_query = {
   'table'      => 'cust_svc',
   'addl_from'  => $addl_from,
   'hashref'    => {},
-  'extra_sql'  => "$extra_sql $orderby",
+  'extra_sql'  => $extra_sql,
+  'order_by'   => $orderby,
 };
 
 my $count_query = "SELECT COUNT(*) FROM cust_svc $addl_from $extra_sql";
index 6f4aaf8..fccb9ee 100755 (executable)
@@ -48,6 +48,7 @@ Examples:
                 'redirect_empty' => $opt{'redirect_empty'},
                 'header'         => \@header,
                 'fields'         => \@fields,
+                'sort_fields'    => \@sort_fields,
                 'align'          => $align,
                 'links'          => \@links,
                 'color'          => \@color,
@@ -135,6 +136,7 @@ if ( $cgi->param('tax_names') ) {
 
 my @header = ();
 my @fields = ();
+my @sort_fields = ();
 my $align = '';
 my @links = ();
 if ( $opt{'pre_header'} ) {
@@ -142,6 +144,7 @@ if ( $opt{'pre_header'} ) {
   $align .= 'c' x scalar(@{ $opt{'pre_header'} });
   push @links, map '', @{ $opt{'pre_header'} };
   push @fields, @{ $opt{'pre_fields'} };
+  push @sort_fields, @{ $opt{'pre_fields'} };
 }
 
 push @header, "\u$name_singular",
@@ -152,18 +155,21 @@ push @links, '', '';
 push @fields, 'payby_payinfo_pretty',
               sub { sprintf('$%.2f', shift->$amount_field() ) },
 ;
+push @sort_fields, '', $amount_field;
 
 if ( $unapplied ) {
   push @header, 'Unapplied';
   $align .= 'r';
   push @links, '';
   push @fields, sub { sprintf('$%.2f', shift->unapplied_amount) };
+  push @sort_fields, '';
 }
 
 push @header, 'Date';
 $align .= 'r';
 push @links, '';
 push @fields, sub { time2str('%b %d %Y', shift->_date ) };
+push @sort_fields, '_date';
 
 unless ( $opt{'disable_by'} ) {
   push @header, 'By';
@@ -414,7 +420,8 @@ if ( $cgi->param('magic') ) {
     'table'     => $table,
     'select'    => join(', ', @select),
     'hashref'   => {},
-    'extra_sql' => "$search $group_by ORDER BY $orderby",
+    'extra_sql' => "$search $group_by",
+    'order_by'  => "ORDER BY $orderby",
     'addl_from' => $addl_from,
   };
 
index 98f9c44..d6cc626 100644 (file)
                 <TR>
 %                 my $h2 = 0;
 %                 my $colspan = 0;
+%                 my @fields = @{ $opt{'sort_fields'} || $opt{'fields'}};
+%                 my $order_by = $cgi->param('order_by');
 %                 foreach my $header ( @{ $opt{header} } ) { 
+%
+%                   my $field = shift @fields;
+%
 %                   $colspan-- if $colspan > 0;
 %                   next if $colspan;
 %
 %                   my $label = ref($header) ? $header->{label} : $header;
+%                   unless ( ref($field) || !$field ) {
+%                     if ( $order_by eq $field ) {
+%                       $cgi->param('order_by', "$field DESC");
+%                     } else {
+%                       $cgi->param('order_by', $field);
+%                     }
+%                     $label = qq(<A HREF="$self_url?). $cgi->query_string.
+%                              qq(">$label</A>);
+%                   }
+%
 %                   $colspan = ref($header) ? $header->{colspan} : 0;
 %                   my $rowspan = 1;
 %                   my $style = '';
index 2188169..a8e9f08 100644 (file)
@@ -132,7 +132,10 @@ Example:
                                   # qsearch hashref and header & fields need to
                                   # be defined)
 
-    # link & display properties for fields
+    # sort, link & display properties for fields
+
+    'sort_fields' => [], #optional list of field names or SQL expressions for
+                         # sorts
    
     #listref - each item is the empty string,
     #          or a listref of link and method name to append,
@@ -369,6 +372,13 @@ unless ( $type =~ /^(csv|\w*.xls)$/) {
 
 }
 
+#order by override
+my $order_by = '';
+#if ( $cgi->param('order_by') =~ /^([\w\, ]+)$/ ) {
+#  $order_by = $1;
+#}
+$order_by = $cgi->param('order_by') if $cgi->param('order_by');
+
 # run the query
 
 my $header = [ map { ref($_) ? $_->{'label'} : $_ } @{$opt{header}} ];
@@ -378,6 +388,20 @@ if ( ref($opt{query}) ) {
   my @query;
   if (ref($opt{query}) eq 'HASH') {
     @query = ( $opt{query} );
+
+    if ( $order_by ) {
+      if ( $opt{query}->{'order_by'} ) {
+        if ( $opt{query}->{'order_by'} =~ /^(\s*ORDER\s+BY\s+)?(\S.*)$/is ) {
+          $opt{query}->{'order_by'} = "ORDER BY $order_by, $2";
+        } else {
+          warn "unparsable query order_by: ". $opt{query}->{'order_by'};
+          die "unparsable query order_by: ". $opt{query}->{'order_by'};
+        }
+      } else {
+        $opt{query}->{'order_by'} = "ORDER BY $order_by";
+      }
+    }
+
   } elsif (ref($opt{query}) eq 'ARRAY') {
     @query = @{ $opt{query} };
   } else {
index ebd323e..6d571b4 100755 (executable)
@@ -3,7 +3,8 @@
                 'name_singular' => 'batch',
                 'query'         => { 'table'     => 'pay_batch',
                                      'hashref'   => $hashref,
-                                     'extra_sql' => "$extra_sql ORDER BY batchnum DESC",
+                                     'extra_sql' => $extra_sql,
+                                      'order_by'  => 'ORDER BY batchnum DESC',
                                    },
                 'count_query'   => "$count_query $extra_sql",
                 'header'        => [ 'Batch',
index e5f7aed..1c12470 100644 (file)
@@ -4,7 +4,7 @@
                 'html_form'   => qq!<FORM NAME="jobForm" ACTION="$p/misc/queue.cgi" METHOD="POST">!,
                  'query'       => { 'table'     => 'queue',
                                     'hashref'   => $hashref,
-                                    'extra_sql' => 'ORDER BY jobnum',
+                                    'order_by' => 'ORDER BY jobnum',
                                   },
                  'count_query' => $count_query,
                  'header' => [ '#',
index 08ffdba..9827b8d 100755 (executable)
@@ -97,7 +97,8 @@ my $sql_query = {
                    'cust_main.custnum',
                    FS::UI::Web::cust_sql_fields(),
                  ),
-  'extra_sql' => "$extra_sql $orderby",
+  'extra_sql' => $extra_sql,
+  'order_by'  => $orderby,
   'addl_from' => $addl_from,
 };
 
index f061754..cb51d44 100755 (executable)
@@ -121,7 +121,8 @@ my $sql_query = {
                    'cust_main.custnum',
                    FS::UI::Web::cust_sql_fields(),
                  ),
-  'extra_sql' => "$extra_sql $orderby",
+  'extra_sql' => $extra_sql,
+  'order_by'  => $orderby,
   'addl_from' => $addl_from,
 };
 
index 2bcd0c8..f17f131 100755 (executable)
@@ -89,7 +89,8 @@ my $sql_query = {
                    'cust_main.custnum',
                    FS::UI::Web::cust_sql_fields(),
                  ),
-  'extra_sql' => "$extra_sql $orderby",
+  'extra_sql' => $extra_sql,
+  'order_by'  => $orderby,
   'addl_from' => $addl_from,
 };
 
index 0ad458b..2943408 100644 (file)
@@ -161,7 +161,8 @@ my $sql_query = {
                    'cust_main.custnum',
                    FS::UI::Web::cust_sql_fields(),
                  ),
-  'extra_sql' => "$extra_sql $orderby",
+  'extra_sql' => $extra_sql,
+  'order_by'  => $orderby,
   'addl_from' => $addl_from,
 };
 
index 2e3c461..adc31c8 100755 (executable)
@@ -96,7 +96,8 @@ my $sql_query = {
                    'cust_main.custnum',
                    FS::UI::Web::cust_sql_fields(),
                  ),
-  'extra_sql' => "$extra_sql $orderby",
+  'extra_sql' => $extra_sql,
+  'order_by'  => $orderby,
   'addl_from' => $addl_from,
 };