1 package FS::cust_credit;
4 use vars qw( @ISA $conf $unsuspendauto );
6 use FS::UID qw( dbh getotaker );
7 use FS::Record qw( qsearch qsearchs );
8 use FS::Misc qw(send_email);
11 use FS::cust_credit_bill;
13 @ISA = qw( FS::Record );
15 #ask FS::UID to run this stuff for us later
16 $FS::UID::callback{'FS::cust_credit'} = sub {
19 $unsuspendauto = $conf->exists('unsuspendauto');
25 FS::cust_credit - Object methods for cust_credit records
31 $record = new FS::cust_credit \%hash;
32 $record = new FS::cust_credit { 'column' => 'value' };
34 $error = $record->insert;
36 $error = $new_record->replace($old_record);
38 $error = $record->delete;
40 $error = $record->check;
44 An FS::cust_credit object represents a credit; the equivalent of a negative
45 B<cust_bill> record (see L<FS::cust_bill>). FS::cust_credit inherits from
46 FS::Record. The following fields are currently supported:
50 =item crednum - primary key (assigned automatically for new credits)
52 =item custnum - customer (see L<FS::cust_main>)
54 =item amount - amount of the credit
56 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
57 L<Time::Local> and L<Date::Parse> for conversion functions.
59 =item otaker - order taker (assigned automatically, see L<FS::UID>)
63 =item closed - books closed flag, empty or `Y'
73 Creates a new credit. To add the credit to the database, see L<"insert">.
77 sub table { 'cust_credit'; }
81 Adds this credit to the database ("Posts" the credit). If there is an error,
82 returns the error, otherwise returns false.
89 local $SIG{HUP} = 'IGNORE';
90 local $SIG{INT} = 'IGNORE';
91 local $SIG{QUIT} = 'IGNORE';
92 local $SIG{TERM} = 'IGNORE';
93 local $SIG{TSTP} = 'IGNORE';
94 local $SIG{PIPE} = 'IGNORE';
96 my $oldAutoCommit = $FS::UID::AutoCommit;
97 local $FS::UID::AutoCommit = 0;
100 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
101 my $old_balance = $cust_main->balance;
103 my $error = $self->SUPER::insert;
105 $dbh->rollback if $oldAutoCommit;
106 return "error inserting $self: $error";
109 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
111 #false laziness w/ cust_credit::insert
112 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
113 my @errors = $cust_main->unsuspend;
115 # side-fx with nested transactions? upstack rolls back?
116 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
122 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
130 Currently unimplemented.
136 return "Can't delete closed credit" if $self->closed =~ /^Y/i;
138 local $SIG{HUP} = 'IGNORE';
139 local $SIG{INT} = 'IGNORE';
140 local $SIG{QUIT} = 'IGNORE';
141 local $SIG{TERM} = 'IGNORE';
142 local $SIG{TSTP} = 'IGNORE';
143 local $SIG{PIPE} = 'IGNORE';
145 my $oldAutoCommit = $FS::UID::AutoCommit;
146 local $FS::UID::AutoCommit = 0;
149 foreach my $cust_credit_bill ( $self->cust_credit_bill ) {
150 my $error = $cust_credit_bill->delete;
152 $dbh->rollback if $oldAutoCommit;
157 my $error = $self->SUPER::delete(@_);
159 $dbh->rollback if $oldAutoCommit;
163 if ( $conf->config('deletecredits') ne '' ) {
165 my $cust_main = qsearchs('cust_main',{ 'custnum' => $self->custnum });
167 my $error = send_email(
168 'from' => $conf->config('invoice_from'), #??? well as good as any
169 'to' => $conf->config('deletecredits'),
170 'subject' => 'FREESIDE NOTIFICATION: Credit deleted',
172 "This is an automatic message from your Freeside installation\n",
173 "informing you that the following credit has been deleted:\n",
175 'crednum: '. $self->crednum. "\n",
176 'custnum: '. $self->custnum.
177 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
178 'amount: $'. sprintf("%.2f", $self->amount). "\n",
179 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
180 'reason: '. $self->reason. "\n",
185 $dbh->rollback if $oldAutoCommit;
186 return "can't send credit deletion notification: $error";
191 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
197 =item replace OLD_RECORD
199 Credits may not be modified; there would then be no record the credit was ever
205 #return "Can't modify credit!"
207 return "Can't modify closed credit" if $self->closed =~ /^Y/i;
208 $self->SUPER::replace(@_);
213 Checks all fields to make sure this is a valid credit. If there is an error,
214 returns the error, otherwise returns false. Called by the insert and replace
223 $self->ut_numbern('crednum')
224 || $self->ut_number('custnum')
225 || $self->ut_numbern('_date')
226 || $self->ut_money('amount')
227 || $self->ut_textn('reason')
228 || $self->ut_enum('closed', [ '', 'Y' ])
230 return $error if $error;
232 return "amount must be > 0 " if $self->amount <= 0;
234 return "Unknown customer"
235 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
237 $self->_date(time) unless $self->_date;
239 $self->otaker(getotaker);
246 Depreciated. See the cust_credit_refund method.
248 #Returns all refunds (see L<FS::cust_refund>) for this credit.
254 croak "FS::cust_credit->cust_pay depreciated; see ".
255 "FS::cust_credit->cust_credit_refund";
257 #sort { $a->_date <=> $b->_date }
258 # qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
262 =item cust_credit_refund
264 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
268 sub cust_credit_refund {
270 sort { $a->_date <=> $b->_date }
271 qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
275 =item cust_credit_bill
277 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
282 sub cust_credit_bill {
284 sort { $a->_date <=> $b->_date }
285 qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
291 Returns the amount of this credit that is still outstanding; which is
292 amount minus all refund applications (see L<FS::cust_credit_refund>) and
293 applications to invoices (see L<FS::cust_credit_bill>).
299 my $amount = $self->amount;
300 $amount -= $_->amount foreach ( $self->cust_credit_refund );
301 $amount -= $_->amount foreach ( $self->cust_credit_bill );
302 sprintf( "%.2f", $amount );
307 Returns the customer (see L<FS::cust_main>) for this credit.
313 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
321 The delete method. The replace method.
325 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
326 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base