add customer fields option with agent, display_custnum, status and name, RT#73721
[freeside.git] / FS / FS / part_referral.pm
1 package FS::part_referral;
2 use base qw(FS::Record);
3
4 use strict;
5 use vars qw( @ISA $setup_hack );
6 use FS::Record qw( dbh qsearch ); #qsearchs );
7
8 @ISA = qw( FS::Record );
9 $setup_hack = 0;
10
11 =head1 NAME
12
13 FS::part_referral - Object methods for part_referral objects
14
15 =head1 SYNOPSIS
16
17   use FS::part_referral;
18
19   $record = new FS::part_referral \%hash
20   $record = new FS::part_referral { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::part_referral represents a advertising source - where a customer heard
33 of your services.  This can be used to track the effectiveness of a particular
34 piece of advertising, for example.  FS::part_referral inherits from FS::Record.
35 The following fields are currently supported:
36
37 =over 4
38
39 =item refnum - primary key (assigned automatically for new referrals)
40
41 =item referral - Text name of this advertising source
42
43 =item disabled - Disabled flag, empty or 'Y'
44
45 =item agentnum - Optional agentnum (see L<FS::agent>)
46
47 =item title - an optional external string that identifies this
48 referral source, such as an advertising campaign code.
49
50 =back
51
52 =head1 NOTE
53
54 These were called B<referrals> before version 1.4.0 - the name was changed
55 so as not to be confused with the new customer-to-customer referrals.
56
57 =head1 METHODS
58
59 =over 4
60
61 =item new HASHREF
62
63 Creates a new advertising source.  To add the referral to the database, see
64 L<"insert">.
65
66 =cut
67
68 sub table { 'part_referral'; }
69
70 =item insert
71
72 Adds this advertising source to the database.  If there is an error, returns
73 the error, otherwise returns false.
74
75 =item delete
76
77 Currently unimplemented.
78
79 =cut
80
81 sub delete {
82   my $self = shift;
83   return "Can't (yet?) delete part_referral records";
84   #need to make sure no customers have this referral!
85 }
86
87 =item replace OLD_RECORD
88
89 Replaces OLD_RECORD with this one in the database.  If there is an error,
90 returns the error, otherwise returns false.
91
92 =item check
93
94 Checks all fields to make sure this is a valid advertising source.  If there is
95 an error, returns the error, otherwise returns false.  Called by the insert and
96 replace methods.
97
98 =cut
99
100 sub check {
101   my $self = shift;
102
103   my $error = $self->ut_numbern('refnum')
104     || $self->ut_text('referral')
105     || $self->ut_enum('disabled', [ '', 'Y' ] )
106     #|| $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
107     || $self->ut_textn('title')
108     || ( $setup_hack
109            ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
110            : $self->ut_agentnum_acl('agentnum', 'Edit global advertising sources')
111        )
112   ;
113   return $error if $error;
114
115   $self->SUPER::check;
116 }
117
118 =item agent 
119
120 Returns the associated agent for this referral, if any, as an FS::agent object.
121
122 =back
123
124 =head1 CLASS METHODS
125
126 =over 4
127
128 =item acl_agentnum_sql [ INCLUDE_GLOBAL_BOOL ]
129
130 Returns an SQL fragment for searching for part_referral records allowed by the
131 current users's agent ACLs (and "Edit global advertising sources" right).
132
133 Pass a true value to include global advertising sources (for example, when
134 simply using rather than editing advertising sources).
135
136 =cut
137
138 sub acl_agentnum_sql {
139   my $self = shift;
140
141   my $curuser = $FS::CurrentUser::CurrentUser;
142   my $sql = $curuser->agentnums_sql;
143   $sql = " ( $sql OR agentnum IS NULL ) "
144     if $curuser->access_right('Edit global advertising sources')
145     or defined($_[0]) && $_[0];
146
147   $sql;
148
149 }
150
151 =item all_part_referral [ INCLUDE_GLOBAL_BOOL ]
152
153 Returns all part_referral records allowed by the current users's agent ACLs
154 (and "Edit global advertising sources" right).
155
156 Pass a true value to include global advertising sources (for example, when
157 simply using rather than editing advertising sources).
158
159 =cut
160
161 sub all_part_referral {
162   my $self = shift;
163   my $global = @_ ? shift : '';
164   my $disabled = @_ ? shift : '';
165
166   my $hashref = $disabled ? {} : { 'disabled' => '' };
167   my $and = $disabled ? ' WHERE ' : ' AND ';
168
169   qsearch({
170     'table'     => 'part_referral',
171     'hashref'   => $hashref,
172     'extra_sql' => $and. $self->acl_agentnum_sql($global). ' ORDER BY refnum ',
173   });
174
175 }
176
177 =item num_part_referral [ INCLUDE_GLOBAL_BOOL ]
178
179 Returns the number of part_referral records allowed by the current users's
180 agent ACLs (and "Edit global advertising sources" right).
181
182 =cut
183
184 sub num_part_referral {
185   my $self = shift;
186
187   my $sth = dbh->prepare(
188     'SELECT COUNT(*) FROM part_referral WHERE '. $self->acl_agentnum_sql(@_)
189   ) or die dbh->errstr;
190   $sth->execute() or die $sth->errstr;
191   $sth->fetchrow_arrayref->[0];
192 }
193
194 =back
195
196 =head1 BUGS
197
198 The delete method is unimplemented.
199
200 `Advertising source'.  Yes, it's a sucky name.  The only other ones I could
201 come up with were "Marketing channel" and "Heard Abouts" and those are
202 definately both worse.
203
204 =head1 SEE ALSO
205
206 L<FS::Record>, L<FS::cust_main>, schema.html from the base documentation.
207
208 =cut
209
210 1;
211