Added a new Plan type, base_rate, which uses the cust_pkg->options to control pricing.
[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   'fields' => {
13     'setup_fee'     => { 'name' => 'Setup fee for this package',
14                          'default' => 0,
15                        },
16     'recur_fee'     => { 'name' => 'Recurring Base fee for this package',
17                          'default' => 0,
18                        },
19     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
20                                    ' of service at cancellation',
21                          'type' => 'checkbox',
22                        },
23     'externalid' => { 'name'   => 'Optional External ID',
24                       'default' => '',
25                     },
26   },
27   'fieldorder' => [ 'setup_fee', 'recur_fee', 'unused_credit', 
28                     'externalid' ],
29   'weight' => 10,
30 );
31
32 sub calc_setup {
33   my($self, $cust_pkg, $sdate, $details ) = @_;
34
35   my $i = 0;
36   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
37   while ($i < $count) {
38     push @$details, $self->option( 'additional_info' . $i++ );
39   }
40
41   $self->option('setup_fee');
42 }
43
44 sub calc_recur {
45   my($self, $cust_pkg) = @_;
46   $self->reset_usage($cust_pkg);
47   $self->base_recur($cust_pkg);
48 }
49
50 sub base_recur {
51   my($self, $cust_pkg) = @_;
52   my $units = $cust_pkg->option('units') ? $cust_pkg->option('units') : 1 ;
53         # default to 1 if not found
54   sprintf("%.2f", 
55           ($self->option('recur_fee') * $units ) 
56   );
57 }
58
59 sub calc_remain {
60   my ($self, $cust_pkg) = @_;
61   my $time = time;  #should be able to pass this in for credit calculation
62   my $next_bill = $cust_pkg->getfield('bill') || 0;
63   my $last_bill = $cust_pkg->last_bill || 0;
64   return 0 if    ! $self->base_recur
65               || ! $self->option('unused_credit', 1)
66               || ! $last_bill
67               || ! $next_bill
68               || $next_bill < $time;
69
70   my %sec = (
71     'h' =>    3600, # 60 * 60
72     'd' =>   86400, # 60 * 60 * 24
73     'w' =>  604800, # 60 * 60 * 24 * 7
74     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
75   );
76
77   $self->freq =~ /^(\d+)([hdwm]?)$/
78     or die 'unparsable frequency: '. $self->freq;
79   my $freq_sec = $1 * $sec{$2||'m'};
80   return 0 unless $freq_sec;
81
82   sprintf("%.2f", $self->base_recur * ( $next_bill - $time ) / $freq_sec );
83
84 }
85
86 sub is_free_options {
87   qw( setup_fee recur_fee );
88 }
89
90 sub is_prepaid {
91   0; #no, we're postpaid
92 }
93
94 sub reset_usage {
95   my($self, $cust_pkg) = @_;
96   my %values = map { $_, $self->option($_) } 
97     grep { $self->option($_, 'hush') } 
98     qw(seconds upbytes downbytes totalbytes);
99   $cust_pkg->set_usage(\%values);
100 }
101
102 1;