add service location to RT ticket search, #19154
[freeside.git] / rt / lib / RT / URI / freeside / Internal.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 2004 Kristian Hoffmann <khoff@fire2wire.com>
4 # Based on the original RT::URI::base and RT::URI::fsck_com_rt.
5
6 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
7
8 # (Except where explictly superceded by other copyright notices)
9
10 # This work is made available to you under the terms of Version 2 of
11 # the GNU General Public License. A copy of that license should have
12 # been provided with this software, but in any event can be snarfed
13 # from www.gnu.org.
14
15 # This work is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # General Public License for more details.
19
20 # Unless otherwise specified, all modifications, corrections or
21 # extensions to this work which alter its source code become the
22 # property of Best Practical Solutions, LLC when submitted for
23 # inclusion in the work.
24
25
26 # END LICENSE BLOCK
27 #
28 use strict;
29 no warnings qw(redefine);
30
31 #use vars qw($conf);
32
33 use FS;
34 use FS::UID qw(dbh);
35 use FS::CGI qw(popurl);
36 use FS::UI::Web::small_custview;
37 use FS::Conf;
38 use FS::Record qw(qsearchs qsearch dbdef);
39 use FS::cust_main;
40 use FS::cust_svc;
41 use FS::part_svc;
42 use FS::payby;
43
44 #can I do this?
45 FS::UID->install_callback(
46   sub { @RT::URI::freeside::svc_tables = FS::part_svc->svc_tables() }
47 );
48
49 =head1 NAME
50
51 RT::URI::freeside::Internal
52
53 =head1 DESCRIPTION
54
55 Overlay for the RT::URI::freeside URI handler implementing the Internal integration type.
56
57 See L<RT::URI::freeside> for public/private interface documentation.
58
59 =cut
60
61
62
63 sub _FreesideGetRecord {
64
65   my $self = shift;
66   my ($table, $pkey) = ($self->{'fstable'}, $self->{'fspkey'});
67
68   $RT::Logger->debug("Called _FreesideGetRecord()");
69
70   #eval "use FS::$table;";
71
72   my $dbdef = dbdef;
73   unless ($dbdef) {
74     $RT::Logger->error("Using Internal freeside integration type, ".
75                        "but it doesn't look like we're running under ".
76                        "freeside's Mason handler.");
77     return;
78   }
79
80   my $pkeyfield = $dbdef->table($table)->primary_key;
81   unless ($pkeyfield) {
82     $RT::Logger->error("No primary key for freeside table '$table'");
83     return;
84   }
85
86   my $fsrec = qsearchs($table, { $pkeyfield => $pkey });
87   unless ($fsrec) {
88     $RT::Logger->error("Record with '$pkeyfield' == '$pkey' does " .
89                        "not exist in table $table");
90     return;
91   }
92
93   return { $fsrec->hash, '_object' => $fsrec };
94
95 }
96
97 sub FreesideVersion {
98
99   return $FS::VERSION;
100
101 }
102
103 sub FreesideGetConfig {
104
105   #$conf = new FS::Conf unless ref($conf);
106   my $conf = new FS::Conf;
107
108   return scalar($conf->config(@_));
109
110 }
111
112 sub smart_search { #Subroutine
113
114     return map { { $_->hash } } &FS::cust_main::Search::smart_search(@_);
115
116 }
117
118 sub service_search {
119
120     return map {
121       my $cust_pkg = $_->cust_pkg;
122       my $custnum = $cust_pkg->custnum if $cust_pkg;
123       my $label = join(': ',($_->label)[0, 1]);
124       my %hash = (
125         $_->hash,
126         'label' => $label,
127         'custnum' => $custnum, # so that it's smart_searchable...
128       );
129       \%hash
130     } &FS::cust_svc::smart_search(@_);
131
132 }
133
134 sub email_search { #Subroutine
135
136   return map { { $_->hash } } &FS::cust_main::Search::email_search(@_);
137
138 }
139
140 sub small_custview {
141
142   return &FS::UI::Web::small_custview::small_custview(@_);
143
144 }
145
146 sub AsStringLong {
147
148   my $self = shift;
149
150   my $table = $self->{'fstable'};
151
152   if ( $table eq 'cust_main' ) {
153
154     my $rec = $self->_FreesideGetRecord();
155     if (!$rec) {
156       return '<I>Customer #'.$self->{'fspkey'}.' (not found)</I>';
157     }
158     return '<A HREF="' . $self->HREF . '">' .
159                         small_custview( $rec->{'_object'},
160                            scalar(FS::Conf->new->config('countrydefault')),
161                            1, #nobalance
162                         ) . '</A>';
163
164   } elsif ( $table eq 'cust_svc' ) {
165
166     my $string = '';
167     my $cust = $self->CustomerResolver;
168     if ( $cust ) {
169       # the customer's small_custview
170       $string = $cust->AsStringLong();
171     }
172     # + the service label and link
173     $string .= $self->ShortLink;
174     return $string;
175
176   } else {
177
178     return $self->SUPER::AsStringLong;
179
180   }
181
182 }
183
184 sub ShortLink {
185   # because I don't want AsString to sometimes return a hunk of HTML, but
186   # on the other hand AsStringLong does something specific.
187   my $self = shift;
188   '<B><A HREF="'.$self->HREF.'">' . $self->_FreesideURILabel . '</A></B>';
189 }
190
191 sub CustomerResolver {
192   my $self = shift;
193   if ( $self->{fstable} eq 'cust_main' ) {
194     return $self;
195   }
196   elsif ( $self->{fstable} eq 'cust_svc' ) {
197     my $rec = $self->_FreesideGetRecord();
198     if ($rec) {
199       my $cust_pkg = $rec->{'_object'}->cust_pkg;
200       if ( $cust_pkg ) {
201         my $URI = RT::URI->new($self->CurrentUser);
202         $URI->FromURI('freeside://freeside/cust_main/'.$cust_pkg->custnum);
203         return $URI->Resolver;
204       }
205     }
206     return;
207   }
208   return;
209 }
210
211 sub CustomerInfo {
212   my $self = shift;
213   return $self->{CustomerInfo} if $self->{CustomerInfo};
214   $self = $self->CustomerResolver;
215
216   my $rec = $self->_FreesideGetRecord() if $self;
217   if (!$rec) {
218     # AsStringLong will report an error;
219     # here, just avoid breaking things
220     my $error = {
221       AgentName     => '',
222       CustomerClass => '',
223       CustomerTags  => [],
224       Referral      => '',
225       InvoiceEmail  => '',
226       BillingType   => '',
227     };
228     return $error;
229   }
230
231   my $cust_main = delete $rec->{_object};
232   my $agent = $cust_main->agent;
233   my $class = $cust_main->cust_class;
234   my $referral = qsearchs('part_referral', { refnum => $cust_main->refnum });
235   my @part_tags = $cust_main->part_tag;
236
237   my @lf = $cust_main->location_fields;
238   my $bill_location = $cust_main->bill_location;
239   my $ship_location = $cust_main->ship_location;
240
241   my $info = {
242     %$rec,
243
244     AgentName     => ($agent ? ($agent->agentnum.': '.$agent->agent) : ''),
245     CustomerClass => ($class ? $class->classname : ''),
246     CustomerTags  => [
247       sort { $a->{'name'} <=> $b->{'name'} }
248       map { 
249         { name => $_->tagname, desc => $_->tagdesc, color => $_->tagcolor }
250       } @part_tags
251     ],
252     Referral      => ($referral ? $referral->referral : ''),
253     InvoiceEmail  => $cust_main->invoicing_list_emailonly_scalar,
254     BillingType   => FS::payby->longname($cust_main->payby),
255   };
256
257   foreach my $field (@lf) {
258     $info->{"bill_$field"} = $bill_location->get($field);
259     $info->{"ship_$field"} = $ship_location->get($field);
260   }
261   $info->{"bill_location"} = $bill_location->location_label(no_prefix => 1);
262   $info->{"ship_location"} = $ship_location->location_label(no_prefix => 1);
263
264   return $self->{CustomerInfo} = $info;
265 }
266
267 sub ServiceInfo {
268   my $self = shift;
269   $self->{fstable} eq 'cust_svc' or return;
270   return $self->{ServiceInfo} if $self->{ServiceInfo};
271
272   my $rec = $self->_FreesideGetRecord() or return;
273   my $cust_svc = $rec->{'_object'};
274   my $svc_x = $cust_svc->svc_x;
275   my $part_svc = $cust_svc->part_svc;
276   my $locationnum = $cust_svc->cust_pkg->locationnum;
277   my $cust_location = qsearchs('cust_location', { locationnum => $locationnum});
278   my @lf = FS::cust_main->location_fields;
279
280   # location fields are not prefixed
281   my $info = {
282     $cust_svc->hash,
283     $svc_x->hash,
284     $cust_location->hash,
285     ServiceType => $part_svc->svc,
286     Label => $self->AsString,
287   };
288   $info->{'location'} = $cust_location->location_label(no_prefix => 1);
289
290   return $self->{ServiceInfo} = $info;
291 }
292
293 1;