communigate provisioning phase 2: add svc_domain.trailer -> communigate TrailerText...
[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   'fields' => {
14     'setup_fee' => { 'name' => 'Setup fee for this package',
15                      'default' => 0,
16                    },
17     'recur_fee' => { 'name' => 'Base recurring fee for this package',
18                      'default' => 0,
19                    },
20     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
21                                    ' of service at cancellation',
22                          'type' => 'checkbox',
23                        },
24
25     'recur_included_hours' => { 'name' => 'Hours included',
26                                 'default' => 0,
27                               },
28     'recur_hourly_charge' => { 'name' => 'Additional charge per hour',
29                                'default' => 0,
30                              },
31     'recur_hourly_cap'    => { 'name' => 'Maximum overage charge for hours'.
32                                          ' (0 means no cap)',
33
34                                'default' => 0,
35                              },
36
37     'recur_included_input' => { 'name' => 'Upload megabytes included',
38                                 'default' => 0,
39                               },
40     'recur_input_charge' => { 'name' =>
41                                       'Additional charge per megabyte upload',
42                               'default' => 0,
43                             },
44     'recur_input_cap'    => { 'name' => 'Maximum overage charge for upload'.
45                                          ' (0 means no cap)',
46                                'default' => 0,
47                              },
48
49     'recur_included_output' => { 'name' => 'Download megabytes included',
50                                  'default' => 0,
51                               },
52     'recur_output_charge' => { 'name' =>
53                                      'Additional charge per megabyte download',
54                               'default' => 0,
55                             },
56     'recur_output_cap'    => { 'name' => 'Maximum overage charge for download'.
57                                          ' (0 means no cap)',
58                                'default' => 0,
59                              },
60
61     'recur_included_total' => { 'name' =>
62                                      'Total megabytes included',
63                                 'default' => 0,
64                               },
65     'recur_total_charge' => { 'name' =>
66                                'Additional charge per megabyte total',
67                               'default' => 0,
68                             },
69     'recur_total_cap'    => { 'name' => 'Maximum overage charge for total'.
70                                         ' megabytes (0 means no cap)',
71                                'default' => 0,
72                              },
73
74     'global_cap'         => { 'name' => 'Global cap on all overage charges'.
75                                         ' (0 means no cap)',
76                               'default' => 0,
77                             },
78
79   },
80   'fieldorder' => [qw( setup_fee recur_fee unused_credit 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 )],
81   #'setup' => 'what.setup_fee.value',
82   #'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 ;\'',
83   'weight' => 40,
84 );
85
86 sub calc_recur {
87   my($self, $cust_pkg, $sdate, $details ) = @_;
88
89   my $last_bill = $cust_pkg->last_bill;
90   my $hours = $cust_pkg->seconds_since_sqlradacct($last_bill, $$sdate ) / 3600;
91   $hours -= $self->option('recur_included_hours');
92   $hours = 0 if $hours < 0;
93
94   my $input = $cust_pkg->attribute_since_sqlradacct(  $last_bill,
95                                                       $$sdate,
96                                                       'AcctInputOctets' )
97               / 1048576;
98
99   my $output = $cust_pkg->attribute_since_sqlradacct( $last_bill,
100                                                       $$sdate,
101                                                       'AcctOutputOctets' )
102                / 1048576;
103
104   my $total = $input + $output - $self->option('recur_included_total');
105   $total = 0 if $total < 0;
106   $input = $input - $self->option('recur_included_input');
107   $input = 0 if $input < 0;
108   $output = $output - $self->option('recur_included_output');
109   $output = 0 if $output < 0;
110
111   my $totalcharge =
112     $total  * sprintf('%.2f', $self->option('recur_total_charge'));
113   $totalcharge = $self->option('recur_total_cap')
114     if $self->option('recur_total_cap')
115     && $totalcharge > $self->option('recur_total_cap');
116
117   my $inputcharge =
118     $input  * sprintf('%.2f', $self->option('recur_input_charge'));
119   $inputcharge = $self->option('recur_input_cap')
120     if $self->option('recur_input_cap')
121     && $inputcharge > $self->option('recur_input_cap');
122
123   my $outputcharge = 
124     $output * sprintf('%.2f', $self->option('recur_output_charge'));
125   $outputcharge = $self->option('recur_output_cap')
126     if $self->option('recur_output_cap')
127     && $outputcharge > $self->option('recur_output_cap');
128
129   my $hourscharge =
130     $hours * sprintf('%.2f', $self->option('recur_hourly_charge'));
131   $hourscharge = $self->option('recur_hourly_cap')
132     if $self->option('recur_hourly_cap')
133     && $hourscharge > $self->option('recur_hourly_cap');
134
135   if ( $self->option('recur_total_charge') > 0 ) {
136     push @$details, "Last month's data ".
137                     sprintf('%.1f', $total). " megs: $totalcharge";
138   }
139   if ( $self->option('recur_input_charge') > 0 ) {
140     push @$details, "Last month's download ".
141                    sprintf('%.1f', $input). " megs: $inputcharge";
142   }
143   if ( $self->option('recur_output_charge') > 0 ) {
144     push @$details, "Last month's upload ".
145                    sprintf('%.1f', $output). " megs: $outputcharge";
146   }
147   if ( $self->option('recur_hourly_charge')  > 0 ) {
148     push @$details, "Last month\'s time ".
149                    sprintf('%.1f', $hours). " hours: $hourscharge";
150   }
151
152   my $charges = $hourscharge + $inputcharge + $outputcharge + $totalcharge;
153   if ( $self->option('global_cap') && $charges > $self->option('global_cap') ) {
154     $charges = $self->option('global_cap');
155     push @$details, "Usage charges capped at: $charges";
156   }
157
158   $self->option('recur_fee') + $charges;
159 }
160
161 sub can_discount { 0; }
162
163 sub is_free_options {
164   qw( setup_fee recur_fee recur_hourly_charge
165       recur_input_charge recur_output_charge recur_total_charge );
166 }
167
168 sub base_recur {
169   my($self, $cust_pkg) = @_;
170   $self->option('recur_fee');
171 }
172
173 1;