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