backup the schema for tables we don't need the data from. RT#85959
[freeside.git] / FS / FS / cgp_rule.pm
index 82e7122..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,7 +77,36 @@ otherwise returns false.
 
 =cut
 
-# the insert method can be inherited from FS::Record
+#  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
 
@@ -119,8 +145,14 @@ sub delete {
     return $error;
   }
 
-  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+  $error = $self->svc_export;
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
 
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+  '';
 }
 
 =item replace OLD_RECORD
@@ -130,7 +162,62 @@ returns the error, otherwise returns false.
 
 =cut
 
-# the replace method can be inherited from FS::Record
+sub replace {
+  my $new = shift;
+
+  my $old = ( ref($_[0]) eq ref($new) )
+              ? shift
+              : $new->replace_old;
+
+  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 = $new->SUPER::replace($old, @_);
+  if ( $error ) {
+    $dbh->rollback or die $dbh->errstr 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;
+  '';
+
+}
+
+=item svc_export
+
+Calls the replace export for any communigate exports attached to this rule's
+service.
+
+=cut
+
+sub svc_export {
+  my $self = shift;
+
+  my $cust_svc = $self->cust_svc;
+  my $svc_x = $cust_svc->svc_x;
+  
+  #_singledomain too
+  my @exports = $cust_svc->part_svc->part_export('communigate_pro');
+  my @errors = map $_->export_replace($svc_x, $svc_x), @exports;
+
+  @errors ? join(' / ', @errors) : '';
+
+}
 
 =item check
 
@@ -158,37 +245,79 @@ 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
+
+#i should probably be transactionalized so i'm all-or-nothing
+sub clone {
+  my( $self, $svcnum ) = @_;
+
+  my $new = $self->new( { $self->hash } );
+  $new->rulenum('');
+  $new->svcnum( $svcnum );
+  my $error = $new->insert;
+  return $error if $error;
+
+  my @dup = $self->cgp_rule_condition;
+  push @dup, $self->cgp_rule_action;
+
+  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 );
+
+    $error = $new_dup->insert;
+    return $error if $error;
+
+  }
+
+  $error = $new->svc_export;
+  return $error if $error;
+
+  '';
+
 }
 
+=item cust_svc
+
 =item cgp_rule_condition
 
 Returns the conditions associated with this rule, as FS::cgp_rule_condition
 objects.
 
-=cut
+=item arrayref
 
-sub cgp_rule_condition {
-  my $self = shift;
-  qsearch('cgp_rule_condition', { 'rulenum' => $self->rulenum } );
-}
+Returns an arraref representing this rule, suitable for Communigate Pro API
+commands:
 
-=item cgp_rule_action
+The first element specifies the rule priority.
 
-Returns the actions associated with this rule, as FS::cgp_rule_action
-objects.
+The second element specifies the rule name.
+
+The third element specifies the rule conditions.
+
+The fourth element specifies the rule actions.
+
+The fifth element specifies the rule comment.
 
 =cut
 
-sub cgp_rule_action {
+sub arrayref {
   my $self = shift;
-  qsearch('cgp_rule_action', { 'rulenum' => $self->rulenum } );
+  [ $self->priority,
+    $self->name,
+    [ map $_->arrayref, $self->cgp_rule_condition ],
+    [ map $_->arrayref, $self->cgp_rule_action ],
+    $self->comment,
+  ],
 }
 
 =back