package FS::prepay_credit;
+use base qw(FS::Record);
use strict;
-use vars qw( @ISA );
+use vars qw( $DEBUG $me );
use FS::Record qw(qsearchs dbh);
-use FS::agent;
-@ISA = qw(FS::Record);
+$DEBUG = 0;
+$me = '[FS::prepay_credit]';
=head1 NAME
=item seconds - time amount of credit (see L<FS::svc_acct/seconds>)
+=item agentnum - optional agent (see L<FS::agent>) for this prepaid card
+
=back
=head1 METHODS
=item new HASHREF
-Creates a new pre-paid credit. To add the example to the database, see
+Creates a new pre-paid credit. To add the pre-paid credit to the database, see
L<"insert">.
Note that this stores the hash reference, not a distinct copy of the hash it
|| $self->ut_alpha('identifier')
|| $self->ut_money('amount')
|| $self->ut_numbern('seconds')
+ || $self->ut_numbern('upbytes')
+ || $self->ut_numbern('downbytes')
+ || $self->ut_numbern('totalbytes')
|| $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
|| $self->SUPER::check
;
Returns the agent (see L<FS::agent>) for this prepaid card, if any.
-=cut
-
-sub agent {
- my $self = shift;
- qsearchs('agent', { 'agentnum' => $self->agentnum } );
-}
-
=back
=head1 SUBROUTINES
=over 4
-=item generate NUM TYPE HASHREF
+=item generate NUM TYPE LENGTH HASHREF
Generates the specified number of prepaid cards. Returns an array reference of
the newly generated card identifiers, or a scalar error message.
#false laziness w/agent::generate_reg_codes
sub generate {
- my( $num, $type, $hashref ) = @_;
+ my( $num, $type, $length, $hashref ) = @_;
my @codeset = ();
push @codeset, ( 'A'..'Z' ) if $type =~ /alpha/;
push @codeset, ( '1'..'9' ) if $type =~ /numeric/;
+ $length ||= 8;
local $SIG{HUP} = 'IGNORE';
local $SIG{INT} = 'IGNORE';
local $FS::UID::AutoCommit = 0;
my $dbh = dbh;
+ my $condup = 0; #don't retry forever
+
my @cards = ();
for ( 1 ... $num ) {
+
+ my $identifier = join('', map($codeset[int(rand $#codeset)], (1..$length) ) );
+
+ if ( qsearchs('prepay_credit',{identifier=>$identifier}) ) {
+ if ( $condup++ < 54 ) {
+ warn "$me generate: duplicate identifier $identifier; retrying\n"
+ if $DEBUG;
+ redo;
+ } else {
+ warn "$me generate: giving up after 54 tries"
+ if $DEBUG;
+ }
+ }
+ $condup = 0;
+
my $prepay_credit = new FS::prepay_credit {
- 'identifier' => join('', map($codeset[int(rand $#codeset)], (0..7) ) ),
+ 'identifier' => $identifier,
%$hashref,
};
my $error = $prepay_credit->check || $prepay_credit->insert;