1 package FS::prepay_credit;
5 use FS::Record qw(qsearchs dbh);
12 FS::prepay_credit - Object methods for prepay_credit records
16 use FS::prepay_credit;
18 $record = new FS::prepay_credit \%hash;
19 $record = new FS::prepay_credit {
20 'identifier' => '4198123455512121'
24 $record = new FS::prepay_credit {
25 'identifier' => '4198123455512121'
30 $error = $record->insert;
32 $error = $new_record->replace($old_record);
34 $error = $record->delete;
36 $error = $record->check;
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:
46 =item field - description
48 =item identifier - identifier entered by the user to receive the credit
50 =item amount - amount of the credit
52 =item seconds - time amount of credit (see L<FS::svc_acct/seconds>)
54 =item agentnum - optional agent (see L<FS::agent>) for this prepaid card
64 Creates a new pre-paid credit. To add the example to the database, see
67 Note that this stores the hash reference, not a distinct copy of the hash it
68 points to. You can ask the object for a copy with the I<hash> method.
72 sub table { 'prepay_credit'; }
76 Adds this record to the database. If there is an error, returns the error,
77 otherwise returns false.
83 Delete this record from the database.
87 =item replace OLD_RECORD
89 Replaces the OLD_RECORD with this one in the database. If there is an error,
90 returns the error, otherwise returns false.
96 Checks all fields to make sure this is a valid pre-paid credit. If there is
97 an error, returns the error, otherwise returns false. Called by the insert
105 my $identifier = $self->identifier;
106 $identifier =~ s/\W//g; #anything else would just confuse things
107 $self->identifier($identifier);
109 $self->ut_numbern('prepaynum')
110 || $self->ut_alpha('identifier')
111 || $self->ut_money('amount')
112 || $self->ut_numbern('seconds')
113 || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
114 || $self->SUPER::check
121 Returns the agent (see L<FS::agent>) for this prepaid card, if any.
127 qsearchs('agent', { 'agentnum' => $self->agentnum } );
136 =item generate NUM TYPE HASHREF
138 Generates the specified number of prepaid cards. Returns an array reference of
139 the newly generated card identifiers, or a scalar error message.
143 #false laziness w/agent::generate_reg_codes
145 my( $num, $type, $hashref ) = @_;
148 push @codeset, ( 'A'..'Z' ) if $type =~ /alpha/;
149 push @codeset, ( '1'..'9' ) if $type =~ /numeric/;
151 local $SIG{HUP} = 'IGNORE';
152 local $SIG{INT} = 'IGNORE';
153 local $SIG{QUIT} = 'IGNORE';
154 local $SIG{TERM} = 'IGNORE';
155 local $SIG{TSTP} = 'IGNORE';
156 local $SIG{PIPE} = 'IGNORE';
158 my $oldAutoCommit = $FS::UID::AutoCommit;
159 local $FS::UID::AutoCommit = 0;
164 my $prepay_credit = new FS::prepay_credit {
165 'identifier' => join('', map($codeset[int(rand $#codeset)], (0..7) ) ),
168 my $error = $prepay_credit->check || $prepay_credit->insert;
170 $dbh->rollback if $oldAutoCommit;
171 return "(inserting prepay_credit) $error";
173 push @cards, $prepay_credit->identifier;
176 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
186 L<FS::svc_acct>, L<FS::Record>, schema.html from the base documentation.