1 package FS::prepay_credit;
2 use base qw(FS::Record);
5 use vars qw( $DEBUG $me );
6 use FS::Record qw(qsearchs dbh);
9 $me = '[FS::prepay_credit]';
13 FS::prepay_credit - Object methods for prepay_credit records
17 use FS::prepay_credit;
19 $record = new FS::prepay_credit \%hash;
20 $record = new FS::prepay_credit {
21 'identifier' => '4198123455512121'
25 $record = new FS::prepay_credit {
26 'identifier' => '4198123455512121'
31 $error = $record->insert;
33 $error = $new_record->replace($old_record);
35 $error = $record->delete;
37 $error = $record->check;
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:
47 =item field - description
49 =item identifier - identifier entered by the user to receive the credit
51 =item amount - amount of the credit
53 =item seconds - time amount of credit (see L<FS::svc_acct/seconds>)
55 =item agentnum - optional agent (see L<FS::agent>) for this prepaid card
65 Creates a new pre-paid credit. To add the pre-paid credit to the database, see
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.
73 sub table { 'prepay_credit'; }
77 Adds this record to the database. If there is an error, returns the error,
78 otherwise returns false.
84 Delete this record from the database.
88 =item replace OLD_RECORD
90 Replaces the OLD_RECORD with this one in the database. If there is an error,
91 returns the error, otherwise returns false.
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
106 my $identifier = $self->identifier;
107 $identifier =~ s/\W//g; #anything else would just confuse things
108 $self->identifier($identifier);
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
125 Returns the agent (see L<FS::agent>) for this prepaid card, if any.
133 =item generate NUM TYPE LENGTH HASHREF
135 Generates the specified number of prepaid cards. Returns an array reference of
136 the newly generated card identifiers, or a scalar error message.
140 #false laziness w/agent::generate_reg_codes
142 my( $num, $type, $length, $hashref ) = @_;
145 push @codeset, ( 'A'..'Z' ) if $type =~ /alpha/;
146 push @codeset, ( '1'..'9' ) if $type =~ /numeric/;
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';
156 my $oldAutoCommit = $FS::UID::AutoCommit;
157 local $FS::UID::AutoCommit = 0;
160 my $condup = 0; #don't retry forever
165 my $identifier = join('', map($codeset[int(rand $#codeset)], (1..$length) ) );
167 if ( qsearchs('prepay_credit',{identifier=>$identifier}) ) {
168 if ( $condup++ < 54 ) {
169 warn "$me generate: duplicate identifier $identifier; retrying\n"
173 warn "$me generate: giving up after 54 tries"
179 my $prepay_credit = new FS::prepay_credit {
180 'identifier' => $identifier,
183 my $error = $prepay_credit->check || $prepay_credit->insert;
185 $dbh->rollback if $oldAutoCommit;
186 return "(inserting prepay_credit) $error";
188 push @cards, $prepay_credit->identifier;
191 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
201 L<FS::svc_acct>, L<FS::Record>, schema.html from the base documentation.