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