option to reprocess CDRs when voiding an invoice, RT#79001
[freeside.git] / FS / FS / cust_bill_pkg.pm
index aea776a..77dce24 100644 (file)
@@ -324,7 +324,7 @@ sub insert {
 
 }
 
-=item void [ REASON ]
+=item void [ REASON [ , REPROCESS_CDRS ] ]
 
 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).
@@ -334,11 +334,12 @@ line item to the FS::cust_bill_pkg_void table (and related tables).
 sub void {
   my $self = shift;
   my $reason = scalar(@_) ? shift : '';
+  my $reprocess_cdrs = scalar(@_) ? shift : '';
 
   unless (ref($reason) || !$reason) {
     $reason = FS::reason->new_or_existing(
-      'class'  => 'X',
-      'type'   => 'Void invoice',
+      'class'  => 'I',
+      'type'   => 'Invoice void',
       'reason' => $reason
     );
   }
@@ -373,6 +374,9 @@ sub void {
     cust_tax_exempt_pkg
     cust_bill_pkg_fee
   )) {
+    my %delete_args = ();
+    $delete_args{'reprocess_cdrs'} = $reprocess_cdrs
+      if $table eq 'cust_bill_pkg_detail';
 
     foreach my $linked ( qsearch($table, { billpkgnum=>$self->billpkgnum }) ) {
 
@@ -380,7 +384,7 @@ sub void {
       my $void = $vclass->new( {
         map { $_ => $linked->get($_) } $linked->fields
       });
-      my $error = $void->insert || $linked->delete;
+      my $error = $void->insert || $linked->delete(%delete_args);
       if ( $error ) {
         $dbh->rollback if $oldAutoCommit;
         return $error;
@@ -832,34 +836,53 @@ sub _item_discount {
   my $self = shift;
   my %options = @_;
 
+  my $d; # this will be returned.
+
   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
-  # show discount line items
-  if ( FS::cust_bill_pkg_detail->count("detail LIKE 'Includes discount%' AND billpkgnum = ?", $self->billpkgnum || 0) > 0 ) {
-    return;
-  } 
-  
-  my @ext;
-  my $d = {
-    _is_discount    => 1,
-    description     => $self->mt('Discount'),
-    setup_amount    => 0,
-    recur_amount    => 0,
-    ext_description => \@ext,
-    pkgpart         => $self->pkgpart,
-    feepart         => $self->feepart,
-    # maybe should show quantity/unit discount?
-  };
-  foreach my $pkg_discount (@pkg_discounts) {
-    push @ext, $pkg_discount->description;
-    my $setuprecur = $pkg_discount->cust_pkg_discount->setuprecur;
-    $d->{$setuprecur.'_amount'} -= $pkg_discount->amount;
-  } 
-  $d->{setup_amount} *= $self->quantity || 1; # ??
-  $d->{recur_amount} *= $self->quantity || 1; # ??
-  
-  return $d;
+  if (@pkg_discounts) {
+    # special case: if there are old "discount details" on this line item,
+    # don't show discount line items
+    if ( FS::cust_bill_pkg_detail->count("detail LIKE 'Includes discount%' AND billpkgnum = ?", $self->billpkgnum || 0) > 0 ) {
+      return;
+    } 
+    
+    my @ext;
+    $d = {
+      _is_discount    => 1,
+      description     => $self->mt('Discount'),
+      setup_amount    => 0,
+      recur_amount    => 0,
+      ext_description => \@ext,
+      pkgpart         => $self->pkgpart,
+      feepart         => $self->feepart,
+      # maybe should show quantity/unit discount?
+    };
+    foreach my $pkg_discount (@pkg_discounts) {
+      push @ext, $pkg_discount->description;
+      my $setuprecur = $pkg_discount->cust_pkg_discount->setuprecur;
+      $d->{$setuprecur.'_amount'} -= $pkg_discount->amount;
+    }
+  }
+
+  # show introductory rate as a pseudo-discount
+  if (!$d) { # this will conflict with showing real discounts
+    my $part_pkg = $self->part_pkg;
+    if ( $part_pkg and $part_pkg->option('show_as_discount',1) ) {
+      my $cust_pkg = $self->cust_pkg;
+      my $intro_end = $part_pkg->intro_end($cust_pkg);
+      my $_date = $self->cust_bill->_date;
+      if ( $intro_end > $_date ) {
+        $d = $part_pkg->item_discount($cust_pkg);
+      }
+    }
+  }
+
+  if ( $d ) {
+    $d->{setup_amount} *= $self->quantity || 1; # ??
+    $d->{recur_amount} *= $self->quantity || 1; # ??
+  }
+    
+  $d;
 }
 
 =item set_display OPTION => VALUE ...
@@ -1101,17 +1124,34 @@ sub cust_bill_pkg_tax_Xlocation {
 
 =item recur_show_zero
 
-=cut
+Whether to show a zero recurring amount. This is true if the package or its
+definition has the recur_show_zero flag, and the recurring fee is actually
+zero for this period.
 
-sub recur_show_zero { shift->_X_show_zero('recur'); }
-sub setup_show_zero { shift->_X_show_zero('setup'); }
+=cut
 
-sub _X_show_zero {
+sub recur_show_zero {
   my( $self, $what ) = @_;
 
-  return 0 unless $self->$what() == 0 && $self->pkgnum;
+  return 0 unless $self->get('recur') == 0 && $self->pkgnum;
+
+  $self->cust_pkg->_X_show_zero('recur');
+}
+
+=item setup_show_zero
+
+Whether to show a zero setup charge. This requires the package or its
+definition to have the setup_show_zero flag, but it also returns false if
+the package's setup date is before this line item's start date.
 
-  $self->cust_pkg->_X_show_zero($what);
+=cut
+
+sub setup_show_zero {
+  my $self = shift;
+  return 0 unless $self->get('setup') == 0 && $self->pkgnum;
+  my $cust_pkg = $self->cust_pkg;
+  return 0 if ( $self->sdate || 0 ) > ( $cust_pkg->setup || 0 );
+  return $cust_pkg->_X_show_zero('setup');
 }
 
 =item credited [ BEFORE, AFTER, OPTIONS ]
@@ -1153,9 +1193,12 @@ sub tax_location {
   if ( $self->pkgnum ) { # normal sales
     return $self->cust_pkg->tax_location;
   } elsif ( $self->feepart ) { # fees
-    my $custnum = $self->fee_origin->custnum;
-    if ( $custnum ) {
-      return FS::cust_main->by_key($custnum)->ship_location;
+    my $fee_origin = $self->fee_origin;
+    if ( $fee_origin ) {
+      my $custnum = $fee_origin->custnum;
+      if ( $custnum ) {
+        return FS::cust_main->by_key($custnum)->ship_location;
+      }
     }
   } else { # taxes
     return;