RT#38671: Do not include charges and credits from failed signup processing [v4 only...
[freeside.git] / FS / FS / cust_bill_pkg.pm
index 8233ce0..e7860e0 100644 (file)
@@ -26,6 +26,8 @@ use FS::cust_bill_pkg_tax_location_void;
 use FS::cust_bill_pkg_tax_rate_location_void;
 use FS::cust_tax_exempt_pkg_void;
 use FS::cust_bill_pkg_fee_void;
+use FS::reason;
+use FS::reason_type;
 
 use FS::Cursor;
 
@@ -322,7 +324,7 @@ sub insert {
 
 }
 
-=item void
+=item void [ REASON ]
 
 Voids this line item: deletes the line item and adds a record of the voided
 line item to the FS::cust_bill_pkg_void table (and related tables).
@@ -333,6 +335,14 @@ sub void {
   my $self = shift;
   my $reason = scalar(@_) ? shift : '';
 
+  unless (ref($reason) || !$reason) {
+    $reason = FS::reason->new_or_existing(
+      'class'  => 'I',
+      'type'   => 'Invoice void',
+      'reason' => $reason
+    );
+  }
+
   local $SIG{HUP} = 'IGNORE';
   local $SIG{INT} = 'IGNORE';
   local $SIG{QUIT} = 'IGNORE';
@@ -347,7 +357,7 @@ sub void {
   my $cust_bill_pkg_void = new FS::cust_bill_pkg_void ( {
     map { $_ => $self->get($_) } $self->fields
   } );
-  $cust_bill_pkg_void->reason($reason);
+  $cust_bill_pkg_void->reasonnum($reason->reasonnum) if $reason;
   my $error = $cust_bill_pkg_void->insert;
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
@@ -820,6 +830,8 @@ quantity.
 
 sub _item_discount {
   my $self = shift;
+  my %options = @_;
+
   my @pkg_discounts = $self->pkg_discount;
   return if @pkg_discounts == 0;
   # special case: if there are old "discount details" on this line item, don't
@@ -832,7 +844,8 @@ sub _item_discount {
   my $d = {
     _is_discount    => 1,
     description     => $self->mt('Discount'),
-    amount          => 0,
+    setup_amount    => 0,
+    recur_amount    => 0,
     ext_description => \@ext,
     pkgpart         => $self->pkgpart,
     feepart         => $self->feepart,
@@ -840,9 +853,11 @@ sub _item_discount {
   };
   foreach my $pkg_discount (@pkg_discounts) {
     push @ext, $pkg_discount->description;
-    $d->{amount} -= $pkg_discount->amount;
+    my $setuprecur = $pkg_discount->cust_pkg_discount->setuprecur;
+    $d->{$setuprecur.'_amount'} -= $pkg_discount->amount;
   } 
-  $d->{amount} *= $self->quantity || 1;
+  $d->{setup_amount} *= $self->quantity || 1; # ??
+  $d->{recur_amount} *= $self->quantity || 1; # ??
   
   return $d;
 }
@@ -1124,7 +1139,10 @@ sub tax_locationnum {
   if ( $self->pkgnum ) { # normal sales
     return $self->cust_pkg->tax_locationnum;
   } elsif ( $self->feepart ) { # fees
-    return $self->cust_bill->cust_main->ship_locationnum;
+    my $custnum = $self->fee_origin->custnum;
+    if ( $custnum ) {
+      return FS::cust_main->by_key($custnum)->ship_locationnum;
+    }
   } else { # taxes
     return '';
   }
@@ -1135,7 +1153,10 @@ sub tax_location {
   if ( $self->pkgnum ) { # normal sales
     return $self->cust_pkg->tax_location;
   } elsif ( $self->feepart ) { # fees
-    return $self->cust_bill->cust_main->ship_location;
+    my $custnum = $self->fee_origin->custnum;
+    if ( $custnum ) {
+      return FS::cust_main->by_key($custnum)->ship_location;
+    }
   } else { # taxes
     return;
   }