Rate CDRs immediately, RT#15839
[freeside.git] / FS / FS / option_Common.pm
index 2950b28..968dcdf 100644 (file)
@@ -1,12 +1,11 @@
 package FS::option_Common;
 
 use strict;
-use vars qw( @ISA $DEBUG );
+use base qw( FS::Record );
+use vars qw( $DEBUG );
 use Scalar::Util qw( blessed );
 use FS::Record qw( qsearch qsearchs dbh );
 
-@ISA = qw( FS::Record );
-
 $DEBUG = 0;
 
 =head1 NAME
@@ -79,10 +78,13 @@ sub insert {
   my $valuecol = $self->_option_valuecol;
 
   foreach my $optionname ( keys %{$options} ) {
+
+    my $optionvalue = $options->{$optionname};
+
     my $href = {
       $pkey     => $self->get($pkey),
       $namecol  => $optionname,
-      $valuecol => $options->{$optionname},
+      $valuecol => ( ref($optionvalue) || $optionvalue ),
     };
 
     #my $option_record = eval "new FS::$option_table \$href";
@@ -92,11 +94,15 @@ sub insert {
     #}
     my $option_record = "FS::$option_table"->new($href);
 
-    $error = $option_record->insert;
+    my @args = ();
+    push @args, $optionvalue if ref($optionvalue); #only hashes supported so far
+
+    $error = $option_record->insert(@args);
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return $error;
     }
+
   }
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
@@ -154,8 +160,8 @@ sub delete {
 Replaces the OLD_RECORD with this one in the database.  If there is an error,
 returns the error, otherwise returns false.
 
-If a list hash reference of options is supplied, part_export_option records are
-created or modified (see L<FS::part_export_option>).
+If a list or hash reference of options is supplied, option records are created
+or modified.
 
 =cut
 
@@ -166,10 +172,15 @@ sub replace {
               ? shift
               : $self->replace_old;
 
-  my $options = 
-    ( ref($_[0]) eq 'HASH' )
-      ? shift
-      : { @_ };
+  my $options;
+  my $options_supplied = 0;
+  if ( ref($_[0]) eq 'HASH' ) {
+    $options = shift;
+    $options_supplied = 1;
+  } else {
+    $options = { @_ };
+    $options_supplied = scalar(@_) ? 1 : 0;
+  }
 
   warn "FS::option_Common::replace called on $self with options ".
        join(', ', map "$_ => ". $options->{$_}, keys %$options)
@@ -208,10 +219,15 @@ sub replace {
         $namecol => $optionname,
     } );
 
+    my $optionvalue = $options->{$optionname};
+
+    my %oldhash = $oldopt ? $oldopt->hash : ();
+
     my $href = {
+        %oldhash,
         $pkey     => $self->get($pkey),
         $namecol  => $optionname,
-        $valuecol => $options->{$optionname},
+        $valuecol => ( ref($optionvalue) || $optionvalue ),
     };
 
     #my $newopt = eval "new FS::$option_table \$href";
@@ -224,10 +240,15 @@ sub replace {
     my $opt_pkey = $newopt->primary_key;
 
     $newopt->$opt_pkey($oldopt->$opt_pkey) if $oldopt;
+
+    my @args = ();
+    push @args, $optionvalue if ref($optionvalue); #only hashes supported so far
+
     warn "FS::option_Common::replace: ".
          ( $oldopt ? "$newopt -> replace($oldopt)" : "$newopt -> insert" )
       if $DEBUG > 2;
-    my $error = $oldopt ? $newopt->replace($oldopt) : $newopt->insert;
+    my $error = $oldopt ? $newopt->replace($oldopt, @args)
+                        : $newopt->insert( @args);
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return $error;
@@ -235,13 +256,15 @@ sub replace {
   }
 
   #remove extraneous old options
-  foreach my $opt (
-    grep { !exists $options->{$_->$namecol()} } $old->option_objects
-  ) {
-    my $error = $opt->delete;
-    if ( $error ) {
-      $dbh->rollback if $oldAutoCommit;
-      return $error;
+  if ( $options_supplied ) {
+    foreach my $opt (
+      grep { !exists $options->{$_->$namecol()} } $old->option_objects
+    ) {
+      my $error = $opt->delete;
+      if ( $error ) {
+        $dbh->rollback if $oldAutoCommit;
+        return $error;
+      }
     }
   }
 
@@ -300,6 +323,19 @@ sub option {
   $obj ? $obj->$valuecol() : '';
 }
 
+=item option_cacheable OPTIONNAME
+
+Same as the option method, but may cache and return a cached value.
+Good for use within loops; otherwise, probably avoid.
+
+=cut
+
+sub option_cacheable {
+  my( $self, $name ) = @_;
+  return $self->{option_cache}{$name} if exists $self->{option_cache}{$name};
+  $self->{option_cache}{$name} = $self->option($name,1);
+}
+
 
 sub option_table {
   my $self = shift;