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