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