This commit was generated by cvs2svn to compensate for changes in r6255,
[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->base_recur($cust_pkg);
47 }
48
49 sub base_recur {
50   my($self, $cust_pkg) = @_;
51   my $units = $cust_pkg->option('units') ? $cust_pkg->option('units') : 1 ;
52         # default to 1 if not found
53   sprintf("%.2f", 
54           ($self->option('recur_fee') * $units ) 
55   );
56 }
57
58 sub calc_remain {
59   my ($self, $cust_pkg) = @_;
60   my $time = time;  #should be able to pass this in for credit calculation
61   my $next_bill = $cust_pkg->getfield('bill') || 0;
62   my $last_bill = $cust_pkg->last_bill || 0;
63   return 0 if    ! $self->base_recur
64               || ! $self->option('unused_credit', 1)
65               || ! $last_bill
66               || ! $next_bill
67               || $next_bill < $time;
68
69   my %sec = (
70     'h' =>    3600, # 60 * 60
71     'd' =>   86400, # 60 * 60 * 24
72     'w' =>  604800, # 60 * 60 * 24 * 7
73     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
74   );
75
76   $self->freq =~ /^(\d+)([hdwm]?)$/
77     or die 'unparsable frequency: '. $self->freq;
78   my $freq_sec = $1 * $sec{$2||'m'};
79   return 0 unless $freq_sec;
80
81   sprintf("%.2f", $self->base_recur * ( $next_bill - $time ) / $freq_sec );
82
83 }
84
85 sub is_free_options {
86   qw( setup_fee recur_fee );
87 }
88
89 sub is_prepaid {
90   0; #no, we're postpaid
91 }
92
93 1;