slightly better voip_cdr package edit: use radio buttons for selecting long things
[freeside.git] / FS / FS / part_pkg / voip_cdr.pm
index 15af77b..ea16031 100644 (file)
@@ -26,7 +26,7 @@ tie my %rating_method, 'Tie::IxHash',
 #;
 
 %info = (
-  'name' => 'VoIP rating by plan of CDR records in an internal (or external?) SQL table',
+  'name' => 'VoIP rating by plan of CDR records in an internal (or external) SQL table',
   'fields' => {
     'setup_fee'     => { 'name' => 'Setup fee for this package',
                          'default' => 0,
@@ -45,15 +45,31 @@ tie my %rating_method, 'Tie::IxHash',
                      'select_label' => 'ratename',
                    },
     'rating_method' => { 'name' => 'Region rating method',
-                         'type' => 'select',
-                         'select_options' => \%rating_method,
+                         'type' => 'radio',
+                         'options' => \%rating_method,
                        },
 
     'default_prefix' => { 'name'    => 'Default prefix optionally prepended to customer DID numbers when searching for CDR records',
                           'default' => '+1',
                         },
 
-    #XXX also have option for an external db??
+    'disable_src' => { 'name' => 'Disable rating of CDR records based on the "src" field in addition to "charged_party"',
+                       'type' => 'checkbox'
+                     },
+
+    'domestic_prefix' => { 'name'    => 'Destination prefix for domestic CDR records',
+                           'default' => '1',
+                         },
+
+#    'domestic_prefix_required' => { 'name' => 'Require explicit destination prefix for domestic CDR records',
+#                                    'type' => 'checkbox',
+#                                  },
+
+    'international_prefix' => { 'name'    => 'Destination prefix for international CDR records',
+                                'default' => '011',
+                              },
+
+    #XXX also have option for an external db
 #    'cdr_location' => { 'name' => 'CDR database location'
 #                        'type' => 'select',
 #                        'select_options' => \%cdr_location,
@@ -77,7 +93,7 @@ tie my %rating_method, 'Tie::IxHash',
 #                  },
 
   },
-  'fieldorder' => [qw( setup_fee recur_flat unused_credit ratenum rating_method default_prefix )],
+  'fieldorder' => [qw( setup_fee recur_flat unused_credit ratenum rating_method default_prefix disable_src domestic_prefix international_prefix )],
   'weight' => 40,
 );
 
@@ -102,9 +118,8 @@ sub calc_recur {
 
   my $downstream_cdr = '';
 
-  # also look for a specific domain??? (username@telephonedomain)
   foreach my $cust_svc (
-    grep { $_->part_svc->svcdb eq 'svc_acct' } $cust_pkg->cust_svc
+    grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc
   ) {
 
     foreach my $cdr (
@@ -125,78 +140,87 @@ sub calc_recur {
          )
       {
 
-        die "rating_method 'prefix' not yet supported";
-
-#        ###
-#        # look up rate details based on called station id
-#        ###
-#  
-#        my $dest = $cdr->dst;
-#  
-#        #remove non-phone# stuff and whitespace
-#        $dest =~ s/\s//g;
+        ###
+        # look up rate details based on called station id
+        # (or calling station id for toll free calls)
+        ###
+
+        my( $to_or_from, $number );
+        if ( $cdr->dst =~ /^(\+?1)?8([02-8])\1/ ) { #tollfree call
+          $to_or_from = 'from';
+          $number = $cdr->src;
+        } else { #regular call
+          $to_or_from = 'to';
+          $number = $cdr->dst;
+        }
+  
+        #remove non-phone# stuff and whitespace
+        $number =~ s/\s//g;
 #        my $proto = '';
 #        $dest =~ s/^(\w+):// and $proto = $1; #sip:
 #        my $siphost = '';
 #        $dest =~ s/\@(.*)$// and $siphost = $1; # @10.54.32.1, @sip.example.com
-#  
-#        #determine the country code
-#        my $countrycode;
-#        if (    $dest =~ /^011(((\d)(\d))(\d))(\d+)$/
-#             || $dest =~ /^\+(((\d)(\d))(\d))(\d+)$/
-#           )
-#        {
-#  
-#          my( $three, $two, $one, $u1, $u2, $rest ) = ( $1,$2,$3,$4,$5,$6 );
-#          #first look for 1 digit country code
-#          if ( qsearch('rate_prefix', { 'countrycode' => $one } ) ) {
-#            $countrycode = $one;
-#            $dest = $u1.$u2.$rest;
-#          } elsif ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) { #or 2
-#            $countrycode = $two;
-#            $dest = $u2.$rest;
-#          } else { #3 digit country code
-#            $countrycode = $three;
-#            $dest = $rest;
-#          }
-#  
-#        } else {
-#          $countrycode = '1';
-#          $dest =~ s/^1//;# if length($dest) > 10;
-#        }
-#  
-#        warn "rating call to +$countrycode $dest\n" if $DEBUG;
-#        $pretty_destnum = "+$countrycode $dest";
-#  
-#        #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
-#        # finally trying the country code only
-#        my $rate_prefix = '';
-#        for my $len ( reverse(1..6) ) {
-#          $rate_prefix = qsearchs('rate_prefix', {
-#            'countrycode' => $countrycode,
-#            #'npa'         => { op=> 'LIKE', value=> substr($dest, 0, $len) }
-#            'npa'         => substr($dest, 0, $len),
-#          } ) and last;
-#        }
-#        $rate_prefix ||= qsearchs('rate_prefix', {
-#          'countrycode' => $countrycode,
-#          'npa'         => '',
-#        });
-#  
-#        die "Can't find rate for call to +$countrycode $dest\n"
-#          unless $rate_prefix;
-#  
-#        $regionnum = $rate_prefix->regionnum;
-#        $rate_detail = qsearchs('rate_detail', {
-#          'ratenum'        => $ratenum,
-#          'dest_regionnum' => $regionnum,
-#        } );
-#  
-#        $rate_region = $rate_prefix->rate_region;
-#
-#        warn "  found rate for regionnum $regionnum ".
-#             "and rate detail $rate_detail\n"
-#          if $DEBUG;
+
+        my $intl = $self->option('international_prefix') || '011';
+  
+        #determine the country code
+        my $countrycode;
+        if (    $number =~ /^$intl(((\d)(\d))(\d))(\d+)$/
+             || $number =~ /^\+(((\d)(\d))(\d))(\d+)$/
+           )
+        {
+  
+          my( $three, $two, $one, $u1, $u2, $rest ) = ( $1,$2,$3,$4,$5,$6 );
+          #first look for 1 digit country code
+          if ( qsearch('rate_prefix', { 'countrycode' => $one } ) ) {
+            $countrycode = $one;
+            $number = $u1.$u2.$rest;
+          } elsif ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) { #or 2
+            $countrycode = $two;
+            $number = $u2.$rest;
+          } else { #3 digit country code
+            $countrycode = $three;
+            $number = $rest;
+          }
+  
+        } else {
+          $countrycode = $self->option('domestic_prefix') || '1';
+          $number =~ s/^$countrycode//;# if length($number) > 10;
+        }
+  
+        warn "rating call $to_or_from +$countrycode $number\n" if $DEBUG;
+        $pretty_destnum = "+$countrycode $number";
+  
+        #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
+        # finally trying the country code only
+        my $rate_prefix = '';
+        for my $len ( reverse(1..6) ) {
+          $rate_prefix = qsearchs('rate_prefix', {
+            'countrycode' => $countrycode,
+            #'npa'         => { op=> 'LIKE', value=> substr($number, 0, $len) }
+            'npa'         => substr($number, 0, $len),
+          } ) and last;
+        }
+        $rate_prefix ||= qsearchs('rate_prefix', {
+          'countrycode' => $countrycode,
+          'npa'         => '',
+        });
+
+        #
+        die "Can't find rate for call $to_or_from +$countrycode $\numbern"
+          unless $rate_prefix;
+  
+        $regionnum = $rate_prefix->regionnum;
+        $rate_detail = qsearchs('rate_detail', {
+          'ratenum'        => $ratenum,
+          'dest_regionnum' => $regionnum,
+        } );
+  
+        $rate_region = $rate_prefix->rate_region;
+
+        warn "  found rate for regionnum $regionnum ".
+             "and rate detail $rate_detail\n"
+          if $DEBUG;
 
       } elsif ( $self->option('rating_method') eq 'upstream' ) {
 
@@ -255,9 +279,13 @@ sub calc_recur {
       
           my $granularity = $rate_detail->sec_granularity;
           my $seconds = $cdr->billsec; # |ength($cdr->billsec) ? $cdr->billsec : $cdr->duration;
-          $seconds += $granularity - ( $seconds % $granularity );
+          $seconds += $granularity - ( $seconds % $granularity )
+            if $granularity; # 0 is per call
           my $minutes = sprintf("%.1f", $seconds / 60);
           $minutes =~ s/\.0$// if $granularity == 60;
+
+          # per call rather than per minute
+          $minutes = 1 unless $granularity;
       
           $included_min{$regionnum} -= $minutes;
       
@@ -274,7 +302,7 @@ sub calc_recur {
           @call_details = (
             #time2str("%Y %b %d - %r", $cdr->calldate_unix ),
             time2str("%c", $cdr->calldate_unix),  #XXX this should probably be a config option dropdown so they can select US vs- rest of world dates or whatnot
-            $minutes.'m',
+            $granularity ? $minutes.'m' : $minutes.' call',
             '$'.$charge,
             $pretty_destnum,
             $rate_region->regionname,