fix one-time charges and package customization for employees who don't have 'Edit...
[freeside.git] / httemplate / edit / part_pkg.cgi
index 3e0a354..4b519f5 100755 (executable)
@@ -7,9 +7,13 @@
               '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,
 
@@ -23,6 +27,7 @@
                             'setuptax'         => 'Setup fee tax exempt',
                             'recurtax'         => 'Recurring fee tax exempt',
                             'taxclass'         => 'Tax class',
+                            'taxproduct_select'=> 'Tax products',
                             'plan'             => 'Price plan',
                             'disabled'         => 'Disable new orders',
                             'pay_weight'       => 'Payment weight',
 
                             { type => 'columnstart' },
                             
-                              {field=>'pkg',      type=>'text', size=>40 }, #32
+                              { field     => 'pkg',
+                                type      => 'text',
+                                size      => 40, #32
+                                maxlength => 50,
+                              },
                               {field=>'comment',  type=>'text', size=>40 }, #32
                               {field=>'classnum', type=>'select-pkg_class' },
-                              {field=>'disabled', type=>'checkbox', value=>'Y'},
+                              {field=>'disabled', type=>$disabled_type, value=>'Y'},
 
                               { type  => 'tablebreak-tr-title',
                                 value => 'Pricing', #better name?
                               {field=>'setuptax', type=>'checkbox', value=>'Y'},
                               {field=>'recurtax', type=>'checkbox', value=>'Y'},
                               {field=>'taxclass', type=>'select-taxclass' },
-                              {field=>'taxproductnum', type=>'select-taxproduct' },
+                              { field => 'taxproductnums',
+                                type  => 'hidden',
+                                value => join(',', @taxproductnums),
+                              },
+                              { field => 'taxproduct_select',
+                                type  => 'selectlayers',
+                                options => [ '(default)', @taxproductnums ],
+                                curr_value => '(default)',
+                                labels  => { ( '(default)' => '(default)' ),
+                                             map {($_=>$usage_class{$_})}
+                                             @taxproductnums
+                                           },
+                                layer_fields => \%taxproduct_fields,
+                                layer_values_callback => $taxproduct_values,
+                                layers_only  =>   !$taxproducts,
+                                cell_style   => ( !$taxproducts
+                                                  ? 'display:none'
+                                                  : ''
+                                                ),
+                              },
 
                               { type  => 'tablebreak-tr-title',
                                 value => 'Promotions', #better name?
 
 my $curuser = $FS::CurrentUser::CurrentUser;
 
-die "access denied"
-  unless $curuser->access_right('Edit package definitions')
-      || $curuser->access_right('Edit global package definitions')
-      || ( $cgi->param('pkgnum') && $curuser->access_right('Customize customer package') );
+my $edit_right = $curuser->access_right('Edit package definitions')
+              || $curuser->access_right('Edit global package definitions');
+
+my $begin_callback = sub {
+  my( $cgi, $fields, $opt ) = @_;
+  die "access denied"
+    unless $edit_right
+        || ( $cgi->param('pkgnum')
+             && $curuser->access_right('Customize customer package')
+           );
+};
+
+my $disabled_type = $edit_right ? 'checkbox' : 'hidden';
+
+my $conf = new FS::Conf;
+my $taxproducts = $conf->exists('enable_taxproducts');
 
 #XXX
 # - tr-part_pkg_freq: month_increments_only (from price plans)
@@ -164,21 +204,30 @@ die "access denied"
 #my $part_pkg = '';
 
 my @agent_type = ();
-my $tax_override;
+my %tax_override = ();
 
-my $clone_part_pkg = '';
+my %taxproductnums = map { ($_->classnum => 1) }
+                     qsearch('usage_class', { 'disabled' => '' });
 
 my %options = ();
 my $recur_disabled = 1;
+
 my $error_callback = sub {
   my($cgi, $object, $fields, $opt ) = @_;
+
   (@agent_type) = $cgi->param('agent_type');
-  $tax_override = $cgi->param('tax_override');
+
   $opt->{action} = 'Custom' if $cgi->param('clone');
-  $clone_part_pkg= qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
 
   $recur_disabled = $cgi->param('freq') ? 0 : 1;
 
+  foreach ($cgi->param) {
+    /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
+  }
+  $tax_override{''} = $cgi->param('tax_override');
+  $tax_override{$_} = $cgi->param('tax_override_$_')
+    foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
+
   #some false laziness w/process
   $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
   my $plan = $1;
@@ -203,40 +252,28 @@ my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
 my $new_object_callback = sub {
   my( $cgi, $hashref, $fields, $opt ) = @_;
 
-  my $part_pkg = '';
-  if ( $cgi->param('clone') ) {
-    $opt->{action} = 'Custom';
-    $clone_part_pkg = qsearchs('part_pkg', { pkgpart=>$cgi->param('clone') } );
-    $part_pkg = $clone_part_pkg->clone;
-    $part_pkg->disabled('Y');
-    %options = $clone_part_pkg->options;
-    $part_pkg->set($_ => $option{$_})
-      foreach (qw( setup_fee recur_fee ));
-  } else {
-    $part_pkg = FS::part_pkg->new( $hashref );
-    $part_pkg->set($_ => '0')
-      foreach (qw( setup_fee recur_fee ));
-  }
+  my $part_pkg = FS::part_pkg->new( $hashref );
+  $part_pkg->set($_ => '0')
+    foreach (qw( setup_fee recur_fee ));
 
   $part_pkg;
 
 };
 
 my $edit_callback = sub {
-  my( $cgi, $object, $fields ) = @_;
+  my( $cgi, $object, $fields, $opt ) = @_;
 
   $recur_disabled = $object->freq ? 0 : 1;
 
   (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
-  $tax_override =
-    join (",", map {$_->taxclassnum}
-               qsearch( 'part_pkg_taxoverride', {'pkgpart' => $1} )
-         );
 
-#    join (",", map {$_->taxclassnum}
-#               $part_pkg->part_pkg_taxrate( 'cch', $conf->config('defaultloc')
-#         );
-#      unless $tax_override;
+  foreach ($object->options) {
+    /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
+  }
+  foreach ($object->part_pkg_taxoverride) {
+    $taxproductnums{$_->usage_class} = 1
+      if $_->usage_class;
+  }
 
   %options = $object->options;
 
@@ -256,6 +293,26 @@ my $new_callback = sub {
 
 };
 
+my $clone_callback = sub {
+  my( $cgi, $object, $fields, $opt ) = @_;
+
+  $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\) /;
+
+  $object->disabled('Y');
+
+  %options = $object->options;
+
+  $object->set($_ => $options{$_})
+    foreach (qw( setup_fee recur_fee ));
+
+  $recur_disabled = $object->freq ? 0 : 1;
+};
+
 my $m2_error_callback_maker = sub {
   my $link_type = shift; #yay closures
   return sub {
@@ -429,11 +486,89 @@ my $html_bottom = sub {
     layer_callback => $layer_callback,
   );
 
-  include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
-  '<SCRIPT TYPE="text/javascript">'.
-    include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 ).
-  '</SCRIPT>';
+  my $return =
+    include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
+    '<SCRIPT TYPE="text/javascript">'.
+      include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
+
+  $return .=
+    "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
+      if $taxproducts;
+
+  $return .= '</SCRIPT>';
 
+  $return;
+
+};
+
+my %usage_class = map { ($_->classnum => $_->classname) }
+                  qsearch('usage_class', {});
+$usage_class{setup} = 'Setup';
+$usage_class{recur} = 'Recurring';
+
+my @taxproductnums = ();
+my %taxproduct_fields = ();
+my $end_callback = sub {
+  my( $cgi, $object, $fields, $opt ) = @_;
+
+  @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
+
+  if ( $object->pkgpart ) {
+    foreach my $usage_class ( '', @taxproductnums ) {
+      $tax_override{$usage_class} =
+        join (",", map $_->taxclassnum,
+                       qsearch( 'part_pkg_taxoverride', {
+                                  'pkgpart'     => $object->pkgpart,
+                                  'usage_class' => $usage_class,
+                              })
+             );
+    }
+  }
+
+  %taxproduct_fields =
+    map { $_ => [ "taxproductnum_$_", 
+                  { type  => 'select-taxproduct',
+                    #label => "$usage_class{$_} tax product",
+                  },
+                  "tax_override_$_", 
+                  { type  => 'select-taxoverride' }
+                ]
+        }
+        @taxproductnums;
+
+  $taxproduct_fields{'(default)'} =
+    [ 'taxproductnum', { type => 'select-taxproduct',
+                         #label => 'Default tax product',
+                       },
+      'tax_override',  { type => 'select-taxoverride' },
+    ];
+};
+
+my $taxproduct_values = sub {
+  my ($cgi, $object, $flags) = @_;
+  my $routine =
+    sub { my $layer = shift;
+          my @fields = @{$taxproduct_fields{$layer}};
+          my @values = ();
+          while( @fields ) {
+            my $field = shift @fields;
+            shift @fields;
+            $field =~ /^taxproductnum_\w+$/ &&
+              push @values, ( $field => $options{"usage_$field"} );
+            $field =~ /^tax_override_(\w+)$/ &&
+              push @values, ( $field => $tax_override{$1} );
+            $field =~ /^taxproductnum$/ &&
+              push @values, ( $field => $object->taxproductnum );
+            $field =~ /^tax_override$/ &&
+              push @values, ( $field => $tax_override{''} );
+          }
+          { (@values) };
+        };
+  
+  my @result = 
+    map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
+  return({ @result });
+  
 };
 
 </%init>