c0fbb5a7fdf26ee6bb138157cb4380f4a2f57928
[freeside.git] / FS / FS / part_pkg / flat.pm
1 package FS::part_pkg::flat;
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' => 'Flat rate (anniversary billing)',
12   'fields' => {
13     'setup_fee'     => { 'name' => 'Setup fee for this package',
14                          'default' => 0,
15                        },
16     'recur_fee'     => { 'name' => 'Recurring 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     'seconds'       => { 'name' => 'Time limit for this package',
27                          'default' => '',
28                        },
29     'upbytes'       => { 'name' => 'Upload limit for this package',
30                          'default' => '',
31                          'format' => \&FS::UI::Web::display_bytecount,
32                          'parse' => \&FS::UI::Web::parse_bytecount,
33                        },
34     'downbytes'     => { 'name' => 'Download limit for this package',
35                          'default' => '',
36                          'format' => \&FS::UI::Web::display_bytecount,
37                          'parse' => \&FS::UI::Web::parse_bytecount,
38                        },
39     'totalbytes'    => { 'name' => 'Transfer limit for this package',
40                          'default' => '',
41                          'format' => \&FS::UI::Web::display_bytecount,
42                          'parse' => \&FS::UI::Web::parse_bytecount,
43                        },
44     'recharge_amount'       => { 'name' => 'Cost of recharge for this package',
45                          'default' => '',
46                          'format' => \&FS::UI::Web::display_bytecount,
47                          'parse' => \&FS::UI::Web::parse_bytecount,
48                        },
49     'recharge_seconds'      => { 'name' => 'Recharge time for this package',
50                          'default' => '',
51                          'format' => \&FS::UI::Web::display_bytecount,
52                          'parse' => \&FS::UI::Web::parse_bytecount,
53                        },
54     'recharge_upbytes'      => { 'name' => 'Recharge upload for this package',
55                          'default' => '',
56                          'format' => \&FS::UI::Web::display_bytecount,
57                          'parse' => \&FS::UI::Web::parse_bytecount,
58                        },
59     'recharge_downbytes'    => { 'name' => 'Recharge download for this package',
60                          'default' => '',
61                          'format' => \&FS::UI::Web::display_bytecount,
62                          'parse' => \&FS::UI::Web::parse_bytecount,
63                        },
64     'recharge_totalbytes'   => { 'name' => 'Recharge transfer for this package',
65                          'default' => '',
66                          'format' => \&FS::UI::Web::display_bytecount,
67                          'parse' => \&FS::UI::Web::parse_bytecount,
68                        },
69   },
70   'fieldorder' => [ 'setup_fee', 'recur_fee', 'unused_credit', 
71                     'seconds', 'upbytes', 'downbytes', 'totalbytes',
72                     'recharge_amount', 'recharge_seconds', 'recharge_upbytes',
73                     'recharge_downbytes', 'recharge_totalbytes',
74                     'externalid' ],
75   'weight' => 10,
76 );
77
78 sub calc_setup {
79   my($self, $cust_pkg, $sdate, $details ) = @_;
80
81   my $i = 0;
82   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
83   while ($i < $count) {
84     push @$details, $self->option( 'additional_info' . $i++ );
85   }
86
87   $self->option('setup_fee');
88 }
89
90 sub calc_recur {
91   my($self, $cust_pkg) = @_;
92   $self->reset_usage($cust_pkg);
93   $self->base_recur($cust_pkg);
94 }
95
96 sub base_recur {
97   my($self, $cust_pkg) = @_;
98   $self->option('recur_fee');
99 }
100
101 sub calc_remain {
102   my ($self, $cust_pkg) = @_;
103   my $time = time;  #should be able to pass this in for credit calculation
104   my $next_bill = $cust_pkg->getfield('bill') || 0;
105   my $last_bill = $cust_pkg->last_bill || 0;
106   return 0 if    ! $self->base_recur
107               || ! $self->option('unused_credit', 1)
108               || ! $last_bill
109               || ! $next_bill
110               || $next_bill < $time;
111
112   my %sec = (
113     'h' =>    3600, # 60 * 60
114     'd' =>   86400, # 60 * 60 * 24
115     'w' =>  604800, # 60 * 60 * 24 * 7
116     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
117   );
118
119   $self->freq =~ /^(\d+)([hdwm]?)$/
120     or die 'unparsable frequency: '. $self->freq;
121   my $freq_sec = $1 * $sec{$2||'m'};
122   return 0 unless $freq_sec;
123
124   sprintf("%.2f", $self->base_recur * ( $next_bill - $time ) / $freq_sec );
125
126 }
127
128 sub is_free_options {
129   qw( setup_fee recur_fee );
130 }
131
132 sub is_prepaid {
133   0; #no, we're postpaid
134 }
135
136 sub reset_usage {
137   my($self, $cust_pkg) = @_;
138   my %values = map { $_, $self->option($_) } 
139     grep { $self->option($_, 'hush') } 
140     qw(seconds upbytes downbytes totalbytes);
141   $cust_pkg->set_usage(\%values);
142 }
143
144 1;