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