move from otaker to proper usernum FK
[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 );
6 use Business::CreditCard;
7 use FS::UID qw(getotaker);
8 use FS::Record qw(qsearchs dbh fields); # qsearch );
9 use FS::cust_pay;
10 #use FS::cust_bill;
11 #use FS::cust_bill_pay;
12 #use FS::cust_pay_refund;
13 #use FS::cust_main;
14 use FS::cust_pkg;
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 otaker
62
63 order taker (see L<FS::access_user>)
64
65 =item payby
66
67 `CARD' (credit cards), `CHEK' (electronic check/ACH),
68 `LECB' (phone bill billing), `BILL' (billing), `CASH' (cash),
69 `WEST' (Western Union), `MCRD' (Manual credit card), or `COMP' (free)
70
71 =item payinfo
72
73 card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
74
75 =item paybatch
76
77 text field for tracking card processing
78
79 =item closed
80
81 books closed flag, empty or `Y'
82
83 =item pkgnum
84
85 Desired pkgnum when using experimental package balances.
86
87 =item void_date
88
89 =item reason
90
91 =back
92
93 =head1 METHODS
94
95 =over 4 
96
97 =item new HASHREF
98
99 Creates a new payment.  To add the payment to the databse, see L<"insert">.
100
101 =cut
102
103 sub table { 'cust_pay_void'; }
104
105 =item insert
106
107 Adds this voided payment to the database.
108
109 =item unvoid 
110
111 "Un-void"s this payment: Deletes the voided payment from the database and adds
112 back a normal payment.
113
114 =cut
115
116 sub unvoid {
117   my $self = shift;
118
119   local $SIG{HUP} = 'IGNORE';
120   local $SIG{INT} = 'IGNORE';
121   local $SIG{QUIT} = 'IGNORE';
122   local $SIG{TERM} = 'IGNORE';
123   local $SIG{TSTP} = 'IGNORE';
124   local $SIG{PIPE} = 'IGNORE';
125
126   my $oldAutoCommit = $FS::UID::AutoCommit;
127   local $FS::UID::AutoCommit = 0;
128   my $dbh = dbh;
129
130   my $cust_pay = new FS::cust_pay ( {
131     map { $_ => $self->get($_) } fields('cust_pay')
132   } );
133   my $error = $cust_pay->insert;
134   if ( $error ) {
135     $dbh->rollback if $oldAutoCommit;
136     return $error;
137   }
138
139   $error = $self->delete;
140   if ( $error ) {
141     $dbh->rollback if $oldAutoCommit;
142     return $error;
143   }
144
145   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
146
147   '';
148
149 }
150
151 =item delete
152
153 Deletes this voided payment.  You probably don't want to use this directly; see
154 the B<unvoid> method to add the original payment back.
155
156 =item replace OLD_RECORD
157
158 Currently unimplemented.
159
160 =cut
161
162 sub replace {
163    return "Can't modify voided payments!";
164 }
165
166 =item check
167
168 Checks all fields to make sure this is a valid voided payment.  If there is an
169 error, returns the error, otherwise returns false.  Called by the insert
170 method.
171
172 =cut
173
174 sub check {
175   my $self = shift;
176
177   my $error =
178     $self->ut_numbern('paynum')
179     || $self->ut_numbern('custnum')
180     || $self->ut_money('paid')
181     || $self->ut_number('_date')
182     || $self->ut_textn('paybatch')
183     || $self->ut_enum('closed', [ '', 'Y' ])
184     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
185     || $self->ut_numbern('void_date')
186     || $self->ut_textn('reason')
187   ;
188   return $error if $error;
189
190   return "paid must be > 0 " if $self->paid <= 0;
191
192   return "unknown cust_main.custnum: ". $self->custnum
193     unless $self->invnum
194            || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
195
196   $self->void_date(time) unless $self->void_date;
197
198   $self->payby =~ /^(CARD|CHEK|LECB|BILL|COMP|PREP|CASH|WEST|MCRD)$/
199     or return "Illegal payby";
200   $self->payby($1);
201
202   #false laziness with cust_refund::check
203   if ( $self->payby eq 'CARD' ) {
204     my $payinfo = $self->payinfo;
205     $payinfo =~ s/\D//g;
206     $self->payinfo($payinfo);
207     if ( $self->payinfo ) {
208       $self->payinfo =~ /^(\d{13,16})$/
209         or return "Illegal (mistyped?) credit card number (payinfo)";
210       $self->payinfo($1);
211       validate($self->payinfo) or return "Illegal credit card number";
212       return "Unknown card type" if cardtype($self->payinfo) eq "Unknown";
213     } else {
214       $self->payinfo('N/A');
215     }
216
217   } else {
218     $error = $self->ut_textn('payinfo');
219     return $error if $error;
220   }
221
222   $self->otaker(getotaker) unless $self->otaker;
223
224   $self->SUPER::check;
225 }
226
227 =item cust_main
228
229 Returns the parent customer object (see L<FS::cust_main>).
230
231 =cut
232
233 sub cust_main {
234   my $self = shift;
235   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
236 }
237
238 # Used by FS::Upgrade to migrate to a new database.
239 sub _upgrade_data {  # class method
240   my ($class, %opts) = @_;
241   $class->_upgrade_otaker(%opts);
242 }
243
244 =back
245
246 =head1 BUGS
247
248 Delete and replace methods.
249
250 =head1 SEE ALSO
251
252 L<FS::cust_pay>, L<FS::Record>, schema.html from the base documentation.
253
254 =cut
255
256 1;
257