X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_credit.pm;h=df2a6cc19d56e82bcda2ea858e2032f38af7702b;hb=3ece1c396f23e04cd659f1085f6eeee8bdfd846f;hp=212be7a37024c0b16bbb49cb624f96ea60d90e98;hpb=8dd41f364aaba88969dfd0908feb22709025e7f6;p=freeside.git diff --git a/FS/FS/cust_credit.pm b/FS/FS/cust_credit.pm index 212be7a37..df2a6cc19 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', ''); @@ -176,6 +199,17 @@ sub insert { return "error inserting $self: $error"; } + if ( $options{'cust_credit_source_bill_pkg'} ) { + foreach my $ccsbr ( @{ $options{'cust_credit_source_bill_pkg'} } ) { + $ccsbr->crednum( $self->crednum ); + $error = $ccsbr->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "error inserting $ccsbr: $error"; + } + } + } + $dbh->commit or die $dbh->errstr if $oldAutoCommit; #false laziness w/ cust_pay::insert