From 3542eb33ca09edef912885d05d1fa47181a61d51 Mon Sep 17 00:00:00 2001 From: mark Date: Mon, 18 Oct 2010 22:23:26 +0000 Subject: [PATCH] rt_ticket export, RT#9936 --- FS/FS/msg_template.pm | 23 +++++- FS/FS/part_export/rt_ticket.pm | 165 ++++++++++++++++++++++++++++++++++++++ httemplate/edit/msg_template.html | 2 + 3 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 FS/FS/part_export/rt_ticket.pm diff --git a/FS/FS/msg_template.pm b/FS/FS/msg_template.pm index 7321351d3..5582d0f3e 100644 --- a/FS/FS/msg_template.pm +++ b/FS/FS/msg_template.pm @@ -191,15 +191,30 @@ sub prepare { # create substitution table ### my %hash; - foreach my $obj ($cust_main, $object || ()) { + my @objects = ($cust_main); + my @prefixes = (''); + if( ref $object ) { + if( ref($object) eq 'ARRAY' ) { + # [new, old], for provisioning tickets + push @objects, $object->[0], $object->[1]; + push @prefixes, 'new_', 'old_'; + } + else { + push @objects, $object; + push @prefixes, ''; + } + } + + foreach my $obj (@objects) { + my $prefix = shift @prefixes; foreach my $name (@{ $subs->{$obj->table} }) { if(!ref($name)) { # simple case - $hash{$name} = $obj->$name(); + $hash{$prefix.$name} = $obj->$name(); } elsif( ref($name) eq 'ARRAY' ) { # [ foo => sub { ... } ] - $hash{$name->[0]} = $name->[1]->($obj); + $hash{$prefix.($name->[0])} = $name->[1]->($obj); } else { warn "bad msg_template substitution: '$name'\n"; @@ -366,7 +381,9 @@ sub substitutions { # for welcome and limit warning messages 'svc_acct' => [qw( + svcnum username + domain ), [ password => sub { shift->getfield('_password') } ], ], diff --git a/FS/FS/part_export/rt_ticket.pm b/FS/FS/part_export/rt_ticket.pm new file mode 100644 index 000000000..87a06dcfd --- /dev/null +++ b/FS/FS/part_export/rt_ticket.pm @@ -0,0 +1,165 @@ +package FS::part_export::rt_ticket; + +use vars qw(@ISA %info); +use Tie::IxHash; +use FS::part_export; +use FS::Record qw(qsearch qsearchs); +use FS::Conf; +use FS::TicketSystem; +use Data::Dumper 'Dumper'; + +@ISA = qw(FS::part_export); + +my %templates; +my %queues; +my %template_select = ( + type => 'select', + option_label => sub { + $templates{$_[0]}; + }, + option_values => sub { + %templates = (0 => '', + map { $_->msgnum, $_->msgname } + qsearch({ table => 'msg_template', + hashref => {}, + order_by => 'ORDER BY msgnum ASC' + }) + ); + sort keys (%templates); + }, +); + +tie my %options, 'Tie::IxHash', ( + 'queue' => { + label => 'Queue', + type => 'select', + option_label => sub { + $queues{$_[0]}; + }, + option_values => sub { + %queues = FS::TicketSystem->queues(); + sort {$queues{$a} cmp $queues{$b}} keys %queues; + }, + }, + 'insert_template' => { + label => 'Insert', + %template_select + }, + 'replace_template' => { + label => 'Replace', + %template_select + }, + 'delete_template' => { + label => 'Delete', + %template_select + }, + 'suspend_template' => { + label => 'Suspend', + %template_select + }, + 'unsuspend_template' => { + label => 'Unsuspend', + %template_select + }, + 'requestor' => { + label => 'Requestor', + 'type' => 'select', + option_label => sub { + my @labels = ( + 'Template From: address', + 'Customer\'s invoice address', + ); + $labels[shift]; + }, + option_values => sub { (0, 1) }, + }, +); + +%info = ( + 'svc' => [qw( svc_acct )], #others? + 'desc' => + 'Create an RT ticket', + 'options' => \%options, + 'nodomain' => '', + 'notes' => <<'END' +Create a ticket in RT. The subject and body of the ticket +will be generated from a message template. +END +); + +sub _export_ticket { + my( $self, $action, $svc ) = (shift, shift, shift); + my $msgnum = $self->option($action.'_template'); + return if !$msgnum; + + my $msg_template = FS::msg_template->by_key($msgnum); + return "Template $msgnum not found\n" if !$msg_template; + + my $cust_pkg = $svc->cust_svc->cust_pkg; + my $cust_main = $svc->cust_svc->cust_pkg->cust_main if $cust_pkg; + my $custnum = $cust_main->custnum if $cust_main; + my $svcnum = $svc->svcnum if $action ne 'delete'; + + my %msg; + if ( $action eq 'replace' ) { + my $old = shift; + %msg = $msg_template->prepare( + 'cust_main' => $cust_main, + 'object' => [ $svc, $old ], + ); + + } + else { + %msg = $msg_template->prepare( + 'cust_main' => $cust_main, + 'object' => $svc, + ); + } + my $requestor = $msg{'from'}; + $requestor = [ $cust_main->invoicing_list_emailonly ] + if $cust_main and $self->option('requestor') == 1; + + my $err_or_ticket = FS::TicketSystem->create_ticket( + '', #session should already exist + 'queue' => $self->option('queue'), + 'subject' => $msg{'subject'}, + 'requestor' => $requestor, + 'message' => $msg{'html_body'}, + 'mime_type' => 'text/html', + 'custnum' => $custnum, + 'svcnum' => $svcnum, + ); + if( ref($err_or_ticket) ) { + return ''; + } + else { + return $err_or_ticket; + } +} + +sub _export_insert { + my($self, $svc) = (shift, shift); + $self->_export_ticket('insert', $svc); +} + +sub _export_replace { + my($self, $new, $old) = (shift, shift, shift); + $self->_export_ticket('replace', $new, $old); +} + +sub _export_delete { + my($self, $svc) = (shift, shift); + $self->_export_ticket('delete', $svc); +} + +sub _export_suspend { + my($self, $svc) = (shift, shift); + $self->_export_ticket('suspend', $svc); +} + +sub _export_unsuspend { + my($self, $svc) = (shift, shift); + $self->_export_ticket('unsuspend', $svc); +} + +1; diff --git a/httemplate/edit/msg_template.html b/httemplate/edit/msg_template.html index 4546db98b..f1cbc5550 100644 --- a/httemplate/edit/msg_template.html +++ b/httemplate/edit/msg_template.html @@ -96,8 +96,10 @@ my %substitutions = ( '$location_label' => 'Service location', ], 'svc_acct' => [ + '$svcnum' => 'Service#', '$username' => 'Login name', '$password' => 'Password', + '$domain' => 'Domain name', ], 'cust_pay' => [ '$paynum' => 'Payment#', -- 2.11.0