kludge to clone customer packages you otherwise couldn't see, RT#4854
[freeside.git] / httemplate / edit / part_pkg.cgi
index 3efc26c..f404699 100755 (executable)
@@ -1,25 +1,28 @@
 <% include( 'elements/edit.html',
-              'post_url'         => popurl(1).'process/part_pkg.cgi',
-              'name'             => "Package definition",
-              'table'            => 'part_pkg',
-
-              'agent_virt'       => 1,
-              'agent_null_right' => $edit_global,
-
-              #'viewall_dir'      => 'browse',
-              'viewall_url'      => $p.'browse/part_pkg.cgi',
-              'html_init'        => include('/elements/init_overlib.html').
-                                    $freq_changed,
-              'html_bottom'      => $html_bottom,
-
-              'begin_callback'       => $begin_callback,
-              'end_callback'         => $end_callback,
-              'new_hashref_callback' => $new_hashref_callback,
-              'new_object_callback'  => $new_object_callback,
-              'new_callback'         => $new_callback,
-              'clone_callback'       => $clone_callback,
-              'edit_callback'        => $edit_callback,
-              'error_callback'       => $error_callback,
+              'post_url'              => popurl(1).'process/part_pkg.cgi',
+              'name'                  => "Package definition",
+              'table'                 => 'part_pkg',
+
+              'agent_virt'            => 1,
+              'agent_null_right'      => $edit_global,
+              'agent_clone_extra_sql' => $agent_clone_extra_sql,
+              #'viewall_dir'           => 'browse',
+              'viewall_url'           => $p.'browse/part_pkg.cgi',
+              'html_init'             => include('/elements/init_overlib.html').
+                                         $javascript,
+              'html_bottom'           => $html_bottom,
+              'body_etc'              =>
+                'onLoad="agent_changed(document.edit_topform.agentnum)"',
+
+              'begin_callback'        => $begin_callback,
+              'end_callback'          => $end_callback,
+              'new_hashref_callback'  => $new_hashref_callback,
+              'new_object_callback'   => $new_object_callback,
+              'new_callback'          => $new_callback,
+              'clone_callback'        => $clone_callback,
+              'edit_callback'         => $edit_callback,
+              'error_callback'        => $error_callback,
+              'field_callback'        => $field_callback,
 
               'labels' => { 
                             'pkgpart'          => 'Package Definition',
@@ -65,6 +68,7 @@
                                 type          => 'select-agent',
                                 disable_empty => ! $acl_edit_global,
                                 empty_label   => '(global)',
+                                onchange      => 'agent_changed',
                               },
                               {field=>'classnum', type=>'select-pkg_class' },
                               {field=>'disabled', type=>$disabled_type, value=>'Y'},
@@ -202,6 +206,12 @@ my $begin_callback = sub {
 
 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
 
+my $agent_clone_extra_sql = 
+  ' ( '. FS::part_pkg->curuser_pkgs_sql.
+  #kludge to clone custom customer packages you otherwise couldn't see
+  "   OR ( part_pkg.disabled = 'Y' AND part_pkg.comment LIKE '(CUSTOM)%' ) ".
+  ' ) ';
+
 my $conf = new FS::Conf;
 my $taxproducts = $conf->exists('enable_taxproducts');
 
@@ -221,6 +231,7 @@ my %tax_override = ();
 
 my %taxproductnums = map { ($_->classnum => 1) }
                      qsearch('usage_class', { 'disabled' => '' });
+my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
 
 my %options = ();
 my $recur_disabled = 1;
@@ -230,7 +241,7 @@ my $error_callback = sub {
 
   (@agent_type) = $cgi->param('agent_type');
 
-  $opt->{action} = 'Custom' if $cgi->param('clone');
+  $opt->{action} = 'Custom' if $cgi->param('pkgnum');
 
   $recur_disabled = $cgi->param('freq') ? 0 : 1;
 
@@ -309,14 +320,21 @@ my $new_callback = sub {
 my $clone_callback = sub {
   my( $cgi, $object, $fields, $opt ) = @_;
 
-  $opt->{action} = 'Custom';
+  if ( $cgi->param('pkgnum') ) {
+
+    my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
+    $object->agentnum( $cust_pkg->cust_main->agentnum );
+
+    $opt->{action} = 'Custom';
+
+    #my $part_pkg = $clone_part_pkg->clone;
+    #this is all clone did anyway
+    $object->comment( '(CUSTOM) '. $object->comment )
+      unless $object->comment =~ /^\(CUSTOM\) /;
 
-  #my $part_pkg = $clone_part_pkg->clone;
-  #this is all clone did anyway
-  $object->comment( '(CUSTOM) '. $object->comment )
-    unless $object->comment =~ /^\(CUSTOM\) /;
+    $object->disabled('Y');
 
-  $object->disabled('Y');
+  }
 
   %options = $object->options;
 
@@ -343,7 +361,7 @@ my $m2_error_callback_maker = sub {
   };
 };
 
-my $freq_changed = <<'END';
+my $javascript = <<'END';
   <SCRIPT TYPE="text/javascript">
 
     function freq_changed(what) {
@@ -359,6 +377,22 @@ my $freq_changed = <<'END';
 
     }
 
+    function agent_changed(what) {
+
+      var agentnum = what.options[what.selectedIndex].value;
+
+      if ( agentnum == 0 ) {
+        what.form.agent_type.disabled = false;
+        //what.form.agent_type.style.backgroundColor = '#ffffff';
+        what.form.agent_type.style.visibility = '';
+      } else {
+        what.form.agent_type.disabled = true;
+        //what.form.agent_type.style.backgroundColor = '#dddddd';
+        what.form.agent_type.style.visibility = 'hidden';
+      }
+
+    }
+
   </SCRIPT>
 END
 
@@ -519,7 +553,6 @@ my %usage_class = map { ($_->classnum => $_->classname) }
 $usage_class{setup} = 'Setup';
 $usage_class{recur} = 'Recurring';
 
-my @taxproductnums = ();
 my %taxproduct_fields = ();
 my $end_callback = sub {
   my( $cgi, $object, $fields, $opt ) = @_;
@@ -584,4 +617,21 @@ my $taxproduct_values = sub {
   
 };
 
+my $field_callback = sub {
+  my ($cgi, $object, $fieldref) = @_;
+
+  my $field = $fieldref->{field};
+  if ($field eq 'taxproductnums') {
+    $fieldref->{value} = join(',', @taxproductnums);
+  } elsif ($field eq 'taxproduct_select') {
+    $fieldref->{options} = [ '(default)', @taxproductnums ];
+    $fieldref->{labels}  = { ( '(default)' => '(default)' ),
+                             map {( $_ => ($usage_class{$_} || $_) )}
+                               @taxproductnums
+                           };
+    $fieldref->{layer_fields} = \%taxproduct_fields;
+    $fieldref->{layer_values_callback} = $taxproduct_values;
+  }
+};
+
 </%init>