maestro cust status as reqeusted, RT#8712
[freeside.git] / FS / FS / svc_pbx.pm
1 package FS::svc_pbx;
2
3 use strict;
4 use base qw( FS::svc_External_Common );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::cust_svc;
7 use FS::svc_phone;
8 use FS::svc_acct;
9
10 =head1 NAME
11
12 FS::svc_pbx - Object methods for svc_pbx records
13
14 =head1 SYNOPSIS
15
16   use FS::svc_pbx;
17
18   $record = new FS::svc_pbx \%hash;
19   $record = new FS::svc_pbx { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29   $error = $record->suspend;
30
31   $error = $record->unsuspend;
32
33   $error = $record->cancel;
34
35 =head1 DESCRIPTION
36
37 An FS::svc_pbx object represents a PBX tenant.  FS::svc_pbx inherits from
38 FS::svc_Common.  The following fields are currently supported:
39
40 =over 4
41
42 =item svcnum
43
44 Primary key (assigned automatcially for new accounts)
45
46 =item id
47
48 (Unique?) number of external record
49
50 =item title
51
52 PBX name
53
54 =item max_extensions
55
56 Maximum number of extensions
57
58 =item max_simultaneous
59
60 Maximum number of simultaneous users
61
62 =back
63
64 =head1 METHODS
65
66 =over 4
67
68 =item new HASHREF
69
70 Creates a new PBX tenant.  To add the PBX tenant to the database, see
71 L<"insert">.
72
73 Note that this stores the hash reference, not a distinct copy of the hash it
74 points to.  You can ask the object for a copy with the I<hash> method.
75
76 =cut
77
78 sub table { 'svc_pbx'; }
79
80 sub table_info {
81   {
82     'name' => 'PBX',
83     'name_plural' => 'PBXs', #optional,
84     'longname_plural' => 'PBXs', #optional
85     'sorts' => 'svcnum', # optional sort field (or arrayref of sort fields, main first)
86     'display_weight' => 70,
87     'cancel_weight'  => 90,
88     'fields' => {
89       'id'    => 'ID',
90       'title' => 'Name',
91       'max_extensions' => 'Maximum number of User Extensions',
92       'max_simultaneous' => 'Maximum number of simultaneous users',
93 #      'field'         => 'Description',
94 #      'another_field' => { 
95 #                           'label'     => 'Description',
96 #                          'def_label' => 'Description for service definitions',
97 #                          'type'      => 'text',
98 #                          'disable_default'   => 1, #disable switches
99 #                          'disable_fixed'     => 1, #
100 #                          'disable_inventory' => 1, #
101 #                        },
102 #      'foreign_key'   => { 
103 #                           'label'        => 'Description',
104 #                          'def_label'    => 'Description for service defs',
105 #                          'type'         => 'select',
106 #                          'select_table' => 'foreign_table',
107 #                          'select_key'   => 'key_field_in_table',
108 #                          'select_label' => 'label_field_in_table',
109 #                        },
110
111     },
112   };
113 }
114
115 =item search_sql STRING
116
117 Class method which returns an SQL fragment to search for the given string.
118
119 =cut
120
121 #XXX
122 #or something more complicated if necessary
123 #sub search_sql {
124 #  my($class, $string) = @_;
125 #  $class->search_sql_field('title', $string);
126 #}
127
128 =item label
129
130 Returns the title field for this PBX tenant.
131
132 =cut
133
134 sub label {
135   my $self = shift;
136   $self->title;
137 }
138
139 =item insert
140
141 Adds this record to the database.  If there is an error, returns the error,
142 otherwise returns false.
143
144 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be 
145 defined.  An FS::cust_svc record will be created and inserted.
146
147 =cut
148
149 sub insert {
150   my $self = shift;
151   my $error;
152
153   $error = $self->SUPER::insert;
154   return $error if $error;
155
156   '';
157 }
158
159 =item delete
160
161 Delete this record from the database.
162
163 =cut
164
165 sub delete {
166   my $self = shift;
167
168   local $SIG{HUP} = 'IGNORE';
169   local $SIG{INT} = 'IGNORE';
170   local $SIG{QUIT} = 'IGNORE';
171   local $SIG{TERM} = 'IGNORE';
172   local $SIG{TSTP} = 'IGNORE';
173   local $SIG{PIPE} = 'IGNORE';
174
175   my $oldAutoCommit = $FS::UID::AutoCommit;
176   local $FS::UID::AutoCommit = 0;
177   my $dbh = dbh;
178
179   foreach my $svc_phone (qsearch('svc_phone', { 'pbxsvc' => $self->svcnum } )) {
180     $svc_phone->pbxsvc('');
181     my $error = $svc_phone->replace;
182     if ( $error ) {
183       $dbh->rollback if $oldAutoCommit;
184       return $error;
185     }
186   }
187
188   foreach my $svc_acct  (qsearch('svc_acct',  { 'pbxsvc' => $self->svcnum } )) {
189     my $error = $svc_acct->delete;
190     if ( $error ) {
191       $dbh->rollback if $oldAutoCommit;
192       return $error;
193     }
194   }
195
196   my $error = $self->SUPER::delete;
197   if ( $error ) {
198     $dbh->rollback if $oldAutoCommit;
199     return $error;
200   }
201
202   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
203   '';
204 }
205
206
207 =item replace OLD_RECORD
208
209 Replaces the OLD_RECORD with this one in the database.  If there is an error,
210 returns the error, otherwise returns false.
211
212 =cut
213
214 #sub replace {
215 #  my ( $new, $old ) = ( shift, shift );
216 #  my $error;
217 #
218 #  $error = $new->SUPER::replace($old);
219 #  return $error if $error;
220 #
221 #  '';
222 #}
223
224 =item suspend
225
226 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
227
228 =item unsuspend
229
230 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
231
232 =item cancel
233
234 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
235
236 =item check
237
238 Checks all fields to make sure this is a valid PBX tenant.  If there is
239 an error, returns the error, otherwise returns false.  Called by the insert
240 and repalce methods.
241
242 =cut
243
244 sub check {
245   my $self = shift;
246
247   my $x = $self->setfixed;
248   return $x unless ref($x);
249   my $part_svc = $x;
250
251
252   $self->SUPER::check;
253 }
254
255 #XXX this is a way-too simplistic implementation
256 # at the very least, title should be unique across exports that need that or
257 # controlled by a conf setting or something
258 sub _check_duplicate {
259   my $self = shift;
260
261   $self->lock_table;
262
263   if ( qsearchs( 'svc_pbx', { 'title' => $self->title } ) ) {
264     return "Name in use";
265   } else {
266     return '';
267   }
268 }
269
270 =back
271
272 =head1 BUGS
273
274 =head1 SEE ALSO
275
276 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
277 L<FS::cust_pkg>, schema.html from the base documentation.
278
279 =cut
280
281 1;
282