1 package FS::svc_broadband;
2 use base qw(FS::svc_Radius_Mixin FS::svc_Tower_Mixin FS::svc_Common);
7 { no warnings 'redefine'; use NetAddr::IP; }
8 use FS::Record qw( qsearchs qsearch dbh );
11 use FS::part_svc_router;
14 $FS::UID::callback{'FS::svc_broadband'} = sub {
20 FS::svc_broadband - Object methods for svc_broadband records
24 use FS::svc_broadband;
26 $record = new FS::svc_broadband \%hash;
27 $record = new FS::svc_broadband { 'column' => 'value' };
29 $error = $record->insert;
31 $error = $new_record->replace($old_record);
33 $error = $record->delete;
35 $error = $record->check;
37 $error = $record->suspend;
39 $error = $record->unsuspend;
41 $error = $record->cancel;
45 An FS::svc_broadband object represents a 'broadband' Internet connection, such
46 as a DSL, cable modem, or fixed wireless link. These services are assumed to
47 have the following properties:
49 FS::svc_broadband inherits from FS::svc_Common. The following fields are
54 =item svcnum - primary key
56 =item blocknum - see FS::addr_block
59 speed_up - maximum upload speed, in bits per second. If set to zero, upload
60 speed will be unlimited. Exports that do traffic shaping should handle this
61 correctly, and not blindly set the upload speed to zero and kill the customer's
65 speed_down - maximum download speed, as above
67 =item ip_addr - the customer's IP address. If the customer needs more than one
68 IP address, set this to the address of the customer's router. As a result, the
69 customer's router will have the same address for both its internal and external
70 interfaces thus saving address space. This has been found to work on most NAT
83 Creates a new svc_broadband. To add the record to the database, see
86 Note that this stores the hash reference, not a distinct copy of the hash it
87 points to. You can ask the object for a copy with the I<hash> method.
93 'name' => 'Wireless broadband',
94 'name_plural' => 'Wireless broadband services',
95 'longname_plural' => 'Fixed wireless broadband services',
96 'display_weight' => 50,
97 'cancel_weight' => 70,
98 'ip_field' => 'ip_addr',
100 'svcnum' => 'Service',
101 'description' => 'Descriptive label for this particular device',
102 'speed_down' => 'Maximum download speed for this service in Kbps. 0 denotes unlimited.',
103 'speed_up' => 'Maximum upload speed for this service in Kbps. 0 denotes unlimited.',
104 #'ip_addr' => 'IP address. Leave blank for automatic assignment.',
106 #{ 'label' => 'Address block',
107 # 'type' => 'select',
108 # 'select_table' => 'addr_block',
109 # 'select_key' => 'blocknum',
110 # 'select_label' => 'cidr',
111 # 'disable_inventory' => 1,
113 'plan_id' => 'Service Plan Id',
114 'performance_profile' => 'Peformance Profile',
115 'authkey' => 'Authentication key',
116 'mac_addr' => 'MAC address',
117 'latitude' => 'Latitude',
118 'longitude' => 'Longitude',
119 'altitude' => 'Altitude',
120 'vlan_profile' => 'VLAN profile',
121 'sectornum' => 'Tower/sector',
122 'routernum' => 'Router/block',
124 label => 'RADIUS groups',
125 type => 'select-radius_group.html',
126 #select_table => 'radius_group',
127 #select_key => 'groupnum',
128 #select_label => 'groupname',
129 disable_inventory => 1,
136 sub table { 'svc_broadband'; }
138 sub table_dupcheck_fields { ( 'mac_addr' ); }
142 Class method which returns a qsearch hash expression to search for parameters
143 specified in HASHREF.
149 =item unlinked - set to search for all unlinked services. Overrides all other options.
159 =item pkgpart - arrayref
161 =item routernum - arrayref
163 =item sectornum - arrayref
165 =item towernum - arrayref
174 my ($class, $params) = @_;
177 'LEFT JOIN cust_svc USING ( svcnum )',
178 'LEFT JOIN part_svc USING ( svcpart )',
179 'LEFT JOIN cust_pkg USING ( pkgnum )',
180 'LEFT JOIN cust_main USING ( custnum )',
183 # based on FS::svc_acct::search, probably the most mature of the bunch
185 push @where, 'pkgnum IS NULL' if $params->{'unlinked'};
188 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
189 push @where, "cust_main.agentnum = $1";
191 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql(
192 'null_right' => 'View/link unlinked services',
193 'table' => 'cust_main'
197 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
198 push @where, "custnum = $1";
201 #pkgpart, now properly untainted, can be arrayref
202 for my $pkgpart ( $params->{'pkgpart'} ) {
203 if ( ref $pkgpart ) {
204 my $where = join(',', map { /^(\d+)$/ ? $1 : () } @$pkgpart );
205 push @where, "cust_pkg.pkgpart IN ($where)" if $where;
207 elsif ( $pkgpart =~ /^(\d+)$/ ) {
208 push @where, "cust_pkg.pkgpart = $1";
212 #routernum, can be arrayref
213 for my $routernum ( $params->{'routernum'} ) {
214 # this no longer uses addr_block
215 if ( ref $routernum and grep { $_ } @$routernum ) {
216 my $in = join(',', map { /^(\d+)$/ ? $1 : () } @$routernum );
218 push @orwhere, "svc_broadband.routernum IN ($in)" if $in;
219 push @orwhere, "svc_broadband.routernum IS NULL"
220 if grep /^none$/, @$routernum;
221 push @where, '( '.join(' OR ', @orwhere).' )';
223 elsif ( $routernum =~ /^(\d+)$/ ) {
224 push @where, "svc_broadband.routernum = $1";
226 elsif ( $routernum eq 'none' ) {
227 push @where, "svc_broadband.routernum IS NULL";
231 #sector and tower, as above
232 my @where_sector = $class->tower_sector_sql($params);
233 if ( @where_sector ) {
234 push @where, @where_sector;
235 push @from, 'LEFT JOIN tower_sector USING ( sectornum )';
239 if ( $params->{'svcnum'} =~ /^(\d+)$/ ) {
240 push @where, "svcnum = $1";
244 if ( $params->{'svcpart'} =~ /^(\d+)$/ ) {
245 push @where, "svcpart = $1";
249 if ( $params->{'ip_addr'} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/ ) {
250 push @where, "ip_addr = '$1'";
254 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1) {
255 push @where, "custnum = $1";
258 my $addl_from = join(' ', @from);
260 $extra_sql = 'WHERE '.join(' AND ', @where) if @where;
261 my $count_query = "SELECT COUNT(*) FROM svc_broadband $addl_from $extra_sql";
263 'table' => 'svc_broadband',
265 'select' => join(', ',
269 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
271 'extra_sql' => $extra_sql,
272 'addl_from' => $addl_from,
273 'order_by' => "ORDER BY ".($params->{'order_by'} || 'svcnum'),
274 'count_query' => $count_query,
278 =item search_sql STRING
280 Class method which returns an SQL fragment to search for the given string.
285 my( $class, $string ) = @_;
286 if ( $string =~ /^(\d{1,3}\.){3}\d{1,3}$/ ) {
287 $class->search_sql_field('ip_addr', $string );
288 }elsif ( $string =~ /^([a-fA-F0-9]{12})$/ ) {
289 $class->search_sql_field('mac_addr', uc($string));
290 }elsif ( $string =~ /^(([a-fA-F0-9]{1,2}:){5}([a-fA-F0-9]{1,2}))$/ ) {
291 $class->search_sql_field('mac_addr', uc("$2$3$4$5$6$7") );
299 Returns the IP address.
308 =item insert [ , OPTION => VALUE ... ]
310 Adds this record to the database. If there is an error, returns the error,
311 otherwise returns false.
313 The additional fields pkgnum and svcpart (see FS::cust_svc) should be
314 defined. An FS::cust_svc record will be created and inserted.
316 Currently available options are: I<depend_jobnum>
318 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
319 jobnums), all provisioning jobs will have a dependancy on the supplied
320 jobnum(s) (they will not run until the specific job(s) complete(s)).
324 # Standard FS::svc_Common::insert
328 Delete this record from the database.
332 # Standard FS::svc_Common::delete
334 =item replace OLD_RECORD
336 Replaces the OLD_RECORD with this one in the database. If there is an error,
337 returns the error, otherwise returns false.
339 # Standard FS::svc_Common::replace
343 Called by the suspend method of FS::cust_pkg (see FS::cust_pkg).
347 Called by the unsuspend method of FS::cust_pkg (see FS::cust_pkg).
351 Called by the cancel method of FS::cust_pkg (see FS::cust_pkg).
355 Checks all fields to make sure this is a valid broadband service. If there is
356 an error, returns the error, otherwise returns false. Called by the insert
363 my $x = $self->setfixed;
365 return $x unless ref($x);
368 my $mac_addr = uc($self->get('mac_addr'));
369 $mac_addr =~ s/[-: ]//g;
370 $self->set('mac_addr', $mac_addr);
373 $self->ut_numbern('svcnum')
374 || $self->ut_numbern('blocknum')
375 || $self->ut_foreign_keyn('routernum', 'router', 'routernum')
376 || $self->ut_foreign_keyn('sectornum', 'tower_sector', 'sectornum')
377 || $self->ut_textn('description')
378 || $self->ut_numbern('speed_up')
379 || $self->ut_numbern('speed_down')
380 || $self->ut_ipn('ip_addr')
381 || $self->ut_hexn('mac_addr')
382 || $self->ut_hexn('auth_key')
383 || $self->ut_coordn('latitude')
384 || $self->ut_coordn('longitude')
385 || $self->ut_sfloatn('altitude')
386 || $self->ut_textn('vlan_profile')
387 || $self->ut_textn('plan_id')
389 return $error if $error;
391 if(($self->speed_up || 0) < 0) { return 'speed_up must be positive'; }
392 if(($self->speed_down || 0) < 0) { return 'speed_down must be positive'; }
394 my $cust_svc = $self->svcnum
395 ? qsearchs('cust_svc', { 'svcnum' => $self->svcnum } )
400 $cust_pkg = $cust_svc->cust_pkg;
401 $svcpart = $cust_svc->svcpart;
403 $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $self->pkgnum } );
404 return "Invalid pkgnum" unless $cust_pkg;
405 $svcpart = $self->svcpart;
407 my $agentnum = $cust_pkg->cust_main->agentnum if $cust_pkg;
409 if ($self->routernum) {
410 return "Router ".$self->routernum." does not provide this service"
411 unless qsearchs('part_svc_router', {
413 routernum => $self->routernum
416 my $router = $self->router;
417 return "Router ".$self->routernum." does not serve this customer"
418 if $router->agentnum and $router->agentnum != $agentnum;
420 if ( $router->auto_addr ) {
421 my $error = $self->assign_ip_addr;
422 return $error if $error;
427 } # if $self->routernum
429 if ( $cust_pkg && ! $self->latitude && ! $self->longitude ) {
430 my $l = $cust_pkg->cust_location_or_main;
431 if ( $l->ship_latitude && $l->ship_longitude ) {
432 $self->latitude( $l->ship_latitude );
433 $self->longitude( $l->ship_longitude );
434 } elsif ( $l->latitude && $l->longitude ) {
435 $self->latitude( $l->latitude );
436 $self->longitude( $l->longitude );
440 $error = $self->_check_ip_addr;
441 return $error if $error;
448 Assign an address block matching the selected router, and the selected block
458 if ( $self->blocknum and $self->addr_block->routernum == $self->routernum ) {
459 # simple case: user chose a block, find an address in that block
460 # (this overrides an existing IP address if it's not in the block)
461 @blocks = ($self->addr_block);
463 elsif ( $self->routernum ) {
464 @blocks = $self->router->auto_addr_block;
470 foreach my $block ( @blocks ) {
471 if ( $self->ip_addr and $block->NetAddr->contains($self->NetAddr) ) {
472 # don't change anything
475 $ip_addr = $block->next_free_addr;
479 $self->set(ip_addr => $ip_addr->addr);
483 return 'No IP address available on this router';
490 if (not($self->ip_addr) or $self->ip_addr eq '0.0.0.0') {
491 return '' if $conf->exists('svc_broadband-allow_null_ip_addr');
492 return 'IP address required';
494 # if (my $dup = qsearchs('svc_broadband', {
495 # ip_addr => $self->ip_addr,
496 # svcnum => {op=>'!=', value => $self->svcnum}
498 # return 'IP address conflicts with svcnum '.$dup->svcnum;
503 sub _check_duplicate {
506 return "MAC already in use"
507 if ( $self->mac_addr &&
508 scalar( qsearch( 'svc_broadband', { 'mac_addr', $self->mac_addr } ) )
517 Returns a NetAddr::IP object containing the IP address of this service. The netmask
524 new NetAddr::IP ($self->ip_addr);
529 Returns the FS::addr_block record (i.e. the address block) for this broadband service.
535 qsearchs('addr_block', { blocknum => $self->blocknum });
540 Returns the FS::router record for this service.
546 qsearchs('router', { routernum => $self->routernum });
549 =item allowed_routers
551 Returns a list of allowed FS::router objects.
555 sub allowed_routers {
557 my $svcpart = $self->svcnum ? $self->cust_svc->svcpart : $self->svcpart;
558 map { $_->router } qsearch('part_svc_router',
559 { svcpart => $self->cust_svc->svcpart });
565 =item mac_addr_formatted CASE DELIMITER
567 Format the MAC address (for use by exports). If CASE starts with "l"
568 (for "lowercase"), it's returned in lowercase. DELIMITER is inserted
573 sub mac_addr_formatted {
575 my ($case, $delim) = @_;
576 my $addr = $self->mac_addr;
577 $addr = lc($addr) if $case =~ /^l/i;
578 join( $delim || '', $addr =~ /../g );
588 # set routernum to addr_block.routernum
589 foreach my $self (qsearch('svc_broadband', {
590 blocknum => {op => '!=', value => ''},
593 my $addr_block = $self->addr_block;
594 if ( my $routernum = $addr_block->routernum ) {
595 $self->set(routernum => $routernum);
596 my $error = $self->replace;
597 die "error assigning routernum $routernum to service ".$self->svcnum.
602 warn "svcnum ".$self->svcnum.
603 ": no routernum in address block ".$addr_block->cidr.", skipped\n";
611 The business with sb_field has been 'fixed', in a manner of speaking.
613 allowed_routers isn't agent virtualized because part_svc isn't agent
616 Having both routernum and blocknum as foreign keys is somewhat dubious.
620 FS::svc_Common, FS::Record, FS::addr_block,
621 FS::part_svc, schema.html from the base documentation.