a2b64c7409838ba1a9e744ab8ced7addc99cf6b7
[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 price_info {
77     my $self = shift;
78     my $str = $self->SUPER::price_info;
79     $str .= " plus usage" if $str;
80     $str;
81 }
82
83 sub calc_recur {
84   my($self, $cust_pkg, $sdate, $details ) = @_;
85
86   my $last_bill = $cust_pkg->last_bill;
87   my $hours = $cust_pkg->seconds_since_sqlradacct($last_bill, $$sdate ) / 3600;
88   $hours -= $self->option('recur_included_hours');
89   $hours = 0 if $hours < 0;
90
91   my $input = $cust_pkg->attribute_since_sqlradacct(  $last_bill,
92                                                       $$sdate,
93                                                       'AcctInputOctets' )
94               / 1048576;
95
96   my $output = $cust_pkg->attribute_since_sqlradacct( $last_bill,
97                                                       $$sdate,
98                                                       'AcctOutputOctets' )
99                / 1048576;
100
101   my $total = $input + $output - $self->option('recur_included_total');
102   $total = 0 if $total < 0;
103   $input = $input - $self->option('recur_included_input');
104   $input = 0 if $input < 0;
105   $output = $output - $self->option('recur_included_output');
106   $output = 0 if $output < 0;
107
108   my $totalcharge =
109      sprintf('%.2f', $total * $self->option('recur_total_charge'));
110   $totalcharge = $self->option('recur_total_cap')
111     if $self->option('recur_total_cap')
112     && $totalcharge > $self->option('recur_total_cap');
113
114   my $inputcharge =
115      sprintf('%.2f', $input * $self->option('recur_input_charge'));
116   $inputcharge = $self->option('recur_input_cap')
117     if $self->option('recur_input_cap')
118     && $inputcharge > $self->option('recur_input_cap');
119
120   my $outputcharge = 
121     sprintf('%.2f', $output * $self->option('recur_output_charge'));
122   $outputcharge = $self->option('recur_output_cap')
123     if $self->option('recur_output_cap')
124     && $outputcharge > $self->option('recur_output_cap');
125
126   my $hourscharge =
127     sprintf('%.2f', $hours * $self->option('recur_hourly_charge'));
128   $hourscharge = $self->option('recur_hourly_cap')
129     if $self->option('recur_hourly_cap')
130     && $hourscharge > $self->option('recur_hourly_cap');
131
132   if ( $self->option('recur_total_charge') > 0 ) {
133     push @$details, "Last month's data ".
134                     sprintf('%.1f', $total). " megs: $totalcharge";
135   }
136   if ( $self->option('recur_input_charge') > 0 ) {
137     push @$details, "Last month's download ".
138                    sprintf('%.1f', $input). " megs: $inputcharge";
139   }
140   if ( $self->option('recur_output_charge') > 0 ) {
141     push @$details, "Last month's upload ".
142                    sprintf('%.1f', $output). " megs: $outputcharge";
143   }
144   if ( $self->option('recur_hourly_charge')  > 0 ) {
145     push @$details, "Last month\'s time ".
146                    sprintf('%.1f', $hours). " hours: $hourscharge";
147   }
148
149   my $charges = $hourscharge + $inputcharge + $outputcharge + $totalcharge;
150   if ( $self->option('global_cap') && $charges > $self->option('global_cap') ) {
151     $charges = $self->option('global_cap');
152     push @$details, "Usage charges capped at: $charges";
153   }
154
155   $self->option('recur_fee') + $charges;
156 }
157
158 sub can_discount { 0; }
159
160 sub is_free_options {
161   qw( setup_fee recur_fee recur_hourly_charge
162       recur_input_charge recur_output_charge recur_total_charge );
163 }
164
165 sub base_recur {
166   my($self, $cust_pkg) = @_;
167   $self->option('recur_fee');
168 }
169
170 1;