3cc46acc4dcfb45d1f6155d7629e9221bcfb11c9
[freeside.git] / FS / FS / part_pkg / sqlradacct_hour.pm
1 package FS::part_pkg::sqlradacct_hour;
2
3 use strict;
4 use vars qw(@ISA %info);
5 #use FS::Record qw(qsearch qsearchs);
6 use FS::part_pkg::flat;
7
8 @ISA = qw(FS::part_pkg::flat);
9
10 %info = (
11   'name' => 'Base charge plus per-hour (and for data) from an SQL RADIUS radacct table',
12   'shortname' => 'Usage charges from RADIUS',
13   'inherit_fields' => [ 'global_Mixin' ],
14   'fields' => {
15     'recur_included_hours' => { 'name' => 'Hours included',
16                                 'default' => 0,
17                               },
18     'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
19                                'default' => 0,
20                              },
21     'recur_hourly_cap'    => { 'name' => 'Maximum overage charge for hours'.
22                                          ' (0 means no cap)',
23
24                                'default' => 0,
25                              },
26
27     'recur_included_input' => { 'name' => 'Upload megabytes included',
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 overage charge for upload'.
35                                          ' (0 means no cap)',
36                                'default' => 0,
37                              },
38
39     'recur_included_output' => { 'name' => 'Download megabytes included',
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 overage charge for download'.
47                                          ' (0 means no cap)',
48                                'default' => 0,
49                              },
50
51     'recur_included_total' => { 'name' =>
52                                      'Total megabytes included',
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 overage charge for total'.
60                                         ' megabytes (0 means no cap)',
61                                'default' => 0,
62                              },
63
64     'global_cap'         => { 'name' => 'Global cap on all overage charges'.
65                                         ' (0 means no cap)',
66                               'default' => 0,
67                             },
68
69   },
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 )],
71   #'setup' => 'what.setup_fee.value',
72   #'recur' => '\'my $last_bill = $cust_pkg->last_bill; my $hours = $cust_pkg->seconds_since_sqlradacct($last_bill, $sdate ) / 3600 - \' + what.recur_included_hours.value + \'; $hours = 0 if $hours < 0; my $input = $cust_pkg->attribute_since_sqlradacct($last_bill, $sdate, \"AcctInputOctets\" ) / 1048576; my $output = $cust_pkg->attribute_since_sqlradacct($last_bill, $sdate, \"AcctOutputOctets\" ) / 1048576; my $total = $input + $output - \' + what.recur_included_total.value + \'; $total = 0 if $total < 0; my $input = $input - \' + what.recur_included_input.value + \'; $input = 0 if $input < 0; my $output = $output - \' + what.recur_included_output.value + \'; $output = 0 if $output < 0; my $totalcharge = sprintf(\"%.2f\", \' + what.recur_total_charge.value + \' * $total); my $inputcharge = sprintf(\"%.2f\", \' + what.recur_input_charge.value + \' * $input); my $outputcharge = sprintf(\"%.2f\", \' + what.recur_output_charge.value + \' * $output); my $hourscharge = sprintf(\"%.2f\", \' + what.recur_hourly_charge.value + \' * $hours); if ( \' + what.recur_total_charge.value + \' > 0 ) { push @details, \"Last month\\\'s data \". sprintf(\"%.1f\", $total). \" megs: \\\$$totalcharge\" } if ( \' + what.recur_input_charge.value + \' > 0 ) { push @details, \"Last month\\\'s download \". sprintf(\"%.1f\", $input). \" megs: \\\$$inputcharge\" } if ( \' + what.recur_output_charge.value + \' > 0 ) { push @details, \"Last month\\\'s upload \". sprintf(\"%.1f\", $output). \" megs: \\\$$outputcharge\" } if ( \' + what.recur_hourly_charge.value + \' > 0 ) { push @details, \"Last month\\\'s time \". sprintf(\"%.1f\", $hours). \" hours: \\\$$hourscharge\"; } \' + what.recur_fee.value + \' + $hourscharge + $inputcharge + $outputcharge + $totalcharge ;\'',
73   'weight' => 40,
74 );
75
76 sub calc_recur {
77   my($self, $cust_pkg, $sdate, $details ) = @_;
78
79   my $last_bill = $cust_pkg->last_bill;
80   my $hours = $cust_pkg->seconds_since_sqlradacct($last_bill, $$sdate ) / 3600;
81   $hours -= $self->option('recur_included_hours');
82   $hours = 0 if $hours < 0;
83
84   my $input = $cust_pkg->attribute_since_sqlradacct(  $last_bill,
85                                                       $$sdate,
86                                                       'AcctInputOctets' )
87               / 1048576;
88
89   my $output = $cust_pkg->attribute_since_sqlradacct( $last_bill,
90                                                       $$sdate,
91                                                       'AcctOutputOctets' )
92                / 1048576;
93
94   my $total = $input + $output - $self->option('recur_included_total');
95   $total = 0 if $total < 0;
96   $input = $input - $self->option('recur_included_input');
97   $input = 0 if $input < 0;
98   $output = $output - $self->option('recur_included_output');
99   $output = 0 if $output < 0;
100
101   my $totalcharge =
102     $total  * sprintf('%.2f', $self->option('recur_total_charge'));
103   $totalcharge = $self->option('recur_total_cap')
104     if $self->option('recur_total_cap')
105     && $totalcharge > $self->option('recur_total_cap');
106
107   my $inputcharge =
108     $input  * sprintf('%.2f', $self->option('recur_input_charge'));
109   $inputcharge = $self->option('recur_input_cap')
110     if $self->option('recur_input_cap')
111     && $inputcharge > $self->option('recur_input_cap');
112
113   my $outputcharge = 
114     $output * sprintf('%.2f', $self->option('recur_output_charge'));
115   $outputcharge = $self->option('recur_output_cap')
116     if $self->option('recur_output_cap')
117     && $outputcharge > $self->option('recur_output_cap');
118
119   my $hourscharge =
120     $hours * sprintf('%.2f', $self->option('recur_hourly_charge'));
121   $hourscharge = $self->option('recur_hourly_cap')
122     if $self->option('recur_hourly_cap')
123     && $hourscharge > $self->option('recur_hourly_cap');
124
125   if ( $self->option('recur_total_charge') > 0 ) {
126     push @$details, "Last month's data ".
127                     sprintf('%.1f', $total). " megs: $totalcharge";
128   }
129   if ( $self->option('recur_input_charge') > 0 ) {
130     push @$details, "Last month's download ".
131                    sprintf('%.1f', $input). " megs: $inputcharge";
132   }
133   if ( $self->option('recur_output_charge') > 0 ) {
134     push @$details, "Last month's upload ".
135                    sprintf('%.1f', $output). " megs: $outputcharge";
136   }
137   if ( $self->option('recur_hourly_charge')  > 0 ) {
138     push @$details, "Last month\'s time ".
139                    sprintf('%.1f', $hours). " hours: $hourscharge";
140   }
141
142   my $charges = $hourscharge + $inputcharge + $outputcharge + $totalcharge;
143   if ( $self->option('global_cap') && $charges > $self->option('global_cap') ) {
144     $charges = $self->option('global_cap');
145     push @$details, "Usage charges capped at: $charges";
146   }
147
148   $self->option('recur_fee') + $charges;
149 }
150
151 sub can_discount { 0; }
152
153 sub is_free_options {
154   qw( setup_fee recur_fee recur_hourly_charge
155       recur_input_charge recur_output_charge recur_total_charge );
156 }
157
158 sub base_recur {
159   my($self, $cust_pkg) = @_;
160   $self->option('recur_fee');
161 }
162
163 1;