This commit was manufactured by cvs2svn to create tag 'freeside_1_9_2'.
[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              %usage_fields %usage_recharge_fields
6              @usage_fieldorder @usage_recharge_fieldorder
7            );
8 use Tie::IxHash;
9 #use FS::Record qw(qsearch);
10 use FS::UI::bytecount;
11 use FS::part_pkg;
12
13 @ISA = qw(FS::part_pkg);
14
15 tie my %temporalities, 'Tie::IxHash',
16   'upcoming'  => "Upcoming (future)",
17   'preceding' => "Preceding (past)",
18 ;
19
20 %usage_fields = (
21
22     'seconds'       => { 'name' => 'Time limit for this package',
23                          'default' => '',
24                          'check' => sub { shift =~ /^\d*$/ },
25                        },
26     'upbytes'       => { 'name' => 'Upload limit for this package',
27                          'default' => '',
28                          'check' => sub { shift =~ /^\d*$/ },
29                          'format' => \&FS::UI::bytecount::display_bytecount,
30                          'parse' => \&FS::UI::bytecount::parse_bytecount,
31                        },
32     'downbytes'     => { 'name' => 'Download 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     'totalbytes'    => { 'name' => 'Transfer 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 );
45
46 %usage_recharge_fields = (
47
48     'recharge_amount'       => { 'name' => 'Cost of recharge for this package',
49                          'default' => '',
50                          'check' => sub { shift =~ /^\d*(\.\d{2})?$/ },
51                        },
52     'recharge_seconds'      => { 'name' => 'Recharge time for this package',
53                          'default' => '',
54                          'check' => sub { shift =~ /^\d*$/ },
55                        },
56     'recharge_upbytes'      => { 'name' => 'Recharge upload 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_downbytes'    => { 'name' => 'Recharge download for this package',
63                          'default' => '',
64                          'check' => sub { shift =~ /^\d*$/ },
65                          'format' => \&FS::UI::bytecount::display_bytecount,
66                          'parse' => \&FS::UI::bytecount::parse_bytecount,
67                        },
68     'recharge_totalbytes'   => { 'name' => 'Recharge transfer for this package',
69                          'default' => '',
70                          'check' => sub { shift =~ /^\d*$/ },
71                          'format' => \&FS::UI::bytecount::display_bytecount,
72                          'parse' => \&FS::UI::bytecount::parse_bytecount,
73                        },
74     'usage_rollover' => { 'name' => 'Allow usage from previous period to roll '.
75                                     ' over into current period',
76                           'type' => 'checkbox',
77                         },
78     'recharge_reset' => { 'name' => 'Reset usage to these values on manual '.
79                                     'package recharge',
80                           'type' => 'checkbox',
81                         },
82 );
83
84 @usage_fieldorder = qw( seconds upbytes downbytes totalbytes );
85 @usage_recharge_fieldorder = qw(
86   recharge_amount recharge_seconds recharge_upbytes
87   recharge_downbytes recharge_totalbytes
88   usage_rollover recharge_reset
89 );
90
91 %info = (
92   'name' => 'Flat rate (anniversary billing)',
93   'shortname' => 'Anniversary',
94   'fields' => {
95     'setup_fee'     => { 'name' => 'Setup fee for this package',
96                          'default' => 0,
97                        },
98     'recur_fee'     => { 'name' => 'Recurring fee for this package',
99                          'default' => 0,
100                        },
101
102     #false laziness w/voip_cdr.pm
103     'recur_temporality' => { 'name' => 'Charge recurring fee for period',
104                              'type' => 'select',
105                              'select_options' => \%temporalities,
106                            },
107     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
108                                    ' of service at cancellation',
109                          'type' => 'checkbox',
110                        },
111
112     #used in cust_pkg.pm so could add to any price plan
113     'expire_months' => { 'name' => 'Auto-add an expiration date this number of months out',
114                        },
115     #used in cust_pkg.pm so could add to any price plan where it made sense
116     'start_1st'     => { 'name' => 'Auto-add a start date to the 1st, ignoring the current month.',
117                          'type' => 'checkbox',
118                        },
119
120     %usage_fields,
121     %usage_recharge_fields,
122
123     'externalid' => { 'name'   => 'Optional External ID',
124                       'default' => '',
125                     },
126   },
127   'fieldorder' => [ qw( setup_fee recur_fee
128                         recur_temporality unused_credit
129                         expire_months start_1st
130                       ),
131                     @usage_fieldorder, @usage_recharge_fieldorder,
132                     qw( externalid ),
133                   ],
134   'weight' => 10,
135 );
136
137 sub calc_setup {
138   my($self, $cust_pkg, $sdate, $details ) = @_;
139
140   my $i = 0;
141   my $count = $self->option( 'additional_count', 'quiet' ) || 0;
142   while ($i < $count) {
143     push @$details, $self->option( 'additional_info' . $i++ );
144   }
145
146   my $quantity = $cust_pkg->quantity || 1;
147
148   sprintf("%.2f", $quantity * $self->unit_setup($cust_pkg, $sdate, $details) );
149 }
150
151 sub unit_setup {
152   my($self, $cust_pkg, $sdate, $details ) = @_;
153
154   $self->option('setup_fee');
155 }
156
157 sub calc_recur {
158   my $self = shift;
159   my($cust_pkg) = @_;
160
161   #my $last_bill = $cust_pkg->last_bill;
162   my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
163
164   return 0
165     if $self->option('recur_temporality', 1) eq 'preceding' && $last_bill == 0;
166
167   $self->base_recur(@_);
168 }
169
170 sub base_recur {
171   my($self, $cust_pkg) = @_;
172   $self->option('recur_fee', 1) || 0;
173 }
174
175 sub base_recur_permonth {
176   my($self, $cust_pkg) = @_;
177
178   return 0 unless $self->freq =~ /^\d+$/ && $self->freq > 0;
179
180   sprintf('%.2f', $self->base_recur($cust_pkg) / $self->freq );
181 }
182
183 sub calc_remain {
184   my ($self, $cust_pkg, %options) = @_;
185
186   my $time;
187   if ($options{'time'}) {
188     $time = $options{'time'};
189   } else {
190     $time = time;
191   }
192
193   my $next_bill = $cust_pkg->getfield('bill') || 0;
194
195   #my $last_bill = $cust_pkg->last_bill || 0;
196   my $last_bill = $cust_pkg->get('last_bill') || 0; #->last_bill falls back to setup
197
198   return 0 if    ! $self->base_recur($cust_pkg)
199               || ! $self->option('unused_credit', 1)
200               || ! $last_bill
201               || ! $next_bill
202               || $next_bill < $time;
203
204   my %sec = (
205     'h' =>    3600, # 60 * 60
206     'd' =>   86400, # 60 * 60 * 24
207     'w' =>  604800, # 60 * 60 * 24 * 7
208     'm' => 2629744, # 60 * 60 * 24 * 365.2422 / 12 
209   );
210
211   $self->freq =~ /^(\d+)([hdwm]?)$/
212     or die 'unparsable frequency: '. $self->freq;
213   my $freq_sec = $1 * $sec{$2||'m'};
214   return 0 unless $freq_sec;
215
216   sprintf("%.2f", $self->base_recur($cust_pkg) * ( $next_bill - $time ) / $freq_sec );
217
218 }
219
220 sub is_free_options {
221   qw( setup_fee recur_fee );
222 }
223
224 sub is_prepaid {
225   0; #no, we're postpaid
226 }
227
228 sub usage_valuehash {
229   my $self = shift;
230   map { $_, $self->option($_) }
231     grep { $self->option($_, 'hush') } 
232     qw(seconds upbytes downbytes totalbytes);
233 }
234
235 sub reset_usage {
236   my($self, $cust_pkg, %opt) = @_;
237   warn "   resetting usage counters" if defined($opt{debug}) && $opt{debug} > 1;
238   my %values = $self->usage_valuehash;
239   if ($self->option('usage_rollover', 1)) {
240     $cust_pkg->recharge(\%values);
241   }else{
242     $cust_pkg->set_usage(\%values, %opt);
243   }
244 }
245
246 1;