on 3.x, don't create a default cancel reason when an expire reason is missing, #71623
[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     'monthly_cap'        => { 'name' => 'Monthly (billing frequency) cap on all overage charges'.
70                                         ' (0 means no cap)',
71                               'default' => 0,
72                             },
73
74   },
75   '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 monthly_cap )],
76   'weight' => 40.1,
77 );
78
79 sub price_info {
80     my $self = shift;
81     my $str = $self->SUPER::price_info;
82     $str .= " plus usage" if $str;
83     $str;
84 }
85
86 #hacked-up false laziness w/sqlradacct_hour,
87 # but keeping it separate to start with is safer for existing folks
88 sub calc_recur {
89   my($self, $cust_pkg, $sdate, $details ) = @_;
90
91   my $last_bill = $cust_pkg->last_bill;
92
93   my $charges = 0;
94
95   #loop over each day starting with last_bill inclusive (since we generated a
96   # bill that day, we didn't have a full picture of the day's usage)
97   # and ending with sdate exclusive (same reason)
98
99   my($l_day, $l_mon, $l_year) = (localtime($last_bill))[3..5];
100   my $day_start = timelocal(0,0,0, $l_day, $l_mon, $l_year);
101
102   my($s_day, $s_mon, $s_year) = (localtime($$sdate))[3..5];
103   my $billday_start = timelocal(0,0,0, $s_day, $s_mon, $s_year);
104
105   while ( $day_start < $billday_start ) {
106
107     my($day, $mon, $year) = (localtime($day_start))[3..5];
108     my $tomorrow = timelocal_nocheck(0,0,0, $day+1, $mon, $year);
109
110     #afact the usage methods already use the lower bound inclusive and the upper
111     # exclusive, so no need for $tomorrow-1
112     my @range = ( $day_start, $tomorrow );
113                                            
114     my $hours = $cust_pkg->seconds_since_sqlradacct(@range) / 3600;
115     $hours -= $self->option('recur_included_hours');
116     $hours = 0 if $hours < 0;
117
118     my $input = $cust_pkg->attribute_since_sqlradacct( @range,
119                                                        'AcctInputOctets')
120                 / 1048576;
121
122     my $output = $cust_pkg->attribute_since_sqlradacct( @range,
123                                                         'AcctOutputOctets' )
124                  / 1048576;
125
126     my $total = $input + $output - $self->option('recur_included_total');
127     $total = 0 if $total < 0;
128     $input = $input - $self->option('recur_included_input');
129     $input = 0 if $input < 0;
130     $output = $output - $self->option('recur_included_output');
131     $output = 0 if $output < 0;
132
133     my $totalcharge =
134        sprintf('%.2f', $total * $self->option('recur_total_charge'));
135     $totalcharge = $self->option('recur_total_cap')
136       if $self->option('recur_total_cap')
137       && $totalcharge > $self->option('recur_total_cap');
138
139     my $inputcharge =
140        sprintf('%.2f', $input * $self->option('recur_input_charge'));
141     $inputcharge = $self->option('recur_input_cap')
142       if $self->option('recur_input_cap')
143       && $inputcharge > $self->option('recur_input_cap');
144
145     my $outputcharge = 
146       sprintf('%.2f', $output * $self->option('recur_output_charge'));
147     $outputcharge = $self->option('recur_output_cap')
148       if $self->option('recur_output_cap')
149       && $outputcharge > $self->option('recur_output_cap');
150
151     my $hourscharge =
152       sprintf('%.2f', $hours * $self->option('recur_hourly_charge'));
153     $hourscharge = $self->option('recur_hourly_cap')
154       if $self->option('recur_hourly_cap')
155       && $hourscharge > $self->option('recur_hourly_cap');
156
157     my $fordate = time2str('for %a %b %o, %Y', $day_start);
158
159     if ( $self->option('recur_total_charge') > 0 ) {
160       push @$details, "Data $fordate ".
161                       sprintf('%.1f', $total). " megs: $totalcharge";
162     }
163     if ( $self->option('recur_input_charge') > 0 ) {
164       push @$details, "Download $fordate ".
165                      sprintf('%.1f', $input). " megs: $inputcharge";
166     }
167     if ( $self->option('recur_output_charge') > 0 ) {
168       push @$details, "Upload $fordate".
169                      sprintf('%.1f', $output). " megs: $outputcharge";
170     }
171     if ( $self->option('recur_hourly_charge')  > 0 ) {
172       push @$details, "Time $fordate ".
173                      sprintf('%.1f', $hours). " hours: $hourscharge";
174     }
175
176     my $daily_charges = $hourscharge + $inputcharge + $outputcharge + $totalcharge;
177     if ( $self->option('global_cap') && $charges > $self->option('global_cap') ) {
178       $charges = $self->option('global_cap');
179       push @$details, "Usage charges $fordate capped at: $charges";
180     }
181
182     $charges += $daily_charges;
183
184     $day_start = $tomorrow;
185   }
186
187   $charges = $self->option('monthly_cap')
188     if $self->option('monthly_cap')
189     && $charges > $self->option('monthly_cap');
190
191   $self->option('recur_fee') + $charges;
192 }
193
194 sub can_discount { 0; }
195
196 sub is_free_options {
197   qw( setup_fee recur_fee recur_hourly_charge
198       recur_input_charge recur_output_charge recur_total_charge );
199 }
200
201 sub base_recur {
202   my($self, $cust_pkg) = @_;
203   $self->option('recur_fee');
204 }
205
206 1;