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