fix cancellation errors with updated flat_introrate, RT#5865
[freeside.git] / FS / FS / part_pkg / agent.pm
index 70ad2ad..d41978c 100644 (file)
@@ -38,10 +38,15 @@ $me = '[FS::part_pkg::agent]';
     'cutoff_day'    => { 'name' => 'Billing Day (1 - 28)',
                          'default' => '1',
                        },
+
+    'no_pkg_prorate'   => { 'name' => 'Disable prorating bulk packages (charge full price for packages active only a portion of the month)',
+                            'type' => 'checkbox',
+                          },
+
   },
 
   #'fieldorder' => [qw( setup_fee recur_fee recur_method cutoff_day ) ],
-  'fieldorder' => [qw( setup_fee recur_fee cutoff_day ) ],
+  'fieldorder' => [qw( setup_fee recur_fee cutoff_day no_pkg_prorate ) ],
 
   'weight' => 51,
 
@@ -60,16 +65,15 @@ sub calc_recur {
   my $conf = new FS::Conf;
   my $money_char = $conf->config('money_char') || '$';
 
-  my $prorate_sdate = $$sdate;
-  
   my $total_agent_charge = 0;
 
   warn "$me billing for agent packages from ". time2str('%x', $last_bill).
-                                       " to ". time2str('%x', $$sdate).
-                                "prorated to". time2str('%x', $prorate_sdate ).
-       "\n"
+                                       " to ". time2str('%x', $$sdate). "\n"
     if $DEBUG;
 
+  my $prorate_ratio =   ( $$sdate                     - $last_bill )
+                      / ( $self->add_freq($last_bill) - $last_bill );
+
   #almost always just one,
   #unless you have multiple agents with same master customer0
   my @agents = qsearch('agent', { 'agent_custnum' => $cust_pkg->custnum } );
@@ -115,7 +119,7 @@ sub calc_recur {
         my $pkg_setup_fee  =
           $part_pkg->setup_cost || $part_pkg->option('setup_fee');
         my $pkg_base_recur =
-          $part_pkg->recur_cost || $part_pkg->base_recur_permonth;
+          $part_pkg->recur_cost || $part_pkg->base_recur_permonth($cust_pkg);
 
         my $pkg_start = $cust_pkg->get('setup');
         if ( $pkg_start < $last_bill ) {
@@ -128,8 +132,13 @@ sub calc_recur {
         my $pkg_end = $cust_pkg->get('cancel');
         $pkg_end = ( !$pkg_end || $pkg_end > $$sdate ) ? $$sdate : $pkg_end;
 
-        my $recur_charge += $pkg_base_recur * ( $pkg_end - $pkg_start )
-                                            / ( $prorate_sdate  - $last_bill );
+
+        my $pkg_recur_charge = $prorate_ratio * $pkg_base_recur;
+        $pkg_recur_charge *= ( $pkg_end - $pkg_start )
+                           / ( $$sdate  - $last_bill )
+          unless $self->option('no_pkg_prorate');
+
+        my $recur_charge += $pkg_recur_charge;
 
         $pkg_details .= $money_char. sprintf('%.2f', $recur_charge ).
                         ' ('.  time2str('%x', $pkg_start).
@@ -138,7 +147,8 @@ sub calc_recur {
 
         $pkg_charge += $recur_charge;
 
-        push @$details, $pkg_details;
+        push @$details, $pkg_details
+          if $pkg_charge;
         $total_agent_charge += $pkg_charge;
 
       } #foreach $cust_pkg