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