fix 'Can't call method "setup" on an undefined value' error when using into rates...
[freeside.git] / FS / FS / prepay_credit.pm
1 package FS::prepay_credit;
2
3 use strict;
4 use vars qw( @ISA $DEBUG $me );
5 use FS::Record qw(qsearchs dbh);
6 use FS::agent;
7
8 @ISA = qw(FS::Record);
9
10 $DEBUG = 0;
11 $me = '[FS::prepay_credit]';
12
13 =head1 NAME
14
15 FS::prepay_credit - Object methods for prepay_credit records
16
17 =head1 SYNOPSIS
18
19   use FS::prepay_credit;
20
21   $record = new FS::prepay_credit \%hash;
22   $record = new FS::prepay_credit {
23     'identifier' => '4198123455512121'
24     'amount'     => '19.95',
25   };
26
27   $record = new FS::prepay_credit {
28     'identifier' => '4198123455512121'
29     'seconds'    => '7200',
30   };
31
32
33   $error = $record->insert;
34
35   $error = $new_record->replace($old_record);
36
37   $error = $record->delete;
38
39   $error = $record->check;
40
41 =head1 DESCRIPTION
42
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:
46
47 =over 4
48
49 =item field - description
50
51 =item identifier - identifier entered by the user to receive the credit
52
53 =item amount - amount of the credit
54
55 =item seconds - time amount of credit (see L<FS::svc_acct/seconds>)
56
57 =item agentnum - optional agent (see L<FS::agent>) for this prepaid card
58
59 =back
60
61 =head1 METHODS
62
63 =over 4
64
65 =item new HASHREF
66
67 Creates a new pre-paid credit.  To add the pre-paid credit to the database, see
68 L<"insert">.
69
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.
72
73 =cut
74
75 sub table { 'prepay_credit'; }
76
77 =item insert
78
79 Adds this record to the database.  If there is an error, returns the error,
80 otherwise returns false.
81
82 =cut
83
84 =item delete
85
86 Delete this record from the database.
87
88 =cut
89
90 =item replace OLD_RECORD
91
92 Replaces the OLD_RECORD with this one in the database.  If there is an error,
93 returns the error, otherwise returns false.
94
95 =cut
96
97 =item check
98
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
101 and replace methods.
102
103 =cut
104
105 sub check {
106   my $self = shift;
107
108   my $identifier = $self->identifier;
109   $identifier =~ s/\W//g; #anything else would just confuse things
110   $self->identifier($identifier);
111
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
121   ;
122
123 }
124
125 =item agent
126
127 Returns the agent (see L<FS::agent>) for this prepaid card, if any.
128
129 =cut
130
131 sub agent {
132   my $self = shift;
133   qsearchs('agent', { 'agentnum' => $self->agentnum } );
134 }
135
136 =back
137
138 =head1 SUBROUTINES
139
140 =over 4
141
142 =item generate NUM TYPE LENGTH HASHREF
143
144 Generates the specified number of prepaid cards.  Returns an array reference of
145 the newly generated card identifiers, or a scalar error message.
146
147 =cut
148
149 #false laziness w/agent::generate_reg_codes
150 sub generate {
151   my( $num, $type, $length, $hashref ) = @_;
152
153   my @codeset = ();
154   push @codeset, ( 'A'..'Z' ) if $type =~ /alpha/;
155   push @codeset, ( '1'..'9' ) if $type =~ /numeric/;
156   $length ||= 8;
157
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';
164
165   my $oldAutoCommit = $FS::UID::AutoCommit;
166   local $FS::UID::AutoCommit = 0;
167   my $dbh = dbh;
168
169   my $condup = 0; #don't retry forever
170   
171   my @cards = ();
172   for ( 1 ... $num ) {
173
174     my $identifier = join('', map($codeset[int(rand $#codeset)], (1..$length) ) );
175
176     if ( qsearchs('prepay_credit',{identifier=>$identifier}) ) {
177       if ( $condup++ < 54 ) {
178         warn "$me generate: duplicate identifier $identifier; retrying\n"
179           if $DEBUG;
180         redo;
181       } else {
182         warn "$me generate: giving up after 54 tries"
183           if $DEBUG;
184       }
185     }
186     $condup = 0;
187
188     my $prepay_credit = new FS::prepay_credit {
189       'identifier' => $identifier,
190       %$hashref,
191     };
192     my $error = $prepay_credit->check || $prepay_credit->insert;
193     if ( $error ) {
194       $dbh->rollback if $oldAutoCommit;
195       return "(inserting prepay_credit) $error";
196     }
197     push @cards, $prepay_credit->identifier;
198   }
199
200   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
201
202   \@cards;
203
204 }
205
206 =head1 BUGS
207
208 =head1 SEE ALSO
209
210 L<FS::svc_acct>, L<FS::Record>, schema.html from the base documentation.
211
212 =cut
213
214 1;
215