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