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'.
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 )],
76 my $str = $self->SUPER::price_info;
77 $str .= " plus usage" if $str;
81 #hacked-up false laziness w/sqlradacct_hour,
82 # but keeping it separate to start with is safer for existing folks
84 my($self, $cust_pkg, $sdate, $details ) = @_;
86 my $last_bill = $cust_pkg->last_bill;
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)
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);
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);
100 while ( $day_start < $billday_start ) {
102 my($day, $mon, $year) = (localtime($day_start))[3,5];
103 my $tomorrow = timelocal_nocheck(0,0,0, $day+1, $mon, $year);
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 );
109 my $hours = $cust_pkg->seconds_since_sqlradacct(@range) / 3600;
110 $hours -= $self->option('recur_included_hours');
111 $hours = 0 if $hours < 0;
113 my $input = $cust_pkg->attribute_since_sqlradacct( @range,
117 my $output = $cust_pkg->attribute_since_sqlradacct( @range,
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;
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');
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');
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');
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');
152 my $fordate = time2str('for %a %b %o, %Y', $day_start);
154 if ( $self->option('recur_total_charge') > 0 ) {
155 push @$details, "Data $fordate ".
156 sprintf('%.1f', $total). " megs: $totalcharge";
158 if ( $self->option('recur_input_charge') > 0 ) {
159 push @$details, "Download $fordate ".
160 sprintf('%.1f', $input). " megs: $inputcharge";
162 if ( $self->option('recur_output_charge') > 0 ) {
163 push @$details, "Upload $fordate".
164 sprintf('%.1f', $output). " megs: $outputcharge";
166 if ( $self->option('recur_hourly_charge') > 0 ) {
167 push @$details, "Time $fordate ".
168 sprintf('%.1f', $hours). " hours: $hourscharge";
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";
177 $charges += $daily_charges;
179 $day_start = $tomorrow;
182 $self->option('recur_fee') + $charges;
185 sub can_discount { 0; }
187 sub is_free_options {
188 qw( setup_fee recur_fee recur_hourly_charge
189 recur_input_charge recur_output_charge recur_total_charge );
193 my($self, $cust_pkg) = @_;
194 $self->option('recur_fee');