cleanup
[freeside.git] / FS / FS / cust_bill_pkg.pm
index 7406eda..e866a3f 100644 (file)
@@ -360,7 +360,7 @@ sub regularize_details {
         # then turn it into one
         my %hash = ();
         if ( ! ref($detail) ) {
-          $hash{'detail'} = 'detail';
+          $hash{'detail'} = $detail;
         }
         elsif ( ref($detail) eq 'HASH' ) {
           %hash = %$detail;
@@ -893,7 +893,7 @@ sub usage {
 
   if ( $self->get('details') ) {
 
-    return sum( 
+    return sum( 0, 
       map { $_->amount || 0 }
       grep { !defined($classnum) or $classnum eq $_->classnum }
       @{ $self->get('details') }
@@ -908,7 +908,7 @@ sub usage {
     my $sth = dbh->prepare($sql) or die dbh->errstr;
     $sth->execute or die $sth->errstr;
 
-    return $sth->fetchrow_arrayref->[0];
+    return $sth->fetchrow_arrayref->[0] || 0;
 
   }
 
@@ -955,8 +955,6 @@ sub cust_bill_pkg_display {
   my $default =
     new FS::cust_bill_pkg_display { billpkgnum =>$self->billpkgnum };
 
-  return ( $default ) unless defined dbdef->table('cust_bill_pkg_display');#hmmm
-
   my $type = $opt{type} if exists $opt{type};
   my @result;
 
@@ -1043,26 +1041,95 @@ sub cust_bill_pkg_discount {
 
 =cut
 
-sub recur_show_zero {
-  #my $self = shift;
-  #   $self->recur == 0
-  #&& $self->pkgnum
-  #&& $self->cust_pkg->part_pkg->recur_show_zero;
+sub recur_show_zero { shift->_X_show_zero('recur'); }
+sub setup_show_zero { shift->_X_show_zero('setup'); }
+
+sub _X_show_zero {
+  my( $self, $what ) = @_;
+
+  return 0 unless $self->$what() == 0 && $self->pkgnum;
+
+  $self->cust_pkg->_X_show_zero($what);
+}
+
+=back
+
+=head1 CLASS METHODS
+
+=over 4
+
+=item owed_sql [ BEFORE, AFTER, OPTIONS ]
+
+Returns an SQL expression for the amount owed.  BEFORE and AFTER specify
+a date window.  OPTIONS may include 'no_usage' (excludes usage charges)
+and 'setuprecur' (set to "setup" or "recur" to limit to one or the other).
+
+=cut
+
+sub owed_sql {
+  my ($class, $start, $end, %opt) = @_;
+  my $charged = 
+    $opt{setuprecur} =~ /^s/ ? 'cust_bill_pkg.setup' :
+    $opt{setuprecur} =~ /^r/ ? 'cust_bill_pkg.recur' :
+    'cust_bill_pkg.setup + cust_bill_pkg.recur';
 
-  shift->_X_show_zero('recur');
+  if ( $opt{no_usage} ) {
+    $charged .= ' - ' . $class->usage_sql;
+  }
 
+  '(' . $charged . 
+  ' - ' . $class->paid_sql($start, $end, %opt) .
+  ' - ' . $class->credited_sql($start, $end, %opt) . ')'
 }
 
-sub setup_show_zero {
-  shift->_X_show_zero('setup');
+=item usage_sql
+
+Returns an SQL expression for the total usage charges in details on
+an item.
+
+=cut
+
+sub usage_sql {
+  my $class = shift;
+  "(SELECT COALESCE(SUM(cust_bill_pkg_detail.amount),0) 
+    FROM cust_bill_pkg_detail 
+    WHERE cust_bill_pkg_detail.billpkgnum = cust_bill_pkg.billpkgnum)"
 }
 
-sub _X_show_zero {
-  my( $self, $what ) = @_;
+=item paid_sql [ BEFORE, AFTER, OPTIONS ]
 
-  return 0 unless $self->$what() == 0 && $self->pkgnum;
+Returns an SQL expression for the sum of payments applied to this item.
 
-  $self->cust_pkg->_X_show_zero($what);
+=cut
+
+sub paid_sql {
+  my ($class, $start, $end, %opt) = @_;
+  $start = $start ? "AND cust_bill_pay._date <= $start" : '';
+  $end   = $end   ? "AND cust_bill_pay._date >  $end"   : '';
+  my $setuprecur = 
+    $opt{setuprecur} =~ /^s/ ? 'setup' :
+    $opt{setuprecur} =~ /^r/ ? 'recur' :
+    '';
+  $setuprecur &&= "AND setuprecur = '$setuprecur'";
+  "( SELECT COALESCE(SUM(cust_bill_pay_pkg.amount),0)
+     FROM cust_bill_pay_pkg JOIN cust_bill_pay USING (billpaynum)
+     WHERE cust_bill_pay_pkg.billpkgnum = cust_bill_pkg.billpkgnum
+           $start $end $setuprecur )";
+}
+
+sub credited_sql {
+  my ($class, $start, $end, %opt) = @_;
+  $start = $start ? "AND cust_credit_bill._date <= $start" : '';
+  $end   = $end   ? "AND cust_credit_bill._date >  $end"   : '';
+  my $setuprecur = 
+    $opt{setuprecur} =~ /^s/ ? 'setup' :
+    $opt{setuprecur} =~ /^r/ ? 'recur' :
+    '';
+  $setuprecur &&= "AND setuprecur = '$setuprecur'";
+  "( SELECT COALESCE(SUM(cust_credit_bill_pkg.amount),0)
+     FROM cust_credit_bill_pkg JOIN cust_credit_bill USING (creditbillnum)
+     WHERE cust_credit_bill_pkg.billpkgnum = cust_bill_pkg.billpkgnum
+           $start $end $setuprecur )";
 }
 
 =back