backport flat, prorate, and subscription plans
authorjeff <jeff>
Fri, 21 Dec 2007 21:38:11 +0000 (21:38 +0000)
committerjeff <jeff>
Fri, 21 Dec 2007 21:38:11 +0000 (21:38 +0000)
FS/FS/cust_main.pm
FS/FS/part_pkg/flat.pm
FS/FS/part_pkg/prorate.pm
FS/FS/part_pkg/subscription.pm

index 3dbe8b3..031f477 100644 (file)
@@ -1983,6 +1983,13 @@ sub bill {
          ( $cust_pkg->getfield('bill') || 0 ) <= $time
     ) {
 
+      # XXX should this be a package event?  probably.  events are called
+      # at collection time at the moment, though...
+      if ( $part_pkg->can('reset_usage') ) {
+        warn "    resetting usage counters" if $DEBUG > 1;
+        $part_pkg->reset_usage($cust_pkg);
+      }
+
       warn "    bill recur\n" if $DEBUG > 1;
 
       # XXX shared with $recur_prog
index 7f795bd..f5ccd01 100644 (file)
@@ -72,12 +72,16 @@ use FS::part_pkg;
                          'format' => \&FS::UI::bytecount::display_bytecount,
                          'parse' => \&FS::UI::bytecount::parse_bytecount,
                        },
+    'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
+                                    ' over into current period',
+                          'type' => 'checkbox',
+                        },
   },
   'fieldorder' => [ 'setup_fee', 'recur_fee', 'unused_credit', 
                     'seconds', 'upbytes', 'downbytes', 'totalbytes',
                     'recharge_amount', 'recharge_seconds', 'recharge_upbytes',
                     'recharge_downbytes', 'recharge_totalbytes',
-                    'externalid' ],
+                    'usage_rollover', 'externalid' ],
   'weight' => 10,
 );
 
@@ -95,18 +99,24 @@ sub calc_setup {
 
 sub calc_recur {
   my($self, $cust_pkg) = @_;
-  $self->reset_usage($cust_pkg);
   $self->base_recur($cust_pkg);
 }
 
 sub base_recur {
   my($self, $cust_pkg) = @_;
-  $self->option('recur_fee');
+  $self->option('recur_fee', 1) || 0;
 }
 
 sub calc_remain {
-  my ($self, $cust_pkg) = @_;
-  my $time = time;  #should be able to pass this in for credit calculation
+  my ($self, $cust_pkg, %options) = @_;
+
+  my $time;
+  if ($options{'time'}) {
+    $time = $options{'time'};
+  } else {
+    $time = time;
+  }
+
   my $next_bill = $cust_pkg->getfield('bill') || 0;
   my $last_bill = $cust_pkg->last_bill || 0;
   return 0 if    ! $self->base_recur
@@ -144,7 +154,11 @@ sub reset_usage {
   my %values = map { $_, $self->option($_) } 
     grep { $self->option($_, 'hush') } 
     qw(seconds upbytes downbytes totalbytes);
-  $cust_pkg->set_usage(\%values);
+  if ($self->option('usage_rollover', 1)) {
+    $cust_pkg->recharge(\%values);
+  }else{
+    $cust_pkg->set_usage(\%values);
+  }
 }
 
 1;
index 9ad0c89..3dfab37 100644 (file)
@@ -70,6 +70,10 @@ use FS::part_pkg::flat;
                         'format' => \&FS::UI::bytecount::display_bytecount,
                         'parse' => \&FS::UI::bytecount::parse_bytecount,
                        },
+    'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
+                                   'over into current period',
+                         'type' => 'checkbox',
+                        },
     #it would be better if this had to be turned on, its confusing
     'externalid' => { 'name'   => 'Optional External ID',
                       'default' => '',
@@ -79,7 +83,7 @@ use FS::part_pkg::flat;
                     'seconds', 'upbyte', 'downbytes', 'totalbytes',
                     'recharge_amount', 'recharge_seconds', 'recharge_upbytes',
                     'recharge_downbytes', 'recharge_totalbytes',
-                    'externalid', ],
+                    'usage_rollover', 'externalid', ],
   'freq' => 'm',
   'weight' => 20,
 );
@@ -92,8 +96,6 @@ sub calc_recur {
   my $mend;
   my $mstart;
   
-  $self->reset_usage($cust_pkg);
-
   if ( $mday >= $cutoff_day ) {
     $mend =
       timelocal(0,0,0,$cutoff_day, $mon == 11 ? 0 : $mon+1, $year+($mon==11));
index 6fe74a8..3e5a416 100644 (file)
@@ -66,6 +66,10 @@ use FS::part_pkg::flat;
                         'format' => \&FS::UI::bytecount::display_bytecount,
                         'parse' => \&FS::UI::bytecount::parse_bytecount,
                        },
+    'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
+                                   'over into current period',
+                         'type' => 'checkbox',
+                        },
     #it would be better if this had to be turned on, its confusing
     'externalid' => { 'name'   => 'Optional External ID',
                       'default' => '',
@@ -80,6 +84,7 @@ use FS::part_pkg::flat;
                     'upbytes', 'downbytes', 'totalbytes',
                     'recharge_amount', 'recharge_seconds', 'recharge_upbytes',
                     'recharge_downbytes', 'recharge_totalbytes',
+                    'usage_rollover',
                    ],
   'freq' => 'm',
   'weight' => 30,
@@ -98,8 +103,6 @@ sub calc_recur {
 
   $$sdate = timelocal(0,0,0,$cutoff_day,$mon,$year);
 
-  $self->reset_usage($cust_pkg);
-
   $self->option('recur_fee');
 }