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