eWay self-signup fixes
[freeside.git] / FS / FS / cust_pay_refund.pm
1 package FS::cust_pay_refund;
2
3 use strict;
4 use vars qw( @ISA ); #$conf );
5 use FS::UID qw( getotaker );
6 use FS::Record qw( qsearchs ); # qsearch );
7 use FS::cust_main;
8 use FS::cust_pay;
9 use FS::cust_refund;
10
11 @ISA = qw( FS::Record );
12
13 #ask FS::UID to run this stuff for us later
14 #FS::UID->install_callback( sub { 
15 #  $conf = new FS::Conf;
16 #} );
17
18 =head1 NAME
19
20 FS::cust_pay_refund - Object methods for cust_pay_refund records
21
22 =head1 SYNOPSIS
23
24   use FS::cust_pay_refund;
25
26   $record = new FS::cust_pay_refund \%hash;
27   $record = new FS::cust_pay_refund { '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_refund object represents application of a refund (see
40 L<FS::cust_refund>) to an payment (see L<FS::cust_pay>).  FS::cust_pay_refund
41 inherits from FS::Record.  The following fields are currently supported:
42
43 =over 4
44
45 =item payrefundnum - primary key
46
47 =item paynum - credit being applied 
48
49 =item refundnum - invoice to which credit is applied (see L<FS::cust_bill>)
50
51 =item amount - amount of the credit applied
52
53 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
54 L<Time::Local> and L<Date::Parse> for conversion functions.
55
56 =back
57
58 =head1 METHODS
59
60 =over 4
61
62 =item new HASHREF
63
64 Creates a new cust_pay_refund.  To add the cust_pay_refund to the database,
65 see L<"insert">.
66
67 =cut
68
69 sub table { 'cust_pay_refund'; }
70
71 =item insert
72
73 Adds this cust_pay_refund to the database.  If there is an error, returns the
74 error, otherwise returns false.
75
76 =cut
77
78 sub insert {
79   my $self = shift;
80   return "Can't apply refund to closed payment"
81     if $self->cust_pay->closed =~ /^Y/i;
82   return "Can't apply payment to closed refund"
83     if $self->cust_refund->closed =~ /^Y/i;
84   $self->SUPER::insert(@_);
85 }
86
87 =item delete
88
89 =cut
90
91 sub delete {
92   my $self = shift;
93   return "Can't remove refund from closed payment"
94     if $self->cust_pay->closed =~ /^Y/i;
95   return "Can't remove payment from closed refund"
96     if $self->cust_refund->closed =~ /^Y/i;
97   $self->SUPER::delete(@_);
98 }
99
100 =item replace OLD_RECORD
101
102 Application of refunds to payments may not be modified.
103
104 =cut
105
106 sub replace {
107   return "Can't modify application of a refund to payment!"
108 }
109
110 =item check
111
112 Checks all fields to make sure this is a valid refund application to a payment.
113 If there is an error, returns the error, otherwise returns false.  Called by
114 the insert and replace methods.
115
116 =cut
117
118 sub check {
119   my $self = shift;
120
121   my $error =
122     $self->ut_numbern('payrefundnum')
123     || $self->ut_number('paynum')
124     || $self->ut_number('refundnum')
125     || $self->ut_numbern('_date')
126     || $self->ut_money('amount')
127   ;
128   return $error if $error;
129
130   return "amount must be > 0" if $self->amount <= 0;
131
132   return "Unknown payment"
133     unless my $cust_pay = 
134       qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
135
136   return "Unknown refund"
137     unless my $cust_refund =
138       qsearchs( 'cust_refund', { 'refundnum' => $self->refundnum } );
139
140   $self->_date(time) unless $self->_date;
141
142   return 'Cannot apply ($'. $self->amount. ') more than'.
143          ' remaining value of refund ($'. $cust_refund->unapplied. ')'
144     unless $self->amount <= $cust_refund->unapplied;
145
146   return "Cannot apply more than remaining value of payment"
147     unless $self->amount <= $cust_pay->unapplied;
148
149   $self->SUPER::check;
150 }
151
152 =item sub cust_pay
153
154 Returns the payment (see L<FS::cust_pay>)
155
156 =cut
157
158 sub cust_pay {
159   my $self = shift;
160   qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
161 }
162
163 =item cust_refund
164
165 Returns the refund (see L<FS::cust_refund>)
166
167 =cut
168
169 sub cust_refund {
170   my $self = shift;
171   qsearchs( 'cust_refund', { 'refundnum' => $self->refundnum } );
172 }
173
174 =back
175
176 =head1 BUGS
177
178 The delete method.
179
180 =head1 SEE ALSO
181
182 L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, L<FS::cust_credit>,
183 schema.html from the base documentation.
184
185 =cut
186
187 1;
188