oops, losing notes!
[freeside.git] / FS / FS / prepay_credit.pm
1 package FS::prepay_credit;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw(qsearchs dbh);
6 use FS::agent;
7
8 @ISA = qw(FS::Record);
9
10 =head1 NAME
11
12 FS::prepay_credit - Object methods for prepay_credit records
13
14 =head1 SYNOPSIS
15
16   use FS::prepay_credit;
17
18   $record = new FS::prepay_credit \%hash;
19   $record = new FS::prepay_credit {
20     'identifier' => '4198123455512121'
21     'amount'     => '19.95',
22   };
23
24   $record = new FS::prepay_credit {
25     'identifier' => '4198123455512121'
26     'seconds'    => '7200',
27   };
28
29
30   $error = $record->insert;
31
32   $error = $new_record->replace($old_record);
33
34   $error = $record->delete;
35
36   $error = $record->check;
37
38 =head1 DESCRIPTION
39
40 An FS::prepay_credit object represents a pre-paid card.  FS::prepay_credit
41 inherits from FS::Record.  The following
42 fields are currently supported:
43
44 =over 4
45
46 =item field - description
47
48 =item identifier - identifier entered by the user to receive the credit
49
50 =item amount - amount of the credit
51
52 =item seconds - time amount of credit (see L<FS::svc_acct/seconds>)
53
54 =back
55
56 =head1 METHODS
57
58 =over 4
59
60 =item new HASHREF
61
62 Creates a new pre-paid credit.  To add the example to the database, see
63 L<"insert">.
64
65 Note that this stores the hash reference, not a distinct copy of the hash it
66 points to.  You can ask the object for a copy with the I<hash> method.
67
68 =cut
69
70 sub table { 'prepay_credit'; }
71
72 =item insert
73
74 Adds this record to the database.  If there is an error, returns the error,
75 otherwise returns false.
76
77 =cut
78
79 =item delete
80
81 Delete this record from the database.
82
83 =cut
84
85 =item replace OLD_RECORD
86
87 Replaces the OLD_RECORD with this one in the database.  If there is an error,
88 returns the error, otherwise returns false.
89
90 =cut
91
92 =item check
93
94 Checks all fields to make sure this is a valid pre-paid credit.  If there is
95 an error, returns the error, otherwise returns false.  Called by the insert
96 and replace methods.
97
98 =cut
99
100 sub check {
101   my $self = shift;
102
103   my $identifier = $self->identifier;
104   $identifier =~ s/\W//g; #anything else would just confuse things
105   $self->identifier($identifier);
106
107   $self->ut_numbern('prepaynum')
108   || $self->ut_alpha('identifier')
109   || $self->ut_money('amount')
110   || $self->ut_numbern('seconds')
111   || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
112   || $self->SUPER::check
113   ;
114
115 }
116
117 =item agent
118
119 Returns the agent (see L<FS::agent>) for this prepaid card, if any.
120
121 =cut
122
123 sub agent {
124   my $self = shift;
125   qsearchs('agent', { 'agentnum' => $self->agentnum } );
126 }
127
128 =back
129
130 =head1 SUBROUTINES
131
132 =over 4
133
134 =item generate NUM TYPE HASHREF
135
136 Generates the specified number of prepaid cards.  Returns an array reference of
137 the newly generated card identifiers, or a scalar error message.
138
139 =cut
140
141 #false laziness w/agent::generate_reg_codes
142 sub generate {
143   my( $num, $type, $hashref ) = @_;
144
145   my @codeset = ();
146   push @codeset, ( 'A'..'Z' ) if $type =~ /alpha/;
147   push @codeset, ( '1'..'9' ) if $type =~ /numeric/;
148
149   local $SIG{HUP} = 'IGNORE';
150   local $SIG{INT} = 'IGNORE';
151   local $SIG{QUIT} = 'IGNORE';
152   local $SIG{TERM} = 'IGNORE';
153   local $SIG{TSTP} = 'IGNORE';
154   local $SIG{PIPE} = 'IGNORE';
155
156   my $oldAutoCommit = $FS::UID::AutoCommit;
157   local $FS::UID::AutoCommit = 0;
158   my $dbh = dbh;
159
160   my @cards = ();
161   for ( 1 ... $num ) {
162     my $prepay_credit = new FS::prepay_credit {
163       'identifier' => join('', map($codeset[int(rand $#codeset)], (0..7) ) ),
164       %$hashref,
165     };
166     my $error = $prepay_credit->check || $prepay_credit->insert;
167     if ( $error ) {
168       $dbh->rollback if $oldAutoCommit;
169       return "(inserting prepay_credit) $error";
170     }
171     push @cards, $prepay_credit->identifier;
172   }
173
174   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
175
176   \@cards;
177
178 }
179
180 =head1 BUGS
181
182 =head1 SEE ALSO
183
184 L<FS::svc_acct>, L<FS::Record>, schema.html from the base documentation.
185
186 =cut
187
188 1;
189