add part_referral external ID for API signups, #39776
[freeside.git] / FS / FS / part_referral.pm
1 package FS::part_referral;
2
3 use strict;
4 use vars qw( @ISA $setup_hack );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::agent;
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 =cut
123
124 sub agent {
125   my $self = shift;
126   qsearchs('agent', { 'agentnum' => $self->agentnum } );
127 }
128
129 =back
130
131 =head1 CLASS METHODS
132
133 =over 4
134
135 =item acl_agentnum_sql [ INCLUDE_GLOBAL_BOOL ]
136
137 Returns an SQL fragment for searching for part_referral records allowed by the
138 current users's agent ACLs (and "Edit global advertising sources" right).
139
140 Pass a true value to include global advertising sources (for example, when
141 simply using rather than editing advertising sources).
142
143 =cut
144
145 sub acl_agentnum_sql {
146   my $self = shift;
147
148   my $curuser = $FS::CurrentUser::CurrentUser;
149   my $sql = $curuser->agentnums_sql;
150   $sql = " ( $sql OR agentnum IS NULL ) "
151     if $curuser->access_right('Edit global advertising sources')
152     or defined($_[0]) && $_[0];
153
154   $sql;
155
156 }
157
158 =item all_part_referral [ INCLUDE_GLOBAL_BOOL ]
159
160 Returns all part_referral records allowed by the current users's agent ACLs
161 (and "Edit global advertising sources" right).
162
163 Pass a true value to include global advertising sources (for example, when
164 simply using rather than editing advertising sources).
165
166 =cut
167
168 sub all_part_referral {
169   my $self = shift;
170   my $global = @_ ? shift : '';
171   my $disabled = @_ ? shift : '';
172
173   my $hashref = $disabled ? {} : { 'disabled' => '' };
174   my $and = $disabled ? ' WHERE ' : ' AND ';
175
176   qsearch({
177     'table'     => 'part_referral',
178     'hashref'   => $hashref,
179     'extra_sql' => $and. $self->acl_agentnum_sql($global). ' ORDER BY refnum ',
180   });
181
182 }
183
184 =item num_part_referral [ INCLUDE_GLOBAL_BOOL ]
185
186 Returns the number of part_referral records allowed by the current users's
187 agent ACLs (and "Edit global advertising sources" right).
188
189 =cut
190
191 sub num_part_referral {
192   my $self = shift;
193
194   my $sth = dbh->prepare(
195     'SELECT COUNT(*) FROM part_referral WHERE '. $self->acl_agentnum_sql(@_)
196   ) or die dbh->errstr;
197   $sth->execute() or die $sth->errstr;
198   $sth->fetchrow_arrayref->[0];
199 }
200
201 =back
202
203 =head1 BUGS
204
205 The delete method is unimplemented.
206
207 `Advertising source'.  Yes, it's a sucky name.  The only other ones I could
208 come up with were "Marketing channel" and "Heard Abouts" and those are
209 definately both worse.
210
211 =head1 SEE ALSO
212
213 L<FS::Record>, L<FS::cust_main>, schema.html from the base documentation.
214
215 =cut
216
217 1;
218