1 package FS::part_pkg::sqlradacct_daily;
2 use base qw( FS::part_pkg::flat );
6 use Time::Local qw( timelocal timelocal_nocheck );
8 #use FS::Record qw(qsearch qsearchs);
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' ],
15 'recur_included_hours' => { 'name' => 'Hours included per day',
18 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
21 'recur_hourly_cap' => { 'name' => 'Maximum daily charge for hours'.
27 'recur_included_input' => { 'name' => 'Upload megabytes included per day',
30 'recur_input_charge' => { 'name' =>
31 'Additional charge per megabyte upload',
34 'recur_input_cap' => { 'name' => 'Maximum daily charge for upload'.
39 'recur_included_output' => { 'name' => 'Download megabytes included per day',
42 'recur_output_charge' => { 'name' =>
43 'Additional charge per megabyte download',
46 'recur_output_cap' => { 'name' => 'Maximum daily charge for download'.
51 'recur_included_total' => { 'name' =>
52 'Total megabytes included per day',
55 'recur_total_charge' => { 'name' =>
56 'Additional charge per megabyte total',
59 'recur_total_cap' => { 'name' => 'Maximum daily charge for total'.
60 ' megabytes (0 means no cap)',
64 'global_cap' => { 'name' => 'Daily cap on all overage charges'.
69 'monthly_cap' => { 'name' => 'Monthly (billing frequency) cap on all overage charges'.
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 )],
81 my $str = $self->SUPER::price_info;
82 $str .= " plus usage" if $str;
86 #hacked-up false laziness w/sqlradacct_hour,
87 # but keeping it separate to start with is safer for existing folks
89 my($self, $cust_pkg, $sdate, $details ) = @_;
91 my $last_bill = $cust_pkg->last_bill;
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)
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);
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);
105 while ( $day_start < $billday_start ) {
107 my($day, $mon, $year) = (localtime($day_start))[3..5];
108 my $tomorrow = timelocal_nocheck(0,0,0, $day+1, $mon, $year);
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 );
114 my $hours = $cust_pkg->seconds_since_sqlradacct(@range) / 3600;
115 $hours -= $self->option('recur_included_hours');
116 $hours = 0 if $hours < 0;
118 my $input = $cust_pkg->attribute_since_sqlradacct( @range,
122 my $output = $cust_pkg->attribute_since_sqlradacct( @range,
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;
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');
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');
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');
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');
157 my $fordate = time2str('for %a %b %o, %Y', $day_start);
159 if ( $self->option('recur_total_charge') > 0 ) {
160 push @$details, "Data $fordate ".
161 sprintf('%.1f', $total). " megs: $totalcharge";
163 if ( $self->option('recur_input_charge') > 0 ) {
164 push @$details, "Download $fordate ".
165 sprintf('%.1f', $input). " megs: $inputcharge";
167 if ( $self->option('recur_output_charge') > 0 ) {
168 push @$details, "Upload $fordate".
169 sprintf('%.1f', $output). " megs: $outputcharge";
171 if ( $self->option('recur_hourly_charge') > 0 ) {
172 push @$details, "Time $fordate ".
173 sprintf('%.1f', $hours). " hours: $hourscharge";
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";
182 $charges += $daily_charges;
184 $day_start = $tomorrow;
187 $charges = $self->option('monthly_cap')
188 if $self->option('monthly_cap')
189 && $charges > $self->option('monthly_cap');
191 $self->option('recur_fee') + $charges;
194 sub can_discount { 0; }
196 sub is_free_options {
197 qw( setup_fee recur_fee recur_hourly_charge
198 recur_input_charge recur_output_charge recur_total_charge );
202 my($self, $cust_pkg) = @_;
203 $self->option('recur_fee');