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