Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / part_pkg / bulk.pm
index 1b52d9f..4a55858 100644 (file)
 package FS::part_pkg::bulk;
+use base qw( FS::part_pkg::bulk_Common );
 
 use strict;
-use vars qw(@ISA $DEBUG $me %info);
+use vars qw($DEBUG $me %info);
 use Date::Format;
-use FS::part_pkg::flat;
+use List::Util qw( max );
+use FS::Conf;
 
-@ISA = qw(FS::part_pkg::flat);
-
-$DEBUG = 1;
+$DEBUG = 0;
 $me = '[FS::part_pkg::bulk]';
 
 %info = (
-  'name' => 'Bulk billing based on number of active services',
+  'name' => 'Bulk billing based on number of active services (during billing period)',
+  'inherit_fields' => [ 'bulk_Common', 'global_Mixin' ],
   'fields' => {
-    'setup_fee' => { 'name'    => 'Setup fee for the entire bulk package',
-                     'default' => 0,
-                   },
-    'recur_fee' => { 'name'    => 'Recurring fee for the entire bulk package',
-                     'default' => 0,
-                   },
-    'svc_setup_fee' => { 'name'    => 'Setup fee for each new service',
-                         'default' => 0,
-                       },
-    'svc_recur_fee' => { 'name'    => 'Recurring fee for each service',
-                         'default' => 0,
-                       },
-    'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
-                                   ' of service at cancellation',
+    'no_prorate'    => { 'name' => 'Don\'t prorate recurring fees on services '.
+                                   'active for a partial month',
                          'type' => 'checkbox',
                        },
   },
-  'fieldorder' => [ 'setup_fee', 'recur_fee', 'svc_setup_fee', 'svc_recur_fee',
-                    'unused_credit', ],
-  'weight' => 50,
+  'fieldorder' => [ 'no_prorate' ],
+  'weight' => 51,
 );
 
-#some false laziness-ish w/agent.pm...  not a lot
-sub calc_recur {
-  my($self, $cust_pkg, $sdate, $details ) = @_;
-
-  my $conf = new FS::Conf;
-  my $money_char = $conf->config('money_char') || '$';
-  
-  my $svc_setup_fee = $self->option('svc_setup_fee');
-
-  my $last_bill = $cust_pkg->last_bill;
-
-  return sprintf("%.2f", $self->base_recur($cust_pkg) )
-    unless $$sdate > $last_bill;
-
-  my $total_svc_charge = 0;
 
-  warn "$me billing for bulk services from ". time2str('%x', $last_bill).
-                                      " to ". time2str('%x', $$sdate). "\n"
-    if $DEBUG;
-
-                                           #   END      START
-  foreach my $h_cust_svc ( $cust_pkg->h_cust_svc( $$sdate, $last_bill ) ) {
-
-    my @label = $h_cust_svc->label_long( $$sdate, $last_bill );
-    die "fatal: no historical label found, wtf?" unless scalar(@label); #?
-    my $svc_details = $label[0]. ': '. $label[1]. ': ';
+sub _bulk_cust_svc {
+  my( $self, $cust_pkg, $sdate ) = @_;
+                       #   END      START
+  $cust_pkg->h_cust_svc( $$sdate, $cust_pkg->last_bill );
+}
 
-    my $svc_charge = 0;
+sub _bulk_setup {
+  my( $self, $cust_pkg, $h_cust_svc ) = @_;
+  ($h_cust_svc->date_inserted < $cust_pkg->last_bill)
+    ? $self->option('svc_setup_fee')
+    : 0;
+}
 
-    my $svc_start = $h_cust_svc->date_inserted;
-    if ( $svc_start < $last_bill ) {
-      $svc_start = $last_bill;
-    } elsif ( $svc_setup_fee ) {
-      $svc_charge += $svc_setup_fee;
-      $svc_details .= $money_char. sprintf('%.2f setup, ', $svc_setup_fee);
-    }
+sub _bulk_recur {
+  my( $self, $cust_pkg, $h_cust_svc, $sdate ) = @_;
 
-    my $svc_end = $h_cust_svc->date_deleted;
-    $svc_end = ( !$svc_end || $svc_end > $$sdate ) ? $$sdate : $svc_end;
+  return ($self->option('svc_recur_fee'), '')
+    if $self->option('no_prorate',1);
 
-    my $recur_charge =
-      $self->option('svc_recur_fee') * ( $svc_end - $svc_start )
-                                     / ( $$sdate  - $last_bill );
+  my $last_bill = $cust_pkg->last_bill;
 
-    $svc_details .= $money_char. sprintf('%.2f', $recur_charge ).
-                    ' ('.  time2str('%x', $svc_start).
-                    ' - '. time2str('%x', $svc_end  ). ')'
-      if $recur_charge;
+  return (0, '') if $$sdate == $last_bill;
 
-    $svc_charge += $recur_charge;
+  my $svc_start = max( $h_cust_svc->date_inserted, $last_bill);
+  my $svc_end = $h_cust_svc->date_deleted;
+  $svc_end = ( !$svc_end || $svc_end > $$sdate ) ? $$sdate : $svc_end;
 
-    push @$details, $svc_details;
-    $total_svc_charge += $svc_charge;
+  my $recur_charge = $self->option('svc_recur_fee') 
+                                   * ( $svc_end - $svc_start )
+                                   / ( $$sdate  - $last_bill );
 
-  }
+  return (0, '') unless $recur_charge;
 
-  sprintf('%.2f', $self->base_recur($cust_pkg) + $total_svc_charge );
-}
+  my $svc_details .= ' ('.  time2str('%x', $svc_start).
+                  ' - '. time2str('%x', $svc_end  ). ')';
 
-sub hide_svc_detail {
-  1;
-}
+  ( $recur_charge, $svc_details );
 
-sub is_free_options {
-  qw( setup_fee recur_fee svc_setup_fee svc_recur_fee );
 }
 
 1;