enable CardFortress in test database, #71513
[freeside.git] / FS / FS / reg_code.pm
1 package FS::reg_code;
2 use base qw(FS::Record);
3
4 use strict;
5 use FS::Record qw( dbh ); # qsearch qsearchs dbh );
6 use FS::reg_code_pkg;
7
8 =head1 NAME
9
10 FS::reg_code - One-time registration codes
11
12 =head1 SYNOPSIS
13
14   use FS::reg_code;
15
16   $record = new FS::reg_code \%hash;
17   $record = new FS::reg_code { 'column' => 'value' };
18
19   $error = $record->insert;
20
21   $error = $new_record->replace($old_record);
22
23   $error = $record->delete;
24
25   $error = $record->check;
26
27 =head1 DESCRIPTION
28
29 An FS::reg_code object is a one-time registration code.  FS::reg_code inherits
30 from FS::Record.  The following fields are currently supported:
31
32 =over 4
33
34 =item codenum - primary key
35
36 =item code - registration code string
37
38 =item agentnum - Agent (see L<FS::agent>)
39
40 =back
41
42 =head1 METHODS
43
44 =over 4
45
46 =item new HASHREF
47
48 Creates a new registration code.  To add the code to the database, see
49 L<"insert">.
50
51 Note that this stores the hash reference, not a distinct copy of the hash it
52 points to.  You can ask the object for a copy with the I<hash> method.
53
54 =cut
55
56 # the new method can be inherited from FS::Record, if a table method is defined
57
58 sub table { 'reg_code'; }
59
60 =item insert [ PKGPART_ARRAYREF ] 
61
62 Adds this record to the database.  If an arrayref of pkgparts
63 (see L<FS::part_pkg>) is specified, the appropriate reg_code_pkg records
64 (see L<FS::reg_code_pkg>) will be inserted.
65
66 If there is an error, returns the error, otherwise returns false.
67
68 =cut
69
70 sub insert {
71   my $self = shift;
72
73   local $SIG{HUP} = 'IGNORE';
74   local $SIG{INT} = 'IGNORE';
75   local $SIG{QUIT} = 'IGNORE';
76   local $SIG{TERM} = 'IGNORE';
77   local $SIG{TSTP} = 'IGNORE';
78   local $SIG{PIPE} = 'IGNORE';
79
80   my $oldAutoCommit = $FS::UID::AutoCommit;
81   local $FS::UID::AutoCommit = 0;
82   my $dbh = dbh;
83
84   my $error = $self->SUPER::insert;
85   if ( $error ) {
86     $dbh->rollback if $oldAutoCommit;
87     return $error;
88   }
89
90   if ( @_ ) {
91     my $pkgparts = shift;
92     foreach my $pkgpart ( @$pkgparts ) {
93       my $reg_code_pkg = new FS::reg_code_pkg ( {
94         'codenum' => $self->codenum,
95         'pkgpart' => $pkgpart,
96       } );
97       $error = $reg_code_pkg->insert;
98       if ( $error ) {
99         $dbh->rollback if $oldAutoCommit;
100         return $error;
101       }
102     }
103   }
104
105   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
106   '';
107
108 }
109
110 =item delete
111
112 Delete this record (and all associated reg_code_pkg records) from the database.
113
114 =cut
115
116 sub delete {
117   my $self = shift;
118
119   local $SIG{HUP} = 'IGNORE';
120   local $SIG{INT} = 'IGNORE';
121   local $SIG{QUIT} = 'IGNORE';
122   local $SIG{TERM} = 'IGNORE';
123   local $SIG{TSTP} = 'IGNORE';
124   local $SIG{PIPE} = 'IGNORE';
125
126   my $oldAutoCommit = $FS::UID::AutoCommit;
127   local $FS::UID::AutoCommit = 0;
128   my $dbh = dbh;
129
130   foreach my $reg_code_pkg ( $self->reg_code_pkg ) {
131     my $error = $reg_code_pkg->delete;
132     if ( $error ) {
133       $dbh->rollback if $oldAutoCommit;
134       return $error;
135     }
136   }
137
138   my $error = $self->SUPER::delete;
139   if ( $error ) {
140     $dbh->rollback if $oldAutoCommit;
141     return $error;
142   }
143
144   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
145   '';
146
147 }
148
149 =item replace OLD_RECORD
150
151 Replaces the OLD_RECORD with this one in the database.  If there is an error,
152 returns the error, otherwise returns false.
153
154 =cut
155
156 # the replace method can be inherited from FS::Record
157
158 =item check
159
160 Checks all fields to make sure this is a valid registration code.  If there is
161 an error, returns the error, otherwise returns false.  Called by the insert
162 and replace methods.
163
164 =cut
165
166 # the check method should currently be supplied - FS::Record contains some
167 # data checking routines
168
169 sub check {
170   my $self = shift;
171
172   my $error = 
173     $self->ut_numbern('codenum')
174     || $self->ut_alpha('code')
175     || $self->ut_foreign_key('agentnum', 'agent', 'agentnum')
176   ;
177   return $error if $error;
178
179   $self->SUPER::check;
180 }
181
182 =item part_pkg
183
184 Returns all package definitions (see L<FS::part_pkg> for this registration
185 code.
186
187 =cut
188
189 sub part_pkg {
190   my $self = shift;
191   map { $_->part_pkg } $self->reg_code_pkg;
192 }
193
194 =item reg_code_pkg
195
196 Returns all FS::reg_code_pkg records for this registration code.
197
198 =back
199
200 =head1 BUGS
201
202 Feeping creaturitis.
203
204 =head1 SEE ALSO
205
206 L<FS::reg_code_pkg>, L<FS::Record>, schema.html from the base documentation.
207
208 =cut
209
210 1;
211
212