1 package FS::part_pkg::sqlradacct_hour;
4 use vars qw(@ISA %info);
5 #use FS::Record qw(qsearch qsearchs);
6 use FS::part_pkg::flat;
8 @ISA = qw(FS::part_pkg::flat);
10 # some constants to facilitate changes
11 # maybe we display charge per petabyte in the future?
12 use constant KB => 1024;
13 use constant MB => KB * 1024;
14 use constant GB => MB * 1024;
15 use constant BU => GB; # base unit
16 use constant BS => 'gigabyte'; # BU spelled out
17 use constant BA => 'gig'; # BU abbreviation
20 'name' => 'Time and data charges from an SQL RADIUS radacct table',
21 'shortname' => 'Usage charges from RADIUS',
22 'inherit_fields' => [ 'global_Mixin' ],
24 'recur_included_hours' => { 'name' => 'Hours included',
27 'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
30 'recur_hourly_cap' => { 'name' => 'Maximum overage charge for hours'.
36 'recur_included_input' => { 'name' => 'Upload ' . BS . 's included',
39 'recur_input_charge' => { 'name' =>
40 'Additional charge per ' . BS . ' upload',
43 'recur_input_cap' => { 'name' => 'Maximum overage charge for upload'.
48 'recur_included_output' => { 'name' => 'Download ' . BS . 's included',
51 'recur_output_charge' => { 'name' =>
52 'Additional charge per ' . BS . ' download',
55 'recur_output_cap' => { 'name' => 'Maximum overage charge for download'.
60 'recur_included_total' => { 'name' =>
61 'Total ' . BS . 's included',
64 'recur_total_charge' => { 'name' =>
65 'Additional charge per ' . BS . ' total',
68 'recur_total_cap' => { 'name' => 'Maximum overage charge for total'.
69 ' ' . BS . 's (0 means no cap)',
73 'global_cap' => { 'name' => 'Global cap on all overage charges'.
79 '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 )],
85 my $str = $self->SUPER::price_info(@_);
86 $str .= " plus usage" if $str;
91 my($self, $cust_pkg, $sdate, $details ) = @_;
93 my $last_bill = $cust_pkg->last_bill;
94 my $hours = $cust_pkg->seconds_since_sqlradacct($last_bill, $$sdate ) / 3600;
95 $hours -= $self->option('recur_included_hours');
96 $hours = 0 if $hours < 0;
98 my $input = $cust_pkg->attribute_since_sqlradacct( $last_bill,
103 my $output = $cust_pkg->attribute_since_sqlradacct( $last_bill,
108 my $included_total = $self->option('recur_included_total') || 0;
110 foreach my $cust_pkg_usageprice ($cust_pkg->cust_pkg_usageprice) {
111 my $part_pkg_usageprice = $cust_pkg_usageprice->part_pkg_usageprice;
112 $included_total += $cust_pkg_usageprice->quantity * $part_pkg_usageprice->amount;
113 $addoncharge += $cust_pkg_usageprice->price;
115 my $raw_total = $input + $output;
116 push(@$details,sprintf( "%.3f %ss included, %.3f %ss used", $included_total, BA, $raw_total, BA ))
119 my $total = $input + $output - $included_total;
120 $total = 0 if $total < 0;
121 $input = $input - $self->option('recur_included_input');
122 $input = 0 if $input < 0;
123 $output = $output - $self->option('recur_included_output');
124 $output = 0 if $output < 0;
127 sprintf('%.2f', $total * $self->option('recur_total_charge'));
128 $totalcharge = $self->option('recur_total_cap')
129 if $self->option('recur_total_cap')
130 && $totalcharge > $self->option('recur_total_cap');
133 sprintf('%.2f', $input * $self->option('recur_input_charge'));
134 $inputcharge = $self->option('recur_input_cap')
135 if $self->option('recur_input_cap')
136 && $inputcharge > $self->option('recur_input_cap');
139 sprintf('%.2f', $output * $self->option('recur_output_charge'));
140 $outputcharge = $self->option('recur_output_cap')
141 if $self->option('recur_output_cap')
142 && $outputcharge > $self->option('recur_output_cap');
145 sprintf('%.2f', $hours * $self->option('recur_hourly_charge'));
146 $hourscharge = $self->option('recur_hourly_cap')
147 if $self->option('recur_hourly_cap')
148 && $hourscharge > $self->option('recur_hourly_cap');
150 if ( $self->option('recur_total_charge') > 0 ) {
152 sprintf( "Last month's data %.3f %ss: %s", $total, BA, $totalcharge );
154 if ( $self->option('recur_input_charge') > 0 ) {
156 sprintf( "Last month's download %.3f %ss: %s", $input, BA, $inputcharge );
158 if ( $self->option('recur_output_charge') > 0 ) {
160 sprintf( "Last month's upload %.3f %ss: %s", $output, BA, $outputcharge );
162 if ( $self->option('recur_hourly_charge') > 0 ) {
163 push @$details, "Last month\'s time ".
164 sprintf('%.1f', $hours). " hours: $hourscharge";
167 my $charges = $hourscharge + $inputcharge + $outputcharge + $totalcharge + $addoncharge;
168 if ( $self->option('global_cap') && $charges > $self->option('global_cap') ) {
169 $charges = $self->option('global_cap');
170 push @$details, "Usage charges capped at: $charges";
173 $self->option('recur_fee') + $charges;
176 sub can_discount { 0; }
178 sub is_free_options {
179 qw( setup_fee recur_fee recur_hourly_charge
180 recur_input_charge recur_output_charge recur_total_charge );
184 my($self, $cust_pkg) = @_;
185 $self->option('recur_fee');