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