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