deprecate cust_bill-latex_lineitem_maxlength as it is irrelevant with new invoice_lat...
[freeside.git] / FS / FS / cust_pay_pending.pm
index 8e29f08..4874802 100644 (file)
@@ -12,6 +12,7 @@ use FS::cust_pay;
 @ISA = qw( FS::payinfo_transaction_Mixin FS::cust_main_Mixin FS::Record );
 
 @encrypted_fields = ('payinfo');
+sub nohistory_fields { ('payinfo'); }
 
 =head1 NAME
 
@@ -132,6 +133,10 @@ L<FS::payment_gateway> id.
 
 Payment number (L<FS::cust_pay>) of the completed payment.
 
+=item void_paynum
+
+Payment number of the payment if it's been voided.
+
 =item invnum
 
 Invoice number (L<FS::cust_bill>) to try to apply this payment to.
@@ -208,7 +213,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')
@@ -220,12 +225,17 @@ sub check {
     || $self->ut_foreign_keyn('paynum', 'cust_pay', 'paynum' )
     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
     || $self->ut_foreign_keyn('invnum', 'cust_bill', 'invnum')
+    || $self->ut_foreign_keyn('void_paynum', 'cust_pay_void', 'paynum' )
     || $self->ut_flag('manual')
     || $self->ut_numbern('discount_term')
     || $self->payinfo_check() #payby/payinfo/paymask/paydate
   ;
   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
@@ -445,6 +455,26 @@ sub decline {
   $self->replace;
 }
 
+=item reverse [ STATUSTEXT ]
+
+Sets the status of this pending payment to "done" (with statustext
+"reversed (manual)" unless otherwise specified).
+
+Currently only used when resolving pending payments manually.
+
+=cut
+
+# almost complete false laziness with decline,
+# but want to avoid confusion, in case any additional steps/defaults are ever added to either
+sub reverse {
+  my $self = shift;
+  my $statustext = shift || "reversed (manual)";
+
+  $self->status('done');
+  $self->statustext($statustext);
+  $self->replace;
+}
+
 # _upgrade_data
 #
 # Used by FS::Upgrade to migrate to a new database.
@@ -458,6 +488,33 @@ sub _upgrade_data {  #class method
   my $sth = dbh->prepare($sql) or die dbh->errstr;
   $sth->execute or die $sth->errstr;
 
+  # For cust_pay_pending records linked to voided payments, move the paynum
+  # to void_paynum.
+  $sql =
+    "UPDATE cust_pay_pending SET void_paynum = paynum, paynum = NULL 
+    WHERE paynum IS NOT NULL AND void_paynum IS NULL AND EXISTS(
+      SELECT 1 FROM cust_pay_void
+      WHERE cust_pay_void.paynum = cust_pay_pending.paynum
+    ) AND NOT EXISTS(
+      SELECT 1 FROM cust_pay
+      WHERE cust_pay.paynum = cust_pay_pending.paynum
+    )";
+  $sth = dbh->prepare($sql) or die dbh->errstr;
+  $sth->execute or die $sth->errstr;
+
+}
+
+sub _upgrade_schema {
+  my ($class, %opts) = @_;
+
+  # fix records where jobnum points to a nonexistent queue job
+  my $sql = 'UPDATE cust_pay_pending SET jobnum = NULL
+    WHERE NOT EXISTS (
+      SELECT 1 FROM queue WHERE queue.jobnum = cust_pay_pending.jobnum
+    )';
+  my $sth = dbh->prepare($sql) or die dbh->errstr;
+  $sth->execute or die $sth->errstr;
+  '';
 }
 
 =back