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