per-day radius usage, RT#19771
[freeside.git] / FS / FS / part_pkg / sqlradacct_daily.pm
1 package FS::part_pkg::sqlradacct_daily;
2 use base qw( FS::part_pkg::flat );
3
4 use strict;
5 use vars qw(%info);
6 use Time::Local qw( timelocal timelocal_nocheck );
7 use Date::Format;
8 #use FS::Record qw(qsearch qsearchs);
9
10 %info = (
11   'name' => 'Time and data charges from an SQL RADIUS radacct table, with per-day limits',
12   'shortname' => 'Daily usage charges from RADIUS',
13   'inherit_fields' => [ 'global_Mixin' ],
14   'fields' => {
15     'recur_included_hours' => { 'name' => 'Hours included per day',
16                                 'default' => 0,
17                               },
18     'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
19                                'default' => 0,
20                              },
21     'recur_hourly_cap'    => { 'name' => 'Maximum daily charge for hours'.
22                                          ' (0 means no cap)',
23
24                                'default' => 0,
25                              },
26
27     'recur_included_input' => { 'name' => 'Upload megabytes included per day',
28                                 'default' => 0,
29                               },
30     'recur_input_charge' => { 'name' =>
31                                       'Additional charge per megabyte upload',
32                               'default' => 0,
33                             },
34     'recur_input_cap'    => { 'name' => 'Maximum daily charge for upload'.
35                                          ' (0 means no cap)',
36                                'default' => 0,
37                              },
38
39     'recur_included_output' => { 'name' => 'Download megabytes included per day',
40                                  'default' => 0,
41                               },
42     'recur_output_charge' => { 'name' =>
43                                      'Additional charge per megabyte download',
44                               'default' => 0,
45                             },
46     'recur_output_cap'    => { 'name' => 'Maximum daily charge for download'.
47                                          ' (0 means no cap)',
48                                'default' => 0,
49                              },
50
51     'recur_included_total' => { 'name' =>
52                                      'Total megabytes included per day',
53                                 'default' => 0,
54                               },
55     'recur_total_charge' => { 'name' =>
56                                'Additional charge per megabyte total',
57                               'default' => 0,
58                             },
59     'recur_total_cap'    => { 'name' => 'Maximum daily charge for total'.
60                                         ' megabytes (0 means no cap)',
61                                'default' => 0,
62                              },
63
64     'global_cap'         => { 'name' => 'Daily cap on all overage charges'.
65                                         ' (0 means no cap)',
66                               'default' => 0,
67                             },
68
69   },
70   'fieldorder' => [qw( recur_included_hours recur_hourly_charge recur_hourly_cap recur_included_input recur_input_charge recur_input_cap recur_included_output recur_output_charge recur_output_cap recur_included_total recur_total_charge recur_total_cap global_cap )],
71   'weight' => 41,
72 );
73
74 sub price_info {
75     my $self = shift;
76     my $str = $self->SUPER::price_info;
77     $str .= " plus usage" if $str;
78     $str;
79 }
80
81 #hacked-up false laziness w/sqlradacct_hour,
82 # but keeping it separate to start  with is safer for existing folks
83 sub calc_recur {
84   my($self, $cust_pkg, $sdate, $details ) = @_;
85
86   my $last_bill = $cust_pkg->last_bill;
87
88   my $charges = 0;
89
90   #loop over each day starting with last_bill inclusive (since we generated a
91   # bill that day, we didn't have a full picture of the day's usage)
92   # and ending with sdate exclusive (same reason)
93
94   my($l_day, $l_mon, $l_year) = (localtime($last_bill))[3,5];
95   my $day_start = timelocal(0,0,0, $l_day, $l_mon, $l_year);
96
97   my($s_day, $s_mon, $s_year) = (localtime($$sdate))[3,5];
98   my $billday_start = timelocal(0,0,0, $s_day, $s_mon, $s_year);
99
100   while ( $day_start < $billday_start ) {
101
102     my($day, $mon, $year) = (localtime($day_start))[3,5];
103     my $tomorrow = timelocal_nocheck(0,0,0, $day+1, $mon, $year);
104
105     #afact the usage methods already use the lower bound inclusive and the upper
106     # exclusive, so no need for $tomorrow-1
107     my @range = ( $day_start, $tomorrow );
108                                            
109     my $hours = $cust_pkg->seconds_since_sqlradacct(@range) / 3600;
110     $hours -= $self->option('recur_included_hours');
111     $hours = 0 if $hours < 0;
112
113     my $input = $cust_pkg->attribute_since_sqlradacct( @range,
114                                                        'AcctInputOctets')
115                 / 1048576;
116
117     my $output = $cust_pkg->attribute_since_sqlradacct( @range,
118                                                         'AcctOutputOctets' )
119                  / 1048576;
120
121     my $total = $input + $output - $self->option('recur_included_total');
122     $total = 0 if $total < 0;
123     $input = $input - $self->option('recur_included_input');
124     $input = 0 if $input < 0;
125     $output = $output - $self->option('recur_included_output');
126     $output = 0 if $output < 0;
127
128     my $totalcharge =
129        sprintf('%.2f', $total * $self->option('recur_total_charge'));
130     $totalcharge = $self->option('recur_total_cap')
131       if $self->option('recur_total_cap')
132       && $totalcharge > $self->option('recur_total_cap');
133
134     my $inputcharge =
135        sprintf('%.2f', $input * $self->option('recur_input_charge'));
136     $inputcharge = $self->option('recur_input_cap')
137       if $self->option('recur_input_cap')
138       && $inputcharge > $self->option('recur_input_cap');
139
140     my $outputcharge = 
141       sprintf('%.2f', $output * $self->option('recur_output_charge'));
142     $outputcharge = $self->option('recur_output_cap')
143       if $self->option('recur_output_cap')
144       && $outputcharge > $self->option('recur_output_cap');
145
146     my $hourscharge =
147       sprintf('%.2f', $hours * $self->option('recur_hourly_charge'));
148     $hourscharge = $self->option('recur_hourly_cap')
149       if $self->option('recur_hourly_cap')
150       && $hourscharge > $self->option('recur_hourly_cap');
151
152     my $fordate = time2str('for %a %b %o, %Y', $day_start);
153
154     if ( $self->option('recur_total_charge') > 0 ) {
155       push @$details, "Data $fordate ".
156                       sprintf('%.1f', $total). " megs: $totalcharge";
157     }
158     if ( $self->option('recur_input_charge') > 0 ) {
159       push @$details, "Download $fordate ".
160                      sprintf('%.1f', $input). " megs: $inputcharge";
161     }
162     if ( $self->option('recur_output_charge') > 0 ) {
163       push @$details, "Upload $fordate".
164                      sprintf('%.1f', $output). " megs: $outputcharge";
165     }
166     if ( $self->option('recur_hourly_charge')  > 0 ) {
167       push @$details, "Time $fordate ".
168                      sprintf('%.1f', $hours). " hours: $hourscharge";
169     }
170
171     my $daily_charges = $hourscharge + $inputcharge + $outputcharge + $totalcharge;
172     if ( $self->option('global_cap') && $charges > $self->option('global_cap') ) {
173       $charges = $self->option('global_cap');
174       push @$details, "Usage charges $fordate capped at: $charges";
175     }
176
177     $charges += $daily_charges;
178
179     $day_start = $tomorrow;
180   }
181
182   $self->option('recur_fee') + $charges;
183 }
184
185 sub can_discount { 0; }
186
187 sub is_free_options {
188   qw( setup_fee recur_fee recur_hourly_charge
189       recur_input_charge recur_output_charge recur_total_charge );
190 }
191
192 sub base_recur {
193   my($self, $cust_pkg) = @_;
194   $self->option('recur_fee');
195 }
196
197 1;