3 # Copyright (c) 2004 Kristian Hoffmann <khoff@fire2wire.com>
4 # Based on the original RT::URI::base and RT::URI::fsck_com_rt.
6 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
8 # (Except where explictly superceded by other copyright notices)
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
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.
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.
29 no warnings qw(redefine);
35 use FS::CGI qw(popurl);
36 use FS::UI::Web::small_custview;
38 use FS::Record qw(qsearchs qsearch dbdef);
45 FS::UID->install_callback(
46 sub { @RT::URI::freeside::svc_tables = FS::part_svc->svc_tables() }
51 RT::URI::freeside::Internal
55 Overlay for the RT::URI::freeside URI handler implementing the Internal integration type.
57 See L<RT::URI::freeside> for public/private interface documentation.
63 sub _FreesideGetRecord {
66 my ($table, $pkey) = ($self->{'fstable'}, $self->{'fspkey'});
68 $RT::Logger->debug("Called _FreesideGetRecord()");
70 #eval "use FS::$table;";
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.");
80 my $pkeyfield = $dbdef->table($table)->primary_key;
82 $RT::Logger->error("No primary key for freeside table '$table'");
86 my $fsrec = qsearchs($table, { $pkeyfield => $pkey });
88 $RT::Logger->error("Record with '$pkeyfield' == '$pkey' does " .
89 "not exist in table $table");
93 return { $fsrec->hash, '_object' => $fsrec };
103 sub FreesideGetConfig {
105 #$conf = new FS::Conf unless ref($conf);
106 my $conf = new FS::Conf;
108 return scalar($conf->config(@_));
112 sub smart_search { #Subroutine
114 return map { { $_->hash } } &FS::cust_main::Search::smart_search(@_);
121 my $cust_pkg = $_->cust_pkg;
122 my $custnum = $cust_pkg->custnum if $cust_pkg;
123 my $label = join(': ',($_->label)[0, 1]);
127 'custnum' => $custnum, # so that it's smart_searchable...
130 } &FS::cust_svc::smart_search(@_);
134 sub email_search { #Subroutine
136 return map { { $_->hash } } &FS::cust_main::Search::email_search(@_);
142 return &FS::UI::Web::small_custview::small_custview(@_);
150 my $table = $self->{'fstable'};
152 if ( $table eq 'cust_main' ) {
154 my $rec = $self->_FreesideGetRecord();
156 return '<I>Customer #'.$self->{'fspkey'}.' (not found)</I>';
158 return '<A HREF="' . $self->HREF . '">' .
159 small_custview( $rec->{'_object'},
160 scalar(FS::Conf->new->config('countrydefault')),
164 } elsif ( $table eq 'cust_svc' ) {
167 my $cust = $self->CustomerResolver;
169 # the customer's small_custview
170 $string = $cust->AsStringLong();
172 # + the service label and link
173 $string .= $self->ShortLink;
178 return $self->SUPER::AsStringLong;
185 # because I don't want AsString to sometimes return a hunk of HTML, but
186 # on the other hand AsStringLong does something specific.
188 '<B><A HREF="'.$self->HREF.'">' . $self->_FreesideURILabel . '</A></B>';
191 sub CustomerResolver {
193 if ( $self->{fstable} eq 'cust_main' ) {
196 elsif ( $self->{fstable} eq 'cust_svc' ) {
197 my $rec = $self->_FreesideGetRecord();
199 my $cust_pkg = $rec->{'_object'}->cust_pkg;
201 my $URI = RT::URI->new($self->CurrentUser);
202 $URI->FromURI('freeside://freeside/cust_main/'.$cust_pkg->custnum);
203 return $URI->Resolver;
213 $self = $self->CustomerResolver;
214 return $self->{CustomerInfo} if $self->{CustomerInfo};
216 my $rec = $self->_FreesideGetRecord() if $self;
218 # AsStringLong will report an error;
219 # here, just avoid breaking things
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;
237 my @lf = $cust_main->location_fields;
238 my $bill_location = $cust_main->bill_location;
239 my $ship_location = $cust_main->ship_location;
244 AgentName => ($agent ? ($agent->agentnum.': '.$agent->agent) : ''),
245 CustomerClass => ($class ? $class->classname : ''),
247 sort { $a->{'name'} <=> $b->{'name'} }
249 { name => $_->tagname, desc => $_->tagdesc, color => $_->tagcolor }
252 Referral => ($referral ? $referral->referral : ''),
253 InvoiceEmail => $cust_main->invoicing_list_emailonly_scalar,
254 BillingType => FS::payby->longname($cust_main->payby),
257 foreach my $field (@lf) {
258 $info->{"bill_$field"} = $bill_location->get($field);
259 $info->{"ship_$field"} = $ship_location->get($field);
261 $info->{"bill_location"} = $bill_location->location_label(no_prefix => 1);
262 $info->{"ship_location"} = $ship_location->location_label(no_prefix => 1);
265 return $self->{CustomerInfo} = $info;
270 $self->{fstable} eq 'cust_svc' or return;
271 my $rec = $self->_FreesideGetRecord() or return;
272 my $cust_svc = $rec->{'_object'};
273 my $svc_x = $cust_svc->svc_x;
274 my $part_svc = $cust_svc->part_svc;
275 return $self->{ServiceInfo} ||= {
278 ServiceType => $part_svc->svc,
279 Label => $self->AsString,