communigate provisioning phase 2: Domain:Account Defaults:Settings: RulesAllowed...
[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 =back
59
60 =head1 METHODS
61
62 =over 4
63
64 =item new HASHREF
65
66 Creates a new PBX tenant.  To add the PBX tenant to the database, see
67 L<"insert">.
68
69 Note that this stores the hash reference, not a distinct copy of the hash it
70 points to.  You can ask the object for a copy with the I<hash> method.
71
72 =cut
73
74 sub table { 'svc_pbx'; }
75
76 sub table_info {
77   {
78     'name' => 'PBX',
79     'name_plural' => 'PBXs', #optional,
80     'longname_plural' => 'PBXs', #optional
81     'sorts' => 'svcnum', # optional sort field (or arrayref of sort fields, main first)
82     'display_weight' => 70,
83     'cancel_weight'  => 90,
84     'fields' => {
85       'id'    => 'ID',
86       'title' => 'Name',
87       'max_extensions' => 'Maximum number of User Extensions',
88 #      'field'         => 'Description',
89 #      'another_field' => { 
90 #                           'label'     => 'Description',
91 #                          'def_label' => 'Description for service definitions',
92 #                          'type'      => 'text',
93 #                          'disable_default'   => 1, #disable switches
94 #                          'disable_fixed'     => 1, #
95 #                          'disable_inventory' => 1, #
96 #                        },
97 #      'foreign_key'   => { 
98 #                           'label'        => 'Description',
99 #                          'def_label'    => 'Description for service defs',
100 #                          'type'         => 'select',
101 #                          'select_table' => 'foreign_table',
102 #                          'select_key'   => 'key_field_in_table',
103 #                          'select_label' => 'label_field_in_table',
104 #                        },
105
106     },
107   };
108 }
109
110 =item search_sql STRING
111
112 Class method which returns an SQL fragment to search for the given string.
113
114 =cut
115
116 #XXX
117 #or something more complicated if necessary
118 #sub search_sql {
119 #  my($class, $string) = @_;
120 #  $class->search_sql_field('title', $string);
121 #}
122
123 =item label
124
125 Returns the title field for this PBX tenant.
126
127 =cut
128
129 sub label {
130   my $self = shift;
131   $self->title;
132 }
133
134 =item insert
135
136 Adds this record to the database.  If there is an error, returns the error,
137 otherwise returns false.
138
139 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be 
140 defined.  An FS::cust_svc record will be created and inserted.
141
142 =cut
143
144 sub insert {
145   my $self = shift;
146   my $error;
147
148   $error = $self->SUPER::insert;
149   return $error if $error;
150
151   '';
152 }
153
154 =item delete
155
156 Delete this record from the database.
157
158 =cut
159
160 sub delete {
161   my $self = shift;
162
163   local $SIG{HUP} = 'IGNORE';
164   local $SIG{INT} = 'IGNORE';
165   local $SIG{QUIT} = 'IGNORE';
166   local $SIG{TERM} = 'IGNORE';
167   local $SIG{TSTP} = 'IGNORE';
168   local $SIG{PIPE} = 'IGNORE';
169
170   my $oldAutoCommit = $FS::UID::AutoCommit;
171   local $FS::UID::AutoCommit = 0;
172   my $dbh = dbh;
173
174   foreach my $svc_phone (qsearch('svc_phone', { 'pbxsvc' => $self->svcnum } )) {
175     $svc_phone->pbxsvc('');
176     my $error = $svc_phone->replace;
177     if ( $error ) {
178       $dbh->rollback if $oldAutoCommit;
179       return $error;
180     }
181   }
182
183   foreach my $svc_acct  (qsearch('svc_acct',  { 'pbxsvc' => $self->svcnum } )) {
184     my $error = $svc_acct->delete;
185     if ( $error ) {
186       $dbh->rollback if $oldAutoCommit;
187       return $error;
188     }
189   }
190
191   my $error = $self->SUPER::delete;
192   if ( $error ) {
193     $dbh->rollback if $oldAutoCommit;
194     return $error;
195   }
196
197   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
198   '';
199 }
200
201
202 =item replace OLD_RECORD
203
204 Replaces the OLD_RECORD with this one in the database.  If there is an error,
205 returns the error, otherwise returns false.
206
207 =cut
208
209 sub replace {
210   my ( $new, $old ) = ( shift, shift );
211   my $error;
212
213   $error = $new->SUPER::replace($old);
214   return $error if $error;
215
216   '';
217 }
218
219 =item suspend
220
221 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
222
223 =item unsuspend
224
225 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
226
227 =item cancel
228
229 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
230
231 =item check
232
233 Checks all fields to make sure this is a valid PBX tenant.  If there is
234 an error, returns the error, otherwise returns false.  Called by the insert
235 and repalce methods.
236
237 =cut
238
239 sub check {
240   my $self = shift;
241
242   my $x = $self->setfixed;
243   return $x unless ref($x);
244   my $part_svc = $x;
245
246
247   $self->SUPER::check;
248 }
249
250 #XXX this is a way-too simplistic implementation
251 # at the very least, title should be unique across exports that need that or
252 # controlled by a conf setting or something
253 sub _check_duplicate {
254   my $self = shift;
255
256   $self->lock_table;
257
258   if ( qsearchs( 'svc_pbx', { 'title' => $self->title } ) ) {
259     return "Name in use";
260   } else {
261     return '';
262   }
263 }
264
265 =back
266
267 =head1 BUGS
268
269 =head1 SEE ALSO
270
271 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
272 L<FS::cust_pkg>, schema.html from the base documentation.
273
274 =cut
275
276 1;
277