1 package FS::prepay_credit;
4 use vars qw( @ISA $DEBUG $me );
5 use FS::Record qw(qsearchs dbh);
11 $me = '[FS::prepay_credit]';
15 FS::prepay_credit - Object methods for prepay_credit records
19 use FS::prepay_credit;
21 $record = new FS::prepay_credit \%hash;
22 $record = new FS::prepay_credit {
23 'identifier' => '4198123455512121'
27 $record = new FS::prepay_credit {
28 'identifier' => '4198123455512121'
33 $error = $record->insert;
35 $error = $new_record->replace($old_record);
37 $error = $record->delete;
39 $error = $record->check;
43 An FS::prepay_credit object represents a pre-paid card. FS::prepay_credit
44 inherits from FS::Record. The following
45 fields are currently supported:
49 =item field - description
51 =item identifier - identifier entered by the user to receive the credit
53 =item amount - amount of the credit
55 =item seconds - time amount of credit (see L<FS::svc_acct/seconds>)
57 =item agentnum - optional agent (see L<FS::agent>) for this prepaid card
67 Creates a new pre-paid credit. To add the pre-paid credit to the database, see
70 Note that this stores the hash reference, not a distinct copy of the hash it
71 points to. You can ask the object for a copy with the I<hash> method.
75 sub table { 'prepay_credit'; }
79 Adds this record to the database. If there is an error, returns the error,
80 otherwise returns false.
86 Delete this record from the database.
90 =item replace OLD_RECORD
92 Replaces the OLD_RECORD with this one in the database. If there is an error,
93 returns the error, otherwise returns false.
99 Checks all fields to make sure this is a valid pre-paid credit. If there is
100 an error, returns the error, otherwise returns false. Called by the insert
108 my $identifier = $self->identifier;
109 $identifier =~ s/\W//g; #anything else would just confuse things
110 $self->identifier($identifier);
112 $self->ut_numbern('prepaynum')
113 || $self->ut_alpha('identifier')
114 || $self->ut_money('amount')
115 || $self->ut_numbern('seconds')
116 || $self->ut_numbern('upbytes')
117 || $self->ut_numbern('downbytes')
118 || $self->ut_numbern('totalbytes')
119 || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
120 || $self->SUPER::check
127 Returns the agent (see L<FS::agent>) for this prepaid card, if any.
133 qsearchs('agent', { 'agentnum' => $self->agentnum } );
142 =item generate NUM TYPE LENGTH HASHREF
144 Generates the specified number of prepaid cards. Returns an array reference of
145 the newly generated card identifiers, or a scalar error message.
149 #false laziness w/agent::generate_reg_codes
151 my( $num, $type, $length, $hashref ) = @_;
154 push @codeset, ( 'A'..'Z' ) if $type =~ /alpha/;
155 push @codeset, ( '1'..'9' ) if $type =~ /numeric/;
158 local $SIG{HUP} = 'IGNORE';
159 local $SIG{INT} = 'IGNORE';
160 local $SIG{QUIT} = 'IGNORE';
161 local $SIG{TERM} = 'IGNORE';
162 local $SIG{TSTP} = 'IGNORE';
163 local $SIG{PIPE} = 'IGNORE';
165 my $oldAutoCommit = $FS::UID::AutoCommit;
166 local $FS::UID::AutoCommit = 0;
169 my $condup = 0; #don't retry forever
174 my $identifier = join('', map($codeset[int(rand $#codeset)], (1..$length) ) );
176 if ( qsearchs('prepay_credit',{identifier=>$identifier}) ) {
177 if ( $condup++ < 54 ) {
178 warn "$me generate: duplicate identifier $identifier; retrying\n"
182 warn "$me generate: giving up after 54 tries"
188 my $prepay_credit = new FS::prepay_credit {
189 'identifier' => $identifier,
192 my $error = $prepay_credit->check || $prepay_credit->insert;
194 $dbh->rollback if $oldAutoCommit;
195 return "(inserting prepay_credit) $error";
197 push @cards, $prepay_credit->identifier;
200 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
210 L<FS::svc_acct>, L<FS::Record>, schema.html from the base documentation.