summaryrefslogtreecommitdiff
path: root/FS/FS/reason_Mixin.pm
diff options
context:
space:
mode:
authorMark Wells <mark@freeside.biz>2015-01-22 17:29:48 -0800
committerMark Wells <mark@freeside.biz>2015-01-22 17:29:48 -0800
commit2073798304acbd9402f73e0dee7507a7a4d22ceb (patch)
treec6cc427365553521fc6bcc2e9397905e69660147 /FS/FS/reason_Mixin.pm
parentc113c2681677d34974e9d63b631904770e25e3c0 (diff)
unsnarl creation of credit/refund reasons, partial fallout from #31702
Diffstat (limited to 'FS/FS/reason_Mixin.pm')
-rw-r--r--FS/FS/reason_Mixin.pm53
1 files changed, 14 insertions, 39 deletions
diff --git a/FS/FS/reason_Mixin.pm b/FS/FS/reason_Mixin.pm
index fdf7962..a397541 100644
--- a/FS/FS/reason_Mixin.pm
+++ b/FS/FS/reason_Mixin.pm
@@ -5,6 +5,7 @@ use Carp qw( croak ); #confess );
use FS::Record qw( qsearch qsearchs dbdef );
use FS::access_user;
use FS::UID qw( dbh );
+use FS::reason;
our $DEBUG = 0;
our $me = '[FS::reason_Mixin]';
@@ -17,49 +18,23 @@ Returns the text of the associated reason (see L<FS::reason>) for this credit.
sub reason {
my ($self, $value, %options) = @_;
- my $dbh = dbh;
- my $reason;
- my $typenum = $options{'reason_type'};
-
- my $oldAutoCommit = $FS::UID::AutoCommit; # this should already be in
- local $FS::UID::AutoCommit = 0; # a transaction if it matters
-
- if ( defined( $value ) ) {
- my $hashref = { 'reason' => $value };
- $hashref->{'reason_type'} = $typenum if $typenum;
- my $addl_from = "LEFT JOIN reason_type ON ( reason_type = typenum ) ";
- my $extra_sql = " AND reason_type.class='F'";
-
- $reason = qsearchs( { 'table' => 'reason',
- 'hashref' => $hashref,
- 'addl_from' => $addl_from,
- 'extra_sql' => $extra_sql,
- } );
-
- if (!$reason && $typenum) {
- $reason = new FS::reason( { 'reason_type' => $typenum,
- 'reason' => $value,
- 'disabled' => 'Y',
- } );
- my $error = $reason->insert;
- if ( $error ) {
- warn "error inserting reason: $error\n";
- $reason = undef;
- }
- }
-
- $self->reasonnum($reason ? $reason->reasonnum : '') ;
- warn "$me reason used in set mode with non-existant reason -- clearing"
- unless $reason;
+ my $reason_text;
+ if ( $self->reasonnum ) {
+ my $reason = FS::reason->by_key($self->reasonnum);
+ $reason_text = $reason->reason;
+ } else { # in case one of these somehow still exists
+ $reason_text = $self->get('reason');
+ }
+ if ( $self->get('addlinfo') ) {
+ $reason_text .= ' ' . $self->get('addlinfo');
}
- $reason = qsearchs( 'reason', { 'reasonnum' => $self->reasonnum } );
-
- $dbh->commit or die $dbh->errstr if $oldAutoCommit;
- ( $reason ? $reason->reason : '' ).
- ( $self->addlinfo ? ' '.$self->addlinfo : '' );
+ return $reason_text;
}
+# it was a mistake to allow setting the reason this way; use
+# FS::reason->new_or_existing
+
# Used by FS::Upgrade to migrate reason text fields to reasonnum.
sub _upgrade_reasonnum { # class method
my $class = shift;