RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / cust_pay_pending.pm
index 3a8322e..8f313f4 100644 (file)
@@ -215,7 +215,7 @@ sub check {
 
   my $error = 
     $self->ut_numbern('paypendingnum')
-    || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
+    || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
     || $self->ut_money('paid')
     || $self->ut_numbern('_date')
     || $self->ut_textn('payunique')
@@ -235,6 +235,10 @@ sub check {
   ;
   return $error if $error;
 
+  if (!$self->custnum and !$self->get('custnum_pending')) {
+    return 'custnum required';
+  }
+
   $self->_date(time) unless $self->_date;
 
   # UNIQUE index should catch this too, without race conditions, but this
@@ -500,6 +504,16 @@ sub _upgrade_schema {
     )';
   my $sth = dbh->prepare($sql) or die dbh->errstr;
   $sth->execute or die $sth->errstr;
+
+  # fix records where custnum points to a nonexistent customer
+  $sql = 'UPDATE cust_pay_pending SET custnum = NULL
+    WHERE NOT EXISTS (
+      SELECT 1 FROM cust_main WHERE cust_main.custnum = cust_pay_pending.custnum
+    )';
+  $sth = dbh->prepare($sql) or die dbh->errstr;
+  $sth->execute or die $sth->errstr;
+
+
   '';
 }