hidden pkg_svc flag, RT#9871
[freeside.git] / httemplate / edit / process / part_pkg.cgi
index d338b82..7248801 100755 (executable)
@@ -1,11 +1,12 @@
 <% include( 'elements/process.html',
               #'debug'             => 1,
               'table'             => 'part_pkg',
+              'agent_virt'        => 1,
+              'agent_null_right'  => \@agent_null_right,
               'redirect'          => $redirect_callback,
               'viewall_dir'       => 'browse',
               'viewall_ext'       => 'cgi',
               'edit_ext'          => 'cgi',
-              #XXX usable with cloning? #'agent_null_right'  => 'Edit global package definitions',
               'precheck_callback' => $precheck_callback,
               'args_callback'     => $args_callback,
               'process_m2m'       => \@process_m2m,
 %>
 <%init>
 
+my $customizing = ( ! $cgi->param('pkgpart') && $cgi->param('pkgnum') );
+
 my $curuser = $FS::CurrentUser::CurrentUser;
 
+my $edit_global = 'Edit global package definitions';
+my $customize   = 'Customize customer package';
+
 die "access denied"
   unless $curuser->access_right('Edit package definitions')
-      || $curuser->access_right('Edit global package definitions')
-      || ( ! $cgi->param('pkgpart') && $cgi->param('pkgnum') && $curuser->access_right('Customize customer package') );
+      || $curuser->access_right($edit_global)
+      || ( $customizing && $curuser->access_right($customize) );
+
+my @agent_null_right = ( $edit_global );
+push @agent_null_right, $customize if $customizing;
+
 
 my $precheck_callback = sub {
   my( $cgi ) = @_;
@@ -41,7 +51,8 @@ my $precheck_callback = sub {
     unless scalar(@agents)
            || ( $cgi->param('clone') && $cgi->param('clone') =~ /^\d+$/ )
            || ( !$cgi->param('pkgpart') && $conf->exists('agent-defaultpkg') )
-           || $cgi->param('disabled');
+           || $cgi->param('disabled')
+           || $cgi->param('agentnum');
 
   return '';
 
@@ -82,6 +93,7 @@ my $args_callback = sub {
           }
           ( $optionname => $value );
         }
+        grep { $_ !~ /^report_option_/ }
         @options;
 
   foreach ( split(',', $cgi->param('taxproductnums') ) ) {
@@ -91,6 +103,11 @@ my $args_callback = sub {
     $options{"usage_taxproductnum_$_"} = $value;
   }
 
+  foreach ( grep $_, $cgi->param('report_option') ) {
+    $error ||= "Illegal optional report class: $_" unless ( $_ =~ /^\d*$/  );
+    $options{"report_option_$_"} = 1;
+  }
+
   $options{$_} = scalar( $cgi->param($_) )
     for (qw( setup_fee recur_fee ));
   
@@ -100,11 +117,11 @@ my $args_callback = sub {
   #pkg_svc
   ###
 
-  my %pkg_svc = map { $_ => scalar($cgi->param("pkg_svc$_")) }
-                map { $_->svcpart }
-                qsearch('part_svc', {} );
+  my @svcparts = map { $_->svcpart } qsearch('part_svc', {});
+  my %pkg_svc = map { $_ => scalar($cgi->param("pkg_svc$_")) } @svcparts;
+  my %hidden_svc = map { $_ => scalar($cgi->param("hidden$_")) } @svcparts;
 
-  push @args, 'pkg_svc' => \%pkg_svc;
+  push @args, 'pkg_svc' => \%pkg_svc, 'hidden_svc' => \%hidden_svc;
 
   ###
   # cust_pkg and custnum_ref (inserts only)
@@ -114,7 +131,7 @@ my $args_callback = sub {
                 'custnum_ref' => \$custnum;
   }
 
-  warn "args: ".join('/', @args). "\n";
+  #warn "args: ".join('/', @args). "\n";
 
   @args;
 
@@ -123,7 +140,13 @@ my $args_callback = sub {
 my $redirect_callback = sub {
   #my( $cgi, $new ) = @_;
   return '' unless $custnum;
-  popurl(3). "view/cust_main.cgi?keywords=$custnum;dummy=";
+  my $show = $curuser->default_customer_view =~ /^(jumbo|packages)$/
+               ? ''
+               : ';show=packages';
+  #my $frag = "cust_pkg$pkgnum"; #hack for IE ignoring real #fragment
+  #can we link back to the specific customized package?  it would be nice...
+  popurl(3). "view/cust_main.cgi?custnum=$custnum$show;dummy=";
 };
 
 #these should probably move to @args above and be processed by part_pkg.pm...
@@ -137,20 +160,38 @@ my @process_m2m = (
     '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_discount',
+    'target_table' => 'discount',
+    'params'       => [ map $cgi->param($_),
+                        grep /^discountnum/, $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 ],
+    'hashref'      => { 'link_type' => 'svc', 'hidden' => '' },
+    'params'       => [ map $cgi->param($_),
+                        grep /^svc_dst_pkgpart/, $cgi->param
+                      ],
   },
+  map { 
+    my $hidden = $_;
+    { 'link_table'   => 'part_pkg_link',
+      'target_table' => 'part_pkg',
+      'base_field'   => 'src_pkgpart',
+      'target_field' => 'dst_pkgpart',
+      'hashref'      => { 'link_type' => 'bill', 'hidden' => $hidden },
+      'params'       => [ map { $cgi->param($_) }
+                          grep { my $param = "bill_dst_pkgpart__hidden";
+                                 my $digit = '';
+                                 (($digit) = /^bill_dst_pkgpart(\d+)/ ) &&
+                                 $cgi->param("$param$digit") eq $hidden;
+                               }
+                          $cgi->param
+                        ],
+    },
+  } ( '', 'Y' ),
 );
 
 foreach my $override_class ($cgi->param) {
@@ -164,7 +205,7 @@ foreach my $override_class ($cgi->param) {
     'link_table'   => 'part_pkg_taxoverride',
     'target_table' => 'tax_class',
     'hashref'      => { 'usage_class' => $class },
-    'params'       => \@tax_overrides,
+    'params'       => [ @tax_overrides ],
   };
 
 }