92e72cf8a7190b0f2a8770ad57c3078cca31e91f
[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::UI::bytecount;
7 use FS::part_pkg;
8
9 @ISA = qw(FS::part_pkg);
10
11 %info = (
12   'name' => 'Flat rate (anniversary billing)',
13   'fields' => {
14     'setup_fee'     => { 'name' => 'Setup fee for this package',
15                          'default' => 0,
16                        },
17     'recur_fee'     => { 'name' => 'Recurring fee for this package',
18                          'default' => 0,
19                        },
20     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
21                                    ' of service at cancellation',
22                          'type' => 'checkbox',
23                        },
24     'externalid' => { 'name'   => 'Optional External ID',
25                       'default' => '',
26                     },
27     'seconds'       => { 'name' => 'Time limit for this package',
28                          'default' => '',
29                          'check' => sub { shift =~ /^\d*$/ },
30                        },
31     'upbytes'       => { 'name' => 'Upload limit for this package',
32                          'default' => '',
33                          'check' => sub { shift =~ /^\d*$/ },
34                          'format' => \&FS::UI::bytecount::display_bytecount,
35                          'parse' => \&FS::UI::bytecount::parse_bytecount,
36                        },
37     'downbytes'     => { 'name' => 'Download limit for this package',
38                          'default' => '',
39                          'check' => sub { shift =~ /^\d*$/ },
40                          'format' => \&FS::UI::bytecount::display_bytecount,
41                          'parse' => \&FS::UI::bytecount::parse_bytecount,
42                        },
43     'totalbytes'    => { 'name' => 'Transfer limit for this package',
44                          'default' => '',
45                          'check' => sub { shift =~ /^\d*$/ },
46                          'format' => \&FS::UI::bytecount::display_bytecount,
47                          'parse' => \&FS::UI::bytecount::parse_bytecount,
48                        },
49     'recharge_amount'       => { 'name' => 'Cost of recharge for this package',
50                          'default' => '',
51                          'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
52                        },
53     'recharge_seconds'      => { 'name' => 'Recharge time for this package',
54                          'default' => '',
55                          'check' => sub { shift =~ /^\d*$/ },
56                        },
57     'recharge_upbytes'      => { 'name' => 'Recharge upload for this package',
58                          'default' => '',
59                          'check' => sub { shift =~ /^\d*$/ },
60                          'format' => \&FS::UI::bytecount::display_bytecount,
61                          'parse' => \&FS::UI::bytecount::parse_bytecount,
62                        },
63     'recharge_downbytes'    => { 'name' => 'Recharge download for this package',
64                          'default' => '',
65                          'check' => sub { shift =~ /^\d*$/ },
66                          'format' => \&FS::UI::bytecount::display_bytecount,
67                          'parse' => \&FS::UI::bytecount::parse_bytecount,
68                        },
69     'recharge_totalbytes'   => { 'name' => 'Recharge transfer for this package',
70                          'default' => '',
71                          'check' => sub { shift =~ /^\d*$/ },
72                          'format' => \&FS::UI::bytecount::display_bytecount,
73                          'parse' => \&FS::UI::bytecount::parse_bytecount,
74                        },
75     'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
76                                     ' over into current period',
77                           'type' => 'checkbox',
78                         },
79     'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
80                                     'package recharge',
81                           'type' => 'checkbox',
82                         },
83   },
84   'fieldorder' => [ 'setup_fee', 'recur_fee', 'unused_credit', 
85                     'seconds', 'upbytes', 'downbytes', 'totalbytes',
86                     'recharge_amount', 'recharge_seconds', 'recharge_upbytes',
87                     'recharge_downbytes', 'recharge_totalbytes',
88                     'usage_rollover', 'recharge_reset', 'externalid' ],
89   'weight' => 10,
90 );
91
92 sub calc_setup {
93   my($self, $cust_pkg, $sdate, $details ) = @_;
94
95   my $i = 0;
96   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
97   while ($i < $count) {
98     push @$details, $self->option( 'additional_info' . $i++ );
99   }
100
101   $self->option('setup_fee');
102 }
103
104 sub calc_recur {
105   my($self, $cust_pkg) = @_;
106   $self->base_recur($cust_pkg);
107 }
108
109 sub base_recur {
110   my($self, $cust_pkg) = @_;
111   $self->option('recur_fee', 1) || 0;
112 }
113
114 sub calc_remain {
115   my ($self, $cust_pkg, %options) = @_;
116
117   my $time;
118   if ($options{'time'}) {
119     $time = $options{'time'};
120   } else {
121     $time = time;
122   }
123
124   my $next_bill = $cust_pkg->getfield('bill') || 0;
125   my $last_bill = $cust_pkg->last_bill || 0;
126   return 0 if    ! $self->base_recur
127               || ! $self->option('unused_credit', 1)
128               || ! $last_bill
129               || ! $next_bill
130               || $next_bill < $time;
131
132   my %sec = (
133     'h' =>    3600, # 60 * 60
134     'd' =>   86400, # 60 * 60 * 24
135     'w' =>  604800, # 60 * 60 * 24 * 7
136     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
137   );
138
139   $self->freq =~ /^(\d+)([hdwm]?)$/
140     or die 'unparsable frequency: '. $self->freq;
141   my $freq_sec = $1 * $sec{$2||'m'};
142   return 0 unless $freq_sec;
143
144   sprintf("%.2f", $self->base_recur * ( $next_bill - $time ) / $freq_sec );
145
146 }
147
148 sub is_free_options {
149   qw( setup_fee recur_fee );
150 }
151
152 sub is_prepaid {
153   0; #no, we're postpaid
154 }
155
156 sub reset_usage {
157   my($self, $cust_pkg) = @_;
158   my %values = map { $_, $self->option($_) } 
159     grep { $self->option($_, 'hush') } 
160     qw(seconds upbytes downbytes totalbytes);
161   if ($self->option('usage_rollover', 1)) {
162     $cust_pkg->recharge(\%values);
163   }else{
164     $cust_pkg->set_usage(\%values);
165   }
166 }
167
168 1;