fix VoIP details on invoices, closes: Bug#1096
[freeside.git] / FS / FS / part_pkg / voip_sqlradacct.pm
1 package FS::part_pkg::voip_sqlradacct;
2
3 use strict;
4 use vars qw(@ISA $DEBUG %info);
5 use FS::Record qw(qsearchs qsearch);
6 use FS::part_pkg;
7 #use FS::rate;
8 use FS::rate_prefix;
9
10 @ISA = qw(FS::part_pkg::flat);
11
12 $DEBUG = 1;
13
14 %info = (
15   'name' => 'VoIP rating by plan of CDR records in an SQL RADIUS radacct table',
16   'fields' => {
17     'setup_fee'     => { 'name' => 'Setup fee for this package',
18                          'default' => 0,
19                        },
20     'recur_flat'     => { 'name' => 'Base monthly charge for this package',
21                           'default' => 0,
22                         },
23     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
24                                    ' of service at cancellation',
25                          'type' => 'checkbox',
26                        },
27     'ratenum'   => { 'name' => 'Rate plan',
28                      'type' => 'select',
29                      'select_table' => 'rate',
30                      'select_key'   => 'ratenum',
31                      'select_label' => 'ratename',
32                    },
33   },
34   'fieldorder' => [qw( setup_fee recur_flat unused_credit ratenum ignore_unrateable )],
35   'weight' => 40,
36 );
37
38 sub calc_setup {
39   my($self, $cust_pkg ) = @_;
40   $self->option('setup_fee');
41 }
42
43 sub calc_recur {
44   my($self, $cust_pkg, $sdate, $details ) = @_;
45
46   my $last_bill = $cust_pkg->last_bill;
47
48   my $ratenum = $cust_pkg->part_pkg->option('ratenum');
49
50   my %included_min = ();
51
52   my $charges = 0;
53
54   foreach my $cust_svc (
55     grep { $_->part_svc->svcdb eq 'svc_acct' } $cust_pkg->cust_svc
56   ) {
57
58     foreach my $session (
59       $cust_svc->get_session_history( $last_bill, $$sdate )
60     ) {
61       if ( $DEBUG > 1 ) {
62         warn "rating session $session\n".
63              join('', map { "  $_ => ". $session->{$_}. "\n" } keys %$session );
64       }
65
66       ###
67       # look up rate details based on called station id
68       ###
69
70       my $dest = $session->{'calledstationid'};
71
72       #remove non-phone# stuff and whitespace
73       $dest =~ s/\s//g;
74       my $proto = '';
75       $dest =~ s/^(\w+):// and $proto = $1; #sip:
76       my $siphost = '';
77       $dest =~ s/\@(.*)$// and $siphost = $1; # @10.54.32.1, @sip.example.com
78
79       #determine the country code
80       my $countrycode;
81       if ( $dest =~ /^011(((\d)(\d))(\d))(\d+)$/ ) {
82
83         my( $three, $two, $one, $u1, $u2, $rest ) = ( $1, $2, $3, $4, $5, $6 );
84         #first look for 1 digit country code
85         if ( qsearch('rate_prefix', { 'countrycode' => $one } ) ) {
86           $countrycode = $one;
87           $dest = $u1.$u2.$rest;
88         } elsif ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) { #or 2
89           $countrycode = $two;
90           $dest = $u2.$rest;
91         } else { #3 digit country code
92           $countrycode = $three;
93           $dest = $rest;
94         }
95
96       } else {
97         $countrycode = '1';
98         $dest =~ s/^1//;# if length($dest) > 10;
99       }
100
101       warn "rating call to +$countrycode $dest\n" if $DEBUG;
102
103       #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
104       # finally trying the country code only
105       my $rate_prefix = '';
106       for my $len ( reverse(1..6) ) {
107         $rate_prefix = qsearchs('rate_prefix', {
108           'countrycode' => $countrycode,
109           #'npa'         => { op=> 'LIKE', value=> substr($dest, 0, $len) }
110           'npa'         => substr($dest, 0, $len),
111         } ) and last;
112       }
113       $rate_prefix ||= qsearchs('rate_prefix', {
114         'countrycode' => $countrycode,
115         'npa'         => '',
116       });
117
118       die "Can't find rate for call to +$countrycode $dest\n"
119         unless $rate_prefix;
120
121       my $regionnum = $rate_prefix->regionnum;
122       my $rate_detail = qsearchs('rate_detail', {
123         'ratenum'        => $ratenum,
124         'dest_regionnum' => $regionnum,
125       } );
126
127       warn "  found rate for regionnum $regionnum ".
128            "and rate detail $rate_detail"
129         if $DEBUG;
130
131       ###
132       # find the price and add detail to the invoice
133       ###
134
135       $included_min{$regionnum} = $rate_detail->min_included
136         unless exists $included_min{$regionnum};
137
138       my $granularity = $rate_detail->sec_granularity;
139       my $seconds = $session->{'acctsessiontime'};
140       $seconds += $granularity - ( $seconds % $granularity );
141       my $minutes = sprintf("%.1f", $seconds / 60);
142       $minutes =~ s/\.0$// if $granularity == 60;
143
144       $included_min{$regionnum} -= $minutes;
145
146       my $charge = 0;
147       if ( $included_min{$regionnum} < 0 ) {
148         my $charge_min = 0 - $included_min{$regionnum};
149         $included_min{$regionnum} = 0;
150         $charge = sprintf('%.2f', $rate_detail->min_charge * $charge_min );
151         $charges += $charge;
152       }
153
154       my $rate_region = $rate_prefix->rate_region;
155       warn "  (rate region $rate_region)" if $DEBUG;
156
157       warn "  adding details on charge to invoice: ".
158            join(' - ', 
159              "+$countrycode $dest",
160              $rate_region->regionname,
161              $minutes.'m',
162              '$'.$charge,
163            )
164         if $DEBUG;
165
166       push @$details, 
167         #[
168         join(' - ', 
169           "+$countrycode $dest",
170           $rate_region->regionname,
171           $minutes.'m',
172           '$'.$charge,
173         #]
174         )
175       ;
176
177     } # $session
178
179   } # $cust_svc
180
181   $self->option('recur_flat') + $charges;
182
183 }
184
185 sub is_free {
186   0;
187 }
188
189 sub base_recur {
190   my($self, $cust_pkg) = @_;
191   $self->option('recur_flat');
192 }
193
194 1;
195