X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fcgp_rule.pm;h=e9c50901ade4c13631a9f9f7ed377070073f5143;hp=82e712204e2e492cdb85d9b196d067edc851b7e8;hb=74e058c8a010ef6feb539248a550d0bb169c1e94;hpb=2ac302e18d723563692cdbbd74f3e9e5331a3014 diff --git a/FS/FS/cgp_rule.pm b/FS/FS/cgp_rule.pm index 82e712204..e9c50901a 100644 --- a/FS/FS/cgp_rule.pm +++ b/FS/FS/cgp_rule.pm @@ -80,7 +80,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 +148,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 +165,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,6 +248,47 @@ sub check { $self->SUPER::check; } +=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 + +#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 =cut @@ -191,6 +322,33 @@ sub cgp_rule_action { qsearch('cgp_rule_action', { 'rulenum' => $self->rulenum } ); } +=item arrayref + +Returns an arraref representing this rule, suitable for Communigate Pro API +commands: + +The first element specifies the rule priority. + +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 arrayref { + my $self = shift; + [ $self->priority, + $self->name, + [ map $_->arrayref, $self->cgp_rule_condition ], + [ map $_->arrayref, $self->cgp_rule_action ], + $self->comment, + ], +} + =back =head1 BUGS