bug squashing for multiple usage classes
[freeside.git] / httemplate / edit / process / part_pkg.cgi
index be6e2f8..669b759 100755 (executable)
@@ -1,6 +1,7 @@
 <% include( 'elements/process.html',
               #'debug'             => 1,
               'table'             => 'part_pkg',
+              'redirect'          => $redirect_callback,
               'viewall_dir'       => 'browse',
               'viewall_ext'       => 'cgi',
               'edit_ext'          => 'cgi',
@@ -8,7 +9,6 @@
               'precheck_callback' => $precheck_callback,
               'args_callback'     => $args_callback,
               'process_m2m'       => \@process_m2m,
-              'debug' => 1,
           )
 %>
 <%init>
@@ -38,10 +38,10 @@ my $precheck_callback = sub {
     push @agents, $1 if $1;
   }
   return "At least one agent type must be specified."
-    unless( scalar(@agents) ||
-            $cgi->param('clone') && $cgi->param('clone') =~ /^\d+$/ ||
-            !$cgi->param('pkgpart') && $conf->exists('agent-defaultpkg')
-          );
+    unless scalar(@agents)
+           || ( $cgi->param('clone') && $cgi->param('clone') =~ /^\d+$/ )
+           || ( !$cgi->param('pkgpart') && $conf->exists('agent-defaultpkg') )
+           || $cgi->param('disabled');
 
   return '';
 
@@ -84,7 +84,15 @@ my $args_callback = sub {
         }
         @options;
 
-  $options{$_} = scalar($cgi->param($_)) for (qw( setup_fee recur_fee ));
+  foreach ( split(',', $cgi->param('taxproductnums') ) ) {
+    my $value = $cgi->param("taxproductnum_$_");
+    $error ||= "Illegal taxproductnum_$_: $value"
+      unless ( $value =~ /^\d*$/  );
+    $options{"usage_taxproductnum_$_"} = $value;
+  }
+
+  $options{$_} = scalar( $cgi->param($_) )
+    for (qw( setup_fee recur_fee ));
   
   push @args, 'options' => \%options;
 
@@ -106,10 +114,20 @@ my $args_callback = sub {
                 'custnum_ref' => \$custnum;
   }
 
+  warn "args: ".join('/', @args). "\n";
+
   @args;
 
 };
 
+my $redirect_callback = sub {
+  #my( $cgi, $new ) = @_;
+  return '' unless $custnum;
+  popurl(3). "view/cust_main.cgi?keywords=$custnum;dummy=";
+};
+
+#these should probably move to @args above and be processed by part_pkg.pm...
+
 $cgi->param('tax_override') =~ /^([\d,]+)$/;
 my (@tax_overrides) = (grep "$_", split (",", $1));
 
@@ -118,9 +136,39 @@ my @process_m2m = (
     'link_table'   => 'part_pkg_taxoverride',
     'target_table' => 'tax_class',
     'params'       => \@tax_overrides,
-  }
+  },
+  { 'link_table'   => 'part_pkg_link',
+    'target_table' => 'part_pkg',
+    'base_field'   => 'src_pkgpart',
+    'target_field' => 'dst_pkgpart',
+    'hashref'      => { 'link_type' => 'bill' },
+    'params'       => [ map $cgi->param($_), grep /^bill_dst_pkgpart/, $cgi->param ],
+  },
+  { 'link_table'   => 'part_pkg_link',
+    'target_table' => 'part_pkg',
+    'base_field'   => 'src_pkgpart',
+    'target_field' => 'dst_pkgpart',
+    'hashref'      => { 'link_type' => 'svc' },
+    'params'       => [ map $cgi->param($_), grep /^svc_dst_pkgpart/, $cgi->param ],
+  },
 );
 
+foreach my $override_class ($cgi->param) {
+  next unless $override_class =~ /^tax_override_(\w+)$/;
+  my $class = $1;
+
+  my (@tax_overrides) = (grep "$_", split (",", $1))
+    if $cgi->param($override_class) =~ /^([\d,]+)$/;
+
+  push @process_m2m, {
+    'link_table'   => 'part_pkg_taxoverride',
+    'target_table' => 'tax_class',
+    'hashref'      => { 'usage_class' => $class },
+    'params'       => [ @tax_overrides ],
+  };
+
+}
+
 my $conf = new FS::Conf;
 
 if ( $cgi->param('pkgpart') || ! $conf->exists('agent_defaultpkg') ) {
@@ -129,7 +177,6 @@ if ( $cgi->param('pkgpart') || ! $conf->exists('agent_defaultpkg') ) {
     /^(\d+)$/;
     push @agents, $1 if $1;
   }
-  warn "AGENTS: @agents";
   push @process_m2m, {
     'link_table'   => 'type_pkgs',
     'target_table' => 'agent_type',