1 package FS::cust_pay_void;
2 use base qw( FS::otaker_Mixin FS::payinfo_transaction_Mixin FS::cust_main_Mixin
3 FS::reason_Mixin FS::Record );
6 use vars qw( @encrypted_fields $otaker_upgrade_kludge );
7 use Business::CreditCard;
8 use FS::Record qw(qsearch qsearchs dbh fields);
13 #use FS::cust_bill_pay;
14 #use FS::cust_pay_refund;
17 @encrypted_fields = ('payinfo');
18 sub nohistory_fields { ('payinfo'); }
20 $otaker_upgrade_kludge = 0;
24 FS::cust_pay_void - Object methods for cust_pay_void objects
28 use FS::cust_pay_void;
30 $record = new FS::cust_pay_void \%hash;
31 $record = new FS::cust_pay_void { 'column' => 'value' };
33 $error = $record->insert;
35 $error = $new_record->replace($old_record);
37 $error = $record->delete;
39 $error = $record->check;
43 An FS::cust_pay_void object represents a voided payment. The following fields
44 are currently supported:
50 primary key (assigned automatically for new payments)
54 customer (see L<FS::cust_main>)
58 Amount of this payment
62 specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
63 L<Time::Local> and L<Date::Parse> for conversion functions.
67 order taker (see L<FS::access_user>)
71 Payment Type (See L<FS::payinfo_Mixin> for valid values)
75 card number, check #, or comp issuer (4-8 lowercase alphanumerics; think username), respectively
79 Credit card type, if appropriate.
83 text field for tracking card processing
87 books closed flag, empty or `Y'
91 Desired pkgnum when using experimental package balances.
95 =item reason - a freeform string (deprecated)
97 =item reasonnum - Reason for voiding the payment (see L<FS::reson>)
107 Creates a new payment. To add the payment to the databse, see L<"insert">.
111 sub table { 'cust_pay_void'; }
115 Adds this voided payment to the database.
119 "Un-void"s this payment: Deletes the voided payment from the database and adds
120 back a normal payment.
127 local $SIG{HUP} = 'IGNORE';
128 local $SIG{INT} = 'IGNORE';
129 local $SIG{QUIT} = 'IGNORE';
130 local $SIG{TERM} = 'IGNORE';
131 local $SIG{TSTP} = 'IGNORE';
132 local $SIG{PIPE} = 'IGNORE';
134 my $oldAutoCommit = $FS::UID::AutoCommit;
135 local $FS::UID::AutoCommit = 0;
138 my $cust_pay = new FS::cust_pay ( {
139 map { $_ => $self->get($_) } fields('cust_pay')
141 my $error = $cust_pay->insert;
143 my $cust_pay_pending =
144 qsearchs('cust_pay_pending', { void_paynum => $self->paynum });
145 if ( $cust_pay_pending ) {
146 $cust_pay_pending->set('paynum', $cust_pay->paynum);
147 $cust_pay_pending->set('void_paynum', '');
148 $error ||= $cust_pay_pending->replace;
151 $error ||= $self->delete;
153 $dbh->rollback if $oldAutoCommit;
157 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
165 Deletes this voided payment. You probably don't want to use this directly; see
166 the B<unvoid> method to add the original payment back.
168 =item replace [ OLD_RECORD ]
170 You can, but probably shouldn't modify voided payments...
172 Replaces the OLD_RECORD with this one in the database, or, if OLD_RECORD is not
173 supplied, replaces this record. If there is an error, returns the error,
174 otherwise returns false.
178 Checks all fields to make sure this is a valid voided payment. If there is an
179 error, returns the error, otherwise returns false. Called by the insert
188 $self->ut_numbern('paynum')
189 || $self->ut_numbern('custnum')
190 || $self->ut_money('paid')
191 || $self->ut_number('_date')
192 || $self->ut_textn('paybatch')
193 || $self->ut_enum('closed', [ '', 'Y' ])
194 || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
195 || $self->ut_numbern('void_date')
196 || $self->ut_textn('reason')
197 # || $self->payinfo_check #we'd rather void what we have than fail on this
198 || $self->ut_foreign_keyn('reasonnum', 'reason', 'reasonnum')
200 return $error if $error;
202 return "paid must be > 0 " if $self->paid <= 0;
204 return "unknown cust_main.custnum: ". $self->custnum
206 || qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
208 $self->void_date(time) unless $self->void_date;
210 $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
211 unless $self->void_usernum;
218 Returns the parent customer object (see L<FS::cust_main>).
220 =item void_access_user
222 Returns the voiding employee object (see L<FS::access_user>).
226 sub void_access_user {
228 qsearchs('access_user', { 'usernum' => $self->void_usernum } );
233 Returns the text of the associated void reason (see L<FS::reason>) for this.
237 # Used by FS::Upgrade to migrate to a new database.
238 sub _upgrade_data { # class method
239 my ($class, %opts) = @_;
241 local $FS::payinfo_Mixin::ignore_masked_payinfo = 1;
243 $class->_upgrade_reasonnum(%opts);
245 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 ) ";
246 my $sth = dbh->prepare($sql) or die dbh->errstr;
248 foreach my $cust_pay_void (qsearch('cust_pay_void', {'void_usernum' => ''})) {
249 $sth->execute($cust_pay_void->paynum) or die $sth->errstr;
250 my $row = $sth->fetchrow_arrayref;
251 my $usernum = $row ? $row->[0] : '';
253 $cust_pay_void->void_usernum($usernum);
254 my $error = $cust_pay_void->replace;
255 die $error if $error;
257 warn "cust_pay_void upgrade: can't find access_user record for ". $cust_pay_void->paynum. "\n";
261 local($otaker_upgrade_kludge) = 1;
262 $class->_upgrade_otaker(%opts);
264 #XXX look for the h_cust_pay delete records and when that's a different
265 # usernum, set usernum
272 Delete and replace methods.
276 L<FS::cust_pay>, L<FS::Record>, schema.html from the base documentation.