stray closing /TABLE in the no-ticket case
[freeside.git] / FS / FS / cgp_rule.pm
index ad5ab1e..1029f5a 100644 (file)
@@ -1,11 +1,8 @@
 package FS::cgp_rule;
+use base qw( FS::o2m_Common FS::Record );
 
 use strict;
-use base qw( FS::o2m_Common FS::Record );
-use FS::Record qw( qsearch qsearchs dbh );
-use FS::cust_svc;
-use FS::cgp_rule_condition;
-use FS::cgp_rule_action;
+use FS::Record qw( dbh );
 
 =head1 NAME
 
@@ -80,35 +77,36 @@ otherwise returns false.
 
 =cut
 
-sub insert {
-  my $self = shift;
-
-  local $SIG{HUP} = 'IGNORE';
-  local $SIG{INT} = 'IGNORE';
-  local $SIG{QUIT} = 'IGNORE';
-  local $SIG{TERM} = 'IGNORE';
-  local $SIG{TSTP} = 'IGNORE';
-  local $SIG{PIPE} = 'IGNORE';
-
-  my $oldAutoCommit = $FS::UID::AutoCommit;
-  local $FS::UID::AutoCommit = 0;
-  my $dbh = dbh;
-
-  my $error = $self->SUPER::insert(@_);
-  if ( $error ) {
-    $dbh->rollback if $oldAutoCommit;
-    return $error;
-  }
-
-  $error = $self->svc_export;
-  if ( $error ) {
-    $dbh->rollback if $oldAutoCommit;
-    return $error;
-  }
-
-  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
-  '';
-}
+#  sub insert {
+#    my $self = shift;
+#  
+#    local $SIG{HUP} = 'IGNORE';
+#    local $SIG{INT} = 'IGNORE';
+#    local $SIG{QUIT} = 'IGNORE';
+#    local $SIG{TERM} = 'IGNORE';
+#    local $SIG{TSTP} = 'IGNORE';
+#    local $SIG{PIPE} = 'IGNORE';
+#  
+#    my $oldAutoCommit = $FS::UID::AutoCommit;
+#    local $FS::UID::AutoCommit = 0;
+#    my $dbh = dbh;
+#  
+#    my $error = $self->SUPER::insert(@_);
+#    if ( $error ) {
+#      $dbh->rollback if $oldAutoCommit;
+#      return $error;
+#    }
+#  
+#    #conditions and actions not in yet
+#    #$error = $self->svc_export;
+#    #if ( $error ) {
+#    #  $dbh->rollback if $oldAutoCommit;
+#    #  return $error;
+#    #}
+#  
+#    $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+#    '';
+#  }
 
 =item delete
 
@@ -188,11 +186,12 @@ sub replace {
     return $error;
   }
 
-  $error = $new->svc_export;
-  if ( $error ) {
-    $dbh->rollback if $oldAutoCommit;
-    return $error;
-  }
+  #conditions and actions not in yet
+  #$error = $new->svc_export;
+  #if ( $error ) {
+  #  $dbh->rollback if $oldAutoCommit;
+  #  return $error;
+  #}
 
   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
   '';
@@ -246,39 +245,54 @@ sub check {
   $self->SUPER::check;
 }
 
-=item cust_svc
+=item clone NEW_SVCNUM
+
+Clones this rule into an identical rule for the specified new service.
+
+If there is an error, returns the error, otherwise returns false.
 
 =cut
 
-sub cust_svc {
-  my $self = shift;
-  qsearchs('cust_svc', { 'svcnum' => $self->svcnum } );
-}
+#should return the newly inserted rule instead? used in misc/clone-cgp_rule.html
 
-=item cgp_rule_condition
+#i should probably be transactionalized so i'm all-or-nothing
+sub clone {
+  my( $self, $svcnum ) = @_;
 
-Returns the conditions associated with this rule, as FS::cgp_rule_condition
-objects.
+  my $new = $self->new( { $self->hash } );
+  $new->rulenum('');
+  $new->svcnum( $svcnum );
+  my $error = $new->insert;
+  return $error if $error;
 
-=cut
+  my @dup = $self->cgp_rule_condition;
+  push @dup, $self->cgp_rule_action;
 
-sub cgp_rule_condition {
-  my $self = shift;
-  qsearch('cgp_rule_condition', { 'rulenum' => $self->rulenum } );
-}
+  foreach my $dup (@dup) {
+    my $new_dup = $dup->new( { $dup->hash } );
+    my $pk = $new_dup->primary_key;
+    $new_dup->$pk('');
+    $new_dup->rulenum( $new->rulenum );
 
-=item cgp_rule_action
+    $error = $new_dup->insert;
+    return $error if $error;
 
-Returns the actions associated with this rule, as FS::cgp_rule_action
-objects.
+  }
 
-=cut
+  $error = $new->svc_export;
+  return $error if $error;
+
+  '';
 
-sub cgp_rule_action {
-  my $self = shift;
-  qsearch('cgp_rule_action', { 'rulenum' => $self->rulenum } );
 }
 
+=item cust_svc
+
+=item cgp_rule_condition
+
+Returns the conditions associated with this rule, as FS::cgp_rule_condition
+objects.
+
 =item arrayref
 
 Returns an arraref representing this rule, suitable for Communigate Pro API