NG auth: fix new customer, remove mapsecrets support, RT#21563
[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_transaction_Mixin FS::cust_main_Mixin
5              FS::Record );
6 use vars qw( @encrypted_fields $otaker_upgrade_kludge );
7 use Business::CreditCard;
8 use FS::Record qw(qsearch qsearchs dbh fields);
9 use FS::CurrentUser;
10 use FS::access_user;
11 use FS::cust_pay;
12 #use FS::cust_bill;
13 #use FS::cust_bill_pay;
14 #use FS::cust_pay_refund;
15 #use FS::cust_main;
16 use FS::cust_pkg;
17
18 @encrypted_fields = ('payinfo');
19 $otaker_upgrade_kludge = 0;
20
21 =head1 NAME
22
23 FS::cust_pay_void - Object methods for cust_pay_void objects
24
25 =head1 SYNOPSIS
26
27   use FS::cust_pay_void;
28
29   $record = new FS::cust_pay_void \%hash;
30   $record = new FS::cust_pay_void { 'column' => 'value' };
31
32   $error = $record->insert;
33
34   $error = $new_record->replace($old_record);
35
36   $error = $record->delete;
37
38   $error = $record->check;
39
40 =head1 DESCRIPTION
41
42 An FS::cust_pay_void object represents a voided payment.  The following fields
43 are currently supported:
44
45 =over 4
46
47 =item paynum
48
49 primary key (assigned automatically for new payments)
50
51 =item custnum
52
53 customer (see L<FS::cust_main>)
54
55 =item paid
56
57 Amount of this payment
58
59 =item _date
60
61 specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
62 L<Time::Local> and L<Date::Parse> for conversion functions.
63
64 =item otaker
65
66 order taker (see L<FS::access_user>)
67
68 =item payby
69
70 Payment Type (See L<FS::payinfo_Mixin> for valid values)
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 You can, but probably shouldn't modify voided payments...
160
161 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
162 supplied, replaces this record.  If there is an error, returns the error,
163 otherwise returns false.
164
165 =item check
166
167 Checks all fields to make sure this is a valid voided payment.  If there is an
168 error, returns the error, otherwise returns false.  Called by the insert
169 method.
170
171 =cut
172
173 sub check {
174   my $self = shift;
175
176   my $error =
177     $self->ut_numbern('paynum')
178     || $self->ut_numbern('custnum')
179     || $self->ut_money('paid')
180     || $self->ut_number('_date')
181     || $self->ut_textn('paybatch')
182     || $self->ut_enum('closed', [ '', 'Y' ])
183     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
184     || $self->ut_numbern('void_date')
185     || $self->ut_textn('reason')
186     || $self->payinfo_check
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->void_usernum($FS::CurrentUser::CurrentUser->usernum)
199     unless $self->void_usernum;
200
201   $self->SUPER::check;
202 }
203
204 =item cust_main
205
206 Returns the parent customer object (see L<FS::cust_main>).
207
208 =cut
209
210 sub cust_main {
211   my $self = shift;
212   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
213 }
214
215 =item void_access_user
216
217 Returns the voiding employee object (see L<FS::access_user>).
218
219 =cut
220
221 sub void_access_user {
222   my $self = shift;
223   qsearchs('access_user', { 'usernum' => $self->void_usernum } );
224 }
225
226 # Used by FS::Upgrade to migrate to a new database.
227 sub _upgrade_data {  # class method
228   my ($class, %opts) = @_;
229
230   my $sql = "SELECT usernum FROM access_user WHERE username = ( SELECT history_user FROM h_cust_pay_void WHERE paynum = ? AND history_action = 'insert' ORDER BY history_date LIMIT 1 ) ";
231   my $sth = dbh->prepare($sql) or die dbh->errstr;
232
233   foreach my $cust_pay_void (qsearch('cust_pay_void', {'void_usernum' => ''})) {
234     $sth->execute($cust_pay_void->paynum) or die $sth->errstr;
235     my $row = $sth->fetchrow_arrayref;
236     my $usernum = $row ? $row->[0] : '';
237     if ( $usernum ) {
238       $cust_pay_void->void_usernum($usernum);
239       my $error = $cust_pay_void->replace;
240       die $error if $error;
241     } else {
242       warn "cust_pay_void upgrade: can't find access_user record for ". $cust_pay_void->paynum. "\n";
243     }
244   }
245
246   local($otaker_upgrade_kludge) = 1;
247   $class->_upgrade_otaker(%opts);
248
249   #XXX look for the h_cust_pay delete records and when that's a different
250   # usernum, set usernum
251 }
252
253 =back
254
255 =head1 BUGS
256
257 Delete and replace methods.
258
259 =head1 SEE ALSO
260
261 L<FS::cust_pay>, L<FS::Record>, schema.html from the base documentation.
262
263 =cut
264
265 1;
266