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