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 sub _check_duplicate {
259 my $conf = new FS::Conf;
263 foreach my $field ('title', 'id') {
264 my $global_unique = $conf->config("global_unique-pbx_$field");
265 # can be 'disabled', 'enabled', or empty.
266 # if empty, check per exports; if not empty or disabled, check
268 next if $global_unique eq 'disabled';
269 my @dup = $self->find_duplicates(
270 ($global_unique ? 'global' : 'export') , $field
273 return "duplicate $field '".$self->getfield($field).
274 "': conflicts with svcnum ".$dup[0]->svcnum;
281 Returns a set of Call Detail Records (see L<FS::cdr>) associated with this
282 service. By default, "associated with" means that the "charged_party" field of
283 the CDR matches the "title" field of the service.
287 Accepts the following options:
289 =item for_update => 1: SELECT the CDRs "FOR UPDATE".
291 =item status => "" (or "done"): Return only CDRs with that processing status.
293 =item inbound => 1: No-op for svc_pbx CDR processing.
295 =item default_prefix => "XXX": Also accept the phone number of the service prepended
296 with the chosen prefix.
298 =item disable_src => 1: No-op for svc_pbx CDR processing.
300 =item by_svcnum => 1: Select CDRs where the svcnum field matches, instead of
301 title/charged_party. Normally this field is set after processing.
308 my($self, %options) = @_;
312 my @fields = ( 'charged_party' );
313 $hash{'freesidestatus'} = $options{'status'}
314 if exists($options{'status'});
316 my $for_update = $options{'for_update'} ? 'FOR UPDATE' : '';
318 if ( $options{'by_svcnum'} ) {
319 $hash{'svcnum'} = $self->svcnum;
323 my $title = $self->title;
325 my $prefix = $options{'default_prefix'};
327 my @orwhere = map " $_ = '$title' ", @fields;
328 push @orwhere, map " $_ = '$prefix$title' ", @fields
330 if ( $prefix =~ /^\+(\d+)$/ ) {
331 push @orwhere, map " $_ = '$1$title' ", @fields
334 push @where, ' ( '. join(' OR ', @orwhere ). ' ) ';
337 if ( $options{'begin'} ) {
338 push @where, 'startdate >= '. $options{'begin'};
340 if ( $options{'end'} ) {
341 push @where, 'startdate < '. $options{'end'};
344 my $extra_sql = ( keys(%hash) ? ' AND ' : ' WHERE ' ). join(' AND ', @where )
351 'extra_sql' => $extra_sql,
352 'order_by' => "ORDER BY startdate $for_update",
364 L<FS::svc_Common>, L<FS::Record>, L<FS::cust_svc>, L<FS::part_svc>,
365 L<FS::cust_pkg>, schema.html from the base documentation.