backoffice API: add new_customer, RT#22830
[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 # Used by FS::Setup to initialize a new database.
135 sub _populate_initial_data {  # class method
136   my ($self, %opts) = @_;
137
138   my $conf = new FS::Conf;
139
140   foreach ( keys %class_name ) {
141     my $object  = $self->new( {'class' => $_,
142                                'type' => ucfirst($class_name{$_}). ' Reason',
143                             } );
144     my $error   = $object->insert();
145     die "error inserting $self into database: $error\n"
146       if $error;
147   }
148
149   my $object = qsearchs('reason_type', { 'class' => 'R' });
150   die "can't find credit reason type just inserted!\n"
151     unless $object;
152
153   foreach ( keys %FS::cust_credit::reasontype_map ) {
154 #   my $object  = $self->new( {'class' => 'R',
155 #                              'type' => $FS::cust_credit::reasontype_map{$_},
156 #                           } );
157 #   my $error   = $object->insert();
158 #   die "error inserting $self into database: $error\n"
159 #     if $error;
160     $conf->set($_, $object->typenum);
161   }
162
163   '';
164
165 }
166
167 # Used by FS::Upgrade to migrate to a new database.
168 sub _upgrade_data {  # class method
169   my ($self, %opts) = @_;
170
171   foreach ( keys %class_name ) {
172     unless (scalar(qsearch('reason_type', { 'class' => $_ }))) {
173       my $object  = $self->new( {'class' => $_,
174                                  'type' => ucfirst($class_name{$_}),
175                               } );
176       my $error   = $object->insert();
177       die "error inserting $self into database: $error\n"
178         if $error;
179     }
180   }
181
182   '';
183
184 }
185
186 =back
187
188 =head1 BUGS
189
190 Here be termintes.  Don't use on wooden computers.
191
192 =head1 SEE ALSO
193
194 L<FS::Record>, schema.html from the base documentation.
195
196 =cut
197
198 1;
199