more efficient invoice voiding, RT#80366
[freeside.git] / FS / FS / cust_bill_pkg.pm
index a36520b..6f9c74a 100644 (file)
@@ -6,7 +6,7 @@ use vars qw( @ISA $DEBUG $me );
 use Carp;
 use List::Util qw( sum min );
 use Text::CSV_XS;
-use FS::Record qw( qsearch qsearchs dbh );
+use FS::Record qw( qsearch qsearchs dbh fields );
 use FS::cust_pkg;
 use FS::cust_bill_pkg_detail;
 use FS::cust_bill_pkg_display;
@@ -365,8 +365,10 @@ sub void {
     return $error;
   }
 
+  #more efficiently than below, because there could be lots
+  $self->void_cust_bill_pkg_detail($reprocess_cdrs);
+
   foreach my $table (qw(
-    cust_bill_pkg_detail
     cust_bill_pkg_display
     cust_bill_pkg_discount
     cust_bill_pkg_tax_location
@@ -374,17 +376,13 @@ 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 }) ) {
 
       my $vclass = 'FS::'.$table.'_void';
       my $void = $vclass->new( {
         map { $_ => $linked->get($_) } $linked->fields
       });
-      my $error = $void->insert || $linked->delete(%delete_args);
+      my $error = $void->insert || $linked->delete;
       if ( $error ) {
         $dbh->rollback if $oldAutoCommit;
         return $error;
@@ -406,6 +404,42 @@ sub void {
 
 }
 
+sub void_cust_bill_pkg_detail {
+  my( $self, $reprocess_cdrs ) = @_;
+
+  my $from_cust_bill_pkg_detail =
+    'FROM cust_bill_pkg_detail WHERE billpkgnum = ?';
+  my $where_detailnum =
+    "WHERE detailnum IN ( SELECT detailnum $from_cust_bill_pkg_detail )";
+
+  if ( $reprocess_cdrs ) {
+    #well, technically this could have been on other invoices / termination
+    # partners... separate flag?
+    $self->scalar_sql(
+      "DELETE FROM cdr_termination
+         WHERE acctid IN ( SELECT acctid FROM cdr $where_detailnum )
+      ",
+      $self->billpkgnum
+    );
+  }
+
+  my $setstatus = $reprocess_cdrs ? ', freesidestatus = NULL' : '';
+  $self->scalar_sql(
+    "UPDATE cdr SET detailnum = NULL $setstatus $where_detailnum",
+    $self->billpkgnum
+  );
+
+  my $fields = join(', ', fields('cust_bill_pkg_detail_void') );
+
+  $self->scalar_sql("INSERT INTO cust_bill_pkg_detail_void ($fields)
+                       SELECT $fields $from_cust_bill_pkg_detail",
+                    $self->billpkgnum
+                   );
+
+  $self->scalar_sql("DELETE $from_cust_bill_pkg_detail", $self->billpkgnum);
+
+}
+
 =item delete
 
 Not recommended.
@@ -459,7 +493,16 @@ sub delete {
     }
   }
 
-  my $error = $self->SUPER::delete(@_);
+  #fix the invoice amount
+
+  my $cust_bill = $self->cust_bill;
+  $cust_bill->charged( $cust_bill->charged - $self->setup - $self->recur );
+
+  #not adding a cc surcharge, but this override lets us modify charged
+  $cust_bill->{'Hash'}{'cc_surcharge_replace_hack'} = 1;
+
+  my $error =  $cust_bill->replace
+            || $self->SUPER::delete(@_);
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
     return $error;
@@ -716,6 +759,7 @@ Returns the customer (L<FS::cust_main> object) for this line item.
 =cut
 
 sub cust_main {
+  carp "->cust_main called" if $DEBUG;
   # required for cust_main_Mixin equivalence
   # and use cust_bill instead of cust_pkg because this might not have a 
   # cust_pkg
@@ -1848,7 +1892,29 @@ sub _pkg_tax_list {
   #   Duplicates can be identified by billpkgtaxlocationnum column.
 
   my $self = shift;
-  return unless $self->pkgnum;
+
+  my $search_selector;
+  if ( $self->pkgnum ) {
+
+    # For taxes applied to normal billing items
+    $search_selector =
+      ' cust_bill_pkg_tax_location.pkgnum = '
+      . dbh->quote( $self->pkgnum );
+
+  } elsif ( $self->feepart ) {
+
+    # For taxes applied to fees, when the fee is not attached to a package
+    # i.e. late fees, billing events fees
+    $search_selector =
+      ' cust_bill_pkg_tax_location.taxable_billpkgnum = '
+      . dbh->quote( $self->billpkgnum );
+
+  } else {
+    warn "_pkg_tax_list() unhandled case breaking taxes into sections";
+    warn "_pkg_tax_list() $_: ".$self->$_
+      for qw(pkgnum billpkgnum feepart);
+    return;
+  }
 
   map +{
       billpkgtaxlocationnum => $_->billpkgtaxlocationnum,
@@ -1874,7 +1940,7 @@ sub _pkg_tax_list {
       ' WHERE '.
       ' cust_bill_pkg.invnum = ' . dbh->quote( $self->invnum ) .
       ' AND '.
-      ' cust_bill_pkg_tax_location.pkgnum = ' . dbh->quote( $self->pkgnum ),
+      $search_selector
   });
 
 }