This commit was generated by cvs2svn to compensate for changes in r4888,
[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   my $error = $self->SUPER::delete(@_);
168   if ( $error ) {
169     $dbh->rollback if $oldAutoCommit;
170     return $error;
171   }
172
173   if ( $conf->config('deletecredits') ne '' ) {
174
175     my $cust_main = $self->cust_main;
176
177     my $error = send_email(
178       'from'    => $conf->config('invoice_from'), #??? well as good as any
179       'to'      => $conf->config('deletecredits'),
180       'subject' => 'FREESIDE NOTIFICATION: Credit deleted',
181       'body'    => [
182         "This is an automatic message from your Freeside installation\n",
183         "informing you that the following credit has been deleted:\n",
184         "\n",
185         'crednum: '. $self->crednum. "\n",
186         'custnum: '. $self->custnum.
187           " (". $cust_main->last. ", ". $cust_main->first. ")\n",
188         'amount: $'. sprintf("%.2f", $self->amount). "\n",
189         'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
190         'reason: '. $self->reason. "\n",
191       ],
192     );
193
194     if ( $error ) {
195       $dbh->rollback if $oldAutoCommit;
196       return "can't send credit deletion notification: $error";
197     }
198
199   }
200
201   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
202
203   '';
204
205 }
206
207 =item replace OLD_RECORD
208
209 You can, but probably shouldn't modify credits... 
210
211 =cut
212
213 sub replace {
214   #return "Can't modify credit!"
215   my $self = shift;
216   return "Can't modify closed credit" if $self->closed =~ /^Y/i;
217   $self->SUPER::replace(@_);
218 }
219
220 =item check
221
222 Checks all fields to make sure this is a valid credit.  If there is an error,
223 returns the error, otherwise returns false.  Called by the insert and replace
224 methods.
225
226 =cut
227
228 sub check {
229   my $self = shift;
230
231   my $error =
232     $self->ut_numbern('crednum')
233     || $self->ut_number('custnum')
234     || $self->ut_numbern('_date')
235     || $self->ut_money('amount')
236     || $self->ut_textn('reason')
237     || $self->ut_enum('closed', [ '', 'Y' ])
238   ;
239   return $error if $error;
240
241   return "amount must be > 0 " if $self->amount <= 0;
242
243   return "Unknown customer"
244     unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
245
246   $self->_date(time) unless $self->_date;
247
248   $self->otaker(getotaker);
249
250   $self->SUPER::check;
251 }
252
253 =item cust_refund
254
255 Depreciated.  See the cust_credit_refund method.
256
257 #Returns all refunds (see L<FS::cust_refund>) for this credit.
258
259 =cut
260
261 sub cust_refund {
262   use Carp;
263   croak "FS::cust_credit->cust_pay depreciated; see ".
264         "FS::cust_credit->cust_credit_refund";
265   #my $self = shift;
266   #sort { $a->_date <=> $b->_date }
267   #  qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
268   #;
269 }
270
271 =item cust_credit_refund
272
273 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
274
275 =cut
276
277 sub cust_credit_refund {
278   my $self = shift;
279   sort { $a->_date <=> $b->_date }
280     qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
281   ;
282 }
283
284 =item cust_credit_bill
285
286 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
287 credit.
288
289 =cut
290
291 sub cust_credit_bill {
292   my $self = shift;
293   sort { $a->_date <=> $b->_date }
294     qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
295   ;
296 }
297
298 =item credited
299
300 Returns the amount of this credit that is still outstanding; which is
301 amount minus all refund applications (see L<FS::cust_credit_refund>) and
302 applications to invoices (see L<FS::cust_credit_bill>).
303
304 =cut
305
306 sub credited {
307   my $self = shift;
308   my $amount = $self->amount;
309   $amount -= $_->amount foreach ( $self->cust_credit_refund );
310   $amount -= $_->amount foreach ( $self->cust_credit_bill );
311   sprintf( "%.2f", $amount );
312 }
313
314 =item cust_main
315
316 Returns the customer (see L<FS::cust_main>) for this credit.
317
318 =cut
319
320 sub cust_main {
321   my $self = shift;
322   qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
323 }
324
325
326 =back
327
328 =head1 BUGS
329
330 The delete method.  The replace method.
331
332 =head1 SEE ALSO
333
334 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
335 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base
336 documentation.
337
338 =cut
339
340 1;
341