8d37a58b5150b41030645853c464b2817d8546c9
[freeside.git] / FS / FS / cust_pay_void.pm
1 package FS::cust_pay_void; 
2 use base qw( FS::otaker_Mixin FS::payinfo_transaction_Mixin FS::cust_main_Mixin
3              FS::reason_Mixin FS::Record );
4
5 use strict;
6 use vars qw( @encrypted_fields $otaker_upgrade_kludge );
7 use Business::CreditCard;
8 use FS::Record qw(qsearch qsearchs dbh fields);
9 use FS::CurrentUser;
10 use FS::access_user;
11 use FS::cust_pay;
12 #use FS::cust_bill;
13 #use FS::cust_bill_pay;
14 #use FS::cust_pay_refund;
15 use FS::cust_pkg;
16
17 @encrypted_fields = ('payinfo');
18 sub nohistory_fields { ('payinfo'); }
19
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 Payment Type (See L<FS::payinfo_Mixin> for valid values)
72
73 =item payinfo
74
75 card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
76
77 =item paybatch
78
79 text field for tracking card processing
80
81 =item closed
82
83 books closed flag, empty or `Y'
84
85 =item pkgnum
86
87 Desired pkgnum when using experimental package balances.
88
89 =item void_date
90
91 =item reason - a freeform string (deprecated)
92
93 =item reasonnum - Reason for voiding the payment (see L<FS::reson>)
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
139   my $cust_pay_pending =
140     qsearchs('cust_pay_pending', { void_paynum => $self->paynum });
141   if ( $cust_pay_pending ) {
142     $cust_pay_pending->set('paynum', $cust_pay->paynum);
143     $cust_pay_pending->set('void_paynum', '');
144     $error ||= $cust_pay_pending->replace;
145   }
146
147   $error ||= $self->delete;
148   if ( $error ) {
149     $dbh->rollback if $oldAutoCommit;
150     return $error;
151   }
152
153   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
154
155   '';
156
157 }
158
159 =item delete
160
161 Deletes this voided payment.  You probably don't want to use this directly; see
162 the B<unvoid> method to add the original payment back.
163
164 =item replace [ OLD_RECORD ]
165
166 You can, but probably shouldn't modify voided payments...
167
168 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
169 supplied, replaces this record.  If there is an error, returns the error,
170 otherwise returns false.
171
172 =item check
173
174 Checks all fields to make sure this is a valid voided payment.  If there is an
175 error, returns the error, otherwise returns false.  Called by the insert
176 method.
177
178 =cut
179
180 sub check {
181   my $self = shift;
182
183   my $error =
184     $self->ut_numbern('paynum')
185     || $self->ut_numbern('custnum')
186     || $self->ut_money('paid')
187     || $self->ut_number('_date')
188     || $self->ut_textn('paybatch')
189     || $self->ut_enum('closed', [ '', 'Y' ])
190     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
191     || $self->ut_numbern('void_date')
192     || $self->ut_textn('reason')
193     # || $self->payinfo_check #we'd rather void what we have than fail on this
194     || $self->ut_foreign_keyn('reasonnum', 'reason', 'reasonnum')
195   ;
196   return $error if $error;
197
198   return "paid must be > 0 " if $self->paid <= 0;
199
200   return "unknown cust_main.custnum: ". $self->custnum
201     unless $self->invnum
202            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
203
204   $self->void_date(time) unless $self->void_date;
205
206   $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
207     unless $self->void_usernum;
208
209   $self->SUPER::check;
210 }
211
212 =item cust_main
213
214 Returns the parent customer object (see L<FS::cust_main>).
215
216 =item void_access_user
217
218 Returns the voiding employee object (see L<FS::access_user>).
219
220 =cut
221
222 sub void_access_user {
223   my $self = shift;
224   qsearchs('access_user', { 'usernum' => $self->void_usernum } );
225 }
226
227 =item reason
228
229 Returns the text of the associated void reason (see L<FS::reason>) for this.
230
231 =cut
232
233 # Used by FS::Upgrade to migrate to a new database.
234 sub _upgrade_data {  # class method
235   my ($class, %opts) = @_;
236
237   local $FS::payinfo_Mixin::ignore_masked_payinfo = 1;
238
239   $class->_upgrade_reasonnum(%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