1 package FS::part_referral;
4 use vars qw( @ISA $setup_hack );
5 use FS::Record qw( qsearch qsearchs dbh );
8 @ISA = qw( FS::Record );
13 FS::part_referral - Object methods for part_referral objects
17 use FS::part_referral;
19 $record = new FS::part_referral \%hash
20 $record = new FS::part_referral { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
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:
39 =item refnum - primary key (assigned automatically for new referrals)
41 =item referral - Text name of this advertising source
43 =item disabled - Disabled flag, empty or 'Y'
45 =item agentnum - Optional agentnum (see L<FS::agent>)
47 =item title - an optional external string that identifies this
48 referral source, such as an advertising campaign code.
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.
63 Creates a new advertising source. To add the referral to the database, see
68 sub table { 'part_referral'; }
72 Adds this advertising source to the database. If there is an error, returns
73 the error, otherwise returns false.
77 Currently unimplemented.
83 return "Can't (yet?) delete part_referral records";
84 #need to make sure no customers have this referral!
87 =item replace OLD_RECORD
89 Replaces OLD_RECORD with this one in the database. If there is an error,
90 returns the error, otherwise returns false.
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
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')
109 ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
110 : $self->ut_agentnum_acl('agentnum', 'Edit global advertising sources')
113 return $error if $error;
120 Returns the associated agent for this referral, if any, as an FS::agent object.
126 qsearchs('agent', { 'agentnum' => $self->agentnum } );
135 =item acl_agentnum_sql [ INCLUDE_GLOBAL_BOOL ]
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).
140 Pass a true value to include global advertising sources (for example, when
141 simply using rather than editing advertising sources).
145 sub acl_agentnum_sql {
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];
158 =item all_part_referral [ INCLUDE_GLOBAL_BOOL ]
160 Returns all part_referral records allowed by the current users's agent ACLs
161 (and "Edit global advertising sources" right).
163 Pass a true value to include global advertising sources (for example, when
164 simply using rather than editing advertising sources).
168 sub all_part_referral {
170 my $global = @_ ? shift : '';
171 my $disabled = @_ ? shift : '';
173 my $hashref = $disabled ? {} : { 'disabled' => '' };
174 my $and = $disabled ? ' WHERE ' : ' AND ';
177 'table' => 'part_referral',
178 'hashref' => $hashref,
179 'extra_sql' => $and. $self->acl_agentnum_sql($global). ' ORDER BY refnum ',
184 =item num_part_referral [ INCLUDE_GLOBAL_BOOL ]
186 Returns the number of part_referral records allowed by the current users's
187 agent ACLs (and "Edit global advertising sources" right).
191 sub num_part_referral {
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];
205 The delete method is unimplemented.
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.
213 L<FS::Record>, L<FS::cust_main>, schema.html from the base documentation.