rt_ticket export, RT#9936
authormark <mark>
Mon, 18 Oct 2010 22:23:26 +0000 (22:23 +0000)
committermark <mark>
Mon, 18 Oct 2010 22:23:26 +0000 (22:23 +0000)
FS/FS/msg_template.pm
FS/FS/part_export/rt_ticket.pm [new file with mode: 0644]
httemplate/edit/msg_template.html

index 7321351..5582d0f 100644 (file)
@@ -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 (file)
index 0000000..87a06dc
--- /dev/null
@@ -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;
index 4546db9..f1cbc55 100644 (file)
@@ -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#',