1 package FS::part_pkg::voip_sqlradacct;
4 use vars qw(@ISA %info);
5 use FS::Record qw(qsearchs qsearch);
10 @ISA = qw(FS::part_pkg);
13 'name' => 'VoIP rating by plan of CDR records in an SQL RADIUS radacct table',
15 'setup_fee' => { 'name' => 'Setup fee for this package',
18 'recur_flat' => { 'name' => 'Base monthly charge for this package',
21 'ratenum' => { 'name' => 'Rate plan',
23 'select_table' => 'rate',
24 'select_key' => 'ratenum',
25 'select_label' => 'ratename',
28 'fieldorder' => [qw( setup_fee recur_flat ratenum )],
33 my($self, $cust_pkg ) = @_;
34 $self->option('setup_fee');
38 my($self, $cust_pkg, $sdate, $details ) = @_;
40 my $last_bill = $cust_pkg->last_bill;
42 my $ratenum = $cust_pkg->part_pkg->option('ratenum');
44 my %included_min = ();
48 foreach my $cust_svc (
49 grep { $_->part_svc->svcdb eq 'svc_acct' } $cust_pkg->cust_svc
53 $cust_svc->get_session_history( $last_bill, $$sdate )
57 # look up rate details based on called station id
60 my $dest = $session->{'calledstationid'};
62 #remove non-phone# stuff and whitespace
65 $dest =~ s/^(\w+):// and $proto = $1; #sip:
67 $dest =~ s/\@((\d{1,3}\.){3}\d{1,3})$// and $ip = $1; # @10.54.32.1
69 #determine the country code
71 if ( $dest =~ /^011((\d\d)(\d))(\d+)$/ ) {
73 my( $three, $two, $unknown, $rest ) = ( $1, $2, $3, $4 );
74 #first look for 2 digit country code
75 if ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) {
77 $dest = $unknown.$rest;
78 } else { #3 digit country code
79 $countrycode = $three;
87 #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
88 # finally trying the country code only
90 for my $len ( reverse(1..4) ) {
91 $rate_prefix = qsearchs('rate_prefix', {
92 'countrycode' => $countrycode,
93 'npa' => { op=> 'LIKE', value=> substr($dest, 0, $len) }
96 $rate_prefix ||= qsearchs('rate_prefix', {
97 'countrycode' => $countrycode,
100 die "Can't find rate for call to countrycode $countrycode number $dest\n"
103 my $regionnum = $rate_prefix->regionnum;
105 my $rate_detail = qsearchs('rate_detail', {
106 'ratenum' => $ratenum,
107 'dest_regionnum' => $regionnum,
111 # find the price and add detail to the invoice
114 $included_min{$regionnum} = $rate_detail->min_included
115 unless exists $included_min{$regionnum};
117 my $granularity = $rate_detail->sec_granularity;
118 my $seconds = $session->{'acctsessiontime'};
119 $seconds += $granularity - ( $seconds % $granularity );
120 my $minutes = sprintf("%.1f", $seconds / 60);
121 $minutes =~ s/\.0$// if $granularity == 60;
123 $included_min{$regionnum} -= $minutes;
126 if ( $included_min{$regionnum} < 0 ) {
127 my $charge_min = 0 - $included_min{$regionnum};
128 $included_min{$regionnum} = 0;
129 $charge = sprintf('%.2f', $rate_detail->min_charge * $charge_min );
136 "+$countrycode $dest",
137 $rate_prefix->rate_region->regionname,
148 $self->option('recur_flat') + $charges;