b2f777b32734edf3043754f797ae0efada92ea01
[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::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
92
93 =back
94
95 =head1 METHODS
96
97 =over 4 
98
99 =item new HASHREF
100
101 Creates a new payment.  To add the payment to the databse, see L<"insert">.
102
103 =cut
104
105 sub table { 'cust_pay_void'; }
106
107 =item insert
108
109 Adds this voided payment to the database.
110
111 =item unvoid 
112
113 "Un-void"s this payment: Deletes the voided payment from the database and adds
114 back a normal payment.
115
116 =cut
117
118 sub unvoid {
119   my $self = shift;
120
121   local $SIG{HUP} = 'IGNORE';
122   local $SIG{INT} = 'IGNORE';
123   local $SIG{QUIT} = 'IGNORE';
124   local $SIG{TERM} = 'IGNORE';
125   local $SIG{TSTP} = 'IGNORE';
126   local $SIG{PIPE} = 'IGNORE';
127
128   my $oldAutoCommit = $FS::UID::AutoCommit;
129   local $FS::UID::AutoCommit = 0;
130   my $dbh = dbh;
131
132   my $cust_pay = new FS::cust_pay ( {
133     map { $_ => $self->get($_) } fields('cust_pay')
134   } );
135   my $error = $cust_pay->insert;
136
137   my $cust_pay_pending =
138     qsearchs('cust_pay_pending', { void_paynum => $self->paynum });
139   if ( $cust_pay_pending ) {
140     $cust_pay_pending->set('paynum', $cust_pay->paynum);
141     $cust_pay_pending->set('void_paynum', '');
142     $error ||= $cust_pay_pending->replace;
143   }
144
145   $error ||= $self->delete;
146   if ( $error ) {
147     $dbh->rollback if $oldAutoCommit;
148     return $error;
149   }
150
151   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
152
153   '';
154
155 }
156
157 =item delete
158
159 Deletes this voided payment.  You probably don't want to use this directly; see
160 the B<unvoid> method to add the original payment back.
161
162 =item replace [ OLD_RECORD ]
163
164 You can, but probably shouldn't modify voided payments...
165
166 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
167 supplied, replaces this record.  If there is an error, returns the error,
168 otherwise returns false.
169
170 =item check
171
172 Checks all fields to make sure this is a valid voided payment.  If there is an
173 error, returns the error, otherwise returns false.  Called by the insert
174 method.
175
176 =cut
177
178 sub check {
179   my $self = shift;
180
181   my $error =
182     $self->ut_numbern('paynum')
183     || $self->ut_numbern('custnum')
184     || $self->ut_money('paid')
185     || $self->ut_number('_date')
186     || $self->ut_textn('paybatch')
187     || $self->ut_enum('closed', [ '', 'Y' ])
188     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
189     || $self->ut_numbern('void_date')
190     || $self->ut_textn('reason')
191     || $self->payinfo_check
192   ;
193   return $error if $error;
194
195   return "paid must be > 0 " if $self->paid <= 0;
196
197   return "unknown cust_main.custnum: ". $self->custnum
198     unless $self->invnum
199            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
200
201   $self->void_date(time) unless $self->void_date;
202
203   $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
204     unless $self->void_usernum;
205
206   $self->SUPER::check;
207 }
208
209 =item cust_main
210
211 Returns the parent customer object (see L<FS::cust_main>).
212
213 =item void_access_user
214
215 Returns the voiding employee object (see L<FS::access_user>).
216
217 =cut
218
219 sub void_access_user {
220   my $self = shift;
221   qsearchs('access_user', { 'usernum' => $self->void_usernum } );
222 }
223
224 # Used by FS::Upgrade to migrate to a new database.
225 sub _upgrade_data {  # class method
226   my ($class, %opts) = @_;
227
228   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 ) ";
229   my $sth = dbh->prepare($sql) or die dbh->errstr;
230
231   foreach my $cust_pay_void (qsearch('cust_pay_void', {'void_usernum' => ''})) {
232     $sth->execute($cust_pay_void->paynum) or die $sth->errstr;
233     my $row = $sth->fetchrow_arrayref;
234     my $usernum = $row ? $row->[0] : '';
235     if ( $usernum ) {
236       $cust_pay_void->void_usernum($usernum);
237       my $error = $cust_pay_void->replace;
238       die $error if $error;
239     } else {
240       warn "cust_pay_void upgrade: can't find access_user record for ". $cust_pay_void->paynum. "\n";
241     }
242   }
243
244   local($otaker_upgrade_kludge) = 1;
245   $class->_upgrade_otaker(%opts);
246
247   #XXX look for the h_cust_pay delete records and when that's a different
248   # usernum, set usernum
249 }
250
251 =back
252
253 =head1 BUGS
254
255 Delete and replace methods.
256
257 =head1 SEE ALSO
258
259 L<FS::cust_pay>, L<FS::Record>, schema.html from the base documentation.
260
261 =cut
262
263 1;
264