1 package FS::cust_credit;
4 use vars qw( @ISA $conf $unsuspendauto );
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;
12 use FS::cust_credit_bill;
14 @ISA = qw( FS::cust_main_Mixin FS::Record );
16 #ask FS::UID to run this stuff for us later
17 $FS::UID::callback{'FS::cust_credit'} = sub {
20 $unsuspendauto = $conf->exists('unsuspendauto');
26 FS::cust_credit - Object methods for cust_credit records
32 $record = new FS::cust_credit \%hash;
33 $record = new FS::cust_credit { 'column' => 'value' };
35 $error = $record->insert;
37 $error = $new_record->replace($old_record);
39 $error = $record->delete;
41 $error = $record->check;
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:
51 =item crednum - primary key (assigned automatically for new credits)
53 =item custnum - customer (see L<FS::cust_main>)
55 =item amount - amount of the credit
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.
60 =item otaker - order taker (assigned automatically, see L<FS::UID>)
64 =item closed - books closed flag, empty or `Y'
74 Creates a new credit. To add the credit to the database, see L<"insert">.
78 sub table { 'cust_credit'; }
79 sub cust_linked { $_[0]->cust_main_custnum; }
80 sub cust_unlinked_msg {
82 "WARNING: can't find cust_main.custnum ". $self->custnum.
83 ' (cust_credit.crednum '. $self->crednum. ')';
88 Adds this credit to the database ("Posts" the credit). If there is an error,
89 returns the error, otherwise returns false.
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';
103 my $oldAutoCommit = $FS::UID::AutoCommit;
104 local $FS::UID::AutoCommit = 0;
107 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
108 my $old_balance = $cust_main->balance;
110 my $error = $self->SUPER::insert;
112 $dbh->rollback if $oldAutoCommit;
113 return "error inserting $self: $error";
116 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
118 #false laziness w/ cust_credit::insert
119 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
120 my @errors = $cust_main->unsuspend;
122 # side-fx with nested transactions? upstack rolls back?
123 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
129 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
137 Currently unimplemented.
143 return "Can't delete closed credit" if $self->closed =~ /^Y/i;
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';
152 my $oldAutoCommit = $FS::UID::AutoCommit;
153 local $FS::UID::AutoCommit = 0;
156 foreach my $cust_credit_bill ( $self->cust_credit_bill ) {
157 my $error = $cust_credit_bill->delete;
159 $dbh->rollback if $oldAutoCommit;
164 my $error = $self->SUPER::delete(@_);
166 $dbh->rollback if $oldAutoCommit;
170 if ( $conf->config('deletecredits') ne '' ) {
172 my $cust_main = qsearchs('cust_main',{ 'custnum' => $self->custnum });
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',
179 "This is an automatic message from your Freeside installation\n",
180 "informing you that the following credit has been deleted:\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",
192 $dbh->rollback if $oldAutoCommit;
193 return "can't send credit deletion notification: $error";
198 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
204 =item replace OLD_RECORD
206 Credits may not be modified; there would then be no record the credit was ever
212 #return "Can't modify credit!"
214 return "Can't modify closed credit" if $self->closed =~ /^Y/i;
215 $self->SUPER::replace(@_);
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
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' ])
237 return $error if $error;
239 return "amount must be > 0 " if $self->amount <= 0;
241 return "Unknown customer"
242 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
244 $self->_date(time) unless $self->_date;
246 $self->otaker(getotaker);
253 Depreciated. See the cust_credit_refund method.
255 #Returns all refunds (see L<FS::cust_refund>) for this credit.
261 croak "FS::cust_credit->cust_pay depreciated; see ".
262 "FS::cust_credit->cust_credit_refund";
264 #sort { $a->_date <=> $b->_date }
265 # qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
269 =item cust_credit_refund
271 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
275 sub cust_credit_refund {
277 sort { $a->_date <=> $b->_date }
278 qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
282 =item cust_credit_bill
284 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
289 sub cust_credit_bill {
291 sort { $a->_date <=> $b->_date }
292 qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
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>).
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 );
314 Returns the customer (see L<FS::cust_main>) for this credit.
320 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
328 The delete method. The replace method.
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