my $range_sub = delete($opt{'range_sub'}); #or die
+my $offset = 0;
+if($cgi->param('as_of')) {
+ $offset = int((time - str2time($cgi->param('as_of'))) / 86400);
+ $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, @$_ ), @$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;
my @where = ();
unless ( $cgi->param('all_customers') ) {
-
+# Exclude entire cust_main records where the balance is >0
my $days = 0;
if ( $cgi->param('days') =~ /^\s*(\d+)\s*$/ ) {
$days = $1;
}
push @where,
- call_range_sub($range_sub, $days, 0, 'no_as'=>1). ' > 0'; # != 0';
+ call_range_sub($range_sub, $days + $offset, 0, 'no_as'=>1). ' > 0'; # != 0';
}
if ( $cgi->param('agentnum') =~ /^(\d+)$/ ) {
my $total_sql =
"SELECT ".
- join(',', map call_range_sub( $range_sub, @$_, 'sum'=>1 ), @$ranges).
+ join(',', map call_range_sub( $range_sub, @$_, 'offset' => $offset, 'sum'=>1 ), @$ranges).
" FROM cust_main $where";
my $total_sth = dbh->prepare($total_sql) or die dbh->errstr;
my $as = $opt{'no_as'} ? '' : " AS rangecol_${start}_$end";
- my $sql = &{$range_sub}( $start, $end ); #%opt?
+ my $sql = &{$range_sub}( $start, $end, $opt{'offset'} ); #%opt?
$sql = "SUM($sql)" if $opt{'sum'};
# )
sub balance {
- my($start, $end) = @_; #, %opt ?
-
+ my($start, $end, $offset) = @_; #, %opt ?
#handle start and end ranges (86400 = 24h * 60m * 60s)
my $str2time = str2time_sql;
my $closing = str2time_sql_closing;
- $start = $start ? "( $str2time now() $closing - ".($start * 86400). ' )' : '';
- $end = $end ? "( $str2time now() $closing - ".($end * 86400). ' )' : '';
+
+ # $end == 0 means "+infinity", while $start == 0 really means 0
+ # so we should always include a start condition
+ $start = "( $str2time now() $closing - ". ($start + $offset) * 86400 . ' )';
+ # but only include an end condition if $end != 0
+ $end = $end ?
+ "( $str2time now() $closing - ". ($end + $offset) * 86400 . ' )'
+ : '';
#$opt{'unapplied_date'} = 1;
<INPUT TYPE="radio" NAME="all_customers" VALUE="0" CHECKED onClick="if ( ! this.checked ) { document.OneTrueForm.days.disabled=true; document.OneTrueForm.days.style.backgroundColor = '#dddddd'; } else { document.OneTrueForm.days.disabled=false; document.OneTrueForm.days.style.backgroundColor = '#ffffff'; }">Customers with a balance over <INPUT NAME="days" TYPE="text" SIZE=4 MAXLENGTH=3 VALUE="0"> days old
</TD>
</TR>
+ <% include( '/elements/tr-input-date-field.html', {
+ 'name' => 'as_of',
+ 'value' => time,
+ 'label' => 'As of date ',
+ 'format' => FS::Conf->new->config('date_format') || '%m/%d/%Y',
+ } ) %>
</TABLE>
<INPUT TYPE="radio" NAME="all_customers" VALUE="0" CHECKED onClick="if ( ! this.checked ) { document.OneTrueForm.days.disabled=true; document.OneTrueForm.days.style.backgroundColor = '#dddddd'; } else { document.OneTrueForm.days.disabled=false; document.OneTrueForm.days.style.backgroundColor = '#ffffff'; }">Customers with unapplied payments over <INPUT NAME="days" TYPE="text" SIZE=4 MAXLENGTH=3 VALUE="0"> days old
</TD>
</TR>
+ <% include( '/elements/tr-input-date-field.html', {
+ 'name' => 'as_of',
+ 'value' => time,
+ 'label' => 'As of date ',
+ 'format' => FS::Conf->new->config('date_format') || '%m/%d/%Y',
+ } ) %>
</TABLE>
<%once>
sub unapplied_payments {
- my($start, $end, %opt) = @_;
+ my($start, $end, $offset) = @_;
#handle start and end ranges (86400 = 24h * 60m * 60s)
my $str2time = str2time_sql;
my $closing = str2time_sql_closing;
- $start = $start ? "( $str2time now() $closing - ".($start * 86400). ' )' : '';
- $end = $end ? "( $str2time now() $closing - ".($end * 86400). ' )' : '';
+ $start = "( $str2time now() $closing - ".($start + $offset) * 86400 . ' )';
+ $end = $end ?
+ "( $str2time now() $closing - ".($end + $offset) * 86400 . ' )'
+ : '';
FS::cust_main->unapplied_payments_date_sql( $start, $end );