fix display_custnum on reports, RT#
[freeside.git] / FS / FS / cust_main_Mixin.pm
1 package FS::cust_main_Mixin;
2
3 use strict;
4 use vars qw( $DEBUG );
5 use FS::UID qw(dbh);
6 use FS::cust_main;
7
8 $DEBUG = 0;
9
10 =head1 NAME
11
12 FS::cust_main_Mixin - Mixin class for records that contain fields from cust_main
13
14 =head1 SYNOPSIS
15
16 package FS::some_table;
17 use vars qw(@ISA);
18 @ISA = qw( FS::cust_main_Mixin FS::Record );
19
20 =head1 DESCRIPTION
21
22 This is a mixin class for records that contain fields from the cust_main table,
23 for example, from a JOINed search.  See httemplate/search/ for examples.
24
25 =head1 METHODS
26
27 =over 4
28
29 =cut
30
31 sub cust_unlinked_msg { '(unlinked)'; }
32 sub cust_linked { $_[0]->custnum; }
33
34 =item display_custnum
35
36 Given an object that contains fields from cust_main (say, from a JOINed
37 search; see httemplate/search/ for examples), returns the equivalent of the
38 FS::cust_main I<name> method, or "(unlinked)" if this object is not linked to
39 a customer.
40
41 =cut
42
43 sub display_custnum {
44   my $self = shift;
45   $self->cust_linked
46     ? FS::cust_main::display_custnum($self)
47     : $self->cust_unlinked_msg;
48 }
49
50 =item name
51
52 Given an object that contains fields from cust_main (say, from a JOINed
53 search; see httemplate/search/ for examples), returns the equivalent of the
54 FS::cust_main I<name> method, or "(unlinked)" if this object is not linked to
55 a customer.
56
57 =cut
58
59 sub name {
60   my $self = shift;
61   $self->cust_linked
62     ? FS::cust_main::name($self)
63     : $self->cust_unlinked_msg;
64 }
65
66 =item ship_name
67
68 Given an object that contains fields from cust_main (say, from a JOINed
69 search; see httemplate/search/ for examples), returns the equivalent of the
70 FS::cust_main I<ship_name> method, or "(unlinked)" if this object is not
71 linked to a customer.
72
73 =cut
74
75 sub ship_name {
76   my $self = shift;
77   $self->cust_linked
78     ? FS::cust_main::ship_name($self)
79     : $self->cust_unlinked_msg;
80 }
81
82 =item contact
83
84 Given an object that contains fields from cust_main (say, from a JOINed
85 search; see httemplate/search/ for examples), returns the equivalent of the
86 FS::cust_main I<contact> method, or "(unlinked)" if this object is not linked
87 to a customer.
88
89 =cut
90
91 sub contact {
92   my $self = shift;
93   $self->cust_linked
94     ? FS::cust_main::contact($self)
95     : $self->cust_unlinked_msg;
96 }
97
98 =item ship_contact
99
100 Given an object that contains fields from cust_main (say, from a JOINed
101 search; see httemplate/search/ for examples), returns the equivalent of the
102 FS::cust_main I<ship_contact> method, or "(unlinked)" if this object is not
103 linked to a customer.
104
105 =cut
106
107 sub ship_contact {
108   my $self = shift;
109   $self->cust_linked
110     ? FS::cust_main::ship_contact($self)
111     : $self->cust_unlinked_msg;
112 }
113
114 =item country_full
115
116 Given an object that contains fields from cust_main (say, from a JOINed
117 search; see httemplate/search/ for examples), returns the equivalent of the
118 FS::cust_main I<country_full> method, or "(unlinked)" if this object is not
119 linked to a customer.
120
121 =cut
122
123 sub country_full {
124   my $self = shift;
125   $self->cust_linked
126     ? FS::cust_main::country_full($self)
127     : $self->cust_unlinked_msg;
128 }
129
130 =item invoicing_list_emailonly
131
132 Given an object that contains fields from cust_main (say, from a JOINed
133 search; see httemplate/search/ for examples), returns the equivalent of the
134 FS::cust_main I<invoicing_list_emailonly> method, or "(unlinked)" if this
135 object is not linked to a customer.
136
137 =cut
138
139 sub invoicing_list_emailonly {
140   my $self = shift;
141   warn "invoicing_list_email only called on $self, ".
142        "custnum ". $self->custnum. "\n"
143     if $DEBUG;
144   $self->cust_linked
145     ? FS::cust_main::invoicing_list_emailonly($self)
146     : $self->cust_unlinked_msg;
147 }
148
149 =item invoicing_list_emailonly_scalar
150
151 Given an object that contains fields from cust_main (say, from a JOINed
152 search; see httemplate/search/ for examples), returns the equivalent of the
153 FS::cust_main I<invoicing_list_emailonly_scalar> method, or "(unlinked)" if
154 this object is not linked to a customer.
155
156 =cut
157
158 sub invoicing_list_emailonly_scalar {
159   my $self = shift;
160   warn "invoicing_list_emailonly called on $self, ".
161        "custnum ". $self->custnum. "\n"
162     if $DEBUG;
163   $self->cust_linked
164     ? FS::cust_main::invoicing_list_emailonly_scalar($self)
165     : $self->cust_unlinked_msg;
166 }
167
168 =item invoicing_list
169
170 Given an object that contains fields from cust_main (say, from a JOINed
171 search; see httemplate/search/ for examples), returns the equivalent of the
172 FS::cust_main I<invoicing_list> method, or "(unlinked)" if this object is not
173 linked to a customer.
174
175 Note: this method is read-only.
176
177 =cut
178
179 #read-only
180 sub invoicing_list {
181   my $self = shift;
182   $self->cust_linked
183     ? FS::cust_main::invoicing_list($self)
184     : ();
185 }
186
187 =item status
188
189 Given an object that contains fields from cust_main (say, from a JOINed
190 search; see httemplate/search/ for examples), returns the equivalent of the
191 FS::cust_main I<status> method, or "(unlinked)" if this object is not linked to
192 a customer.
193
194 =cut
195
196 sub cust_status {
197   my $self = shift;
198   return $self->cust_unlinked_msg unless $self->cust_linked;
199
200   #FS::cust_main::status($self)
201   #false laziness w/actual cust_main::status
202   # (make sure FS::cust_main methods are called)
203   for my $status (qw( prospect active inactive suspended cancelled )) {
204     my $method = $status.'_sql';
205     my $sql = FS::cust_main->$method();;
206     my $numnum = ( $sql =~ s/cust_main\.custnum/?/g );
207     my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
208     $sth->execute( ($self->custnum) x $numnum )
209       or die "Error executing 'SELECT $sql': ". $sth->errstr;
210     return $status if $sth->fetchrow_arrayref->[0];
211   }
212 }
213
214 =item ucfirst_cust_status
215
216 Given an object that contains fields from cust_main (say, from a JOINed
217 search; see httemplate/search/ for examples), returns the equivalent of the
218 FS::cust_main I<ucfirst_status> method, or "(unlinked)" if this object is not
219 linked to a customer.
220
221 =cut
222
223 sub ucfirst_cust_status {
224   my $self = shift;
225   $self->cust_linked
226     ? ucfirst( $self->cust_status(@_) ) 
227     : $self->cust_unlinked_msg;
228 }
229
230 =item cust_statuscolor
231
232 Given an object that contains fields from cust_main (say, from a JOINed
233 search; see httemplate/search/ for examples), returns the equivalent of the
234 FS::cust_main I<statuscol> method, or "000000" if this object is not linked to
235 a customer.
236
237 =cut
238
239 sub cust_statuscolor {
240   my $self = shift;
241
242   $self->cust_linked
243     ? FS::cust_main::cust_statuscolor($self)
244     : '000000';
245 }
246
247 =item prospect_sql
248
249 =item active_sql
250
251 =item inactive_sql
252
253 =item suspended_sql
254
255 =item cancelled_sql
256
257 Given an object that contains fields from cust_main (say, from a JOINed
258 search; see httemplate/search/ for examples), returns the equivalent of the
259 corresponding FS::cust_main method, or "0" if this object is not linked to
260 a customer.
261
262 =cut
263
264 foreach my $sub (qw( prospect active inactive suspended cancelled )) {
265   eval "
266     sub ${sub}_sql {
267       my \$self = shift;
268       \$self->cust_linked
269         ? FS::cust_main::${sub}_sql(\$self)
270         : '0';
271       }
272   ";
273   die $@ if $@;
274 }
275
276 =back
277
278 =head1 BUGS
279
280 =head1 SEE ALSO
281
282 L<FS::cust_main>, L<FS::Record>
283
284 =cut
285
286 1;
287