add svc_external
[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   $self->SUPER::delete(@_);
134 }
135
136 =item replace OLD_RECORD
137
138 Credits may not be modified; there would then be no record the credit was ever
139 posted.
140
141 =cut
142
143 sub replace {
144   #return "Can't modify credit!"
145   my $self = shift;
146   return "Can't modify closed credit" if $self->closed =~ /^Y/i;
147   $self->SUPER::replace(@_);
148 }
149
150 =item check
151
152 Checks all fields to make sure this is a valid credit.  If there is an error,
153 returns the error, otherwise returns false.  Called by the insert and replace
154 methods.
155
156 =cut
157
158 sub check {
159   my $self = shift;
160
161   my $error =
162     $self->ut_numbern('crednum')
163     || $self->ut_number('custnum')
164     || $self->ut_numbern('_date')
165     || $self->ut_money('amount')
166     || $self->ut_textn('reason')
167     || $self->ut_enum('closed', [ '', 'Y' ])
168   ;
169   return $error if $error;
170
171   return "amount must be > 0 " if $self->amount <= 0;
172
173   return "Unknown customer"
174     unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
175
176   $self->_date(time) unless $self->_date;
177
178   $self->otaker(getotaker);
179
180   $self->SUPER::check;
181 }
182
183 =item cust_refund
184
185 Depreciated.  See the cust_credit_refund method.
186
187 #Returns all refunds (see L<FS::cust_refund>) for this credit.
188
189 =cut
190
191 sub cust_refund {
192   use Carp;
193   croak "FS::cust_credit->cust_pay depreciated; see ".
194         "FS::cust_credit->cust_credit_refund";
195   #my $self = shift;
196   #sort { $a->_date <=> $b->_date }
197   #  qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
198   #;
199 }
200
201 =item cust_credit_refund
202
203 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
204
205 =cut
206
207 sub cust_credit_refund {
208   my $self = shift;
209   sort { $a->_date <=> $b->_date }
210     qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
211   ;
212 }
213
214 =item cust_credit_bill
215
216 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
217 credit.
218
219 =cut
220
221 sub cust_credit_bill {
222   my $self = shift;
223   sort { $a->_date <=> $b->_date }
224     qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
225   ;
226 }
227
228 =item credited
229
230 Returns the amount of this credit that is still outstanding; which is
231 amount minus all refund applications (see L<FS::cust_credit_refund>) and
232 applications to invoices (see L<FS::cust_credit_bill>).
233
234 =cut
235
236 sub credited {
237   my $self = shift;
238   my $amount = $self->amount;
239   $amount -= $_->amount foreach ( $self->cust_credit_refund );
240   $amount -= $_->amount foreach ( $self->cust_credit_bill );
241   sprintf( "%.2f", $amount );
242 }
243
244 =back
245
246 =head1 BUGS
247
248 The delete method.  The replace method.
249
250 =head1 SEE ALSO
251
252 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
253 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base
254 documentation.
255
256 =cut
257
258 1;
259