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