broadband_nas export, #15284
[freeside.git] / httemplate / search / elements / cust_main_dayranges.html
index 476f138..cc16014 100644 (file)
@@ -11,10 +11,11 @@ Example:
     my( $start, $end ) = @_;
 
     "SQL EXPRESSION BASED ON $start AND $end";
+    # where $start and $end are unix timestamps
   };
 
 </%doc>
-<% include( 'search.html',
+<& search.html,
                  'name'        => 'customers',
                  'query'       => $sql_query,
                  'count_query' => $count_sql,
@@ -24,7 +25,7 @@ Example:
                                     '30-60',
                                     '60-90',
                                     '90+',
-                                    'Total',
+                                    emt('Total'),
                                     @pay_head,
                                   ],
                  'footer'      => [
@@ -67,12 +68,9 @@ Example:
                                     '',
                                     @pay_links,
                                   ],
-                 #'align'       => 'rlccrrrrr',
                  'align'       => FS::UI::Web::cust_aligns(). 
                                    'rrrrr'.
                                   ('c' x @pay_labels),
-                 #'size'        => [ '', '', '-1', '-1', '', '', '', '',  '', ],
-                 #'style'       => [ '', '',  'b',  'b', '', '', '', '', 'b', ],
                  'size'        => [ ( map '', FS::UI::Web::cust_header() ),
                                     #'-1', '', '', '', '',  '', ],
                                     '', '', '', '', '',  '', 
@@ -94,8 +92,7 @@ Example:
                                     ( map '', @pay_labels ),
                                   ],
                %opt,
-             )
-%>
+&>
 <%init>
 
 my %opt = @_;
@@ -117,7 +114,6 @@ if($cgi->param('as_of')) {
   $opt{'title'} .= ' ('.$cgi->param('as_of').')' if $offset > 0;
 }
 
-#my $range_cols = join(',', map &{$range_sub}( @$_ ), @ranges );
 my $range_cols = join(',', map call_range_sub($range_sub, @$_, 'offset' => $offset ), @$ranges );
 
 my $select_count_pkgs = FS::cust_main->select_count_pkgs_sql;
@@ -144,8 +140,12 @@ unless ( $cgi->param('all_customers') ) {
     $days = $1;
   }
 
+  # If this is set, allow cust_main records with nonzero balances
+  my $negative = $cgi->param('negative') || 0;
+
   push @where,
-    call_range_sub($range_sub, $days + $offset, 0, 'no_as'=>1). ' > 0'; # != 0';
+    call_range_sub($range_sub, $days, 0, 'offset' => $offset, 'no_as'=>1). 
+    ($negative ? ' != 0' : ' > 0');
 }
 
 if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
@@ -158,7 +158,6 @@ if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
 #prospect active inactive suspended cancelled
 if ( grep { $cgi->param('status') eq $_ } FS::cust_main->statuses() ) {
   my $method = $cgi->param('status'). '_sql';
-  #push @where, $class->$method();
   push @where, FS::cust_main->$method();
 }
 
@@ -217,7 +216,7 @@ if($opt{'payment_links'} && $curuser->access_right('Process payment') && @payby)
 
   @pay_labels = (map { my $payby = $_; 
                        my $label = $label{$payby};
-                       sub {($payby eq $_[0]->payby) ? "<b>$label</b>" : $label}
+                       sub {($payby eq $_[0]->payby) ? "<b>".emt("$label (on file)")."</b>" : emt($label)}
                      } @payby );
 
   @pay_links = (map { [ "${p}misc/payment.cgi?payby=$_;custnum=", 'custnum' ] }
@@ -248,11 +247,25 @@ my $money_char = $conf->config('money_char') || '$';
 # )
 
 sub call_range_sub {
-  my($range_sub, $start, $end, %opt) = @_;
+  my($range_sub, $startdays, $enddays, %opt) = @_;
+
+  my $as = $opt{'no_as'} ? '' : " AS rangecol_${startdays}_$enddays";
+
+  my $offset = $opt{'offset'} || 0;
+  # Always use $offset - 1day + 1sec = the last second of that day
+  my $cutoff = DateTime->now->set(hour => 23, minute => 59, second => 59);
+  $cutoff->subtract(days => $offset);
 
-  my $as = $opt{'no_as'} ? '' : " AS rangecol_${start}_$end";
+  my $start = $cutoff->clone;
+  $start->subtract(days => $startdays);
+  my $end = $cutoff->clone;
+  $end->subtract(days => $enddays);
 
-  my $sql = &{$range_sub}( $start, $end, $opt{'offset'} ); #%opt?
+  #warn "offset $offset (".$cutoff->epoch."), range $startdays-$enddays (".$start->epoch . '-' . ($enddays ? $end->epoch : '').")\n";
+  my $sql = &{$range_sub}( $start->epoch, 
+                           $enddays ? $end->epoch : '', 
+                           $cutoff->epoch ); #%opt?
 
   $sql = "SUM($sql)" if $opt{'sum'};