fix more paymask regressions: allow editing records with existing expired cards,...
[freeside.git] / FS / FS / cust_pay_void.pm
1 package FS::cust_pay_void; 
2 use strict;
3 use vars qw( @ISA @encrypted_fields );
4 use Business::CreditCard;
5 use FS::UID qw(getotaker);
6 use FS::Record qw(qsearchs dbh fields); # qsearch );
7 use FS::cust_pay;
8 #use FS::cust_bill;
9 #use FS::cust_bill_pay;
10 #use FS::cust_pay_refund;
11 #use FS::cust_main;
12
13 @ISA = qw( FS::Record FS::payinfo_Mixin );
14
15 @encrypted_fields = ('payinfo');
16
17 =head1 NAME
18
19 FS::cust_pay_void - Object methods for cust_pay_void objects
20
21 =head1 SYNOPSIS
22
23   use FS::cust_pay_void;
24
25   $record = new FS::cust_pay_void \%hash;
26   $record = new FS::cust_pay_void { 'column' => 'value' };
27
28   $error = $record->insert;
29
30   $error = $new_record->replace($old_record);
31
32   $error = $record->delete;
33
34   $error = $record->check;
35
36 =head1 DESCRIPTION
37
38 An FS::cust_pay_void object represents a voided payment.  The following fields
39 are currently supported:
40
41 =over 4
42
43 =item paynum - primary key (assigned automatically for new payments)
44
45 =item custnum - customer (see L<FS::cust_main>)
46
47 =item paid - Amount of this payment
48
49 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
50 L<Time::Local> and L<Date::Parse> for conversion functions.
51
52 =item payby - `CARD' (credit cards), `CHEK' (electronic check/ACH),
53 `LECB' (phone bill billing), `BILL' (billing), `CASH' (cash),
54 `WEST' (Western Union), `MCRD' (Manual credit card), or `COMP' (free)
55
56 =item payinfo - card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
57
58 =item paybatch - text field for tracking card processing
59
60 =item closed - books closed flag, empty or `Y'
61
62 =item void_date
63
64 =item reason
65
66 =back
67
68 =head1 METHODS
69
70 =over 4 
71
72 =item new HASHREF
73
74 Creates a new payment.  To add the payment to the databse, see L<"insert">.
75
76 =cut
77
78 sub table { 'cust_pay_void'; }
79
80 =item insert
81
82 Adds this voided payment to the database.
83
84 =item unvoid 
85
86 "Un-void"s this payment: Deletes the voided payment from the database and adds
87 back a normal payment.
88
89 =cut
90
91 sub unvoid {
92   my $self = shift;
93
94   local $SIG{HUP} = 'IGNORE';
95   local $SIG{INT} = 'IGNORE';
96   local $SIG{QUIT} = 'IGNORE';
97   local $SIG{TERM} = 'IGNORE';
98   local $SIG{TSTP} = 'IGNORE';
99   local $SIG{PIPE} = 'IGNORE';
100
101   my $oldAutoCommit = $FS::UID::AutoCommit;
102   local $FS::UID::AutoCommit = 0;
103   my $dbh = dbh;
104
105   my $cust_pay = new FS::cust_pay ( {
106     map { $_ => $self->get($_) } fields('cust_pay')
107   } );
108   my $error = $cust_pay->insert;
109   if ( $error ) {
110     $dbh->rollback if $oldAutoCommit;
111     return $error;
112   }
113
114   $error = $self->delete;
115   if ( $error ) {
116     $dbh->rollback if $oldAutoCommit;
117     return $error;
118   }
119
120   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
121
122   '';
123
124 }
125
126 =item delete
127
128 Deletes this voided payment.  You probably don't want to use this directly; see
129 the B<unvoid> method to add the original payment back.
130
131 =item replace OLD_RECORD
132
133 Currently unimplemented.
134
135 =cut
136
137 sub replace {
138    return "Can't modify voided payments!";
139 }
140
141 =item check
142
143 Checks all fields to make sure this is a valid voided payment.  If there is an
144 error, returns the error, otherwise returns false.  Called by the insert
145 method.
146
147 =cut
148
149 sub check {
150   my $self = shift;
151
152   my $error =
153     $self->ut_numbern('paynum')
154     || $self->ut_numbern('custnum')
155     || $self->ut_money('paid')
156     || $self->ut_number('_date')
157     || $self->ut_textn('paybatch')
158     || $self->ut_enum('closed', [ '', 'Y' ])
159     || $self->ut_numbern('void_date')
160     || $self->ut_textn('reason')
161   ;
162   return $error if $error;
163
164   return "paid must be > 0 " if $self->paid <= 0;
165
166   return "unknown cust_main.custnum: ". $self->custnum
167     unless $self->invnum
168            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
169
170   $self->void_date(time) unless $self->void_date;
171
172   $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP|PREP|CASH|WEST|MCRD)$/
173     or return "Illegal payby";
174   $self->payby($1);
175
176   #false laziness with cust_refund::check
177   if ( $self->payby eq 'CARD' ) {
178     my $payinfo = $self->payinfo;
179     $payinfo =~ s/\D//g;
180     $self->payinfo($payinfo);
181     if ( $self->payinfo ) {
182       $self->payinfo =~ /^(\d{13,16})$/
183         or return "Illegal (mistyped?) credit card number (payinfo)";
184       $self->payinfo($1);
185       validate($self->payinfo) or return "Illegal credit card number";
186       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
187     } else {
188       $self->payinfo('N/A');
189     }
190
191   } else {
192     $error = $self->ut_textn('payinfo');
193     return $error if $error;
194   }
195
196   $self->otaker(getotaker);
197
198   $self->SUPER::check;
199 }
200
201 =item cust_main
202
203 Returns the parent customer object (see L<FS::cust_main>).
204
205 =cut
206
207 sub cust_main {
208   my $self = shift;
209   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
210 }
211
212 =back
213
214 =head1 BUGS
215
216 Delete and replace methods.
217
218 =head1 SEE ALSO
219
220 L<FS::cust_pay>, L<FS::Record>, schema.html from the base documentation.
221
222 =cut
223
224 1;
225