add customer fields option with agent, display_custnum, status and name, RT#73721
[freeside.git] / FS / FS / nas.pm
index 97b0ea1..c7f2459 100644 (file)
@@ -1,11 +1,10 @@
 package FS::nas;
 
 use strict;
 package FS::nas;
 
 use strict;
-use vars qw( @ISA );
-use FS::Record qw(qsearchs); #qsearch);
-use FS::UID qw( dbh );
-
-@ISA = qw(FS::Record);
+use base qw( FS::m2m_Common FS::Record );
+use FS::Record qw( qsearch qsearchs dbh );
+use FS::export_nas;
+use FS::part_export;
 
 =head1 NAME
 
 
 =head1 NAME
 
@@ -16,11 +15,7 @@ FS::nas - Object methods for nas records
   use FS::nas;
 
   $record = new FS::nas \%hash;
   use FS::nas;
 
   $record = new FS::nas \%hash;
-  $record = new FS::nas {
-    'nasnum'  => 1,
-    'nasip'   => '10.4.20.23',
-    'nasfqdn' => 'box1.brc.nv.us.example.net',
-  };
+  $record = new FS::nas { 'column' => 'value' };
 
   $error = $record->insert;
 
 
   $error = $record->insert;
 
@@ -30,26 +25,32 @@ FS::nas - Object methods for nas records
 
   $error = $record->check;
 
 
   $error = $record->check;
 
-  $error = $record->heartbeat($timestamp);
-
 =head1 DESCRIPTION
 
 =head1 DESCRIPTION
 
-An FS::nas object represents an Network Access Server on your network, such as
-a terminal server or equivalent.  FS::nas inherits from FS::Record.  The
-following fields are currently supported:
+An FS::nas object represents a RADIUS client.  FS::nas inherits from
+FS::Record.  The following fields are currently supported:
 
 =over 4
 
 =item nasnum - primary key
 
 
 =over 4
 
 =item nasnum - primary key
 
-=item nas - NAS name
+=item nasname - "NAS name", i.e. IP address
+
+=item shortname - short descriptive name
+
+=item type - the equipment vendor
+
+=item ports
+
+=item secret - the authentication secret for this client
+
+=item server - virtual server name (optional)
 
 
-=item nasip - NAS ip address
+=item community
 
 
-=item nasfqdn - NAS fully-qualified domain name
+=item description - a longer descriptive name
 
 
-=item last - timestamp indicating the last instant the NAS was in a known
-             state (used by the session monitoring).
+=item svcnum - the L<FS::svc_broadband> record that 'owns' this device
 
 =back
 
 
 =back
 
@@ -75,26 +76,65 @@ sub table { 'nas'; }
 Adds this record to the database.  If there is an error, returns the error,
 otherwise returns false.
 
 Adds this record to the database.  If there is an error, returns the error,
 otherwise returns false.
 
-=cut
-
-# the insert method can be inherited from FS::Record
-
 =item delete
 
 =item delete
 
-Delete this record from the database.
+Delete this record from the database and remove all linked exports.
 
 =cut
 
 
 =cut
 
-# the delete method can be inherited from FS::Record
+sub delete {
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  my $self = shift;
+  my $error = $self->process_m2m(
+    link_table    => 'export_nas',
+    target_table  => 'part_export',
+    params        => []
+  ) || $self->SUPER::delete;
+
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+  
+  $dbh->commit if $oldAutoCommit;
+  '';
+}
 
 =item replace OLD_RECORD
 
 Replaces the OLD_RECORD with this one in the database.  If there is an error,
 returns the error, otherwise returns false.
 
 
 =item replace OLD_RECORD
 
 Replaces the OLD_RECORD with this one in the database.  If there is an error,
 returns the error, otherwise returns false.
 
+To change the list of linked exports, see the C<export_nas> method.
+
 =cut
 
 =cut
 
-# the replace method can be inherited from FS::Record
+sub replace {
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  my ($self, $old) = @_;
+  $old ||= qsearchs('nas', { 'nasnum' => $self->nasnum });
+
+  my $error;
+  foreach my $part_export ( $self->part_export ) {
+    $error ||= $part_export->export_nas_replace($self, $old);
+  }
+
+  $error ||= $self->SUPER::replace($old);
+
+  if ( $error ) {
+    $dbh->rollback;
+    return $error;
+  }
+
+  $dbh->commit if $oldAutoCommit;
+  '';
+}
 
 =item check
 
 
 =item check
 
@@ -110,36 +150,39 @@ and replace methods.
 sub check {
   my $self = shift;
 
 sub check {
   my $self = shift;
 
-  $self->ut_numbern('nasnum')
-    || $self->ut_text('nas')
-    || $self->ut_ip('nasip')
-    || $self->ut_domain('nasfqdn')
-    || $self->ut_numbern('last')
-    || $self->SUPER::check
-    ;
+  my $error = 
+    $self->ut_numbern('nasnum')
+    || $self->ut_text('nasname')
+    || $self->ut_textn('shortname')
+    || $self->ut_text('type')
+    || $self->ut_numbern('ports')
+    || $self->ut_text('secret')
+    || $self->ut_textn('server')
+    || $self->ut_textn('community')
+    || $self->ut_text('description')
+    || $self->ut_foreign_keyn('svcnum', 'svc_broadband', 'svcnum')
+  ;
+  return $error if $error;
+
+  $self->SUPER::check;
 }
 
 }
 
-=item heartbeat TIMESTAMP
+=item part_export
 
 
-Updates the timestamp for this nas
+Return all L<FS::part_export> objects to which this NAS is being exported.
 
 =cut
 
 
 =cut
 
-sub heartbeat {
-  my($self, $timestamp) = @_;
-  my $dbh = dbh;
-  my $sth =
-    $dbh->prepare("UPDATE nas SET last = ? WHERE nasnum = ? AND last < ?");
-  $sth->execute($timestamp, $self->nasnum, $timestamp) or die $sth->errstr;
-  $self->last($timestamp);
+sub part_export {
+  my $self = shift;
+  map { qsearchs('part_export', { exportnum => $_->exportnum }) } 
+        qsearch('export_nas', { nasnum => $self->nasnum})
 }
 
 =back
 
 =head1 BUGS
 
 }
 
 =back
 
 =head1 BUGS
 
-heartbeat method uses SQL directly and doesn't update history tables.
-
 =head1 SEE ALSO
 
 L<FS::Record>, schema.html from the base documentation.
 =head1 SEE ALSO
 
 L<FS::Record>, schema.html from the base documentation.