improve display of ticket service links, #17067
[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 _FreesideURILabelLong {
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     return '<A HREF="' . $self->HREF . '">' .
156                         small_custview( $rec->{'_object'},
157                            scalar(FS::Conf->new->config('countrydefault')),
158                            1, #nobalance
159                         ) . '</A>';
160
161   } elsif ( $table eq 'cust_svc' ) {
162
163     my $string = '';
164     # we now do this within the UI
165     #my $cust = $self->CustomerResolver;
166     #if ( $cust ) {
167     #  $string = $cust->AsStringLong;
168     #}
169     $string .= $self->AsString;
170     return $string;
171
172   } else {
173
174     return $self->_FreesideURILabel();
175
176   }
177
178 }
179
180 sub AsString {
181   my $self = shift;
182   if ( $self->{'fstable'} eq 'cust_svc' ) {
183     return '<B><A HREF="' . $self->HREF . '">' . 
184           $self->_FreesideURILabel . '</A></B>';
185   } else {
186     $self->SUPER::AsString;
187   }
188 }
189
190 sub CustomerResolver {
191   my $self = shift;
192   if ( $self->{fstable} eq 'cust_main' ) {
193     return $self;
194   }
195   elsif ( $self->{fstable} eq 'cust_svc' ) {
196     my $rec = $self->_FreesideGetRecord();
197     return if !$rec;
198     my $cust_pkg = $rec->{'_object'}->cust_pkg;
199     if ( $cust_pkg ) {
200       my $URI = RT::URI->new($self->CurrentUser);
201       $URI->FromURI('freeside://freeside/cust_main/'.$cust_pkg->custnum);
202       return $URI->Resolver;
203     }
204   }
205   return;
206 }
207
208 sub CustomerInfo {
209   my $self = shift;
210   $self = $self->CustomerResolver or return;
211   my $rec = $self->_FreesideGetRecord() or return;
212   my $cust_main = delete $rec->{_object};
213   my $agent = $cust_main->agent;
214   my $class = $cust_main->cust_class;
215   my $referral = qsearchs('part_referral', { refnum => $cust_main->refnum });
216   my @part_tags = $cust_main->part_tag;
217
218   return $self->{CustomerInfo} ||= {
219     %$rec,
220
221     AgentName     => ($agent ? ($agent->agentnum.': '.$agent->agent) : ''),
222     CustomerClass => ($class ? $class->classname : ''),
223     CustomerTags  => [
224       sort { $a->{'name'} <=> $b->{'name'} }
225       map { 
226         { name => $_->tagname, desc => $_->tagdesc, color => $_->tagcolor }
227       } @part_tags
228     ],
229     Referral      => ($referral ? $referral->referral : ''),
230     InvoiceEmail  => $cust_main->invoicing_list_emailonly_scalar,
231     BillingType   => FS::payby->longname($cust_main->payby),
232   }
233 }
234
235 sub ServiceInfo {
236   my $self = shift;
237   $self->{fstable} eq 'cust_svc' or return;
238   my $rec = $self->_FreesideGetRecord() or return;
239   my $cust_svc = $rec->{'_object'};
240   my $svc_x = $cust_svc->svc_x;
241   my $part_svc = $cust_svc->part_svc;
242   return $self->{ServiceInfo} ||= {
243     $cust_svc->hash,
244     $svc_x->hash,
245     ServiceType => $part_svc->svc,
246     Label => $self->AsString,
247   }
248 }
249
250 1;