fix payment voiding, fallout from #13971
authorMark Wells <mark@freeside.biz>
Fri, 28 Feb 2014 01:06:41 +0000 (17:06 -0800)
committerMark Wells <mark@freeside.biz>
Fri, 28 Feb 2014 01:06:41 +0000 (17:06 -0800)
FS/FS/Schema.pm
FS/FS/cust_pay.pm
FS/FS/cust_pay_pending.pm
FS/FS/cust_pay_void.pm

index 295ddbd..321fb3f 100644 (file)
@@ -2235,6 +2235,7 @@ sub tables_hashref {
         'gatewaynum',   'int',     'NULL',  '', '', '',
         #'cust_balance', @money_type,            '', '',
         'paynum',       'int',     'NULL',  '', '', '',
+        'void_paynum',  'int',     'NULL',  '', '', '',
         'jobnum',    'bigint',     'NULL',  '', '', '', 
         'invnum',       'int',     'NULL',  '', '', '',
         'manual',       'char',    'NULL',   1, '', '',
@@ -2257,6 +2258,10 @@ sub tables_hashref {
                           { columns    => [ 'paynum' ],
                             table      => 'cust_pay',
                           },
+                          { columns    => [ 'void_paynum' ],
+                            table      => 'cust_pay_void',
+                            references => [ 'paynum' ],
+                          },
                           { columns    => [ 'jobnum' ],
                             table      => 'queue',
                           },
index 63d7c48..0f643c9 100644 (file)
@@ -414,12 +414,17 @@ sub void {
   } );
   $cust_pay_void->reason(shift) if scalar(@_);
   my $error = $cust_pay_void->insert;
-  if ( $error ) {
-    $dbh->rollback if $oldAutoCommit;
-    return $error;
+
+  my $cust_pay_pending =
+    qsearchs('cust_pay_pending', { paynum => $self->paynum });
+  if ( $cust_pay_pending ) {
+    $cust_pay_pending->set('void_paynum', $self->paynum);
+    $cust_pay_pending->set('paynum', '');
+    $error ||= $cust_pay_pending->replace;
   }
 
-  $error = $self->delete;
+  $error ||= $self->delete;
+
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
     return $error;
index f5de73d..63274b1 100644 (file)
@@ -135,6 +135,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.
@@ -224,6 +228,7 @@ 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
index 55b6c67..b2f777b 100644 (file)
@@ -133,12 +133,16 @@ sub unvoid {
     map { $_ => $self->get($_) } fields('cust_pay')
   } );
   my $error = $cust_pay->insert;
-  if ( $error ) {
-    $dbh->rollback if $oldAutoCommit;
-    return $error;
+
+  my $cust_pay_pending =
+    qsearchs('cust_pay_pending', { void_paynum => $self->paynum });
+  if ( $cust_pay_pending ) {
+    $cust_pay_pending->set('paynum', $cust_pay->paynum);
+    $cust_pay_pending->set('void_paynum', '');
+    $error ||= $cust_pay_pending->replace;
   }
 
-  $error = $self->delete;
+  $error ||= $self->delete;
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
     return $error;