1 package FS::cust_main_Mixin;
4 use vars qw( $DEBUG $me );
5 use Carp qw( confess );
10 $me = '[FS::cust_main_Mixin]';
14 FS::cust_main_Mixin - Mixin class for records that contain fields from cust_main
18 package FS::some_table;
20 @ISA = qw( FS::cust_main_Mixin FS::Record );
24 This is a mixin class for records that contain fields from the cust_main table,
25 for example, from a JOINed search. See httemplate/search/ for examples.
33 sub cust_unlinked_msg { '(unlinked)'; }
34 sub cust_linked { $_[0]->custnum; }
38 Given an object that contains fields from cust_main (say, from a JOINed
39 search; see httemplate/search/ for examples), returns the equivalent of the
40 FS::cust_main I<name> method, or "(unlinked)" if this object is not linked to
48 ? FS::cust_main::display_custnum($self)
49 : $self->cust_unlinked_msg;
54 Given an object that contains fields from cust_main (say, from a JOINed
55 search; see httemplate/search/ for examples), returns the equivalent of the
56 FS::cust_main I<name> method, or "(unlinked)" if this object is not linked to
64 ? FS::cust_main::name($self)
65 : $self->cust_unlinked_msg;
70 Given an object that contains fields from cust_main (say, from a JOINed
71 search; see httemplate/search/ for examples), returns the equivalent of the
72 FS::cust_main I<ship_name> method, or "(unlinked)" if this object is not
80 ? FS::cust_main::ship_name($self)
81 : $self->cust_unlinked_msg;
86 Given an object that contains fields from cust_main (say, from a JOINed
87 search; see httemplate/search/ for examples), returns the equivalent of the
88 FS::cust_main I<contact> method, or "(unlinked)" if this object is not linked
96 ? FS::cust_main::contact($self)
97 : $self->cust_unlinked_msg;
102 Given an object that contains fields from cust_main (say, from a JOINed
103 search; see httemplate/search/ for examples), returns the equivalent of the
104 FS::cust_main I<ship_contact> method, or "(unlinked)" if this object is not
105 linked to a customer.
112 ? FS::cust_main::ship_contact($self)
113 : $self->cust_unlinked_msg;
118 Given an object that contains fields from cust_main (say, from a JOINed
119 search; see httemplate/search/ for examples), returns the equivalent of the
120 FS::cust_main I<country_full> method, or "(unlinked)" if this object is not
121 linked to a customer.
128 ? FS::cust_main::country_full($self)
129 : $self->cust_unlinked_msg;
132 =item invoicing_list_emailonly
134 Given an object that contains fields from cust_main (say, from a JOINed
135 search; see httemplate/search/ for examples), returns the equivalent of the
136 FS::cust_main I<invoicing_list_emailonly> method, or "(unlinked)" if this
137 object is not linked to a customer.
141 sub invoicing_list_emailonly {
143 warn "invoicing_list_email only called on $self, ".
144 "custnum ". $self->custnum. "\n"
147 ? FS::cust_main::invoicing_list_emailonly($self)
148 : $self->cust_unlinked_msg;
151 =item invoicing_list_emailonly_scalar
153 Given an object that contains fields from cust_main (say, from a JOINed
154 search; see httemplate/search/ for examples), returns the equivalent of the
155 FS::cust_main I<invoicing_list_emailonly_scalar> method, or "(unlinked)" if
156 this object is not linked to a customer.
160 sub invoicing_list_emailonly_scalar {
162 warn "invoicing_list_emailonly called on $self, ".
163 "custnum ". $self->custnum. "\n"
166 ? FS::cust_main::invoicing_list_emailonly_scalar($self)
167 : $self->cust_unlinked_msg;
172 Given an object that contains fields from cust_main (say, from a JOINed
173 search; see httemplate/search/ for examples), returns the equivalent of the
174 FS::cust_main I<invoicing_list> method, or "(unlinked)" if this object is not
175 linked to a customer.
177 Note: this method is read-only.
185 ? FS::cust_main::invoicing_list($self)
191 Given an object that contains fields from cust_main (say, from a JOINed
192 search; see httemplate/search/ for examples), returns the equivalent of the
193 FS::cust_main I<status> method, or "(unlinked)" if this object is not linked to
200 return $self->cust_unlinked_msg unless $self->cust_linked;
202 #FS::cust_main::status($self)
203 #false laziness w/actual cust_main::status
204 # (make sure FS::cust_main methods are called)
205 for my $status (qw( prospect active inactive suspended cancelled )) {
206 my $method = $status.'_sql';
207 my $sql = FS::cust_main->$method();;
208 my $numnum = ( $sql =~ s/cust_main\.custnum/?/g );
209 my $sth = dbh->prepare("SELECT $sql") or die dbh->errstr;
210 $sth->execute( ($self->custnum) x $numnum )
211 or die "Error executing 'SELECT $sql': ". $sth->errstr;
212 return $status if $sth->fetchrow_arrayref->[0];
216 =item ucfirst_cust_status
218 Given an object that contains fields from cust_main (say, from a JOINed
219 search; see httemplate/search/ for examples), returns the equivalent of the
220 FS::cust_main I<ucfirst_status> method, or "(unlinked)" if this object is not
221 linked to a customer.
225 sub ucfirst_cust_status {
228 ? ucfirst( $self->cust_status(@_) )
229 : $self->cust_unlinked_msg;
232 =item cust_statuscolor
234 Given an object that contains fields from cust_main (say, from a JOINed
235 search; see httemplate/search/ for examples), returns the equivalent of the
236 FS::cust_main I<statuscol> method, or "000000" if this object is not linked to
241 sub cust_statuscolor {
245 ? FS::cust_main::cust_statuscolor($self)
259 Class methods that return SQL framents, equivalent to the corresponding
260 FS::cust_main method.
265 # \$self->cust_linked
266 # ? FS::cust_main::${sub}_sql(\$self)
269 foreach my $sub (qw( prospect active inactive suspended cancelled )) {
272 confess 'cust_main_Mixin ${sub}_sql called with object' if ref(\$_[0]);
273 'cust_main.custnum IS NOT NULL AND '. FS::cust_main->${sub}_sql();
279 =item cust_search_sql
281 Returns a list of SQL WHERE fragments to search for parameters specified
282 in HASHREF. Valid parameters are:
296 sub cust_search_sql {
297 my($class, $param) = @_;
300 warn "$me cust_search_sql called with params: \n".
301 join("\n", map { " $_: ". $param->{$_} } keys %$param ). "\n";
306 if ( $param->{'agentnum'} && $param->{'agentnum'} =~ /^(\d+)$/ ) {
307 push @search, "cust_main.agentnum = $1";
310 #status (prospect active inactive suspended cancelled)
311 if ( grep { $param->{'status'} eq $_ } FS::cust_main->statuses() ) {
312 my $method = $param->{'status'}. '_sql';
313 push @search, $class->$method();
317 my @payby = ref($param->{'payby'})
318 ? @{ $param->{'payby'} }
319 : split(',', $param->{'payby'});
320 @payby = grep /^([A-Z]{4})$/, @payby;
322 push @search, 'cust_main.payby IN ('. join(',', map "'$_'", @payby). ')';
325 #here is the agent virtualization
327 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table' => 'cust_main' );
339 L<FS::cust_main>, L<FS::Record>