eWay self-signup fixes
[freeside.git] / FS / FS / cust_pay_void.pm
1 package FS::cust_pay_void; 
2
3 use strict;
4 use base qw( FS::otaker_Mixin FS::payinfo_Mixin FS::cust_main_Mixin
5              FS::Record );
6 use vars qw( @encrypted_fields $otaker_upgrade_kludge );
7 use Business::CreditCard;
8 use FS::UID qw(getotaker);
9 use FS::Record qw(qsearch qsearchs dbh fields);
10 use FS::CurrentUser;
11 use FS::access_user;
12 use FS::cust_pay;
13 #use FS::cust_bill;
14 #use FS::cust_bill_pay;
15 #use FS::cust_pay_refund;
16 #use FS::cust_main;
17 use FS::cust_pkg;
18
19 @encrypted_fields = ('payinfo');
20 $otaker_upgrade_kludge = 0;
21
22 =head1 NAME
23
24 FS::cust_pay_void - Object methods for cust_pay_void objects
25
26 =head1 SYNOPSIS
27
28   use FS::cust_pay_void;
29
30   $record = new FS::cust_pay_void \%hash;
31   $record = new FS::cust_pay_void { 'column' => 'value' };
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::cust_pay_void object represents a voided payment.  The following fields
44 are currently supported:
45
46 =over 4
47
48 =item paynum
49
50 primary key (assigned automatically for new payments)
51
52 =item custnum
53
54 customer (see L<FS::cust_main>)
55
56 =item paid
57
58 Amount of this payment
59
60 =item _date
61
62 specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
63 L<Time::Local> and L<Date::Parse> for conversion functions.
64
65 =item otaker
66
67 order taker (see L<FS::access_user>)
68
69 =item payby
70
71 `CARD' (credit cards), `CHEK' (electronic check/ACH),
72 `LECB' (phone bill billing), `BILL' (billing), `CASH' (cash),
73 `WEST' (Western Union), `MCRD' (Manual credit card), or `COMP' (free)
74
75 =item payinfo
76
77 card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
78
79 =item paybatch
80
81 text field for tracking card processing
82
83 =item closed
84
85 books closed flag, empty or `Y'
86
87 =item pkgnum
88
89 Desired pkgnum when using experimental package balances.
90
91 =item void_date
92
93 =item reason
94
95 =back
96
97 =head1 METHODS
98
99 =over 4 
100
101 =item new HASHREF
102
103 Creates a new payment.  To add the payment to the databse, see L<"insert">.
104
105 =cut
106
107 sub table { 'cust_pay_void'; }
108
109 =item insert
110
111 Adds this voided payment to the database.
112
113 =item unvoid 
114
115 "Un-void"s this payment: Deletes the voided payment from the database and adds
116 back a normal payment.
117
118 =cut
119
120 sub unvoid {
121   my $self = shift;
122
123   local $SIG{HUP} = 'IGNORE';
124   local $SIG{INT} = 'IGNORE';
125   local $SIG{QUIT} = 'IGNORE';
126   local $SIG{TERM} = 'IGNORE';
127   local $SIG{TSTP} = 'IGNORE';
128   local $SIG{PIPE} = 'IGNORE';
129
130   my $oldAutoCommit = $FS::UID::AutoCommit;
131   local $FS::UID::AutoCommit = 0;
132   my $dbh = dbh;
133
134   my $cust_pay = new FS::cust_pay ( {
135     map { $_ => $self->get($_) } fields('cust_pay')
136   } );
137   my $error = $cust_pay->insert;
138   if ( $error ) {
139     $dbh->rollback if $oldAutoCommit;
140     return $error;
141   }
142
143   $error = $self->delete;
144   if ( $error ) {
145     $dbh->rollback if $oldAutoCommit;
146     return $error;
147   }
148
149   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
150
151   '';
152
153 }
154
155 =item delete
156
157 Deletes this voided payment.  You probably don't want to use this directly; see
158 the B<unvoid> method to add the original payment back.
159
160 =item replace [ OLD_RECORD ]
161
162 You can, but probably shouldn't modify voided payments...
163
164 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
165 supplied, replaces this record.  If there is an error, returns the error,
166 otherwise returns false.
167
168 =item check
169
170 Checks all fields to make sure this is a valid voided payment.  If there is an
171 error, returns the error, otherwise returns false.  Called by the insert
172 method.
173
174 =cut
175
176 sub check {
177   my $self = shift;
178
179   my $error =
180     $self->ut_numbern('paynum')
181     || $self->ut_numbern('custnum')
182     || $self->ut_money('paid')
183     || $self->ut_number('_date')
184     || $self->ut_textn('paybatch')
185     || $self->ut_enum('closed', [ '', 'Y' ])
186     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
187     || $self->ut_numbern('void_date')
188     || $self->ut_textn('reason')
189   ;
190   return $error if $error;
191
192   return "paid must be > 0 " if $self->paid <= 0;
193
194   return "unknown cust_main.custnum: ". $self->custnum
195     unless $self->invnum
196            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
197
198   $self->void_date(time) unless $self->void_date;
199
200   $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP|PREP|CASH|WEST|MCRD)$/
201     or return "Illegal payby";
202   $self->payby($1);
203
204   #false laziness with cust_refund::check
205   if ( $self->payby eq 'CARD' ) {
206     my $payinfo = $self->payinfo;
207     $payinfo =~ s/\D//g;
208     $self->payinfo($payinfo);
209     if ( $self->payinfo ) {
210       $self->payinfo =~ /^(\d{13,16})$/
211         or return "Illegal (mistyped?) credit card number (payinfo)";
212       $self->payinfo($1);
213       validate($self->payinfo) or return "Illegal credit card number";
214       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
215     } else {
216       $self->payinfo('N/A');
217     }
218
219   } else {
220     $error = $self->ut_textn('payinfo');
221     return $error if $error;
222   }
223
224   $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
225     unless $self->void_usernum;
226
227   $self->SUPER::check;
228 }
229
230 =item cust_main
231
232 Returns the parent customer object (see L<FS::cust_main>).
233
234 =cut
235
236 sub cust_main {
237   my $self = shift;
238   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
239 }
240
241 =item void_access_user
242
243 Returns the voiding employee object (see L<FS::access_user>).
244
245 =cut
246
247 sub void_access_user {
248   my $self = shift;
249   qsearchs('access_user', { 'usernum' => $self->void_usernum } );
250 }
251
252 # Used by FS::Upgrade to migrate to a new database.
253 sub _upgrade_data {  # class method
254   my ($class, %opts) = @_;
255
256   my $sql = "SELECT usernum FROM access_user WHERE username = ( SELECT history_user FROM h_cust_pay_void WHERE paynum = ? AND history_action = 'insert' ORDER BY history_date LIMIT 1 ) ";
257   my $sth = dbh->prepare($sql) or die dbh->errstr;
258
259   foreach my $cust_pay_void (qsearch('cust_pay_void', {'void_usernum' => ''})) {
260     $sth->execute($cust_pay_void->paynum) or die $sth->errstr;
261     my $usernum = $sth->fetchrow_arrayref->[0] or next;
262     if ( $usernum ) {
263       $cust_pay_void->void_usernum($usernum);
264       my $error = $cust_pay_void->replace;
265       die $error if $error;
266     } else {
267       warn "cust_pay_void upgrade: can't find access_user record for ". $cust_pay_void->paynum. "\n";
268     }
269   }
270
271   local($otaker_upgrade_kludge) = 1;
272   $class->_upgrade_otaker(%opts);
273
274   #XXX look for the h_cust_pay delete records and when that's a different
275   # usernum, set usernum
276 }
277
278 =back
279
280 =head1 BUGS
281
282 Delete and replace methods.
283
284 =head1 SEE ALSO
285
286 L<FS::cust_pay>, L<FS::Record>, schema.html from the base documentation.
287
288 =cut
289
290 1;
291