1 package FS::reason_Mixin;
4 use Carp qw( croak ); #confess );
5 use FS::Record qw( qsearch qsearchs dbdef );
11 our $me = '[FS::reason_Mixin]';
15 Returns the text of the associated reason (see L<FS::reason>) for this credit.
20 my ($self, $value, %options) = @_;
22 if ( $self->reasonnum ) {
23 my $reason = FS::reason->by_key($self->reasonnum);
24 $reason_text = $reason->reason;
25 } else { # in case one of these somehow still exists
26 $reason_text = $self->get('reason');
28 if ( $self->get('addlinfo') ) {
29 $reason_text .= ' ' . $self->get('addlinfo');
35 # it was a mistake to allow setting the reason this way; use
36 # FS::reason->new_or_existing
38 # Used by FS::Upgrade to migrate reason text fields to reasonnum.
39 sub _upgrade_reasonnum { # class method
41 my $table = $class->table;
43 if (defined dbdef->table($table)->column('reason')) {
45 warn "$me Checking for unmigrated reasons\n" if $DEBUG;
47 my @cust_refunds = qsearch({ 'table' => $table,
49 'extra_sql' => 'WHERE reason IS NOT NULL',
52 if (scalar(grep { $_->getfield('reason') =~ /\S/ } @cust_refunds)) {
53 warn "$me Found unmigrated reasons\n" if $DEBUG;
54 my $hashref = { 'class' => 'F', 'type' => 'Legacy' };
55 my $reason_type = qsearchs( 'reason_type', $hashref );
56 unless ($reason_type) {
57 $reason_type = new FS::reason_type( $hashref );
58 my $error = $reason_type->insert();
59 die "$class had error inserting FS::reason_type into database: $error\n"
63 $hashref = { 'reason_type' => $reason_type->typenum,
66 my $noreason = qsearchs( 'reason', $hashref );
68 $hashref->{'disabled'} = 'Y';
69 $noreason = new FS::reason( $hashref );
70 my $error = $noreason->insert();
71 die "can't insert legacy reason '(none)' into database: $error\n"
75 foreach my $cust_refund ( @cust_refunds ) {
76 my $reason = $cust_refund->getfield('reason');
77 warn "Contemplating reason $reason\n" if $DEBUG > 1;
78 if ($reason =~ /\S/) {
79 $cust_refund->reason($reason, 'reason_type' => $reason_type->typenum)
80 or die "can't insert legacy reason $reason into database\n";
82 $cust_refund->reasonnum($noreason->reasonnum);
85 $cust_refund->setfield('reason', '');
86 my $error = $cust_refund->replace;
88 warn "*** WARNING: error replacing reason in $class ".
89 $cust_refund->refundnum. ": $error ***\n"