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