4 use base qw( FS::svc_External_Common );
5 use FS::Record qw( qsearch qsearchs dbh );
13 FS::svc_pbx - Object methods for svc_pbx records
19 $record = new FS::svc_pbx \%hash;
20 $record = new FS::svc_pbx { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
30 $error = $record->suspend;
32 $error = $record->unsuspend;
34 $error = $record->cancel;
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:
45 Primary key (assigned automatcially for new accounts)
49 (Unique?) number of external record
57 Maximum number of extensions
59 =item max_simultaneous
61 Maximum number of simultaneous users
71 Creates a new PBX tenant. To add the PBX tenant to the database, see
74 Note that this stores the hash reference, not a distinct copy of the hash it
75 points to. You can ask the object for a copy with the I<hash> method.
79 sub table { 'svc_pbx'; }
84 'name_plural' => 'PBXs', #optional,
85 'longname_plural' => 'PBXs', #optional
86 'sorts' => 'svcnum', # optional sort field (or arrayref of sort fields, main first)
87 'display_weight' => 70,
88 'cancel_weight' => 90,
92 'max_extensions' => 'Maximum number of User Extensions',
93 'max_simultaneous' => 'Maximum number of simultaneous users',
94 # 'field' => 'Description',
95 # 'another_field' => {
96 # 'label' => 'Description',
97 # 'def_label' => 'Description for service definitions',
99 # 'disable_default' => 1, #disable switches
100 # 'disable_fixed' => 1, #
101 # 'disable_inventory' => 1, #
104 # 'label' => 'Description',
105 # 'def_label' => 'Description for service defs',
106 # 'type' => 'select',
107 # 'select_table' => 'foreign_table',
108 # 'select_key' => 'key_field_in_table',
109 # 'select_label' => 'label_field_in_table',
116 =item search_sql STRING
118 Class method which returns an SQL fragment to search for the given string.
123 #or something more complicated if necessary
125 # my($class, $string) = @_;
126 # $class->search_sql_field('title', $string);
131 Returns the title field for this PBX tenant.
142 Adds this record to the database. If there is an error, returns the error,
143 otherwise returns false.
145 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
146 defined. An FS::cust_svc record will be created and inserted.
154 $error = $self->SUPER::insert;
155 return $error if $error;
162 Delete this record from the database.
169 local $SIG{HUP} = 'IGNORE';
170 local $SIG{INT} = 'IGNORE';
171 local $SIG{QUIT} = 'IGNORE';
172 local $SIG{TERM} = 'IGNORE';
173 local $SIG{TSTP} = 'IGNORE';
174 local $SIG{PIPE} = 'IGNORE';
176 my $oldAutoCommit = $FS::UID::AutoCommit;
177 local $FS::UID::AutoCommit = 0;
180 foreach my $svc_phone (qsearch('svc_phone', { 'pbxsvc' => $self->svcnum } )) {
181 $svc_phone->pbxsvc('');
182 my $error = $svc_phone->replace;
184 $dbh->rollback if $oldAutoCommit;
189 foreach my $svc_acct (qsearch('svc_acct', { 'pbxsvc' => $self->svcnum } )) {
190 my $error = $svc_acct->delete;
192 $dbh->rollback if $oldAutoCommit;
197 my $error = $self->SUPER::delete;
199 $dbh->rollback if $oldAutoCommit;
203 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
208 =item replace OLD_RECORD
210 Replaces the OLD_RECORD with this one in the database. If there is an error,
211 returns the error, otherwise returns false.
216 # my ( $new, $old ) = ( shift, shift );
219 # $error = $new->SUPER::replace($old);
220 # return $error if $error;
227 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
231 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
235 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
239 Checks all fields to make sure this is a valid PBX tenant. If there is
240 an error, returns the error, otherwise returns false. Called by the insert
248 my $x = $self->setfixed;
249 return $x unless ref($x);
256 #XXX this is a way-too simplistic implementation
257 # at the very least, title should be unique across exports that need that or
258 # controlled by a conf setting or something
259 sub _check_duplicate {
262 my $conf = new FS::Conf;
263 return '' if $conf->config('global_unique-pbx_title') eq 'disabled';
267 if ( qsearchs( 'svc_pbx', { 'title' => $self->title } ) ) {
268 return "Name in use";
276 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
277 service. By default, "associated with" means that the "charged_party" field of
278 the CDR matches the "title" field of the service.
282 Accepts the following options:
284 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
286 =item status => "" (or "done"): Return only CDRs with that processing status.
288 =item inbound => 1: No-op for svc_pbx CDR processing.
290 =item default_prefix => "XXX": Also accept the phone number of the service prepended
291 with the chosen prefix.
293 =item disable_src => 1: No-op for svc_pbx CDR processing.
300 my($self, %options) = @_;
304 my @fields = ( 'charged_party' );
305 $hash{'freesidestatus'} = $options{'status'}
306 if exists($options{'status'});
308 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
310 my $title = $self->title;
312 my $prefix = $options{'default_prefix'};
314 my @orwhere = map " $_ = '$title' ", @fields;
315 push @orwhere, map " $_ = '$prefix$title' ", @fields
317 if ( $prefix =~ /^\+(\d+)$/ ) {
318 push @orwhere, map " $_ = '$1$title' ", @fields
321 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
323 if ( $options{'begin'} ) {
324 push @where, 'startdate >= '. $options{'begin'};
326 if ( $options{'end'} ) {
327 push @where, 'startdate < '. $options{'end'};
330 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where );
336 'extra_sql' => $extra_sql,
337 'order_by' => "ORDER BY startdate $for_update",
349 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
350 L<FS::cust_pkg>, schema.html from the base documentation.