1 package FS::cust_credit;
4 use vars qw( @ISA $conf $unsuspendauto );
5 use FS::UID qw( dbh getotaker );
6 use FS::Record qw( qsearch qsearchs );
9 use FS::cust_credit_bill;
11 @ISA = qw( FS::Record );
13 #ask FS::UID to run this stuff for us later
14 $FS::UID::callback{'FS::cust_credit'} = sub {
17 $unsuspendauto = $conf->exists('unsuspendauto');
23 FS::cust_credit - Object methods for cust_credit records
29 $record = new FS::cust_credit \%hash;
30 $record = new FS::cust_credit { 'column' => 'value' };
32 $error = $record->insert;
34 $error = $new_record->replace($old_record);
36 $error = $record->delete;
38 $error = $record->check;
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:
48 =item crednum - primary key (assigned automatically for new credits)
50 =item custnum - customer (see L<FS::cust_main>)
52 =item amount - amount of the credit
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.
57 =item otaker - order taker (assigned automatically, see L<FS::UID>)
61 =item closed - books closed flag, empty or `Y'
71 Creates a new credit. To add the credit to the database, see L<"insert">.
75 sub table { 'cust_credit'; }
79 Adds this credit to the database ("Posts" the credit). If there is an error,
80 returns the error, otherwise returns false.
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';
94 my $oldAutoCommit = $FS::UID::AutoCommit;
95 local $FS::UID::AutoCommit = 0;
98 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
99 my $old_balance = $cust_main->balance;
101 my $error = $self->SUPER::insert;
103 $dbh->rollback if $oldAutoCommit;
104 return "error inserting $self: $error";
107 #false laziness w/ cust_credit::insert
108 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
109 my @errors = $cust_main->unsuspend;
111 # side-fx with nested transactions? upstack rolls back?
112 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
118 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
126 Currently unimplemented.
132 return "Can't delete closed credit" if $self->closed =~ /^Y/i;
134 local $SIG{HUP} = 'IGNORE';
135 local $SIG{INT} = 'IGNORE';
136 local $SIG{QUIT} = 'IGNORE';
137 local $SIG{TERM} = 'IGNORE';
138 local $SIG{TSTP} = 'IGNORE';
139 local $SIG{PIPE} = 'IGNORE';
141 my $oldAutoCommit = $FS::UID::AutoCommit;
142 local $FS::UID::AutoCommit = 0;
145 foreach my $cust_credit_bill ( $self->cust_credit_bill ) {
146 my $error = $cust_credit_bill->delete;
148 $dbh->rollback if $oldAutoCommit;
153 my $error = $self->SUPER::delete(@_);
155 $dbh->rollback if $oldAutoCommit;
159 if ( $conf->config('deletecredits') ne '' ) {
161 my $cust_main = qsearchs('cust_main',{ 'custnum' => $self->custnum });
163 my $error = send_email(
164 'from' => $conf->config('invoice_from'), #??? well as good as any
165 'to' => $conf->config('deletecredits'),
166 'subject' => 'FREESIDE NOTIFICATION: Credit deleted',
168 "This is an automatic message from your Freeside installation\n",
169 "informing you that the following credit has been deleted:\n",
171 'crednum: '. $self->crednum. "\n",
172 'custnum: '. $self->custnum.
173 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
174 'amount: $'. sprintf("%.2f", $self->amount). "\n",
175 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
176 'reason: '. $self->reason. "\n",
181 $dbh->rollback if $oldAutoCommit;
182 return "can't send credit deletion notification: $error";
187 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
193 =item replace OLD_RECORD
195 Credits may not be modified; there would then be no record the credit was ever
201 #return "Can't modify credit!"
203 return "Can't modify closed credit" if $self->closed =~ /^Y/i;
204 $self->SUPER::replace(@_);
209 Checks all fields to make sure this is a valid credit. If there is an error,
210 returns the error, otherwise returns false. Called by the insert and replace
219 $self->ut_numbern('crednum')
220 || $self->ut_number('custnum')
221 || $self->ut_numbern('_date')
222 || $self->ut_money('amount')
223 || $self->ut_textn('reason')
224 || $self->ut_enum('closed', [ '', 'Y' ])
226 return $error if $error;
228 return "amount must be > 0 " if $self->amount <= 0;
230 return "Unknown customer"
231 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
233 $self->_date(time) unless $self->_date;
235 $self->otaker(getotaker);
242 Depreciated. See the cust_credit_refund method.
244 #Returns all refunds (see L<FS::cust_refund>) for this credit.
250 croak "FS::cust_credit->cust_pay depreciated; see ".
251 "FS::cust_credit->cust_credit_refund";
253 #sort { $a->_date <=> $b->_date }
254 # qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
258 =item cust_credit_refund
260 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
264 sub cust_credit_refund {
266 sort { $a->_date <=> $b->_date }
267 qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
271 =item cust_credit_bill
273 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
278 sub cust_credit_bill {
280 sort { $a->_date <=> $b->_date }
281 qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
287 Returns the amount of this credit that is still outstanding; which is
288 amount minus all refund applications (see L<FS::cust_credit_refund>) and
289 applications to invoices (see L<FS::cust_credit_bill>).
295 my $amount = $self->amount;
296 $amount -= $_->amount foreach ( $self->cust_credit_refund );
297 $amount -= $_->amount foreach ( $self->cust_credit_bill );
298 sprintf( "%.2f", $amount );
305 The delete method. The replace method.
309 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
310 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base