90f9b59edf0661adc829b274839fe2e90a341697
[freeside.git] / FS / FS / cust_credit.pm
1 package FS::cust_credit;
2
3 use strict;
4 use vars qw( @ISA $conf $unsuspendauto );
5 use FS::UID qw( dbh getotaker );
6 use FS::Record qw( qsearch qsearchs );
7 use FS::cust_main;
8 use FS::cust_refund;
9 use FS::cust_credit_bill;
10
11 @ISA = qw( FS::Record );
12
13 #ask FS::UID to run this stuff for us later
14 $FS::UID::callback{'FS::cust_credit'} = sub { 
15
16   $conf = new FS::Conf;
17   $unsuspendauto = $conf->exists('unsuspendauto');
18
19 };
20
21 =head1 NAME
22
23 FS::cust_credit - Object methods for cust_credit records
24
25 =head1 SYNOPSIS
26
27   use FS::cust_credit;
28
29   $record = new FS::cust_credit \%hash;
30   $record = new FS::cust_credit { 'column' => 'value' };
31
32   $error = $record->insert;
33
34   $error = $new_record->replace($old_record);
35
36   $error = $record->delete;
37
38   $error = $record->check;
39
40 =head1 DESCRIPTION
41
42 An FS::cust_credit object represents a credit; the equivalent of a negative
43 B<cust_bill> record (see L<FS::cust_bill>).  FS::cust_credit inherits from
44 FS::Record.  The following fields are currently supported:
45
46 =over 4
47
48 =item crednum - primary key (assigned automatically for new credits)
49
50 =item custnum - customer (see L<FS::cust_main>)
51
52 =item amount - amount of the credit
53
54 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
55 L<Time::Local> and L<Date::Parse> for conversion functions.
56
57 =item otaker - order taker (assigned automatically, see L<FS::UID>)
58
59 =item reason - text
60
61 =item closed - books closed flag, empty or `Y'
62
63 =back
64
65 =head1 METHODS
66
67 =over 4
68
69 =item new HASHREF
70
71 Creates a new credit.  To add the credit to the database, see L<"insert">.
72
73 =cut
74
75 sub table { 'cust_credit'; }
76
77 =item insert
78
79 Adds this credit to the database ("Posts" the credit).  If there is an error,
80 returns the error, otherwise returns false.
81
82 =cut
83
84 sub insert {
85   my $self = shift;
86
87   local $SIG{HUP} = 'IGNORE';
88   local $SIG{INT} = 'IGNORE';
89   local $SIG{QUIT} = 'IGNORE';
90   local $SIG{TERM} = 'IGNORE';
91   local $SIG{TSTP} = 'IGNORE';
92   local $SIG{PIPE} = 'IGNORE';
93
94   my $oldAutoCommit = $FS::UID::AutoCommit;
95   local $FS::UID::AutoCommit = 0;
96   my $dbh = dbh;
97
98   my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
99   my $old_balance = $cust_main->balance;
100
101   my $error = $self->SUPER::insert;
102   if ( $error ) {
103     $dbh->rollback if $oldAutoCommit;
104     return "error inserting $self: $error";
105   }
106
107   #false laziness w/ cust_credit::insert
108   if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
109     my @errors = $cust_main->unsuspend;
110     #return 
111     # side-fx with nested transactions?  upstack rolls back?
112     warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
113          join(' / ', @errors)
114       if @errors;
115   }
116   #eslaf
117
118   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
119
120   '';
121
122 }
123
124 =item delete
125
126 Currently unimplemented.
127
128 =cut
129
130 sub delete {
131   my $self = shift;
132   return "Can't delete closed credit" if $self->closed =~ /^Y/i;
133
134   local $SIG{HUP} = 'IGNORE';
135   local $SIG{INT} = 'IGNORE';
136   local $SIG{QUIT} = 'IGNORE';
137   local $SIG{TERM} = 'IGNORE';
138   local $SIG{TSTP} = 'IGNORE';
139   local $SIG{PIPE} = 'IGNORE';
140
141   my $oldAutoCommit = $FS::UID::AutoCommit;
142   local $FS::UID::AutoCommit = 0;
143   my $dbh = dbh;
144
145   foreach my $cust_credit_bill ( $self->cust_credit_bill ) {
146     my $error = $cust_credit_bill->delete;
147     if ( $error ) {
148       $dbh->rollback if $oldAutoCommit;
149       return $error;
150     }
151   }
152
153   my $error = $self->SUPER::delete(@_);
154   if ( $error ) {
155     $dbh->rollback if $oldAutoCommit;
156     return $error;
157   }
158
159   if ( $conf->config('deletecredits') ne '' ) {
160
161     my $cust_main = qsearchs('cust_main',{ 'custnum' => $self->custnum });
162
163     my $error = send_email(
164       'from'    => $conf->config('invoice_from'), #??? well as good as any
165       'to'      => $conf->config('deletecredits'),
166       'subject' => 'FREESIDE NOTIFICATION: Credit deleted',
167       'body'    => [
168         "This is an automatic message from your Freeside installation\n",
169         "informing you that the following credit has been deleted:\n",
170         "\n",
171         'crednum: '. $self->crednum. "\n",
172         'custnum: '. $self->custnum.
173           " (". $cust_main->last. ", ". $cust_main->first. ")\n",
174         'amount: $'. sprintf("%.2f", $self->amount). "\n",
175         'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
176         'reason: '. $self->reason. "\n",
177       ],
178     );
179
180     if ( $error ) {
181       $dbh->rollback if $oldAutoCommit;
182       return "can't send credit deletion notification: $error";
183     }
184
185   }
186
187   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
188
189   '';
190
191 }
192
193 =item replace OLD_RECORD
194
195 Credits may not be modified; there would then be no record the credit was ever
196 posted.
197
198 =cut
199
200 sub replace {
201   #return "Can't modify credit!"
202   my $self = shift;
203   return "Can't modify closed credit" if $self->closed =~ /^Y/i;
204   $self->SUPER::replace(@_);
205 }
206
207 =item check
208
209 Checks all fields to make sure this is a valid credit.  If there is an error,
210 returns the error, otherwise returns false.  Called by the insert and replace
211 methods.
212
213 =cut
214
215 sub check {
216   my $self = shift;
217
218   my $error =
219     $self->ut_numbern('crednum')
220     || $self->ut_number('custnum')
221     || $self->ut_numbern('_date')
222     || $self->ut_money('amount')
223     || $self->ut_textn('reason')
224     || $self->ut_enum('closed', [ '', 'Y' ])
225   ;
226   return $error if $error;
227
228   return "amount must be > 0 " if $self->amount <= 0;
229
230   return "Unknown customer"
231     unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
232
233   $self->_date(time) unless $self->_date;
234
235   $self->otaker(getotaker);
236
237   $self->SUPER::check;
238 }
239
240 =item cust_refund
241
242 Depreciated.  See the cust_credit_refund method.
243
244 #Returns all refunds (see L<FS::cust_refund>) for this credit.
245
246 =cut
247
248 sub cust_refund {
249   use Carp;
250   croak "FS::cust_credit->cust_pay depreciated; see ".
251         "FS::cust_credit->cust_credit_refund";
252   #my $self = shift;
253   #sort { $a->_date <=> $b->_date }
254   #  qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
255   #;
256 }
257
258 =item cust_credit_refund
259
260 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
261
262 =cut
263
264 sub cust_credit_refund {
265   my $self = shift;
266   sort { $a->_date <=> $b->_date }
267     qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
268   ;
269 }
270
271 =item cust_credit_bill
272
273 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
274 credit.
275
276 =cut
277
278 sub cust_credit_bill {
279   my $self = shift;
280   sort { $a->_date <=> $b->_date }
281     qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
282   ;
283 }
284
285 =item credited
286
287 Returns the amount of this credit that is still outstanding; which is
288 amount minus all refund applications (see L<FS::cust_credit_refund>) and
289 applications to invoices (see L<FS::cust_credit_bill>).
290
291 =cut
292
293 sub credited {
294   my $self = shift;
295   my $amount = $self->amount;
296   $amount -= $_->amount foreach ( $self->cust_credit_refund );
297   $amount -= $_->amount foreach ( $self->cust_credit_bill );
298   sprintf( "%.2f", $amount );
299 }
300
301 =back
302
303 =head1 BUGS
304
305 The delete method.  The replace method.
306
307 =head1 SEE ALSO
308
309 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
310 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base
311 documentation.
312
313 =cut
314
315 1;
316