book closing schema changes
[freeside.git] / FS / FS / cust_bill.pm
index 4ffb8e2..8b326a5 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 );
 
@@ -90,6 +91,8 @@ L<Time::Local> and L<Date::Parse> for conversion functions.
 =item printed - how many times this invoice has been printed automatically
 (see L<FS::cust_main/"collect">).
 
+=item closed - books closed flag, empty or `Y'
+
 =back
 
 =head1 METHODS
@@ -119,7 +122,9 @@ no record you ever posted this invoice (which is bad, no?)
 =cut
 
 sub delete {
-  return "Can't remove invoice!"
+  my $self = shift;
+  return "Can't delete closed invoice" if $self->closed =~ /^Y/i;
+  $self->SUPER::delete(@_);
 }
 
 =item replace OLD_RECORD
@@ -159,6 +164,7 @@ sub check {
     || $self->ut_numbern('_date')
     || $self->ut_money('charged')
     || $self->ut_numbern('printed')
+    || $self->ut_enum('closed', [ '', 'Y' ])
   ;
   return $error if $error;
 
@@ -203,33 +209,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_pay', { 'invnum' => $self->invnum } )
+    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_credit_bill', { 'invnum' => $self->invnum } )
   ;
 }
 
@@ -251,14 +291,18 @@ 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 -= $_->amount foreach ( $self->cust_bill_pay );
+  $balance -= $_->amount foreach ( $self->cust_credited );
+  $balance = sprintf( "%.2f", $balance);
+  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
   $balance;
 }
 
@@ -283,10 +327,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,25 +392,41 @@ sub print_text {
   push @buf,['',''];
 
   #credits
-  foreach ( @cr_cust_credit ) {
+  foreach ( $self->cust_credited ) {
+
+    #something more elaborate if $_->amount ne $_->cust_credit->credited ?
+
+    my $reason = substr($_->cust_credit->reason,0,32);
+    $reason .= '...' if length($reason) < length($_->cust_credit->reason);
+    $reason = " ($reason) " if $reason;
     push @buf,[
-      "Credit #". $_->crednum. " (" . time2str("%x",$_->_date) .")",
-      $money_char. sprintf("%10.2f",$_->credited)
+      "Credit #". $_->crednum. " (". time2str("%x",$_->cust_credit->_date) .")".
+        $reason,
+      $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 ),
-      $money_char. sprintf("%10.2f",$_->paid )
+      "Payment received ". time2str("%x",$_->cust_pay->_date ),
+      $money_char. sprintf("%10.2f",$_->amount )
     ];
   }
 
   #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 +500,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.15 2002-01-28 06:57:23 ivan Exp $
 
 =head1 BUGS
 
@@ -455,8 +514,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