default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / bin / rt-setup-support-time
1 #!/usr/bin/perl
2 use FS::UID 'adminsuidsetup';
3 use FS::TicketSystem;
4 use strict;
5
6 my $fieldname = 'Support time';
7 my $queue = 0; #global; change to a queue id if desired
8
9 my $fsuser = shift @ARGV or die "Usage: rt-setup-support-time user\n";
10
11 my $dbh = adminsuidsetup($fsuser);
12 FS::TicketSystem->init;
13 my $session = FS::TicketSystem->session();
14 my $CurrentUser = $session->{CurrentUser}
15   or die "rt-setup-support-time must run as a valid RT user.\n";
16
17 $RT::Handle->BeginTransaction;
18
19 sub try {
20   my ($val, $msg) = @_;
21   if ( !$val ) {
22     $RT::Handle->Rollback;
23     die "$msg (reverted changes)\n";
24   }
25 }
26
27 my $TicketCF = RT::CustomField->new($CurrentUser);
28 $TicketCF->LoadByCols(
29   Name => $fieldname,
30   LookupType => 'RT::Queue-RT::Ticket',
31 );
32 if (!defined($TicketCF->Id)) {
33   print "Creating ticket custom field.\n";
34   try( $TicketCF->Create(
35     Name  => $fieldname,
36     Type  => 'TimeValue',
37     MaxValues => 1,
38     LookupType => 'RT::Queue-RT::Ticket',
39   ) );
40   my $OCF = RT::ObjectCustomField->new($CurrentUser);
41   try( $OCF->Create(
42     CustomField => $TicketCF->Id,
43     ObjectId => $queue,
44   ) );
45 }
46
47 my $TxnCF = RT::CustomField->new($CurrentUser);
48 $TxnCF->LoadByCols(
49   Name => $fieldname,
50   LookupType => 'RT::Queue-RT::Ticket-RT::Transaction',
51 );
52 if (!defined($TxnCF->Id)) {
53   print "Creating transaction custom field.\n";
54   try( $TxnCF->Create(
55     Name => $fieldname,
56     Type => 'TimeValue',
57     MaxValues => 1,
58     LookupType => 'RT::Queue-RT::Ticket-RT::Transaction',
59     UILocation => 'TimeWorked',
60   ) );
61   my $OCF = RT::ObjectCustomField->new($CurrentUser);
62   try( $OCF->Create(
63     CustomField => $TxnCF->Id,
64     ObjectId => $queue,
65   ) );
66 }
67
68 my $ScripCondition = RT::ScripCondition->new($CurrentUser);
69 $ScripCondition->Load('On Update');
70 if (!defined($ScripCondition->Id)) {
71   print "Creating On Update condition.\n";
72   try( $ScripCondition->Create(
73     Name => 'On Update',
74     Description => 'Whenever a ticket is updated',
75     ExecModule => 'AnyTransaction',
76     ApplicableTransTypes => 'Any',
77   ) );
78 }
79
80 my $ScripAction = RT::ScripAction->new($CurrentUser);
81 $ScripAction->Load("Update $fieldname");
82 if (!defined($ScripAction->Id)) {
83   print "Creating Update $fieldname action.\n";
84   try( $ScripAction->Create(
85     Name => "Update $fieldname",
86     Description => 'Increment ticket time',
87     ExecModule => 'Accumulate',
88     Argument => $fieldname,
89   ) );
90 }
91
92 my $Template = RT::Template->new($CurrentUser);
93 $Template->Load('Blank');
94 try(0, "No blank template found") if !$Template->Id;
95
96 my $Scrip = RT::Scrip->new($CurrentUser);
97 $Scrip->LoadByCols(
98   ScripCondition => $ScripCondition->Id,
99   ScripAction    => $ScripAction->Id,
100   Queue          => $queue);
101 if (!defined($Scrip->Id)) {
102   print "Creating scrip.\n";
103   try( $Scrip->Create(
104     Description => "On Transaction Update $fieldname",
105     ScripCondition => $ScripCondition->Id,
106     ScripAction => $ScripAction->Id,
107     Stage => 'TransactionCreate',
108     Queue => $queue,
109     Template => $Template->Id,
110   ) );
111 }
112
113 $RT::Handle->Commit;
114 print "Done.\n";
115