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