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