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