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 #false laziness w/ cust_credit::insert
110 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
111 my @errors = $cust_main->unsuspend;
113 # side-fx with nested transactions? upstack rolls back?
114 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
120 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
128 Currently unimplemented.
134 return "Can't delete closed credit" if $self->closed =~ /^Y/i;
136 local $SIG{HUP} = 'IGNORE';
137 local $SIG{INT} = 'IGNORE';
138 local $SIG{QUIT} = 'IGNORE';
139 local $SIG{TERM} = 'IGNORE';
140 local $SIG{TSTP} = 'IGNORE';
141 local $SIG{PIPE} = 'IGNORE';
143 my $oldAutoCommit = $FS::UID::AutoCommit;
144 local $FS::UID::AutoCommit = 0;
147 foreach my $cust_credit_bill ( $self->cust_credit_bill ) {
148 my $error = $cust_credit_bill->delete;
150 $dbh->rollback if $oldAutoCommit;
155 my $error = $self->SUPER::delete(@_);
157 $dbh->rollback if $oldAutoCommit;
161 if ( $conf->config('deletecredits') ne '' ) {
163 my $cust_main = qsearchs('cust_main',{ 'custnum' => $self->custnum });
165 my $error = send_email(
166 'from' => $conf->config('invoice_from'), #??? well as good as any
167 'to' => $conf->config('deletecredits'),
168 'subject' => 'FREESIDE NOTIFICATION: Credit deleted',
170 "This is an automatic message from your Freeside installation\n",
171 "informing you that the following credit has been deleted:\n",
173 'crednum: '. $self->crednum. "\n",
174 'custnum: '. $self->custnum.
175 " (". $cust_main->last. ", ". $cust_main->first. ")\n",
176 'amount: $'. sprintf("%.2f", $self->amount). "\n",
177 'date: '. time2str("%a %b %e %T %Y", $self->_date). "\n",
178 'reason: '. $self->reason. "\n",
183 $dbh->rollback if $oldAutoCommit;
184 return "can't send credit deletion notification: $error";
189 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
195 =item replace OLD_RECORD
197 Credits may not be modified; there would then be no record the credit was ever
203 #return "Can't modify credit!"
205 return "Can't modify closed credit" if $self->closed =~ /^Y/i;
206 $self->SUPER::replace(@_);
211 Checks all fields to make sure this is a valid credit. If there is an error,
212 returns the error, otherwise returns false. Called by the insert and replace
221 $self->ut_numbern('crednum')
222 || $self->ut_number('custnum')
223 || $self->ut_numbern('_date')
224 || $self->ut_money('amount')
225 || $self->ut_textn('reason')
226 || $self->ut_enum('closed', [ '', 'Y' ])
228 return $error if $error;
230 return "amount must be > 0 " if $self->amount <= 0;
232 return "Unknown customer"
233 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
235 $self->_date(time) unless $self->_date;
237 $self->otaker(getotaker);
244 Depreciated. See the cust_credit_refund method.
246 #Returns all refunds (see L<FS::cust_refund>) for this credit.
252 croak "FS::cust_credit->cust_pay depreciated; see ".
253 "FS::cust_credit->cust_credit_refund";
255 #sort { $a->_date <=> $b->_date }
256 # qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
260 =item cust_credit_refund
262 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
266 sub cust_credit_refund {
268 sort { $a->_date <=> $b->_date }
269 qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
273 =item cust_credit_bill
275 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
280 sub cust_credit_bill {
282 sort { $a->_date <=> $b->_date }
283 qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
289 Returns the amount of this credit that is still outstanding; which is
290 amount minus all refund applications (see L<FS::cust_credit_refund>) and
291 applications to invoices (see L<FS::cust_credit_bill>).
297 my $amount = $self->amount;
298 $amount -= $_->amount foreach ( $self->cust_credit_refund );
299 $amount -= $_->amount foreach ( $self->cust_credit_bill );
300 sprintf( "%.2f", $amount );
305 Returns the customer (see L<FS::cust_main>) for this credit.
311 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
319 The delete method. The replace method.
323 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
324 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base