1 package FS::reason_Mixin;
4 use Carp qw( croak ); #confess );
5 use FS::Record qw( qsearch qsearchs dbdef );
12 our $me = '[FS::reason_Mixin]';
16 Returns the text of the associated reason (see L<FS::reason>) for this credit /
17 voided payment / voided invoice. This can no longer be used to set the
18 (deprecated) free-text "reason" field; see L<FS::reason/new_or_existing>.
26 if ( $self->reasonnum ) {
27 my $reason = FS::reason->by_key($self->reasonnum);
28 $reason_text = $reason->reason;
29 } else { # in case one of these somehow still exists
30 $reason_text = $self->get('reason');
32 if ( $self->get('addlinfo') ) {
33 $reason_text .= ' ' . $self->get('addlinfo');
39 # Used by FS::Upgrade to migrate reason text fields to reasonnum.
40 # Note that any new tables that get reasonnum fields do NOT need to be
41 # added here unless they have previously had a free-text "reason" field.
43 sub _upgrade_reasonnum { # class method
45 my $table = $class->table;
48 if ( $table =~ /^cust_bill/ ) { # also includes cust_bill_pkg
50 } elsif ( $table =~ /^cust_pay/ ) {
52 } elsif ( $table eq 'cust_refund' ) {
54 } elsif ( $table =~ /^cust_credit/ ) {
57 die "don't know the reason class to use for upgrading $table";
60 for my $fieldname (qw(reason void_reason)) {
62 if ( $table =~ /^cust_credit/ and $fieldname eq 'void_reason' ) {
66 if ( defined dbdef->table($table)->column($fieldname)
67 && defined dbdef->table($table)->column( $fieldname . 'num' ) )
70 warn "$me Checking for unmigrated reasons\n" if $DEBUG;
72 my @legacy_reason_records = qsearch(
76 'extra_sql' => 'WHERE ' . $fieldname . ' IS NOT NULL',
80 if ( @legacy_reason_records ) {
82 warn "$me Found unmigrated reasons\n" if $DEBUG;
85 $class->_upgrade_get_legacy_reason_type( $reason_class );
86 # XXX "noreason" does not actually work, because we limited to
87 # "reason is not null" above. Records where the reason string
88 # is null will end up with a reasonnum of null also.
89 my $noreason = $class->_upgrade_get_no_reason( $reason_type );
91 foreach my $record_to_upgrade (@legacy_reason_records) {
92 my $reason = $record_to_upgrade->getfield($fieldname);
93 warn "Contemplating reason $reason\n" if $DEBUG > 1;
94 if ( $reason =~ /\S/ ) {
96 $class->_upgrade_get_reason( $reason, $reason_type );
97 $record_to_upgrade->set( $fieldname . 'num',
101 $record_to_upgrade->set( $fieldname . 'num',
102 $noreason->reasonnum );
105 $record_to_upgrade->setfield( $fieldname, '' );
106 my $error = $record_to_upgrade->replace;
108 my $primary_key = $record_to_upgrade->primary_key;
109 warn "*** WARNING: error replacing $fieldname in $class "
110 . $record_to_upgrade->get($primary_key)
119 # internal methods for upgrade
121 # _upgrade_get_legacy_reason_type is class method supposed to be used only
122 # within the reason_Mixin class which will either find or create a reason_type
123 sub _upgrade_get_legacy_reason_type {
126 my $reason_class = shift;
127 my $reason_type_params = { 'class' => $reason_class, 'type' => 'Legacy' };
128 my $reason_type = qsearchs( 'reason_type', $reason_type_params );
129 unless ($reason_type) {
130 $reason_type = new FS::reason_type($reason_type_params);
131 my $error = $reason_type->insert();
132 die "$class had error inserting FS::reason_type into database: $error\n"
138 # _upgrade_get_no_reason is class method supposed to be used only within the
139 # reason_Mixin class which will either find or create a default (no reason)
141 sub _upgrade_get_no_reason {
144 my $reason_type = shift;
145 return $class->_upgrade_get_reason( '(none)', $reason_type );
148 # _upgrade_get_reason is class method supposed to be used only within the
149 # reason_Mixin class which will either find or create a reason
150 sub _upgrade_get_reason {
153 my $reason_text = shift;
154 my $reason_type = shift;
156 my $reason_params = {
157 'reason_type' => $reason_type->typenum,
158 'reason' => $reason_text
160 my $reason = qsearchs( 'reason', $reason_params );
162 $reason_params->{'disabled'} = 'Y';
163 $reason = new FS::reason($reason_params);
164 my $error = $reason->insert();
165 die "can't insert legacy reason '$reason_text' into database: $error\n"