This commit was generated by cvs2svn to compensate for changes in r3921,
[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;
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     'fields' => {
13       'setup_fee' => { 'name' => 'Setup fee for this package',
14                        'default' => 0,
15                      },
16       'recur_flat' => { 'name' => 'Base monthly charge for this package',
17                         'default' => 0,
18                       },
19       'recur_included_hours' => { 'name' => 'Hours included',
20                                   'default' => 0,
21                                 },
22       'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
23                                  'default' => 0,
24                                },
25       'recur_included_input' => { 'name' => 'Upload megabytes included',
26                                   'default' => 0,
27                                 },
28       'recur_input_charge' => { 'name' =>
29                                         'Additional charge per megabyte upload',
30                                 'default' => 0,
31                               },
32       'recur_included_output' => { 'name' => 'Download megabytes included',
33                                    'default' => 0,
34                                 },
35       'recur_output_charge' => { 'name' =>
36                                        'Additional charge per megabyte download',
37                                 'default' => 0,
38                               },
39       'recur_included_total' => { 'name' =>
40                                        'Total megabytes included',
41                                   'default' => 0,
42                                 },
43       'recur_total_charge' => { 'name' =>
44                                  'Additional charge per megabyte total',
45                                 'default' => 0,
46                               },
47     },
48     'fieldorder' => [qw( setup_fee recur_flat recur_included_hours recur_hourly_charge recur_included_input recur_input_charge recur_included_output recur_output_charge recur_included_total recur_total_charge )],
49     #'setup' => 'what.setup_fee.value',
50     #'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_flat.value + \' + $hourscharge + $inputcharge + $outputcharge + $totalcharge ;\'',
51     'weight' => 40,
52 );
53
54 sub calc_recur {
55   my($self, $cust_pkg, $sdate, $details ) = @_;
56
57   my $last_bill = $cust_pkg->last_bill;
58   my $hours = $cust_pkg->seconds_since_sqlradacct($last_bill, $$sdate ) / 3600;
59   $hours -= $self->option('recur_included_hours');
60   $hours = 0 if $hours < 0;
61
62   my $input = $cust_pkg->attribute_since_sqlradacct(  $last_bill,
63                                                       $$sdate,
64                                                       'AcctInputOctets' )
65               / 1048576;
66
67   my $output = $cust_pkg->attribute_since_sqlradacct( $last_bill,
68                                                       $$sdate,
69                                                       'AcctOutputOctets' )
70                / 1048576;
71
72   my $total = $input + $output - $self->option('recur_included_total');
73   $total = 0 if $total < 0;
74   $input = $input - $self->option('recur_included_input');
75   $input = 0 if $input < 0;
76   $output = $output - $self->option('recur_included_output');
77   $output = 0 if $output < 0;
78
79   my $totalcharge =
80     $total  * sprintf('%.2f', $self->option('recur_total_charge'));
81   my $inputcharge =
82     $input  * sprintf('%.2f', $self->option('recur_input_charge'));
83   my $outputcharge = 
84     $output * sprintf('%.2f', $self->option('recur_output_charge'));
85
86   my $hourscharge =
87     $hours * sprintf('%.2f', $self->option('recur_hourly_charge'));
88
89   if ( $self->option('recur_total_charge') > 0 ) {
90     push @$details, "Last month's data ".
91                     sprintf('%.1f', $total). " megs: $totalcharge";
92   }
93   if ( $self->option('recur_input_charge') > 0 ) {
94     push @$details, "Last month's download ".
95                    sprintf('%.1f', $input). " megs: $inputcharge";
96   }
97   if ( $self->option('recur_output_charge') > 0 ) {
98     push @$details, "Last month's upload ".
99                    sprintf('%.1f', $output). " megs: $outputcharge";
100   }
101   if ( $self->option('recur_hourly_charge')  > 0 ) {
102     push @$details, "Last month\'s time ".
103                    sprintf('%.1f', $hours). " hours: $hourscharge";
104   }
105
106   $self->option('recur_flat')
107     + $hourscharge + $inputcharge + $outputcharge + $totalcharge;
108 }
109
110 sub is_free_options {
111   qw( setup_fee recur_flat recur_hourly_charge
112       recur_input_charge recur_output_charge recur_total_charge );
113 }
114
115 1;