add upcoming/preceding option, RT#3851
[freeside.git] / FS / FS / cust_main.pm
index b72079c..8b4181e 100644 (file)
@@ -2047,7 +2047,6 @@ Used in conjunction with the I<time> option, this option specifies the date of f
 sub bill {
   my( $self, %options ) = @_;
   return '' if $self->payby eq 'COMP';
-  local $DEBUG = 1;
   warn "$me bill customer ". $self->custnum. "\n"
     if $DEBUG;
 
@@ -2068,7 +2067,6 @@ sub bill {
   $self->select_for_update; #mutex
 
   my @cust_bill_pkg = ();
-  my @appended_cust_bill_pkg = ();
 
   ###
   # find the packages which are due for billing, find out how much they are
@@ -2107,7 +2105,6 @@ sub bill {
                             'cust_pkg'            => $cust_pkg,
                             'precommit_hooks'     => \@precommit_hooks,
                             'line_items'          => \@cust_bill_pkg,
-                            'appended_line_items' => \@appended_cust_bill_pkg,
                             'setup'               => \$total_setup,
                             'recur'               => \$total_recur,
                             'tax_matrix'          => \%taxlisthash,
@@ -2123,8 +2120,6 @@ sub bill {
 
   } #foreach my $cust_pkg
 
-  push @cust_bill_pkg, @appended_cust_bill_pkg;
-
   unless ( @cust_bill_pkg ) { #don't create an invoice w/o line items
     #but do commit any package date cycling that happened
     $dbh->commit or die $dbh->errstr if $oldAutoCommit;
@@ -2144,7 +2139,6 @@ sub bill {
                             'cust_pkg'            => $postal_pkg,
                             'precommit_hooks'     => \@precommit_hooks,
                             'line_items'          => \@cust_bill_pkg,
-                            'appended_line_items' => \@appended_cust_bill_pkg,
                             'setup'               => \$total_setup,
                             'recur'               => \$total_recur,
                             'tax_matrix'          => \%taxlisthash,
@@ -2296,8 +2290,6 @@ sub _make_lines {
   my $cust_pkg = $params{cust_pkg} or die "no cust_pkg specified";
   my $precommit_hooks = $params{precommit_hooks} or die "no package specified";
   my $cust_bill_pkgs = $params{line_items} or die "no line buffer specified";
-  my $appended_cust_bill_pkg = $params{appended_line_items}
-    or die "no appended line buffer specified";
   my $total_setup = $params{setup} or die "no setup accumulator specified";
   my $total_recur = $params{recur} or die "no recur accumulator specified";
   my $taxlisthash = $params{tax_matrix} or die "no tax accumulator specified";
@@ -2389,6 +2381,7 @@ sub _make_lines {
     # only for figuring next bill date, nothing else, so, reset $sdate again
     # here
     $sdate = $cust_pkg->bill || $cust_pkg->setup || $time;
+    #no need, its in $hash{last_bill}# my $last_bill = $cust_pkg->last_bill;
     $cust_pkg->last_bill($sdate);
     
     if ( $part_pkg->freq =~ /^\d+$/ ) {
@@ -2448,6 +2441,14 @@ sub _make_lines {
 
       warn "    charges (setup=$setup, recur=$recur); adding line items\n"
         if $DEBUG > 1;
+
+      my @cust_pkg_detail = map { $_->detail } $cust_pkg->cust_pkg_detail('I');
+      if ( $DEBUG > 1 ) {
+        warn "      adding customer package invoice detail: $_\n"
+          foreach @cust_pkg_detail;
+      }
+      push @details, @cust_pkg_detail;
+
       my $cust_bill_pkg = new FS::cust_bill_pkg {
         'pkgnum'    => $cust_pkg->pkgnum,
         'setup'     => $setup,
@@ -2455,10 +2456,17 @@ sub _make_lines {
         'recur'     => $recur,
         'unitrecur' => $unitrecur,
         'quantity'  => $cust_pkg->quantity,
-        'sdate'     => $sdate,
-        'edate'     => $cust_pkg->bill,
         'details'   => \@details,
       };
+
+      if ( $part_pkg->option('recur_temporality') eq 'preceding' ) {
+        $cust_bill_pkg->sdate( $hash{last_bill} );
+        $cust_bill_pkg->edate( $sdate - 86399   );2#60s*60m*24h-1
+      } else { #if ( $part_pkg->option('recur_temporality') eq 'upcoming' ) {
+        $cust_bill_pkg->sdate( $sdate );
+        $cust_bill_pkg->edate( $cust_pkg->bill );
+      }
+
       $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
         unless $part_pkg->pkgpart == $real_pkgpart;
 
@@ -2469,61 +2477,17 @@ sub _make_lines {
       # handle taxes
       ###
 
-      unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
-
-        #some garbage disappears on cust_bill_pkg refactor
-        my $err_or_cust_bill_pkg =
-          $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg);
-
-        return $err_or_cust_bill_pkg
-          unless ( ref($err_or_cust_bill_pkg) );
-
-        push @$cust_bill_pkgs, @$err_or_cust_bill_pkg;
+      my $error = 
+        $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg, $cust_pkg);
+      return $error if $error;
 
-      } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
+      push @$cust_bill_pkgs, $cust_bill_pkg;
 
     } #if $setup != 0 || $recur != 0
       
   } #if $line_items
 
-  if ( $part_pkg->can('append_cust_bill_pkgs') ) {
-    my %param = ( 'precommit_hooks' => $precommit_hooks, );
-    my ($more_cust_bill_pkgs) =
-      eval { $part_pkg->append_cust_bill_pkgs( $cust_pkg, \$sdate, \%param ) };
-
-    return "$@ running append_cust_bill_pkgs for $cust_pkg\n"
-      if ( $@ );
-    return "$more_cust_bill_pkgs"
-      unless ( ref($more_cust_bill_pkgs) );
-
-    foreach my $cust_bill_pkg ( @{$more_cust_bill_pkgs} ) {
-
-      $cust_bill_pkg->pkgpart_override($part_pkg->pkgpart)
-        unless $part_pkg->pkgpart == $real_pkgpart;
-
-      unless ($cust_bill_pkg->duplicate) {
-        $$total_setup += $cust_bill_pkg->setup;
-        $$total_recur += $cust_bill_pkg->recur;
-
-        ###
-        # handle taxes
-        ###
-
-        unless ( $self->tax =~ /Y/i || $self->payby eq 'COMP' ) {
-
-          #some garbage disappears on cust_bill_pkg refactor
-          my $err_or_cust_bill_pkg =
-            $self->_handle_taxes($part_pkg, $taxlisthash, $cust_bill_pkg);
-
-          return $err_or_cust_bill_pkg
-            unless ( ref($err_or_cust_bill_pkg) );
-
-          push @$appended_cust_bill_pkg, @$err_or_cust_bill_pkg;
-
-        } #unless $self->tax =~ /Y/i || $self->payby eq 'COMP'
-      }
-    }
-  }
+  '';
 
 }
 
@@ -2532,6 +2496,7 @@ sub _handle_taxes {
   my $part_pkg = shift;
   my $taxlisthash = shift;
   my $cust_bill_pkg = shift;
+  my $cust_pkg = shift;
 
   my %cust_bill_pkg = ();
   my %taxes = ();
@@ -2542,12 +2507,14 @@ sub _handle_taxes {
     : '';
 
   my @classes;
-  push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->type eq 'U';
+  #push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->type eq 'U';
+  push @classes, $cust_bill_pkg->usage_classes if $cust_bill_pkg->usage;
   push @classes, 'setup' if $cust_bill_pkg->setup;
   push @classes, 'recur' if $cust_bill_pkg->recur;
 
   if ( $conf->exists('enable_taxproducts')
        && (scalar($part_pkg->part_pkg_taxoverride) || $part_pkg->has_taxproduct)
+       && ( $self->tax !~ /Y/i && $self->payby ne 'COMP' )
      )
   { 
 
@@ -2563,7 +2530,7 @@ sub _handle_taxes {
       $taxes{''} = $err_or_ref;
     }
 
-  }else{
+  } elsif ( $self->tax !~ /Y/i && $self->payby ne 'COMP' ) {
 
     my %taxhash = map { $_ => $self->get("$prefix$_") }
                       qw( state county country );
@@ -2598,69 +2565,91 @@ sub _handle_taxes {
                   $part_pkg->taxclass ). "\n";
     }
 
-  } #if $conf->exists('enable_taxproducts') 
-
-  # XXX all this goes away with cust_bill_pay refactor
-
-  $cust_bill_pkg{setup} = $cust_bill_pkg if $cust_bill_pkg->setup;
-  $cust_bill_pkg{recur} = $cust_bill_pkg if $cust_bill_pkg->recur;
-    
-  #split setup and recur
-  if ($cust_bill_pkg->setup && $cust_bill_pkg->recur) {
-    my $cust_bill_pkg_recur = new FS::cust_bill_pkg { $cust_bill_pkg->hash };
-    $cust_bill_pkg->set('details', []);
-    $cust_bill_pkg->recur(0);
-    $cust_bill_pkg->type('');
-    $cust_bill_pkg{recur} = $cust_bill_pkg_recur;
-  }
-
-  #split usage from recur
-  my $usage = $cust_bill_pkg->usage;
-  if ($usage) {
-    my $cust_bill_pkg_usage =
-        new FS::cust_bill_pkg { $cust_bill_pkg{recur}->hash };
-    $cust_bill_pkg_usage->recur($usage);
-    $cust_bill_pkg{recur}->recur( $cust_bill_pkg{recur}->recur - $usage );
-    $cust_bill_pkg{recur}->type( '' );
-    $cust_bill_pkg{''} = $cust_bill_pkg_usage;
-  }
-
-  #subdivide usage by usage_class
-  if (exists($cust_bill_pkg{''})) {
-    foreach my $class (grep {$_} @classes) {
-      my $usage = $cust_bill_pkg{''}->usage($class);
-      my $cust_bill_pkg_usage =
-          new FS::cust_bill_pkg { $cust_bill_pkg{''}->hash };
-      $cust_bill_pkg_usage->recur($usage);
-      $cust_bill_pkg{''}->recur( $cust_bill_pkg{''}->recur - $usage );
-      $cust_bill_pkg{$class} = $cust_bill_pkg_usage;
-    }
-    $cust_bill_pkg{''}->set('details', []);
-    delete $cust_bill_pkg{''} unless $cust_bill_pkg{''}->recur;
-  }
-
-  foreach my $key (keys %cust_bill_pkg) {
-    my @taxes = @{ $taxes{$key} };
-    my $cust_bill_pkg = $cust_bill_pkg{$key};
-
-    foreach my $tax ( @taxes ) {
-      my $taxname = ref( $tax ). ' '. $tax->taxnum;
-      if ( exists( $taxlisthash->{ $taxname } ) ) {
-        push @{ $taxlisthash->{ $taxname  } }, $cust_bill_pkg;
-      }else{
-        $taxlisthash->{ $taxname } = [ $tax, $cust_bill_pkg ];
-      }
-    }
-  }
-
-  # sort setup,recur,'', and the rest numeric && return
-  my @result = map { $cust_bill_pkg{$_} }
-               sort { my $ad = ($a=~/^\d+$/); my $bd = ($b=~/^\d+$/);
-                      ( $ad cmp $bd ) || ( $ad ? $a<=>$b : $b cmp $a )
-                    }
-               keys %cust_bill_pkg;
+  } #if $conf->exists('enable_taxproducts') ...
+  my $section = $cust_pkg->part_pkg->option('usage_section', 'Hush!')
+    if $cust_pkg->part_pkg->option('separate_usage', 'Hush!' );
+  my $want_duplicate =
+    $cust_pkg->part_pkg->option('summarize_usage', 'Hush!') &&
+    $cust_pkg->part_pkg->option('usage_section', 'Hush!');
 
-  \@result;
+#BUNK.  DO NOT CREATE DUPLICATE cust_bill_pkg!!!!!!!!!!!!
+#
+#  # XXX this mostly goes away with cust_bill_pkg refactor
+# 
+#  $cust_bill_pkg{setup} = $cust_bill_pkg if $cust_bill_pkg->setup;
+#  $cust_bill_pkg{recur} = $cust_bill_pkg if $cust_bill_pkg->recur;
+#
+#    
+#  #split setup and recur
+#  if ($cust_bill_pkg->setup && $cust_bill_pkg->recur) {
+#    my $cust_bill_pkg_recur = new FS::cust_bill_pkg { $cust_bill_pkg->hash };
+#    $cust_bill_pkg_recur->details($cust_bill_pkg->
+#    $cust_bill_pkg_recur->setup(0);
+#    $cust_bill_pkg_recur->unitsetup(0);
+#    $cust_bill_pkg{recur} = $cust_bill_pkg_recur;
+#
+#    $cust_bill_pkg->set('details', []);
+#    $cust_bill_pkg->recur(0);
+#    $cust_bill_pkg->unitrecur(0);
+#    $cust_bill_pkg->type('');
+#  }
+#
+#  #split usage from recur
+#  my $usage = sprintf( "%.2f", $cust_bill_pkg{recur}->usage );
+#  warn "usage is $usage\n" if $DEBUG;
+#  if ($usage) {
+#    my $cust_bill_pkg_usage =
+#        new FS::cust_bill_pkg { $cust_bill_pkg{recur}->hash };
+#    $cust_bill_pkg_usage->recur( $usage );
+#    $cust_bill_pkg_usage->type( 'U' );
+#    $cust_bill_pkg_usage->duplicate( $want_duplicate ? 'Y' :  '' );
+#    $cust_bill_pkg_usage->section( $section );
+#    $cust_bill_pkg_usage->post_total( $want_duplicate ? 'Y' :  '' );
+#    my $recur = sprintf( "%.2f", $cust_bill_pkg{recur}->recur - $usage );
+#    $cust_bill_pkg{recur}->recur( $recur );
+#    $cust_bill_pkg{recur}->type( '' );
+#    $cust_bill_pkg{recur}->set('details', []);
+#    $cust_bill_pkg{''} = $cust_bill_pkg_usage;
+#  }
+#
+#  #subdivide usage by usage_class
+#  if (exists($cust_bill_pkg{''})) {
+#    foreach my $class (grep {$_ && $_ ne 'setup' && $_ ne 'recur' } @classes) {
+#      my $usage = sprintf( "%.2f", $cust_bill_pkg{''}->usage($class) );
+#      my $cust_bill_pkg_usage =
+#          new FS::cust_bill_pkg { $cust_bill_pkg{''}->hash };
+#      $cust_bill_pkg_usage->recur( $usage );
+#      $cust_bill_pkg_usage->set('details', []);
+#      my $classless = sprintf( "%.2f", $cust_bill_pkg{''}->recur - $usage );
+#      $cust_bill_pkg{''}->recur( $classless );
+#      $cust_bill_pkg{$class} = $cust_bill_pkg_usage;
+#    }
+#    delete $cust_bill_pkg{''} unless $cust_bill_pkg{''}->recur;
+#  }
+#
+#  foreach my $key (keys %cust_bill_pkg) {
+#    my @taxes = @{ $taxes{$key} };
+#    my $cust_bill_pkg = $cust_bill_pkg{$key};
+#
+#    foreach my $tax ( @taxes ) {
+#      my $taxname = ref( $tax ). ' '. $tax->taxnum;
+#      if ( exists( $taxlisthash->{ $taxname } ) ) {
+#        push @{ $taxlisthash->{ $taxname  } }, $cust_bill_pkg;
+#      }else{
+#        $taxlisthash->{ $taxname } = [ $tax, $cust_bill_pkg ];
+#      }
+#    }
+#  }
+#
+#  # sort setup,recur,'', and the rest numeric && return
+#  my @result = map { $cust_bill_pkg{$_} }
+#               sort { my $ad = ($a=~/^\d+$/); my $bd = ($b=~/^\d+$/);
+#                      ( $ad cmp $bd ) || ( $ad ? $a<=>$b : $b cmp $a )
+#                    }
+#               keys %cust_bill_pkg;
+#
+#  \@result;
 }
 
 sub _gather_taxes {
@@ -4892,6 +4881,7 @@ the error, otherwise returns false.
 sub charge {
   my $self = shift;
   my ( $amount, $quantity, $pkg, $comment, $taxclass, $additional, $classnum );
+  my ( $taxproduct, $override );
   if ( ref( $_[0] ) ) {
     $amount     = $_[0]->{amount};
     $quantity   = exists($_[0]->{quantity}) ? $_[0]->{quantity} : 1;
@@ -4901,6 +4891,8 @@ sub charge {
     $taxclass   = exists($_[0]->{taxclass}) ? $_[0]->{taxclass} : '';
     $classnum   = exists($_[0]->{classnum}) ? $_[0]->{classnum} : '';
     $additional = $_[0]->{additional};
+    $taxproduct = $_[0]->{taxproductnum};
+    $override   = { '' => $_[0]->{tax_override} };
   }else{
     $amount     = shift;
     $quantity   = 1;
@@ -4922,13 +4914,14 @@ sub charge {
   my $dbh = dbh;
 
   my $part_pkg = new FS::part_pkg ( {
-    'pkg'      => $pkg,
-    'comment'  => $comment,
-    'plan'     => 'flat',
-    'freq'     => 0,
-    'disabled' => 'Y',
-    'classnum' => $classnum ? $classnum : '',
-    'taxclass' => $taxclass,
+    'pkg'           => $pkg,
+    'comment'       => $comment,
+    'plan'          => 'flat',
+    'freq'          => 0,
+    'disabled'      => 'Y',
+    'classnum'      => $classnum ? $classnum : '',
+    'taxclass'      => $taxclass,
+    'taxproductnum' => $taxproduct,
   } );
 
   my %options = ( ( map { ("additional_info$_" => $additional->[$_] ) }
@@ -4938,7 +4931,9 @@ sub charge {
                   'setup_fee' => $amount,
                 );
 
-  my $error = $part_pkg->insert( options => \%options );
+  my $error = $part_pkg->insert( options       => \%options,
+                                 tax_overrides => $override,
+                               );
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
     return $error;