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