fix 'Can't call method "setup" on an undefined value' error when using into rates...
[freeside.git] / FS / FS / TicketSystem.pm
1 package FS::TicketSystem;
2
3 use strict;
4 use vars qw( $conf $system $AUTOLOAD );
5 use FS::Conf;
6 use FS::UID qw( dbh driver_name );
7 use FS::Record qw( dbdef );
8
9 FS::UID->install_callback( sub { 
10   $conf = new FS::Conf;
11   $system = $conf->config('ticket_system');
12 } );
13
14 sub AUTOLOAD {
15   my $self = shift;
16
17   my($sub)=$AUTOLOAD;
18   $sub =~ s/.*://;
19
20   my $conf = new FS::Conf;
21   die "FS::TicketSystem::$AUTOLOAD called, but no ticket system configured\n"
22     unless $system;
23
24   eval "use FS::TicketSystem::$system;";
25   die $@ if $@;
26
27   $self .= "::$system";
28   $self->$sub(@_);
29 }
30
31 # Our schema changes
32 my %columns = (
33   Tickets => {
34     WillResolve => { type => 'timestamp', null => 1, default => '', },
35   },
36   CustomFields => {
37     Required => { type => 'integer', default => 0, null => 0 },
38   },
39 );
40
41 sub _upgrade_schema {
42   my $system = FS::Conf->new->config('ticket_system');
43   return if !defined($system) || $system ne 'RT_Internal';
44   my ($class, %opts) = @_;
45
46   my $dbh = dbh;
47   my @sql;
48   my $case = driver_name eq 'mysql' ? sub {@_} : sub {map lc, @_};
49   foreach my $tablename (keys %columns) {
50     my $table = dbdef->table(&$case($tablename));
51     if ( !$table ) {
52       warn 
53       "$tablename table does not exist.  Your RT installation is incomplete.\n";
54       next;
55     }
56     foreach my $colname (keys %{ $columns{$tablename} }) {
57       if ( !$table->column(&$case($colname)) ) {
58         my $col = new DBIx::DBSchema::Column {
59             table_obj => $table,
60             name => &$case($colname),
61             %{ $columns{$tablename}->{$colname} }
62           };
63         $col->table_obj($table);
64         push @sql, $col->sql_add_column($dbh);
65       }
66     } #foreach $colname
67   } #foreach $tablename
68
69   return if !@sql;
70   warn "Upgrading RT schema:\n";
71   foreach my $statement (@sql) {
72     warn "$statement\n";
73     $dbh->do( $statement )
74       or die "Error: ". $dbh->errstr. "\n executing: $statement";
75   }
76   return;
77 }
78
79 sub _upgrade_data {
80   return if !defined($system) || $system ne 'RT_Internal';
81   my ($class, %opts) = @_;
82
83   # go ahead and use the RT API for this
84   
85   FS::TicketSystem->init;
86   my $session = FS::TicketSystem->session();
87   # bypass RT ACLs--we're going to do lots of things
88   my $CurrentUser = $RT::SystemUser;
89
90   # selfservice and cron users
91   foreach my $username ('%%%SELFSERVICE_USER%%%', 'fs_daily') {
92     my $User = RT::User->new($CurrentUser);
93     $User->Load($username);
94     if (!defined($User->Id)) {
95       my ($val, $msg) = $User->Create(
96         'Name' => $username,
97         'Gecos' => $username,
98         'Privileged' => 1,
99         # any other fields needed?
100       );
101       die $msg if !$val;
102     }
103     my $Principal = $User->PrincipalObj; # can this ever fail?
104     my @rights = ( qw(ShowTicket SeeQueue ModifyTicket ReplyToTicket 
105                       CreateTicket SeeCustomField) );
106     foreach (@rights) {
107       next if $Principal->HasRight( 'Right' => $_, Object => $RT::System );
108       my ($val, $msg) = $Principal->GrantRight(
109         'Right' => $_,
110         'Object' => $RT::System,
111       );
112       die $msg if !$val;
113     }
114   } #foreach $username
115
116   # EscalateQueue custom field and friends
117   my $CF = RT::CustomField->new($CurrentUser);
118   $CF->Load('EscalateQueue');
119   if (!defined($CF->Id)) {
120     my ($val, $msg) = $CF->Create(
121       'Name' => 'EscalateQueue',
122       'Type' => 'Select',
123       'MaxValues' => 1,
124       'LookupType' => 'RT::Queue',
125       'Description' => 'Escalate to Queue',
126       'ValuesClass' => 'RT::CustomFieldValues::Queues', #magic!
127     );
128     die $msg if !$val;
129     my $OCF = RT::ObjectCustomField->new($CurrentUser);
130     ($val, $msg) = $OCF->Create(
131       'CustomField' => $CF->Id,
132       'ObjectId' => 0,
133     );
134     die $msg if !$val;
135   }
136
137   # Load from RT data file
138   our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
139        @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final,
140        %Delete_Scrips);
141   my $datafile = '%%%RT_PATH%%%/etc/initialdata';
142   eval { require $datafile };
143   if ( $@ ) {
144     warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
145     return;
146   }
147
148   # Cache existing ScripCondition, ScripAction, and Template IDs.
149   # Complicated because we don't want to just step on multiple IDs 
150   # with the same name.
151   my $cachify = sub {
152     my ($class, $hash) = @_;
153     my $search = $class->new($CurrentUser);
154     $search->UnLimit;
155     while ( my $item = $search->Next ) {
156       my $ids = $hash->{lc($item->Name)} ||= [];
157       if ( $item->Creator == 1 ) { # RT::SystemUser
158         unshift @$ids, $item->Id;
159       }
160       else {
161         push @$ids, $item->Id;
162       }
163     }
164   };
165
166   my (%condition, %action, %template);
167   &$cachify('RT::ScripConditions', \%condition);
168   &$cachify('RT::ScripActions', \%action);
169   &$cachify('RT::Templates', \%template);
170   # $condition{name} = [ ids... ]
171   # with the id of the system-created object first, if there is one
172
173   # ScripConditions
174   my $ScripCondition = RT::ScripCondition->new($CurrentUser);
175   foreach my $sc (@ScripConditions) {
176     # $sc: Name, Description, ApplicableTransTypes, ExecModule, Argument
177     next if exists( $condition{ lc($sc->{Name}) } );
178     my ($val, $msg) = $ScripCondition->Create( %$sc );
179     die $msg if !$val;
180     $condition{ lc($ScripCondition->Name) } = [ $ScripCondition->Id ];
181   }
182
183   # ScripActions
184   my $ScripAction = RT::ScripAction->new($CurrentUser);
185   foreach my $sa (@ScripActions) {
186     # $sa: Name, Description, ExecModule, Argument
187     next if exists( $action{ lc($sa->{Name}) } );
188     my ($val, $msg) = $ScripAction->Create( %$sa );
189     die $msg if !$val;
190     $action{ lc($ScripAction->Name) } = [ $ScripAction->Id ];
191   }
192
193   # Templates
194   my $Template = RT::Template->new($CurrentUser);
195   foreach my $t (@Templates) {
196     # $t: Queue, Name, Description, Content
197     next if exists( $template{ lc($t->{Name}) } );
198     my ($val, $msg) = $Template->Create( %$t );
199     die $msg if !$val;
200     $template{ lc($Template->Name) } = [ $Template->Id ];
201   }
202
203   # Scrips
204   my %scrip; # $scrips{condition}{action}{template} = id
205   my $search = RT::Scrips->new($CurrentUser);
206   $search->Limit(FIELD => 'Queue', VALUE => 0);
207   while (my $item = $search->Next) {
208     my ($c, $a, $t) = map {lc $item->$_->Name} 
209       ('ScripConditionObj', 'ScripActionObj', 'TemplateObj');
210     if ( exists $scrip{$c}{$a}{$t} and $item->Creator == 1 ) {
211       warn "Deleting duplicate scrip $c $a [$t]\n";
212       my ($val, $msg) = $item->Delete;
213       warn "error deleting scrip: $msg\n" if !$val;
214     }
215     elsif ( exists $Delete_Scrips{$c}{$a}{$t} and $item->Creator == 1 ) {
216       warn "Deleting obsolete scrip $c $a [$t]\n";
217       my ($val, $msg) = $item->Delete;
218       warn "error deleting scrip: $msg\n" if !$val;
219     }
220     else {
221       $scrip{$c}{$a}{$t} = $item->id;
222     }
223   }
224   my $Scrip = RT::Scrip->new($CurrentUser);
225   foreach my $s ( @Scrips ) {
226     my $desc = $s->{'Description'};
227     my ($c, $a, $t) = map lc,
228       @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
229     # skip existing scrips
230     next if ( exists($scrip{$c}{$a}{$t}) );
231     if ( !exists($condition{$c}) ) {
232       warn "ScripCondition '$c' not found.\n";
233       next;
234     }
235     if ( !exists($action{$a}) ) {
236       warn "ScripAction '$a' not found.\n";
237       next;
238     }
239     if ( !exists($template{$t}) ) {
240       warn "Template '$t' not found.\n";
241       next;
242     }
243     my %new_param = (
244       ScripCondition => $condition{$c}->[0],
245       ScripAction => $action{$a}->[0],
246       Template => $template{$t}->[0],
247       Queue => 0,
248       Description => $desc,
249     );
250     warn "Creating scrip: $c $a [$t]\n";
251     my ($val, $msg) = $Scrip->Create(%new_param);
252     die $msg if !$val;
253   } #foreach (@Scrips)
254
255   return;
256 }
257
258 1;