0983ff6e2ec686f03afa14c7ea8244f7b96b8ff3
[freeside.git] / FS / FS / pkg_referral.pm
1 package FS::pkg_referral;
2 use base qw( FS::Record );
3
4 use strict;
5 use FS::Record qw( dbh );
6
7 =head1 NAME
8
9 FS::pkg_referral - Object methods for pkg_referral records
10
11 =head1 SYNOPSIS
12
13   use FS::pkg_referral;
14
15   $record = new FS::pkg_referral \%hash;
16   $record = new FS::pkg_referral { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::pkg_referral object represents the association of an advertising source
29 with a specific customer package (purchase).  FS::pkg_referral inherits from
30 FS::Record.  The following fields are currently supported:
31
32 =over 4
33
34 =item pkgrefnum - primary key
35
36 =item pkgnum - Customer package.  See L<FS::cust_pkg>
37
38 =item refnum - Advertising source.  See L<FS::part_referral>
39
40 =back
41
42 =head1 METHODS
43
44 =over 4
45
46 =item new HASHREF
47
48 Creates a new record.  To add the record to the database, see L<"insert">.
49
50 Note that this stores the hash reference, not a distinct copy of the hash it
51 points to.  You can ask the object for a copy with the I<hash> method.
52
53 =cut
54
55 # the new method can be inherited from FS::Record, if a table method is defined
56
57 sub table { 'pkg_referral'; }
58
59 =item insert
60
61 Adds this record to the database.  If there is an error, returns the error,
62 otherwise returns false.
63
64 =cut
65
66 # the insert method can be inherited from FS::Record
67
68 =item delete
69
70 Delete this record from the database.
71
72 =cut
73
74 # the delete method can be inherited from FS::Record
75
76 =item replace OLD_RECORD
77
78 Replaces the OLD_RECORD with this one in the database.  If there is an error,
79 returns the error, otherwise returns false.
80
81 =cut
82
83 # the replace method can be inherited from FS::Record
84
85 =item check
86
87 Checks all fields to make sure this is a valid record.  If there is
88 an error, returns the error, otherwise returns false.  Called by the insert
89 and replace methods.
90
91 =cut
92
93 # the check method should currently be supplied - FS::Record contains some
94 # data checking routines
95
96 sub check {
97   my $self = shift;
98
99   my $error = 
100     $self->ut_numbern('pkgrefnum')
101     || $self->ut_foreign_key('pkgnum', 'cust_pkg',      'pkgnum' )
102     || $self->ut_foreign_key('refnum', 'part_referral', 'refnum' )
103   ;
104   return $error if $error;
105
106   $self->SUPER::check;
107 }
108
109 sub _upgrade_schema {
110   my ($class, %opts) = @_;
111
112   my $sql = '
113     DELETE FROM pkg_referral WHERE NOT EXISTS
114       ( SELECT 1 FROM cust_pkg WHERE cust_pkg.pkgnum = pkg_referral.pkgnum )
115   ';
116
117   my $sth = dbh->prepare($sql) or die dbh->errstr;
118   $sth->execute or die $sth->errstr;
119   '';
120 }
121
122 =back
123
124 =head1 BUGS
125
126 Multiple pkg_referral records for a single package (configured off by default)
127 still seems weird.
128
129 =head1 SEE ALSO
130
131 L<FS::part_referral>, L<FS::cust_pkg>, L<FS::Record>, schema.html from the
132 base documentation.
133
134 =cut
135
136 1;
137