eWay self-signup fixes
[freeside.git] / FS / FS / reason_type.pm
1 package FS::reason_type;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs );
6
7 @ISA = qw(FS::Record);
8
9 our %class_name = (  
10   'C' => 'cancel',
11   'R' => 'credit',
12   'S' => 'suspend',
13 );
14
15 our %class_purpose = (  
16   'C' => 'explain why a customer package was cancelled',
17   'R' => 'explain why a customer was credited',
18   'S' => 'explain why a customer package was suspended',
19 );
20
21 =head1 NAME
22
23 FS::reason_type - Object methods for reason_type records
24
25 =head1 SYNOPSIS
26
27   use FS::reason_type;
28
29   $record = new FS::reason_type \%hash;
30   $record = new FS::reason_type { 'column' => 'value' };
31
32   $error = $record->insert;
33
34   $error = $new_record->replace($old_record);
35
36   $error = $record->delete;
37
38   $error = $record->check;
39
40 =head1 DESCRIPTION
41
42 An FS::reason_type object represents a grouping of reasons.  FS::reason_type
43 inherits from FS::Record.  The following fields are currently supported:
44
45 =over 4
46
47 =item typenum - primary key
48
49 =item class - currently 'C', 'R',  or 'S' for cancel, credit, or suspend 
50
51 =item type - name of the type of reason
52
53
54 =back
55
56 =head1 METHODS
57
58 =over 4
59
60 =item new HASHREF
61
62 Creates a new reason_type.  To add the example to the database, see L<"insert">.
63
64 Note that this stores the hash reference, not a distinct copy of the hash it
65 points to.  You can ask the object for a copy with the I<hash> method.
66
67 =cut
68
69 sub table { 'reason_type'; }
70
71 =item insert
72
73 Adds this record to the database.  If there is an error, returns the error,
74 otherwise returns false.
75
76 =cut
77
78 =item delete
79
80 Delete this record from the database.
81
82 =cut
83
84 =item replace OLD_RECORD
85
86 Replaces the OLD_RECORD with this one in the database.  If there is an error,
87 returns the error, otherwise returns false.
88
89 =cut
90
91 =item check
92
93 Checks all fields to make sure this is a valid reason_type.  If there is
94 an error, returns the error, otherwise returns false.  Called by the insert
95 and replace methods.
96
97 =cut
98
99 sub check {
100   my $self = shift;
101
102   my $error = 
103     $self->ut_numbern('typenum')
104     || $self->ut_enum('class', [ keys %class_name ] )
105     || $self->ut_text('type')
106   ;
107   return $error if $error;
108
109   $self->SUPER::check;
110 }
111
112 =item reasons
113
114 Returns a list of all reasons associated with this type.
115
116 =cut
117
118 sub reasons {
119   qsearch( 'reason', { 'reason_type' => shift->typenum } );
120 }
121
122 =item enabled_reasons
123
124 Returns a list of enabled reasons associated with this type.
125
126 =cut
127
128 sub enabled_reasons {
129   qsearch( 'reason', { 'reason_type' => shift->typenum,
130                        'enabled'     => '',
131                      } );
132 }
133
134 # _populate_initial_data
135 #
136 # Used by FS::Setup to initialize a new database.
137 #
138 #
139
140 sub _populate_initial_data {  # class method
141   my ($self, %opts) = @_;
142
143   my $conf = new FS::Conf;
144
145   foreach ( keys %class_name ) {
146     my $object  = $self->new( {'class' => $_,
147                                'type' => ucfirst($class_name{$_}). ' Reason',
148                             } );
149     my $error   = $object->insert();
150     die "error inserting $self into database: $error\n"
151       if $error;
152   }
153
154   my $object = qsearchs('reason_type', { 'class' => 'R' });
155   die "can't find credit reason type just inserted!\n"
156     unless $object;
157
158   foreach ( keys %FS::cust_credit::reasontype_map ) {
159 #   my $object  = $self->new( {'class' => 'R',
160 #                              'type' => $FS::cust_credit::reasontype_map{$_},
161 #                           } );
162 #   my $error   = $object->insert();
163 #   die "error inserting $self into database: $error\n"
164 #     if $error;
165     $conf->set($_, $object->typenum);
166   }
167
168   '';
169
170 }
171
172 # _upgrade_data
173 #
174 # Used by FS::Upgrade to migrate to a new database.
175 #
176 #
177
178 sub _upgrade_data {  # class method
179   my ($self, %opts) = @_;
180
181   foreach ( keys %class_name ) {
182     unless (scalar(qsearch('reason_type', { 'class' => $_ }))) {
183       my $object  = $self->new( {'class' => $_,
184                                  'type' => ucfirst($class_name{$_}),
185                               } );
186       my $error   = $object->insert();
187       die "error inserting $self into database: $error\n"
188         if $error;
189     }
190   }
191
192   '';
193
194 }
195
196 =back
197
198 =head1 BUGS
199
200 Here be termintes.  Don't use on wooden computers.
201
202 =head1 SEE ALSO
203
204 L<FS::Record>, schema.html from the base documentation.
205
206 =cut
207
208 1;
209