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