unsnarl creation of credit/refund reasons, partial fallout from #31702
[freeside.git] / FS / FS / reason_Mixin.pm
1 package FS::reason_Mixin;
2
3 use strict;
4 use Carp qw( croak ); #confess );
5 use FS::Record qw( qsearch qsearchs dbdef );
6 use FS::access_user;
7 use FS::UID qw( dbh );
8 use FS::reason;
9
10 our $DEBUG = 0;
11 our $me = '[FS::reason_Mixin]';
12
13 =item reason
14
15 Returns the text of the associated reason (see L<FS::reason>) for this credit.
16
17 =cut
18
19 sub reason {
20   my ($self, $value, %options) = @_;
21   my $reason_text;
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');
27   }
28   if ( $self->get('addlinfo') ) {
29     $reason_text .= ' ' . $self->get('addlinfo');
30   }
31
32   return $reason_text;
33 }
34
35 # it was a mistake to allow setting the reason this way; use 
36 # FS::reason->new_or_existing
37  
38 # Used by FS::Upgrade to migrate reason text fields to reasonnum.
39 sub _upgrade_reasonnum {  # class method
40   my $class = shift;
41   my $table = $class->table;
42
43   if (defined dbdef->table($table)->column('reason')) {
44
45     warn "$me Checking for unmigrated reasons\n" if $DEBUG;
46
47     my @cust_refunds = qsearch({ 'table'     => $table,
48                                  'hashref'   => {},
49                                  'extra_sql' => 'WHERE reason IS NOT NULL',
50                               });
51
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"
60           if $error;
61       }
62
63       $hashref = { 'reason_type' => $reason_type->typenum,
64                    'reason' => '(none)'
65                  };
66       my $noreason = qsearchs( 'reason', $hashref );
67       unless ($noreason) {
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"
72           if $error;
73       }
74
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";
81         }else{
82           $cust_refund->reasonnum($noreason->reasonnum);
83         }
84
85         $cust_refund->setfield('reason', '');
86         my $error = $cust_refund->replace;
87
88         warn "*** WARNING: error replacing reason in $class ".
89              $cust_refund->refundnum. ": $error ***\n"
90           if $error;
91       }
92     }
93   }
94 }
95
96 1;