store credit card type in cust_payby and transaction records, #71291, schema support
[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_transaction_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 sub nohistory_fields { ('payinfo'); }
21
22 $otaker_upgrade_kludge = 0;
23
24 =head1 NAME
25
26 FS::cust_pay_void - Object methods for cust_pay_void objects
27
28 =head1 SYNOPSIS
29
30   use FS::cust_pay_void;
31
32   $record = new FS::cust_pay_void \%hash;
33   $record = new FS::cust_pay_void { 'column' => 'value' };
34
35   $error = $record->insert;
36
37   $error = $new_record->replace($old_record);
38
39   $error = $record->delete;
40
41   $error = $record->check;
42
43 =head1 DESCRIPTION
44
45 An FS::cust_pay_void object represents a voided payment.  The following fields
46 are currently supported:
47
48 =over 4
49
50 =item paynum
51
52 primary key (assigned automatically for new payments)
53
54 =item custnum
55
56 customer (see L<FS::cust_main>)
57
58 =item paid
59
60 Amount of this payment
61
62 =item _date
63
64 specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
65 L<Time::Local> and L<Date::Parse> for conversion functions.
66
67 =item otaker
68
69 order taker (see L<FS::access_user>)
70
71 =item payby
72
73 Payment Type (See L<FS::payinfo_Mixin> for valid values)
74
75 =item payinfo
76
77 card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
78
79 =item cardtype
80
81 Credit card type, if appropriate.
82
83 =item paybatch
84
85 text field for tracking card processing
86
87 =item closed
88
89 books closed flag, empty or `Y'
90
91 =item pkgnum
92
93 Desired pkgnum when using experimental package balances.
94
95 =item void_date
96
97 =item reason
98
99 =back
100
101 =head1 METHODS
102
103 =over 4 
104
105 =item new HASHREF
106
107 Creates a new payment.  To add the payment to the databse, see L<"insert">.
108
109 =cut
110
111 sub table { 'cust_pay_void'; }
112
113 =item insert
114
115 Adds this voided payment to the database.
116
117 =item unvoid 
118
119 "Un-void"s this payment: Deletes the voided payment from the database and adds
120 back a normal payment.
121
122 =cut
123
124 sub unvoid {
125   my $self = shift;
126
127   local $SIG{HUP} = 'IGNORE';
128   local $SIG{INT} = 'IGNORE';
129   local $SIG{QUIT} = 'IGNORE';
130   local $SIG{TERM} = 'IGNORE';
131   local $SIG{TSTP} = 'IGNORE';
132   local $SIG{PIPE} = 'IGNORE';
133
134   my $oldAutoCommit = $FS::UID::AutoCommit;
135   local $FS::UID::AutoCommit = 0;
136   my $dbh = dbh;
137
138   my $cust_pay = new FS::cust_pay ( {
139     map { $_ => $self->get($_) } fields('cust_pay')
140   } );
141   my $error = $cust_pay->insert;
142
143   my $cust_pay_pending =
144     qsearchs('cust_pay_pending', { void_paynum => $self->paynum });
145   if ( $cust_pay_pending ) {
146     $cust_pay_pending->set('paynum', $cust_pay->paynum);
147     $cust_pay_pending->set('void_paynum', '');
148     $error ||= $cust_pay_pending->replace;
149   }
150
151   $error ||= $self->delete;
152   if ( $error ) {
153     $dbh->rollback if $oldAutoCommit;
154     return $error;
155   }
156
157   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
158
159   '';
160
161 }
162
163 =item delete
164
165 Deletes this voided payment.  You probably don't want to use this directly; see
166 the B<unvoid> method to add the original payment back.
167
168 =item replace [ OLD_RECORD ]
169
170 You can, but probably shouldn't modify voided payments...
171
172 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
173 supplied, replaces this record.  If there is an error, returns the error,
174 otherwise returns false.
175
176 =item check
177
178 Checks all fields to make sure this is a valid voided payment.  If there is an
179 error, returns the error, otherwise returns false.  Called by the insert
180 method.
181
182 =cut
183
184 sub check {
185   my $self = shift;
186
187   my $error =
188     $self->ut_numbern('paynum')
189     || $self->ut_numbern('custnum')
190     || $self->ut_money('paid')
191     || $self->ut_number('_date')
192     || $self->ut_textn('paybatch')
193     || $self->ut_enum('closed', [ '', 'Y' ])
194     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
195     || $self->ut_numbern('void_date')
196     || $self->ut_textn('reason')
197     # || $self->payinfo_check #we'd rather void what we have than fail on this
198   ;
199   return $error if $error;
200
201   return "paid must be > 0 " if $self->paid <= 0;
202
203   return "unknown cust_main.custnum: ". $self->custnum
204     unless $self->invnum
205            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
206
207   $self->void_date(time) unless $self->void_date;
208
209   $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
210     unless $self->void_usernum;
211
212   $self->SUPER::check;
213 }
214
215 =item cust_main
216
217 Returns the parent customer object (see L<FS::cust_main>).
218
219 =cut
220
221 sub cust_main {
222   my $self = shift;
223   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
224 }
225
226 =item void_access_user
227
228 Returns the voiding employee object (see L<FS::access_user>).
229
230 =cut
231
232 sub void_access_user {
233   my $self = shift;
234   qsearchs('access_user', { 'usernum' => $self->void_usernum } );
235 }
236
237 # Used by FS::Upgrade to migrate to a new database.
238 sub _upgrade_data {  # class method
239   my ($class, %opts) = @_;
240
241   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 ) ";
242   my $sth = dbh->prepare($sql) or die dbh->errstr;
243
244   foreach my $cust_pay_void (qsearch('cust_pay_void', {'void_usernum' => ''})) {
245     $sth->execute($cust_pay_void->paynum) or die $sth->errstr;
246     my $row = $sth->fetchrow_arrayref;
247     my $usernum = $row ? $row->[0] : '';
248     if ( $usernum ) {
249       $cust_pay_void->void_usernum($usernum);
250       my $error = $cust_pay_void->replace;
251       die $error if $error;
252     } else {
253       warn "cust_pay_void upgrade: can't find access_user record for ". $cust_pay_void->paynum. "\n";
254     }
255   }
256
257   local($otaker_upgrade_kludge) = 1;
258   $class->_upgrade_otaker(%opts);
259
260   #XXX look for the h_cust_pay delete records and when that's a different
261   # usernum, set usernum
262 }
263
264 =back
265
266 =head1 BUGS
267
268 Delete and replace methods.
269
270 =head1 SEE ALSO
271
272 L<FS::cust_pay>, L<FS::Record>, schema.html from the base documentation.
273
274 =cut
275
276 1;
277