improve customer field access in RT queries, #16490
[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::payby;
42
43 =head1 NAME
44
45 RT::URI::freeside::Internal
46
47 =head1 DESCRIPTION
48
49 Overlay for the RT::URI::freeside URI handler implementing the Internal integration type.
50
51 See L<RT::URI::freeside> for public/private interface documentation.
52
53 =cut
54
55
56
57 sub _FreesideGetRecord {
58
59   my $self = shift;
60   my ($table, $pkey) = ($self->{'fstable'}, $self->{'fspkey'});
61
62   $RT::Logger->debug("Called _FreesideGetRecord()");
63
64   #eval "use FS::$table;";
65
66   my $dbdef = dbdef;
67   unless ($dbdef) {
68     $RT::Logger->error("Using Internal freeside integration type, ".
69                        "but it doesn't look like we're running under ".
70                        "freeside's Mason handler.");
71     return;
72   }
73
74   my $pkeyfield = $dbdef->table($table)->primary_key;
75   unless ($pkeyfield) {
76     $RT::Logger->error("No primary key for freeside table '$table'");
77     return;
78   }
79
80   my $fsrec = qsearchs($table, { $pkeyfield => $pkey });
81   unless ($fsrec) {
82     $RT::Logger->error("Record with '$pkeyfield' == '$pkey' does " .
83                        "not exist in table $table");
84     return;
85   }
86
87   return { $fsrec->hash, '_object' => $fsrec };
88
89 }
90
91 sub FreesideVersion {
92
93   return $FS::VERSION;
94
95 }
96
97 sub FreesideGetConfig {
98
99   #$conf = new FS::Conf unless ref($conf);
100   my $conf = new FS::Conf;
101
102   return scalar($conf->config(@_));
103
104 }
105
106 sub smart_search { #Subroutine
107
108   return map { { $_->hash } } &FS::cust_main::Search::smart_search(@_);
109
110 }
111
112 sub email_search { #Subroutine
113
114   return map { { $_->hash } } &FS::cust_main::Search::email_search(@_);
115
116 }
117
118 sub small_custview {
119
120   return &FS::UI::Web::small_custview::small_custview(@_);
121
122 }
123
124 sub _FreesideURILabelLong {
125
126   my $self = shift;
127
128   my $table = $self->{'fstable'};
129
130   if ( $table eq 'cust_main' ) {
131
132     my $rec = $self->_FreesideGetRecord();
133     return small_custview( $rec->{'_object'},
134                            scalar(FS::Conf->new->config('countrydefault')),
135                            1 #nobalance
136                          );
137
138   } else {
139
140     return $self->_FreesideURILabel();
141
142   }
143
144 }
145
146 # no need to have a separate wrapper method for every one of these things
147 sub CustomerInfo {
148   my $self = shift;
149   my $rec = $self->_FreesideGetRecord() or return;
150   my $cust_main = $rec->{'_object'};
151   my $agent = $cust_main->agent;
152   my $class = $cust_main->cust_class;
153   my $referral = qsearchs('part_referral', { refnum => $cust_main->refnum });
154   my @part_tags = $cust_main->part_tag;
155
156   return $self->{CustomerInfo} ||= {
157     $cust_main->hash,
158
159     AgentName     => ($agent ? ($agent->agentnum.': '.$agent->agent) : ''),
160     CustomerClass => ($class ? $class->classname : ''),
161     CustomerTags  => [
162       sort { $a->{'name'} <=> $b->{'name'} }
163       map { 
164         { name => $_->tagname, desc => $_->tagdesc, color => $_->tagcolor }
165       } @part_tags
166     ],
167     Referral      => ($referral ? $referral->referral : ''),
168     InvoiceEmail  => $cust_main->invoicing_list_emailonly_scalar,
169     BillingType   => FS::payby->longname($cust_main->payby),
170   }
171 }
172
173 1;