1 package FS::cust_pkg_reason;
5 use FS::Record qw( qsearch qsearchs );
11 FS::cust_pkg_reason - Object methods for cust_pkg_reason records
15 use FS::cust_pkg_reason;
17 $record = new FS::cust_pkg_reason \%hash;
18 $record = new FS::cust_pkg_reason { 'column' => 'value' };
20 $error = $record->insert;
22 $error = $new_record->replace($old_record);
24 $error = $record->delete;
26 $error = $record->check;
30 An FS::cust_pkg_reason object represents a relationship between a cust_pkg
31 and a reason, for example cancellation or suspension reasons.
32 FS::cust_pkg_reason inherits from FS::Record. The following fields are
37 =item num - primary key
56 Creates a new cust_pkg_reason. To add the example to the database, see
59 Note that this stores the hash reference, not a distinct copy of the hash it
60 points to. You can ask the object for a copy with the I<hash> method.
64 sub table { 'cust_pkg_reason'; }
68 Adds this record to the database. If there is an error, returns the error,
69 otherwise returns false.
75 Delete this record from the database.
79 =item replace OLD_RECORD
81 Replaces the OLD_RECORD with this one in the database. If there is an error,
82 returns the error, otherwise returns false.
88 Checks all fields to make sure this is a valid cust_pkg_reason. If there is
89 an error, returns the error, otherwise returns false. Called by the insert
98 $self->ut_numbern('num')
99 || $self->ut_number('pkgnum')
100 || $self->ut_number('reasonnum')
101 || $self->ut_enum('action', [ 'A', 'C', 'E', 'S' ])
102 || $self->ut_text('otaker')
103 || $self->ut_numbern('date')
105 return $error if $error;
112 Returns the reason (see L<FS::reason>) associated with this cust_pkg_reason.
118 qsearchs( 'reason', { 'reasonnum' => $self->reasonnum } );
123 Returns the text of the reason (see L<FS::reason>) associated with this
129 my $reason = shift->reason;
130 $reason ? $reason->reason : '';
135 # Used by FS::Upgrade to migrate to a new database.
137 sub _upgrade_data { # class method
138 my ($class, %opts) = @_;
140 my $test_cust_pkg_reason = new FS::cust_pkg_reason;
141 return '' unless $test_cust_pkg_reason->dbdef_table->column('action');
144 my @unmigrated = qsearch('cust_pkg_reason', { 'action' => '' } );
145 foreach ( @unmigrated ) {
146 # we could create h_cust_pkg_reason and h_cust_pkg_reason packages
147 @FS::h_cust_pkg::ISA = qw( FS::h_Common FS::cust_pkg );
148 sub FS::h_cust_pkg::table { 'h_cust_pkg' };
149 @FS::h_cust_pkg_reason::ISA = qw( FS::h_Common FS::cust_pkg_reason );
150 sub FS::h_cust_pkg_reason::table { 'h_cust_pkg_reason' };
152 my @history_cust_pkg_reason = qsearch( 'h_cust_pkg_reason', { $_->hash } );
154 next unless scalar(@history_cust_pkg_reason) == 1;
156 my %action_value = ( op => 'LIKE',
157 value => 'replace_%',
159 my $hashref = { pkgnum => $_->pkgnum,
160 history_date => $history_cust_pkg_reason[0]->history_date,
161 history_action => { %action_value },
164 my @history = qsearch({ table => 'h_cust_pkg',
166 order_by => 'ORDER BY history_action',
170 $hashref->{history_date}++; # more fuzz?
171 $hashref->{history_action} = { %action_value }; # qsearch distorts this!
172 push @history, qsearch({ table => 'h_cust_pkg',
174 order_by => 'ORDER BY history_action',
178 next unless scalar(@history) == 2;
180 my @new = grep { $_->history_action eq 'replace_new' } @history;
181 my @old = grep { $_->history_action eq 'replace_old' } @history;
183 next if (scalar(@new) == 2 || scalar(@old) == 2);
185 if ( !$old[0]->get('cancel') && $new[0]->get('cancel') ) {
187 }elsif( !$old[0]->susp && $new[0]->susp ){
189 }elsif( $new[0]->expire &&
190 (!$old[0]->expire || !$old[0]->expire != $new[0]->expire )
193 }elsif( $new[0]->adjourn &&
194 (!$old[0]->adjourn || $old[0]->adjourn != $new[0]->adjourn )
199 my $error = $_->replace
202 die $error if $error;
207 #remove nullability if scalar(@migrated) - $count == 0 && ->column('action');
217 Here be termites. Don't use on wooden computers.
221 L<FS::Record>, schema.html from the base documentation.