and still create credit source records on 4.x+, #42729
authorMark Wells <mark@freeside.biz>
Wed, 21 Sep 2016 21:30:01 +0000 (14:30 -0700)
committerMark Wells <mark@freeside.biz>
Wed, 21 Sep 2016 21:30:01 +0000 (14:30 -0700)
FS/FS/cust_credit.pm
FS/FS/cust_pkg.pm
FS/FS/part_pkg/flat.pm

index 3fea561..aebf259 100644 (file)
@@ -798,6 +798,7 @@ Example:
     'setuprecurs'       => \@setuprecurs,
     'amounts'           => \@amounts,
     'apply'             => 1, #0 leaves the credit unapplied
     'setuprecurs'       => \@setuprecurs,
     'amounts'           => \@amounts,
     'apply'             => 1, #0 leaves the credit unapplied
+    'set_source'        => 1, #creates credit source records for the line items
 
     #the credit
     map { $_ => scalar($cgi->param($_)) }
 
     #the credit
     map { $_ => scalar($cgi->param($_)) }
@@ -862,6 +863,8 @@ sub credit_lineitems {
     $arg{amount} = sprintf('%.2f', $tax_adjust{subtotal} + $tax_adjust{taxtotal});
   }
 
     $arg{amount} = sprintf('%.2f', $tax_adjust{subtotal} + $tax_adjust{taxtotal});
   }
 
+  my $set_source = $arg{set_source};
+
   # create the credit
   my $cust_credit = new FS::cust_credit ( {
     map { $_ => $arg{$_} }
   # create the credit
   my $cust_credit = new FS::cust_credit ( {
     map { $_ => $arg{$_} }
@@ -885,6 +888,7 @@ sub credit_lineitems {
   my %cust_bill_pkg = ();
   my %cust_credit_bill_pkg = ();
   my %unapplied_payments = (); #invoice numbers, and then billpaynums
   my %cust_bill_pkg = ();
   my %cust_credit_bill_pkg = ();
   my %unapplied_payments = (); #invoice numbers, and then billpaynums
+  my %currency;
 
   # little private function to unapply payments from a cust_bill_pkg until
   # there's a specified amount of unpaid balance on it.
 
   # little private function to unapply payments from a cust_bill_pkg until
   # there's a specified amount of unpaid balance on it.
@@ -957,6 +961,17 @@ sub credit_lineitems {
     # unapply payments if necessary
     $error = &{$unapply_sub}($cust_bill_pkg, $setuprecur, $amount);
 
     # unapply payments if necessary
     $error = &{$unapply_sub}($cust_bill_pkg, $setuprecur, $amount);
 
+    if ( $set_source ) {
+      $currency{$invnum} ||= $cust_bill_pkg->cust_bill->currency;
+      my $source = FS::cust_credit_source_bill_pkg->new({
+        'crednum'     => $cust_credit->crednum,
+        'billpkgnum'  => $billpkgnum,
+        'amount'      => $amount,
+        'currency'    => $currency{invnum},
+      });
+      $error ||= $source->insert;
+    }
+
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return "Error unapplying payment: $error";
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return "Error unapplying payment: $error";
@@ -994,6 +1009,19 @@ sub credit_lineitems {
       };
 
     $error = &{$unapply_sub}($cust_bill_pkg, 'setup', $amount);
       };
 
     $error = &{$unapply_sub}($cust_bill_pkg, 'setup', $amount);
+
+    # I guess it's correct to do this for taxes also?
+    if ( $set_source ) {
+      $currency{$invnum} ||= $cust_bill_pkg->cust_bill->currency;
+      my $source = FS::cust_credit_source_bill_pkg->new({
+        'crednum'     => $cust_credit->crednum,
+        'billpkgnum'  => $billpkgnum,
+        'amount'      => $amount,
+        'currency'    => $currency{invnum},
+      });
+      $error ||= $source->insert;
+    }
+
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return "Error unapplying payment: $error";
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return "Error unapplying payment: $error";
index e9d80da..2c75506 100644 (file)
@@ -1859,6 +1859,7 @@ sub credit_remaining {
     'date'        => time,
     'reasonnum'   => $reason->reasonnum,
     'apply'       => 1,
     'date'        => time,
     'reasonnum'   => $reason->reasonnum,
     'apply'       => 1,
+    'set_source'  => 1,
   );
 
   '';
   );
 
   '';
index 84599ea..97d4363 100644 (file)
@@ -225,6 +225,8 @@ sub calc_cancel {
   }
 }
 
   }
 }
 
+# no longer used; see credit_remaining in FS::cust_pkg
+
 sub calc_remain {
   my ($self, $cust_pkg, %options) = @_;
 
 sub calc_remain {
   my ($self, $cust_pkg, %options) = @_;