import torrus 1.0.9
[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 =back
48
49 =head1 NOTE
50
51 These were called B<referrals> before version 1.4.0 - the name was changed
52 so as not to be confused with the new customer-to-customer referrals.
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 Creates a new advertising source.  To add the referral to the database, see
61 L<"insert">.
62
63 =cut
64
65 sub table { 'part_referral'; }
66
67 =item insert
68
69 Adds this advertising source to the database.  If there is an error, returns
70 the error, otherwise returns false.
71
72 =item delete
73
74 Currently unimplemented.
75
76 =cut
77
78 sub delete {
79   my $self = shift;
80   return "Can't (yet?) delete part_referral records";
81   #need to make sure no customers have this referral!
82 }
83
84 =item replace OLD_RECORD
85
86 Replaces OLD_RECORD with this one in the database.  If there is an error,
87 returns the error, otherwise returns false.
88
89 =item check
90
91 Checks all fields to make sure this is a valid advertising source.  If there is
92 an error, returns the error, otherwise returns false.  Called by the insert and
93 replace methods.
94
95 =cut
96
97 sub check {
98   my $self = shift;
99
100   my $error = $self->ut_numbern('refnum')
101     || $self->ut_text('referral')
102     || $self->ut_enum('disabled', [ '', 'Y' ] )
103     #|| $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
104     || ( $setup_hack
105            ? $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum' )
106            : $self->ut_agentnum_acl('agentnum', 'Edit global advertising sources')
107        )
108   ;
109   return $error if $error;
110
111   $self->SUPER::check;
112 }
113
114 =item agent 
115
116 Returns the associated agent for this referral, if any, as an FS::agent object.
117
118 =cut
119
120 sub agent {
121   my $self = shift;
122   qsearchs('agent', { 'agentnum' => $self->agentnum } );
123 }
124
125 =back
126
127 =head1 CLASS METHODS
128
129 =over 4
130
131 =item acl_agentnum_sql [ INCLUDE_GLOBAL_BOOL ]
132
133 Returns an SQL fragment for searching for part_referral records allowed by the
134 current users's agent ACLs (and "Edit global advertising sources" right).
135
136 Pass a true value to include global advertising sources (for example, when
137 simply using rather than editing advertising sources).
138
139 =cut
140
141 sub acl_agentnum_sql {
142   my $self = shift;
143
144   my $curuser = $FS::CurrentUser::CurrentUser;
145   my $sql = $curuser->agentnums_sql;
146   $sql = " ( $sql OR agentnum IS NULL ) "
147     if $curuser->access_right('Edit global advertising sources')
148     or defined($_[0]) && $_[0];
149
150   $sql;
151
152 }
153
154 =item all_part_referral [ INCLUDE_GLOBAL_BOOL ]
155
156 Returns all part_referral records allowed by the current users's agent ACLs
157 (and "Edit global advertising sources" right).
158
159 Pass a true value to include global advertising sources (for example, when
160 simply using rather than editing advertising sources).
161
162 =cut
163
164 sub all_part_referral {
165   my $self = shift;
166
167   qsearch({
168     'table'     => 'part_referral',
169     'extra_sql' => ' WHERE '. $self->acl_agentnum_sql(@_). ' ORDER BY refnum ',
170   });
171
172 }
173
174 =item num_part_referral [ INCLUDE_GLOBAL_BOOL ]
175
176 Returns the number of part_referral records allowed by the current users's
177 agent ACLs (and "Edit global advertising sources" right).
178
179 =cut
180
181 sub num_part_referral {
182   my $self = shift;
183
184   my $sth = dbh->prepare(
185     'SELECT COUNT(*) FROM part_referral WHERE '. $self->acl_agentnum_sql(@_)
186   ) or die dbh->errstr;
187   $sth->execute() or die $sth->errstr;
188   $sth->fetchrow_arrayref->[0];
189 }
190
191 =back
192
193 =head1 BUGS
194
195 The delete method is unimplemented.
196
197 `Advertising source'.  Yes, it's a sucky name.  The only other ones I could
198 come up with were "Marketing channel" and "Heard Abouts" and those are
199 definately both worse.
200
201 =head1 SEE ALSO
202
203 L<FS::Record>, L<FS::cust_main>, schema.html from the base documentation.
204
205 =cut
206
207 1;
208