9293ef6d703460bd412400ea9567a8cf34ac5b88
[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::Record );
5 use vars qw( @encrypted_fields $otaker_upgrade_kludge );
6 use Business::CreditCard;
7 use FS::UID qw(getotaker);
8 use FS::Record qw(qsearchs dbh fields); # qsearch );
9 use FS::CurrentUser;
10 use FS::cust_pay;
11 #use FS::cust_bill;
12 #use FS::cust_bill_pay;
13 #use FS::cust_pay_refund;
14 #use FS::cust_main;
15 use FS::cust_pkg;
16
17 @encrypted_fields = ('payinfo');
18 $otaker_upgrade_kludge = 0;
19
20 =head1 NAME
21
22 FS::cust_pay_void - Object methods for cust_pay_void objects
23
24 =head1 SYNOPSIS
25
26   use FS::cust_pay_void;
27
28   $record = new FS::cust_pay_void \%hash;
29   $record = new FS::cust_pay_void { 'column' => 'value' };
30
31   $error = $record->insert;
32
33   $error = $new_record->replace($old_record);
34
35   $error = $record->delete;
36
37   $error = $record->check;
38
39 =head1 DESCRIPTION
40
41 An FS::cust_pay_void object represents a voided payment.  The following fields
42 are currently supported:
43
44 =over 4
45
46 =item paynum
47
48 primary key (assigned automatically for new payments)
49
50 =item custnum
51
52 customer (see L<FS::cust_main>)
53
54 =item paid
55
56 Amount of this payment
57
58 =item _date
59
60 specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
61 L<Time::Local> and L<Date::Parse> for conversion functions.
62
63 =item otaker
64
65 order taker (see L<FS::access_user>)
66
67 =item payby
68
69 `CARD' (credit cards), `CHEK' (electronic check/ACH),
70 `LECB' (phone bill billing), `BILL' (billing), `CASH' (cash),
71 `WEST' (Western Union), `MCRD' (Manual credit card), or `COMP' (free)
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   if ( $error ) {
137     $dbh->rollback if $oldAutoCommit;
138     return $error;
139   }
140
141   $error = $self->delete;
142   if ( $error ) {
143     $dbh->rollback if $oldAutoCommit;
144     return $error;
145   }
146
147   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
148
149   '';
150
151 }
152
153 =item delete
154
155 Deletes this voided payment.  You probably don't want to use this directly; see
156 the B<unvoid> method to add the original payment back.
157
158 =item replace OLD_RECORD
159
160 Currently unimplemented.
161
162 =cut
163
164 sub replace {
165    return "Can't modify voided payments!" unless $otaker_upgrade_kludge;
166    shift->SUPER::replace(@_);
167 }
168
169 =item check
170
171 Checks all fields to make sure this is a valid voided payment.  If there is an
172 error, returns the error, otherwise returns false.  Called by the insert
173 method.
174
175 =cut
176
177 sub check {
178   my $self = shift;
179
180   my $error =
181     $self->ut_numbern('paynum')
182     || $self->ut_numbern('custnum')
183     || $self->ut_money('paid')
184     || $self->ut_number('_date')
185     || $self->ut_textn('paybatch')
186     || $self->ut_enum('closed', [ '', 'Y' ])
187     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
188     || $self->ut_numbern('void_date')
189     || $self->ut_textn('reason')
190   ;
191   return $error if $error;
192
193   return "paid must be > 0 " if $self->paid <= 0;
194
195   return "unknown cust_main.custnum: ". $self->custnum
196     unless $self->invnum
197            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
198
199   $self->void_date(time) unless $self->void_date;
200
201   $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP|PREP|CASH|WEST|MCRD)$/
202     or return "Illegal payby";
203   $self->payby($1);
204
205   #false laziness with cust_refund::check
206   if ( $self->payby eq 'CARD' ) {
207     my $payinfo = $self->payinfo;
208     $payinfo =~ s/\D//g;
209     $self->payinfo($payinfo);
210     if ( $self->payinfo ) {
211       $self->payinfo =~ /^(\d{13,16})$/
212         or return "Illegal (mistyped?) credit card number (payinfo)";
213       $self->payinfo($1);
214       validate($self->payinfo) or return "Illegal credit card number";
215       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
216     } else {
217       $self->payinfo('N/A');
218     }
219
220   } else {
221     $error = $self->ut_textn('payinfo');
222     return $error if $error;
223   }
224
225   $self->usernum($FS::CurrentUser::CurrentUser->usernum) unless $self->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 # Used by FS::Upgrade to migrate to a new database.
242 sub _upgrade_data {  # class method
243   my ($class, %opts) = @_;
244   local($otaker_upgrade_kludge) = 1;
245   $class->_upgrade_otaker(%opts);
246 }
247
248 =back
249
250 =head1 BUGS
251
252 Delete and replace methods.
253
254 =head1 SEE ALSO
255
256 L<FS::cust_pay>, L<FS::Record>, schema.html from the base documentation.
257
258 =cut
259
260 1;
261