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