svc_broadband MAC searches RT#2985
[freeside.git] / FS / FS / cust_credit.pm
index 4c94e15..70d7874 100644 (file)
@@ -262,11 +262,14 @@ methods.
 sub check {
   my $self = shift;
 
+  $self->otaker(getotaker) unless ($self->otaker);
+
   my $error =
     $self->ut_numbern('crednum')
     || $self->ut_number('custnum')
     || $self->ut_numbern('_date')
     || $self->ut_money('amount')
+    || $self->ut_alpha('otaker')
     || $self->ut_textn('reason')
     || $self->ut_foreign_key('reasonnum', 'reason', 'reasonnum')
     || $self->ut_enum('closed', [ '', 'Y' ])
@@ -280,8 +283,6 @@ sub check {
 
   $self->_date(time) unless $self->_date;
 
-  $self->otaker(getotaker);
-
   $self->SUPER::check;
 }
 
@@ -330,15 +331,15 @@ sub cust_credit_bill {
   ;
 }
 
-=item credited
+=item unapplied
 
-Returns the amount of this credit that is still outstanding; which is
+Returns the amount of this credit that is still unapplied/outstanding; 
 amount minus all refund applications (see L<FS::cust_credit_refund>) and
 applications to invoices (see L<FS::cust_credit_bill>).
 
 =cut
 
-sub credited {
+sub unapplied {
   my $self = shift;
   my $amount = $self->amount;
   $amount -= $_->amount foreach ( $self->cust_credit_refund );
@@ -346,6 +347,18 @@ sub credited {
   sprintf( "%.2f", $amount );
 }
 
+=item credited
+
+Deprecated name for the unapplied method.
+
+=cut
+
+sub credited {
+  my $self = shift;
+  #carp "cust_credit->credited deprecated; use ->unapplied";
+  $self->unapplied(@_);
+}
+
 =item cust_main
 
 Returns the customer (see L<FS::cust_main>) for this credit.
@@ -388,6 +401,7 @@ sub reason {
     if (!$reason && $typenum) {
       $reason = new FS::reason( { 'reason_type' => $typenum,
                                   'reason' => $value,
+                                  'disabled' => 'Y', 
                               } );
       $reason->insert and $reason = undef;
     }
@@ -410,17 +424,17 @@ sub reason {
 #
 
 sub _upgrade_data {  # class method
-  my ($self, %opts) = @_;
+  my ($class, %opts) = @_;
 
-  warn "$me upgrading $self\n" if $DEBUG;
+  warn "$me upgrading $class\n" if $DEBUG;
 
-  if (defined dbdef->table($self->table)->column('reason')) {
+  if (defined dbdef->table($class->table)->column('reason')) {
 
     warn "$me Checking for unmigrated reasons\n" if $DEBUG;
 
-    my @cust_credits = qsearch({ 'table' => $self->table,
-                                 'hashref' => {},
-                                 'extrasql' => 'WHERE reason IS NOT NULL',
+    my @cust_credits = qsearch({ 'table'     => $class->table,
+                                 'hashref'   => {},
+                                 'extra_sql' => 'WHERE reason IS NOT NULL',
                               });
 
     if (scalar(grep { $_->getfield('reason') =~ /\S/ } @cust_credits)) {
@@ -430,7 +444,7 @@ sub _upgrade_data {  # class method
       unless ($reason_type) {
         $reason_type  = new FS::reason_type( $hashref );
         my $error   = $reason_type->insert();
-        die "$self had error inserting FS::reason_type into database: $error\n"
+        die "$class had error inserting FS::reason_type into database: $error\n"
           if $error;
       }
 
@@ -439,6 +453,7 @@ sub _upgrade_data {  # class method
                  };
       my $noreason = qsearchs( 'reason', $hashref );
       unless ($noreason) {
+        $hashref->{'disabled'} = 'Y';
         $noreason = new FS::reason( $hashref );
         my $error  = $noreason->insert();
         die "can't insert legacy reason '(none)' into database: $error\n"
@@ -458,7 +473,8 @@ sub _upgrade_data {  # class method
         $cust_credit->setfield('reason', '');
         my $error = $cust_credit->replace;
 
-        die "error inserting $self into database: $error\n"
+        warn "*** WARNING: error replacing reason in $class ".
+             $cust_credit->crednum. ": $error ***\n"
           if $error;
       }
     }
@@ -474,11 +490,10 @@ sub _upgrade_data {  # class method
         unless ($reason_type) {
           $reason_type  = new FS::reason_type( $hashref );
           my $error   = $reason_type->insert();
-          die "$self had error inserting FS::reason_type into database: $error\n"
+          die "$class had error inserting FS::reason_type into database: $error\n"
             if $error;
         }
-                                            # or clause for 1.7.x
-        $conf->set($_, $reason_type->typenum) or die "failed setting config";
+        $conf->set($_, $reason_type->typenum);
       }
     }
 
@@ -489,7 +504,7 @@ sub _upgrade_data {  # class method
     unless ($reason_type) {
       $reason_type  = new FS::reason_type( $hashref );
       my $error   = $reason_type->insert();
-      die "$self had error inserting FS::reason_type into database: $error\n"
+      die "$class had error inserting FS::reason_type into database: $error\n"
         if $error;
     }
 
@@ -524,13 +539,13 @@ sub _upgrade_data {  # class method
 
 =over 4
 
-=item credited_sql
+=item unapplied_sql
 
 Returns an SQL fragment to retreive the unapplied amount.
 
 =cut
 
-sub credited_sql {
+sub unapplied_sql {
   #my $class = shift;
 
   "amount
@@ -548,14 +563,29 @@ sub credited_sql {
 
 }
 
+=item credited_sql
+
+Deprecated name for the unapplied_sql method.
+
+=cut
+
+sub credited_sql {
+  #my $class = shift;
+
+  #carp "cust_credit->credited_sql deprecated; use ->unapplied_sql";
+
+  #$class->unapplied_sql(@_);
+  unapplied_sql();
+}
+
 =back
 
 =head1 BUGS
 
 The delete method.  The replace method.
 
-B<credited> and B<credited_sql> should probably be called B<unapplied> and
-B<unapplied_sql>.
+B<credited> and B<credited_sql> are now called B<unapplied> and
+B<unapplied_sql>.  The old method names should start to give warnings.
 
 =head1 SEE ALSO