Merge branch 'master' of git.freeside.biz:/home/git/freeside
authorIvan Kohler <ivan@freeside.biz>
Tue, 9 Oct 2012 19:32:39 +0000 (12:32 -0700)
committerIvan Kohler <ivan@freeside.biz>
Tue, 9 Oct 2012 19:32:39 +0000 (12:32 -0700)
FS/FS/part_export/dma_radiusmanager.pm
httemplate/edit/process/quick-cust_pkg.cgi
httemplate/elements/table-tickets.html
httemplate/view/elements/svc_Common.html
httemplate/view/svc_acct.cgi
httemplate/view/svc_domain.cgi
httemplate/view/svc_external.cgi
httemplate/view/svc_forward.cgi
httemplate/view/svc_www.cgi

index ab77c46..d46a996 100644 (file)
@@ -9,14 +9,17 @@ use FS::radius_group;
 use Tie::IxHash;
 use Digest::MD5 'md5_hex';
 
+use Locale::Country qw(code2country);
+use Locale::SubCountry;
+use Date::Format 'time2str';
+
 tie %options, 'Tie::IxHash',
   'dbname'    => { label=>'Database name', default=>'radius' },
   'username'  => { label=>'Database username' },
   'password'  => { label=>'Database password' },
   'manager'   => { label=>'Manager name' },
-  'groupid'   => { label=>'Group ID', default=>'1' },
-  'service_prefix' => { label=>'Service name prefix' },
-  'nasnames'  => { label=>'NAS IDs/addresses' },
+  'template_name'   => { label=>'Template service name' },
+  'service_prefix'  => { label=>'Service name prefix' },
   'debug'     => { label=>'Enable debugging', type=>'checkbox' },
 ;
 
@@ -57,6 +60,12 @@ sub dma_rm_queue {
   my $cust_main = $cust_pkg->cust_main;
   my $location = $cust_pkg->cust_location;
 
+  my $address = $location->address1;
+  $address .= ' '.$location->address2 if $location->address2;
+  my $country = code2country($location->country);
+  my $lsc = Locale::SubCountry->new($location->country);
+  my $state = $lsc->full_name($location->state) if defined($lsc);
+
   my %params = (
     # for the remote side
     username    => $svc_acct->username,
@@ -70,9 +79,9 @@ sub dma_rm_queue {
     mobile      => $cust_main->mobile,
     address     => $location->address1, # address2?
     city        => $location->city,
-    state       => $location->state,
+    state       => $state, #full name
     zip         => $location->zip,
-    country     => $location->country,
+    country     => $country, #full name
     gpslat      => $location->latitude,
     gpslong     => $location->longitude,
     comment     => 'svcnum'.$svcnum,
@@ -118,6 +127,8 @@ sub dma_rm_action {
   $params{srvid} = $srvid;
 
   if ( $action eq 'insert' ) {
+    $params{'createdon'} = time2str('%Y-%m-%d', time);
+    $params{'expiration'} = time2str('%Y-%m-%d', time);
     warn "rm_users: inserting svcnum$svcnum\n" if $DEBUG;
     my $sth = $dbh->prepare( 'INSERT INTO rm_users ( '.
       join(', ', keys(%params)).
@@ -223,6 +234,14 @@ not, create one.  Then return its srvid.
 sub export_part_svc {
   my ($self, $part_svc, $dbh) = @_;
 
+  # if $dbh exists, use the existing transaction
+  # otherwise create our own and commit when finished
+  my $commit = 0;
+  if (!$dbh) {
+    $dbh = $self->connect;
+    $commit = 1;
+  }
+
   my $name = $self->option('service_prefix').$part_svc->svc;
 
   my %params = (
@@ -230,19 +249,22 @@ sub export_part_svc {
     'enableservice'   => 1,
     'nextsrvid'       => -1,
     'dailynextsrvid'  => -1,
+    # force price-related fields to zero
+    'unitprice'       => 0,
+    'unitpriceadd'    => 0,
+    'unitpricetax'    => 0,
+    'unitpriceaddtax' => 0,
   );
   my @fixed_groups;
   # use speed settings from fixed usergroups configured on this part_svc
   if ( my $psc = $part_svc->part_svc_column('usergroup') ) {
-    if ( $psc->columnflag eq 'F' )  {
-      # each part_svc really should only have one fixed group with non-null 
-      # speed settings, but go by priority order for consistency
-      @fixed_groups = 
-        sort { $a->priority <=> $b->priority }
-        grep { $_ }
-        map { FS::radius_group->by_key($_) }
-        split(/\s*,\s*/, $psc->columnvalue);
-    }
+    # each part_svc really should only have one fixed group with non-null 
+    # speed settings, but go by priority order for consistency
+    @fixed_groups = 
+      sort { $a->priority <=> $b->priority }
+      grep { $_ }
+      map { FS::radius_group->by_key($_) }
+      split(/\s*,\s*/, $psc->columnvalue);
   } # otherwise there are no fixed groups, so leave speed empty
 
   foreach (qw(down up)) {
@@ -263,74 +285,71 @@ sub export_part_svc {
   $sth->execute($name) or die $dbh->errstr;
   if ( $sth->rows > 1 ) {
     die "Multiple services with name '$name' found in Radius Manager.\n";
-  } elsif ( $sth->rows == 1 ) {
-    my $row = $sth->fetchrow_arrayref;
-    my $srvid = $row->[0];
-    warn "rm_services: updating srvid#$srvid\n" if $DEBUG;
-    $sth = $dbh->prepare(
-      'UPDATE rm_services SET '.join(', ', map {"$_ = ?"} keys %params) .
-      ' WHERE srvid = ?'
-    );
-    $sth->execute(values(%params), $srvid) or die $dbh->errstr;
-    return $srvid;
-  } else { # $sth->rows == 0
-    # create a new one
-    # but first... get the next available srvid
+
+  } elsif ( $sth->rows == 0 ) {
+    # leave this blank to disable creating new service defs
+    my $template_name = $self->option('template_name');
+
+    die "Can't create a new service profile--no template service specified.\n"
+      unless $template_name;
+
+    warn "rm_services: fetching template '$template_name'\n" if $DEBUG;
+    $sth = $dbh->prepare('SELECT * FROM rm_services WHERE srvname = ? LIMIT 1');
+    $sth->execute($template_name);
+    die "Can't create a new service profile--template service ".
+      "'$template_name' not found.\n" unless $sth->rows == 1;
+    my $template = $sth->fetchrow_hashref;
+    %params = (%$template, %params);
+
+    # get the next available srvid
     $sth = $dbh->prepare('SELECT MAX(srvid) FROM rm_services');
     $sth->execute or die $dbh->errstr;
-    my $srvid = 1; # just in case you somehow have nothing in your database
+    my $srvid;
     if ( $sth->rows ) {
       $srvid = $sth->fetchrow_arrayref->[0] + 1;
     }
     $params{'srvid'} = $srvid;
-    # NOW create a new one
+
+    # create a new one based on the template
     warn "rm_services: inserting '$name' as srvid#$srvid\n" if $DEBUG;
     $sth = $dbh->prepare(
       'INSERT INTO rm_services ('.join(', ', keys %params).
       ') VALUES ('.join(', ', map {'?'} keys %params).')'
     );
     $sth->execute(values(%params)) or die $dbh->errstr;
-    # also link it to our manager name
+    # also link it to all the managers allowed on the template service
     warn "rm_services: linking to manager\n" if $DEBUG;
     $sth = $dbh->prepare(
-      'INSERT INTO rm_allowedmanagers (srvid, managername) VALUES (?, ?)'
+      'INSERT INTO rm_allowedmanagers (srvid, managername) '.
+      'SELECT ?, managername FROM rm_allowedmanagers WHERE srvid = ?'
     );
-    $sth->execute($srvid, $self->option('manager')) or die $dbh->errstr;
-    # and allow it on our NAS
+    $sth->execute($srvid, $template->{srvid}) or die $dbh->errstr;
+    # and the same for NASes
+    warn "rm_services: linking to nas\n" if $DEBUG;
     $sth = $dbh->prepare(
-      'INSERT INTO rm_allowednases (srvid, nasid) VALUES (?, ?)'
+      'INSERT INTO rm_allowednases (srvid, nasid) '.
+      'SELECT ?, nasid FROM rm_allowednases WHERE srvid = ?'
     );
-    foreach my $nasid ($self->nas_ids($dbh)) {
-      warn "rm_services: linking to nasid#$nasid\n" if $DEBUG;
-      $sth->execute($srvid, $nasid) or die $dbh->errstr;
-    }
-    return $srvid;
-  }
-}
-
-=item nas_ids DBH
+    $sth->execute($srvid, $template->{srvid}) or die $dbh->errstr;
 
-Convert the 'nasnames  option into a list of real NAS ids.
+    $dbh->commit if $commit;
+    return $srvid;
 
-=cut
+  } else { # $sth->rows == 1, it already exists
 
-sub nas_ids {
-  my $self = shift;
-  my $dbh = shift;
-
-  my @nasnames = split(/\s*,\s*/, $self->option('nasnames'));
-  return unless @nasnames;
-  # pass these through unchanged
-  my @ids = grep { /^\d+$/ } @nasnames;
-  @nasnames = grep { not /^\d+$/ } @nasnames;
-  my $in_nasnames = join(',', map {$dbh->quote($_)} @nasnames);
+    my $row = $sth->fetchrow_arrayref;
+    my $srvid = $row->[0];
+    warn "rm_services: updating srvid#$srvid\n" if $DEBUG;
+    $sth = $dbh->prepare(
+      'UPDATE rm_services SET '.join(', ', map {"$_ = ?"} keys %params) .
+      ' WHERE srvid = ?'
+    );
+    $sth->execute(values(%params), $srvid) or die $dbh->errstr;
 
-  my $sth = $dbh->prepare("SELECT id FROM nas WHERE nasname IN ($in_nasnames)");
-  $sth->execute or die $dbh->errstr;
-  my $rows = $sth->fetchall_arrayref;
-  push @ids, $_->[0] foreach @$rows;
+    $dbh->commit if $commit;
+    return $srvid;
 
-  return @ids;
+  }
 }
 
 1;
index c5eee0c..2dadbcc 100644 (file)
@@ -64,9 +64,9 @@ die 'no custnum or prospectnum' unless $cust_main || $prospect_main;
 $cgi->param('pkgpart') =~ /^(\d+)$/
   or die 'illegal pkgpart '. $cgi->param('pkgpart');
 my $pkgpart = $1;
-$cgi->param('quantity') =~ /^(\d+)$/
+$cgi->param('quantity') =~ /^(\d*)$/
   or die 'illegal quantity '. $cgi->param('quantity');
-my $quantity = $1;
+my $quantity = $1 || 1;
 $cgi->param('refnum') =~ /^(\d*)$/
   or die 'illegal refnum '. $cgi->param('refnum');
 my $refnum = $1;
index 6d1a45a..ffcaf06 100644 (file)
@@ -94,12 +94,12 @@ View
 </TABLE>
 
 <%init>
-use Date::Parse qw(str2time);
-use Date::Format qw(time2str);
 
 my %opt = @_;
 my $conf = new FS::Conf;
 
+return '' unless $conf->config('ticket_system');
+
 my $object = $opt{'object'};
 $object = $object->cust_svc if $object->isa('FS::svc_Common');
 my( @tickets )  = $object->tickets;
@@ -116,8 +116,10 @@ if ( $object->isa('FS::cust_main') ) {
                     $object->custnum,
                     { 'statuses' => [ 'resolved' ] }
                   );
-}
-elsif ( $object->isa('FS::cust_svc') ) {
+} elsif ( $object->isa('FS::cust_svc') ) {
+
+  return '' unless $object->pkgnum;
+
   $thing = 'service';
   $open_link = FS::TicketSystem->href_service_tickets($object->svcnum);
 
index 04d2b29..f7c685c 100644 (file)
@@ -114,9 +114,7 @@ function areyousure(href) {
 
 % }
 
-% if ( $conf->config('ticket_system') ) {
 <& /elements/table-tickets.html, object => $cust_svc &>
-% }
 
 <% joblisting({'svcnum'=>$svcnum}, 1) %>
 
index 3ad21bb..1995913 100755 (executable)
@@ -76,9 +76,7 @@
 
 <& elements/svc_export_settings.html, $svc_acct &>
 
-% if ( $conf->config('ticket_system') ) {
 <& /elements/table-tickets.html, object => $cust_svc &>
-% }
 
 <BR>
 <% joblisting({'svcnum'=>$svcnum}, 1) %>
index fcccd74..c9592a5 100755 (executable)
@@ -36,9 +36,7 @@
 
 <% include('elements/svc_export_settings.html', $svc_domain) %>
 
-% if ( $conf->config('ticket_system') ) {
 <& /elements/table-tickets.html, object => $cust_svc &>
-% }
 
 <% joblisting({'svcnum'=>$svcnum}, 1) %>
 
index 72e5535..077dc77 100644 (file)
@@ -24,9 +24,7 @@
 
 </TABLE></TD></TR></TABLE>
 
-% if ( $conf->config('ticket_system') ) {
 <& /elements/table-tickets.html, object => $cust_svc &>
-% }
 
 <BR><% joblisting({'svcnum'=>$svcnum}, 1) %>
 
index 2cb78eb..75c8f5c 100755 (executable)
@@ -53,9 +53,7 @@
 
 <% include('elements/svc_export_settings.html', $svc_forward) %>
 
-% if ( $conf->config('ticket_system') ) {
 <& /elements/table-tickets.html, object => $cust_svc &>
-% }
 
 <% joblisting({'svcnum'=>$svcnum}, 1) %>
 
index fbb02a0..f51c760 100644 (file)
@@ -49,9 +49,7 @@
 </TABLE>
 
 <BR>
-% if ( $conf->config('ticket_system') ) {
 <& /elements/table-tickets.html, object => $cust_svc &>
-% }
 
 <% joblisting({'svcnum'=>$svcnum}, 1) %>