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