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