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