X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_credit.pm;h=76fdecbdf9e77b426375a48c73f2180dfe24f462;hb=251d07aa41b6830a0a2f2a51c14fa94586d843c2;hp=dde847a56eeadd9abb95d8064a384fee09631645;hpb=908a4522939c0225ef4e7ba4236dab2eebe9daca;p=freeside.git diff --git a/FS/FS/cust_credit.pm b/FS/FS/cust_credit.pm index dde847a56..76fdecbdf 100644 --- a/FS/FS/cust_credit.pm +++ b/FS/FS/cust_credit.pm @@ -132,11 +132,27 @@ sub cust_unlinked_msg { ' (cust_credit.crednum '. $self->crednum. ')'; } -=item insert +=item insert [ OPTION => VALUE ... ] Adds this credit to the database ("Posts" the credit). If there is an error, returns the error, otherwise returns false. +Ooptions are passed as a list of keys and values. Available options: + +=over 4 + +=item reason_type + +L type for newly-inserted reason + +=item cust_credit_source_bill_pkg + +An arrayref of +L objects. +They will have their crednum set and will be inserted along with this credit. + +=back + =cut sub insert { @@ -156,16 +172,23 @@ sub insert { my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } ); my $old_balance = $cust_main->balance; - unless ($self->reasonnum) { - my $result = $self->reason( $self->getfield('reason'), - exists($options{ 'reason_type' }) - ? ('reason_type' => $options{ 'reason_type' }) - : (), - ); - unless($result) { + if (!$self->reasonnum) { + my $reason_text = $self->get('reason') + or return "reason text or existing reason required"; + my $reason_type = $options{'reason_type'} + or return "reason type required"; + + local $@; + my $reason = FS::reason->new_or_existing( + reason => $reason_text, + type => $reason_type, + class => 'R', + ); + if ($@) { $dbh->rollback if $oldAutoCommit; - return "failed to set reason for $me"; #: ". $dbh->errstr; + return "failed to set credit reason: $@"; } + $self->set('reasonnum', $reason->reasonnum); } $self->setfield('reason', ''); @@ -259,7 +282,7 @@ sub delete { my $cust_main = $self->cust_main; my $error = send_email( - 'from' => $conf->config('invoice_from', $self->cust_main->agentnum), + 'from' => $conf->invoice_from_full($self->cust_main->agentnum), #invoice_from??? well as good as any 'to' => $conf->config('deletecredits'), 'subject' => 'FREESIDE NOTIFICATION: Credit deleted', @@ -359,13 +382,18 @@ adds a record of the voided credit to the cust_credit_void table. =cut -# yes, false laziness with cust_pay and cust_bill -# but frankly I don't have time to fix it now - sub void { my $self = shift; my $reason = shift; + unless (ref($reason) || !$reason) { + $reason = FS::reason->new_or_existing( + 'class' => 'X', + 'type' => 'Void credit', + 'reason' => $reason + ); + } + local $SIG{HUP} = 'IGNORE'; local $SIG{INT} = 'IGNORE'; local $SIG{QUIT} = 'IGNORE'; @@ -380,7 +408,7 @@ sub void { my $cust_credit_void = new FS::cust_credit_void ( { map { $_ => $self->get($_) } $self->fields } ); - $cust_credit_void->set('void_reason', $reason); + $cust_credit_void->set('void_reasonnum', $reason->reasonnum); my $error = $cust_credit_void->insert; if ( $error ) { $dbh->rollback if $oldAutoCommit;