RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / part_export / broadband_shellcommands.pm
index cf9c36c..d3e495c 100644 (file)
@@ -8,21 +8,26 @@ use FS::part_export;
 @ISA = qw(FS::part_export);
 
 tie my %options, 'Tie::IxHash',
-  'user' => { label=>'Remote username', default=>'freeside' },
-  'insert' => { label=>'Insert command',
-                 default=>'php provision.php --mac=$mac_addr --plan=$plan_id --account=active',
-               },
-  'delete'  => { label=>'Delete command',
-                  default=>'',
-                },
-  'suspend'  => { label=>'Suspension command',
-                  default=>'php provision.php --mac=$mac_addr --plan=$plan_id --account=suspend',
-                },
-  'unsuspend'=> { label=>'Unsuspension command',
-                  default=>'',
-                },
-  'uppercase_mac' => { label => 'Force MACs to uppercase', 
-                       type => 'checkbox', }
+  'user'          => { label   => 'Remote username',
+                       default => 'freeside' },
+  'insert'        => { label   => 'Insert command',
+                       default => 'php provision.php --mac=$mac_addr --plan=$plan_id --account=active',
+                     },
+  'delete'        => { label   => 'Delete command',
+                       default  => '',
+                     },
+  'replace'       => { label   => 'Modification command',
+                       default => '',
+                     },
+  'suspend'       => { label   => 'Suspension command',
+                       default => 'php provision.php --mac=$mac_addr --plan=$plan_id --account=suspend',
+                     },
+  'unsuspend'     => { label   => 'Unsuspension command',
+                       default => '',
+                     },
+  'uppercase_mac' => { label   => 'Force MACs to uppercase', 
+                       type   => 'checkbox',
+                     }
 ;
 
 %info = (
@@ -30,6 +35,10 @@ tie my %options, 'Tie::IxHash',
   'desc'    => 'Run remote commands via SSH, for svc_broadband services',
   'options' => \%options,
   'notes'   => <<'END'
+Run remote commands via SSH, for broadband services.
+<BR><BR>
+All fields in svc_broadband are available for interpolation, as well as pkgnum, locationnum and custnum (prefixed with <code>new_</code> or <code>old_</code>
+for replace operations).
 END
 );
 
@@ -61,30 +70,76 @@ sub _export_command {
   my $command = $self->option($action);
   return '' if $command =~ /^\s*$/;
 
-  #set variable for the command
+  my $command_string = $self->_export_subvars( $svc_broadband, $command );
+
+  $self->shellcommands_queue( $svc_broadband->svcnum,
+    user    => $self->option('user')||'root',
+    host    => $self->machine,
+    command => $command_string,
+  );
+}
+
+sub _export_subvars {
+  my( $self, $svc_broadband, $command ) = @_;
+
   no strict 'vars';
   {
     no strict 'refs';
     ${$_} = $svc_broadband->getfield($_) foreach $svc_broadband->fields;
   }
 
-  if ( $self->option('uppercase_mac') ) {
-       $mac_addr = uc $mac_addr;
-  }
+  $mac_addr = uc $mac_addr
+    if $self->option('uppercase_mac');
 
-  #done setting variables for the command
+  my $cust_pkg = $svc_broadband->cust_svc->cust_pkg;
+  $pkgnum = $cust_pkg ? $cust_pkg->pkgnum : '';
+  $locationnum = $cust_pkg ? $cust_pkg->locationnum : '';
+  $custnum = $cust_pkg ? $cust_pkg->custnum : '';
 
-  $self->shellcommands_queue( $svc_broadband->svcnum,
-    user         => $self->option('user')||'root',
-    host         => $self->machine,
-    command      => eval(qq("$command")),
-  );
+  eval(qq("$command"));
 }
 
 sub _export_replace {
-    '';
+  my($self, $new, $old ) = (shift, shift, shift);
+  my $command = $self->option('replace');
+
+  my $command_string = $self->_export_subvars_replace( $new, $old, $command );
+
+  $self->shellcommands_queue( $new->svcnum,
+    user    => $self->option('user')||'root',
+    host    => $self->machine,
+    command => $command_string,
+  );
 }
 
+sub _export_subvars_replace {
+  my( $self, $new, $old, $command ) = @_;
+
+  no strict 'vars';
+  {
+    no strict 'refs';
+    ${"old_$_"} = $old->getfield($_) foreach $old->fields;
+    ${"new_$_"} = $new->getfield($_) foreach $new->fields;
+  }
+
+  if ( $self->option('uppercase_mac') ) {
+    $old_mac_addr = uc $old_mac_addr;
+    $new_mac_addr = uc $new_mac_addr;
+  }
+
+  my $old_cust_pkg = $old->cust_svc->cust_pkg;
+  my $new_cust_pkg = $new->cust_svc->cust_pkg;
+  $old_pkgnum = $old_cust_pkg ? $old_cust_pkg->pkgnum : '';
+  $old_locationnum = $old_cust_pkg ? $old_cust_pkg->locationnum : '';
+  $old_custnum = $old_cust_pkg ? $old_cust_pkg->custnum : '';
+  $new_pkgnum = $new_cust_pkg ? $new_cust_pkg->pkgnum : '';
+  $new_locationnum = $new_cust_pkg ? $new_cust_pkg->locationnum : '';
+  $new_custnum = $new_cust_pkg ? $new_cust_pkg->custnum : '';
+
+  eval(qq("$command"));
+}
+
+
 #a good idea to queue anything that could fail or take any time
 sub shellcommands_queue {
   my( $self, $svcnum ) = (shift, shift);