import torrus 1.0.9
[freeside.git] / FS / FS / part_pkg / base_rate.pm
1 package FS::part_pkg::base_rate;
2
3 use strict;
4 use vars qw(@ISA %info);
5 #use FS::Record qw(qsearch);
6 use FS::part_pkg;
7
8 @ISA = qw(FS::part_pkg);
9
10 %info = (
11   'name' => 'Base rate (anniversary billing, Times units ordered)',
12             # XXX it multiplies recurring fee by cust_pkg option "units", how to
13             # express that
14   'shortname' => 'Bulk (manual from "units" option)',
15   'inherit_fields' => [ 'global_Mixin' ],
16   'fields' => {
17     'externalid' => { 'name'    => 'Optional External ID',
18                       'default' => '',
19                     },
20   },
21   'fieldorder' => [ qw( externalid ) ],
22   'weight' => 52,
23 );
24
25 sub calc_setup {
26   my($self, $cust_pkg, $sdate, $details ) = @_;
27
28   my $i = 0;
29   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
30   while ($i < $count) {
31     push @$details, $self->option( 'additional_info' . $i++ );
32   }
33
34   $self->option('setup_fee');
35 }
36
37 sub calc_recur {
38   my($self, $cust_pkg) = @_;
39   $self->base_recur($cust_pkg);
40 }
41
42 sub base_recur {
43   my($self, $cust_pkg) = @_;
44   my $units = $cust_pkg->option('units') ? $cust_pkg->option('units') : 1 ;
45         # default to 1 if not found
46   sprintf("%.2f", 
47           ($self->option('recur_fee') * $units ) 
48   );
49 }
50
51 sub calc_remain {
52   my ($self, $cust_pkg, %options) = @_;
53   my $time = $options{'time'} || time;
54   my $next_bill = $cust_pkg->getfield('bill') || 0;
55   return 0 if  ! $self->base_recur($cust_pkg)
56               || ! $next_bill
57               || $next_bill < $time;
58
59   my %sec = (
60     'h' =>    3600, # 60 * 60
61     'd' =>   86400, # 60 * 60 * 24
62     'w' =>  604800, # 60 * 60 * 24 * 7
63     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
64   );
65
66   $self->freq =~ /^(\d+)([hdwm]?)$/
67     or die 'unparsable frequency: '. $self->freq;
68   my $freq_sec = $1 * $sec{$2||'m'};
69   return 0 unless $freq_sec;
70
71   sprintf("%.2f", $self->base_recur($cust_pkg) * ( $next_bill - $time ) / $freq_sec );
72
73 }
74
75 sub is_free_options {
76   qw( setup_fee recur_fee );
77 }
78
79 sub is_prepaid {
80   0; #no, we're postpaid
81 }
82
83 1;