more udpates for the new world of unapplied stuff. yay.
[freeside.git] / FS / FS / cust_bill.pm
index 4ffb8e2..2ca4b52 100644 (file)
@@ -11,6 +11,7 @@ use FS::cust_bill_pkg;
 use FS::cust_credit;
 use FS::cust_pay;
 use FS::cust_pkg;
+use FS::cust_credit_bill;
 
 @ISA = qw( FS::Record );
 
@@ -203,33 +204,67 @@ sub cust_bill_pkg {
 
 =item cust_credit
 
-Returns a list consisting of the total previous credited (see
-L<FS::cust_credit>) for this customer, followed by the previous outstanding
-credits (FS::cust_credit objects).
+Depreciated.  See the cust_credited method.
+
+ #Returns a list consisting of the total previous credited (see
+ #L<FS::cust_credit>) and unapplied for this customer, followed by the previous
+ #outstanding credits (FS::cust_credit objects).
 
 =cut
 
 sub cust_credit {
-  my $self = shift;
-  my $total = 0;
-  my @cust_credit = sort { $a->_date <=> $b->_date }
-    grep { $_->credited != 0 && $_->_date < $self->_date }
-      qsearch('cust_credit', { 'custnum' => $self->custnum } )
-  ;
-  foreach (@cust_credit) { $total += $_->credited; }
-  $total, @cust_credit;
+  use Carp;
+  croak "FS::cust_bill->cust_credit depreciated; see ".
+        "FS::cust_bill->cust_credit_bill";
+  #my $self = shift;
+  #my $total = 0;
+  #my @cust_credit = sort { $a->_date <=> $b->_date }
+  #  grep { $_->credited != 0 && $_->_date < $self->_date }
+  #    qsearch('cust_credit', { 'custnum' => $self->custnum } )
+  #;
+  #foreach (@cust_credit) { $total += $_->credited; }
+  #$total, @cust_credit;
 }
 
 =item cust_pay
 
-Returns all payments (see L<FS::cust_pay>) for this invoice.
+Depreciated.  See the cust_bill_pay method.
+
+#Returns all payments (see L<FS::cust_pay>) for this invoice.
 
 =cut
 
 sub cust_pay {
+  use Carp;
+  croak "FS::cust_bill->cust_pay depreciated; see FS::cust_bill->cust_bill_pay";
+  #my $self = shift;
+  #sort { $a->_date <=> $b->_date }
+  #  qsearch( 'cust_pay', { 'invnum' => $self->invnum } )
+  #;
+}
+
+=item cust_bill_pay
+
+Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice.
+
+=cut
+
+sub cust_bill_pay {
+  my $self = shift;
+  sort { $a->_date <=> $b->date }
+    qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum } );
+}
+
+=item cust_credited
+
+Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice.
+
+=cut
+
+sub cust_credited {
   my $self = shift;
   sort { $a->_date <=> $b->_date }
-    qsearch( 'cust_pay', { 'invnum' => $self->invnum } )
+    qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum } )
   ;
 }
 
@@ -251,15 +286,17 @@ sub tax {
 =item owed
 
 Returns the amount owed (still outstanding) on this invoice, which is charged
-minus all payments (see L<FS::cust_pay>).
+minus all payment applications (see L<FS::cust_bill_pay>) and credit
+applications (see L<FS::cust_credit_bill>).
 
 =cut
 
 sub owed {
   my $self = shift;
   my $balance = $self->charged;
-  $balance -= $_->paid foreach ( $self->cust_pay );
-  $balance;
+  $balance -= $_->amount foreach ( $self->cust_bill_pay );
+  $balance -= $_->amount foreach ( $self->cust_credited );
+  $balance = sprintf( "%.2f", $balance);
 }
 
 =item print_text [TIME];
@@ -283,10 +320,9 @@ sub print_text {
     unless $cust_main->payname;
 
   my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
-  my( $cr_total, @cr_cust_credit ) = $self->cust_credit; #credits
-  my $balance_due = $self->owed + $pr_total - $cr_total;
-
-  #
+#  my( $cr_total, @cr_cust_credit ) = $self->cust_credit; #credits
+  #my $balance_due = $self->owed + $pr_total - $cr_total;
+  my $balance_due = $self->owed + $pr_total;
 
   #my @collect = ();
   #my($description,$amount);
@@ -349,17 +385,29 @@ sub print_text {
   push @buf,['',''];
 
   #credits
-  foreach ( @cr_cust_credit ) {
+  foreach ( $self->cust_credited ) {
+
+    #something more elaborate if $_->amount ne $_->cust_credit->credited ?
+
     push @buf,[
-      "Credit #". $_->crednum. " (" . time2str("%x",$_->_date) .")",
-      $money_char. sprintf("%10.2f",$_->credited)
+      "Credit #". $_->crednum. " (". time2str("%x",$_->cust_credit->_date) .")",
+      $money_char. sprintf("%10.2f",$_->amount)
     ];
   }
+  #foreach ( @cr_cust_credit ) {
+  #  push @buf,[
+  #    "Credit #". $_->crednum. " (" . time2str("%x",$_->_date) .")",
+  #    $money_char. sprintf("%10.2f",$_->credited)
+  #  ];
+  #}
 
   #get & print payments
-  foreach ( $self->cust_pay ) {
+  foreach ( $self->cust_bill_pay ) {
+
+    #something more elaborate if $_->amount ne ->cust_pay->paid ?
+
     push @buf,[
-      "Payment received ". time2str("%x",$_->_date ),
+      "Payment received ". time2str("%x",$_->cust_pay->_date ),
       $money_char. sprintf("%10.2f",$_->paid )
     ];
   }
@@ -367,7 +415,7 @@ sub print_text {
   #balance due
   push @buf,['','-----------'];
   push @buf,['Balance Due', $money_char. 
-    sprintf("%10.2f",$self->owed + $pr_total - $cr_total ) ];
+    sprintf("%10.2f", $balance_due ) ];
 
   #setup template variables
   
@@ -441,7 +489,7 @@ sub print_text {
 
 =head1 VERSION
 
-$Id: cust_bill.pm,v 1.8 2001-08-03 20:34:28 jeff Exp $
+$Id: cust_bill.pm,v 1.10 2001-09-02 01:27:10 ivan Exp $
 
 =head1 BUGS
 
@@ -455,8 +503,9 @@ or something similar so the look can be completely customized?)
 
 =head1 SEE ALSO
 
-L<FS::Record>, L<FS::cust_main>, L<FS::cust_pay>, L<FS::cust_bill_pkg>,
-L<FS::cust_credit>, schema.html from the base documentation.
+L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill_pay>, L<FS:;cust_pay>,
+L<FS::cust_bill_pkg>, L<FS::cust_bill_credit>, schema.html from the base
+documentation.
 
 =cut