summaryrefslogtreecommitdiff
path: root/bin/rt-setup-support-time
blob: 5f1007d8b27ac328f99f84e5fd51996cef8aaafa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/perl
use FS::UID 'adminsuidsetup';
use FS::TicketSystem;
use strict;

my $fieldname = 'Support time';
my $queue = 0; #global; change to a queue id if desired

my $fsuser = shift @ARGV or die "Usage: rt-setup-support-time user\n";

my $dbh = adminsuidsetup($fsuser);
FS::TicketSystem->init;
my $session = FS::TicketSystem->session();
my $CurrentUser = $session->{CurrentUser}
  or die "rt-setup-support-time must run as a valid RT user.\n";

$RT::Handle->BeginTransaction;

sub try {
  my ($val, $msg) = @_;
  if ( !$val ) {
    $RT::Handle->Rollback;
    die "$msg (reverted changes)\n";
  }
}

my $TicketCF = RT::CustomField->new($CurrentUser);
$TicketCF->LoadByCols(
  Name => $fieldname,
  LookupType => 'RT::Queue-RT::Ticket',
);
if (!defined($TicketCF->Id)) {
  print "Creating ticket custom field.\n";
  try( $TicketCF->Create(
    Name  => $fieldname,
    Type  => 'TimeValue',
    MaxValues => 1,
    LookupType => 'RT::Queue-RT::Ticket',
  ) );
  my $OCF = RT::ObjectCustomField->new($CurrentUser);
  try( $OCF->Create(
    CustomField => $TicketCF->Id,
    ObjectId => $queue,
  ) );
}

my $TxnCF = RT::CustomField->new($CurrentUser);
$TxnCF->LoadByCols(
  Name => $fieldname,
  LookupType => 'RT::Queue-RT::Ticket-RT::Transaction',
);
if (!defined($TxnCF->Id)) {
  print "Creating transaction custom field.\n";
  try( $TxnCF->Create(
    Name => $fieldname,
    Type => 'TimeValue',
    MaxValues => 1,
    LookupType => 'RT::Queue-RT::Ticket-RT::Transaction',
    UILocation => 'TimeWorked',
  ) );
  my $OCF = RT::ObjectCustomField->new($CurrentUser);
  try( $OCF->Create(
    CustomField => $TxnCF->Id,
    ObjectId => $queue,
  ) );
}

my $ScripCondition = RT::ScripCondition->new($CurrentUser);
$ScripCondition->Load('On Update');
if (!defined($ScripCondition->Id)) {
  print "Creating On Update condition.\n";
  try( $ScripCondition->Create(
    Name => 'On Update',
    Description => 'Whenever a ticket is updated',
    ExecModule => 'AnyTransaction',
    ApplicableTransTypes => 'Any',
  ) );
}

my $ScripAction = RT::ScripAction->new($CurrentUser);
$ScripAction->Load("Update $fieldname");
if (!defined($ScripAction->Id)) {
  print "Creating Update $fieldname action.\n";
  try( $ScripAction->Create(
    Name => "Update $fieldname",
    Description => 'Increment ticket time',
    ExecModule => 'Accumulate',
    Argument => $fieldname,
  ) );
}

my $Template = RT::Template->new($CurrentUser);
$Template->Load('Blank');
try(0, "No blank template found") if !$Template->Id;

my $Scrip = RT::Scrip->new($CurrentUser);
$Scrip->LoadByCols(
  ScripCondition => $ScripCondition->Id,
  ScripAction    => $ScripAction->Id,
  Queue          => $queue);
if (!defined($Scrip->Id)) {
  print "Creating scrip.\n";
  try( $Scrip->Create(
    Description => "On Transaction Update $fieldname",
    ScripCondition => $ScripCondition->Id,
    ScripAction => $ScripAction->Id,
    Stage => 'TransactionCreate',
    Queue => $queue,
    Template => $Template->Id,
  ) );
}

$RT::Handle->Commit;
print "Done.\n";