RT#7266: aging report "as of" date now limits applied payments
authormark <mark>
Thu, 20 May 2010 01:34:20 +0000 (01:34 +0000)
committermark <mark>
Thu, 20 May 2010 01:34:20 +0000 (01:34 +0000)
FS/FS/cust_bill.pm
FS/FS/cust_credit.pm
FS/FS/cust_main.pm
FS/FS/cust_pay.pm
FS/FS/cust_refund.pm
httemplate/search/report_receivables.cgi

index 9ba2e6e..2c67895 100644 (file)
@@ -4321,7 +4321,10 @@ Returns an SQL fragment to retreive the amount owed (charged minus credited and
 
 sub owed_sql {
   my $class = shift;
-  'charged - '. $class->paid_sql. ' - '. $class->credited_sql;
+  my ($start, $end) = @_;
+  'charged - '. 
+    $class->paid_sql($start, $end). ' - '. 
+    $class->credited_sql($start, $end);
 }
 
 =item net_sql
@@ -4332,7 +4335,8 @@ Returns an SQL fragment to retreive the net amount (charged minus credited).
 
 sub net_sql {
   my $class = shift;
-  'charged - '. $class->credited_sql;
+  my ($start, $end) = @_;
+  'charged - '. $class->credited_sql($start, $end);
 }
 
 =item paid_sql
@@ -4342,9 +4346,11 @@ Returns an SQL fragment to retreive the amount paid against this invoice.
 =cut
 
 sub paid_sql {
-  #my $class = shift;
+  my ($class, $start, $end) = @_;
+  $start &&= "AND cust_bill_pay._date <= $start";
+  $end &&=   "AND cust_bill_pay._date > $end";
   "( SELECT COALESCE(SUM(amount),0) FROM cust_bill_pay
-       WHERE cust_bill.invnum = cust_bill_pay.invnum   )";
+       WHERE cust_bill.invnum = cust_bill_pay.invnum $start $end  )";
 }
 
 =item credited_sql
@@ -4354,9 +4360,11 @@ Returns an SQL fragment to retreive the amount credited against this invoice.
 =cut
 
 sub credited_sql {
-  #my $class = shift;
+  my ($class, $start, $end) = shift;
+  $start &&= "AND cust_credit_bill._date <= $start";
+  $end   &&= "AND cust_credit_bill._date > $end";
   "( SELECT COALESCE(SUM(amount),0) FROM cust_credit_bill
-       WHERE cust_bill.invnum = cust_credit_bill.invnum   )";
+       WHERE cust_bill.invnum = cust_credit_bill.invnum $start $end  )";
 }
 
 =item search_sql_where HASHREF
index d0aa3a4..a4e335e 100644 (file)
@@ -557,17 +557,24 @@ Returns an SQL fragment to retreive the unapplied amount.
 =cut
 
 sub unapplied_sql {
-  #my $class = shift;
+  my ($class, $start, $end) = @_;
+
+  my $bill_start   = $start ? "AND cust_credit_bill._date <= $start"   : '';
+  my $bill_end     = $end   ? "AND cust_credit_bill._date > $end"     : '';
+  my $refund_start = $start ? "AND cust_credit_refund._date <= $start" : '';
+  my $refund_end   = $end   ? "AND cust_credit_refund._date > $end"   : '';
 
   "amount
         - COALESCE(
                     ( SELECT SUM(amount) FROM cust_credit_refund
-                        WHERE cust_credit.crednum = cust_credit_refund.crednum )
+                        WHERE cust_credit.crednum = cust_credit_refund.crednum
+                        $refund_start $refund_end )
                     ,0
                   )
         - COALESCE(
                     ( SELECT SUM(amount) FROM cust_credit_bill
-                        WHERE cust_credit.crednum = cust_credit_bill.crednum )
+                        WHERE cust_credit.crednum = cust_credit_bill.crednum
+                        $bill_start $bill_end )
                     ,0
                   )
   ";
index 88aceb9..267c0d2 100644 (file)
@@ -7392,6 +7392,12 @@ sub credit {
 }
 
 =item charge HASHREF || AMOUNT [ PKG [ COMMENT [ TAXCLASS ] ] ]
+=item cutoff
+
+An absolute cutoff time.  Payments, credits, and refunds I<applied> after this 
+time will be ignored.  Note that START_TIME and END_TIME only limit the date 
+range for invoices and I<unapplied> payments, credits, and refunds.
+
 
 Creates a one-time charge for this customer.  If there is an error, returns
 the error, otherwise returns false.
@@ -8257,10 +8263,12 @@ JOIN clause (typically used with the total option)
 sub balance_date_sql {
   my( $class, $start, $end, %opt ) = @_;
 
-  my $owed         = FS::cust_bill->owed_sql;
-  my $unapp_refund = FS::cust_refund->unapplied_sql;
-  my $unapp_credit = FS::cust_credit->unapplied_sql;
-  my $unapp_pay    = FS::cust_pay->unapplied_sql;
+  my $cutoff = $opt{'cutoff'};
+
+  my $owed         = FS::cust_bill->owed_sql($cutoff);
+  my $unapp_refund = FS::cust_refund->unapplied_sql($cutoff);
+  my $unapp_credit = FS::cust_credit->unapplied_sql($cutoff);
+  my $unapp_pay    = FS::cust_pay->unapplied_sql($cutoff);
 
   my $j = $opt{'join'} || '';
 
index 69bcd87..f9185cc 100644 (file)
@@ -657,17 +657,23 @@ Returns an SQL fragment to retreive the unapplied amount.
 =cut 
 
 sub unapplied_sql {
-  #my $class = shift;
+  my ($class, $start, $end) = shift;
+  my $bill_start   = $start ? "AND cust_bill_pay._date <= $start"   : '';
+  my $bill_end     = $end   ? "AND cust_bill_pay._date > $end"     : '';
+  my $refund_start = $start ? "AND cust_pay_refund._date <= $start" : '';
+  my $refund_end   = $end   ? "AND cust_pay_refund._date > $end"   : '';
 
   "paid
         - COALESCE( 
                     ( SELECT SUM(amount) FROM cust_bill_pay
-                        WHERE cust_pay.paynum = cust_bill_pay.paynum )
+                        WHERE cust_pay.paynum = cust_bill_pay.paynum
+                        $bill_start $bill_end )
                     ,0
                   )
         - COALESCE(
                     ( SELECT SUM(amount) FROM cust_pay_refund
-                        WHERE cust_pay.paynum = cust_pay_refund.paynum )
+                        WHERE cust_pay.paynum = cust_pay_refund.paynum
+                        $refund_start $refund_end )
                     ,0
                   )
   ";
index abc131e..894d190 100644 (file)
@@ -321,17 +321,23 @@ Returns an SQL fragment to retreive the unapplied amount.
 =cut 
 
 sub unapplied_sql {
-  #my $class = shift;
+  my ($class, $start, $end) = shift;
+  my $credit_start = $start ? "AND cust_credit_refund._date <= $start" : '';
+  my $credit_end   = $end   ? "AND cust_credit_refund._date > $end"   : '';
+  my $pay_start    = $start ? "AND cust_pay_refund._date <= $start"    : '';
+  my $pay_end      = $end   ? "AND cust_pay_refund._date > $end"      : '';
 
   "refund
     - COALESCE( 
                 ( SELECT SUM(amount) FROM cust_credit_refund
-                    WHERE cust_refund.refundnum = cust_credit_refund.refundnum )
+                    WHERE cust_refund.refundnum = cust_credit_refund.refundnum
+                    $credit_start $credit_end )
                 ,0
               )
     - COALESCE(
                 ( SELECT SUM(amount) FROM cust_pay_refund
-                    WHERE cust_refund.refundnum = cust_pay_refund.refundnum )
+                    WHERE cust_refund.refundnum = cust_pay_refund.refundnum
+                    $pay_start $pay_end )
                 ,0
               )
   ";
index 73ebee4..3696ed4 100755 (executable)
@@ -16,7 +16,7 @@ die "access denied"
 #Example:
 #
 # my $balance = balance(
-#   $start, $end, 
+#   $start, $end, $offset,
 #   'no_as'  => 1, #set to true when using in a WHERE clause (supress AS clause)
 #                 #or 0 / omit when using in a SELECT clause as a column
 #                 #  ("AS balance_$start_$end")
@@ -44,7 +44,8 @@ sub balance {
 
   #$opt{'unapplied_date'} = 1;
 
-  FS::cust_main->balance_date_sql( $start, $end, 'unapplied_date'=>1,);
+  FS::cust_main->balance_date_sql( $start, $end, 'unapplied_date'=>1,
+           'cutoff' => "( $str2time now() $closing - ".$offset * 86400 . ')' );
 
 }