4 use vars qw( @ISA $DEBUG $me );
6 use DBIx::DBSchema::Table;
7 use DBIx::DBSchema::Column;
8 use FS::Record qw( qsearch qsearchs dbh dbdef );
11 @ISA = qw(FS::Record);
17 FS::reason - Object methods for reason records
23 $record = new FS::reason \%hash;
24 $record = new FS::reason { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::reason object represents a reason message. FS::reason inherits from
37 FS::Record. The following fields are currently supported:
41 =item reasonnum - primary key
43 =item reason_type - index into FS::reason_type
45 =item reason - text of the reason
47 =item disabled - 'Y' or ''
49 =item unsuspend_pkgpart - for suspension reasons only, the pkgpart (see
50 L<FS::part_pkg>) of a package to be ordered when the package is unsuspended.
51 Typically this will be some kind of reactivation fee. Attaching it to
52 a suspension reason allows the reactivation fee to be charged for some
53 suspensions but not others.
55 =item unsuspend_hold - 'Y' or ''. If unsuspend_pkgpart is set, this tells
56 whether to bill the unsuspend package immediately ('') or to wait until
57 the customer's next invoice ('Y').
67 Creates a new reason. To add the example to the database, see L<"insert">.
69 Note that this stores the hash reference, not a distinct copy of the hash it
70 points to. You can ask the object for a copy with the I<hash> method.
74 sub table { 'reason'; }
78 Adds this record to the database. If there is an error, returns the error,
79 otherwise returns false.
85 Delete this record from the database.
89 =item replace OLD_RECORD
91 Replaces the OLD_RECORD with this one in the database. If there is an error,
92 returns the error, otherwise returns false.
98 Checks all fields to make sure this is a valid reason. If there is
99 an error, returns the error, otherwise returns false. Called by the insert
108 $self->ut_numbern('reasonnum')
109 || $self->ut_number('reason_type')
110 || $self->ut_foreign_key('reason_type', 'reason_type', 'typenum')
111 || $self->ut_text('reason')
112 || $self->ut_flag('disabled')
114 return $error if $error;
116 if ( $self->reasontype->class eq 'S' ) {
117 $error = $self->ut_numbern('unsuspend_pkgpart')
118 || $self->ut_foreign_keyn('unsuspend_pkgpart', 'part_pkg', 'pkgpart')
119 || $self->ut_flag('unsuspend_hold')
121 return $error if $error;
123 $self->set('unsuspend_pkgpart' => '');
124 $self->set('unsuspend_hold' => '');
132 Returns the reason_type (see L<FS::reason_type>) associated with this reason.
137 qsearchs( 'reason_type', { 'typenum' => shift->reason_type } );
146 =item new_or_existing reason => REASON, type => TYPE, class => CLASS
148 Fetches the reason matching these parameters if there is one. If not,
149 inserts one. Will also insert the reason type if necessary. CLASS must
150 be one of 'C' (cancel reasons), 'R' (credit reasons), or 'S' (suspend reasons).
152 This will die if anything fails.
156 sub new_or_existing {
161 my %hash = ('class' => $opt{'class'}, 'type' => $opt{'type'});
162 my $reason_type = qsearchs('reason_type', \%hash)
163 || FS::reason_type->new(\%hash);
165 $error = $reason_type->insert unless $reason_type->typenum;
166 die "error inserting reason type: $error\n" if $error;
168 %hash = ('reason_type' => $reason_type->typenum, 'reason' => $opt{'reason'});
169 my $reason = qsearchs('reason', \%hash)
170 || FS::reason->new(\%hash);
172 $error = $reason->insert unless $reason->reasonnum;
173 die "error inserting reason: $error\n" if $error;
181 Here by termintes. Don't use on wooden computers.
185 L<FS::Record>, schema.html from the base documentation.