don't enable 'Void invoices' by default, being overused in lieu of more accounting...
[freeside.git] / FS / FS / rate.pm
index a2511cf..aef9d8b 100644 (file)
@@ -1,12 +1,11 @@
 package FS::rate;
+use base qw(FS::Record);
 
 use strict;
-use vars qw( @ISA $DEBUG );
+use vars qw( $DEBUG );
 use FS::Record qw( qsearch qsearchs dbh fields );
 use FS::rate_detail;
 
-@ISA = qw(FS::Record);
-
 $DEBUG = 0;
 
 =head1 NAME
@@ -35,10 +34,18 @@ FS::Record.  The following fields are currently supported:
 
 =over 4
 
-=item ratenum - primary key
+=item ratenum
+
+primary key
 
 =item ratename
 
+Rate name
+
+=item agentnum
+
+Optional agent (see L<FS::agent>) for agent-virtualized rates.
+
 =back
 
 =head1 METHODS
@@ -254,15 +261,13 @@ and replace methods.
 
 =cut
 
-# the check method should currently be supplied - FS::Record contains some
-# data checking routines
-
 sub check {
   my $self = shift;
 
   my $error =
        $self->ut_numbern('ratenum')
     || $self->ut_text('ratename')
+    #|| $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
   ;
   return $error if $error;
 
@@ -308,17 +313,28 @@ sub dest_detail {
     #find a rate prefix, first look at most specific, then fewer digits,
     # finally trying the country code only
     my $rate_prefix = '';
-    for my $len ( reverse(1..10) ) {
-      $rate_prefix = qsearchs('rate_prefix', {
+    $rate_prefix = qsearchs({
+        'table'     => 'rate_prefix',
+        'addl_from' => ' JOIN rate_region USING (regionnum)',
+        'hashref'   => {
+          'countrycode' => $countrycode,
+          'npa'         => $phonenum,
+        },
+        'extra_sql' => ' AND exact_match = \'Y\''
+    });
+    if (!$rate_prefix) {
+      for my $len ( reverse(1..10) ) {
+        $rate_prefix = qsearchs('rate_prefix', {
+          'countrycode' => $countrycode,
+          #'npa'         => { op=> 'LIKE', value=> substr($number, 0, $len) }
+          'npa'         => substr($phonenum, 0, $len),
+        } ) and last;
+      }
+      $rate_prefix ||= qsearchs('rate_prefix', {
         'countrycode' => $countrycode,
-        #'npa'         => { op=> 'LIKE', value=> substr($number, 0, $len) }
-        'npa'         => substr($phonenum, 0, $len),
-      } ) and last;
+        'npa'         => '',
+      });
     }
-    $rate_prefix ||= qsearchs('rate_prefix', {
-      'countrycode' => $countrycode,
-      'npa'         => '',
-    });
 
     return '' unless $rate_prefix;
 
@@ -371,14 +387,6 @@ sub dest_detail {
 
 Returns all region-specific details  (see L<FS::rate_detail>) for this rate.
 
-=cut
-
-sub rate_detail {
-  my $self = shift;
-  qsearch( 'rate_detail', { 'ratenum' => $self->ratenum } );
-}
-
-
 =back
 
 =head1 SUBROUTINES