soft-limit package names to 50 chars to avoid problems with typeset invoices, RT...
[freeside.git] / httemplate / edit / part_pkg.cgi
index 959fb30..f79e759 100755 (executable)
@@ -23,6 +23,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',
                           },
 
               'fields' => [
-                            { field=>'clone',  type=>'hidden' },
-                            { field=>'pkgnum', type=>'hidden' },
+                            { field=>'clone',  type=>'hidden',
+                              curr_value_callback =>
+                                sub { shift->param('clone') },
+                            },
+                            { field=>'pkgnum', type=>'hidden',
+                              curr_value_callback =>
+                                sub { shift->param('pkgnum') },
+                            },
 
                             { 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'},
@@ -58,7 +69,7 @@
                               },
                               { field    => 'freq',
                                 type     => 'part_pkg_freq',
-                                onchange => 'freq_changed', #XXX enable recurring fee
+                                onchange => 'freq_changed',
                               },
                               { field    => 'recur_fee',
                                 type     => 'money',
                               {type=>'justtitle', value=>'Taxation' },
                               {field=>'setuptax', type=>'checkbox', value=>'Y'},
                               {field=>'recurtax', type=>'checkbox', value=>'Y'},
-                              {field=>'classnum', type=>'select-taxclass' },
-                              {field=>'taxproductnum', type=>'select-taxproduct' },
+                              {field=>'taxclass', type=>'select-taxclass' },
+                              { 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', #XXX better name?
+                                value => 'Promotions', #better name?
                               },
                               { field=>'promo_code', type=>'text', size=>15 },
 
                               { type  => 'tablebreak-tr-title',
-                                value => 'Line-item revenue recogition', #XXX better name?
+                                value => 'Line-item revenue recogition', #better name?
                               },
                               { field=>'pay_weight',    type=>'text', size=>6 },
                               { field=>'credit_weight', type=>'text', size=>6 },
                             { 'type'  => 'tablebreak-tr-title',
                               'value' => 'Pricing add-ons',
                             },
-                            { 'field'    => 'bill_dst_pkgpart',
-                              'type'     => 'select-part_pkg',
-                              'm2_label' => 'Include line item(s) from package',
-                              'm2m_table'        => 'part_pkg_link',
-                              'm2m_target_table' => 'part_pkg', #XXX actually just the method name...
-                              'm2m_dstcol'       => 'dst_pkgpart',
-                              'm2m_static_or_something' => { 'link_type' => 'bill' }, #XXX
-                              'm2_error_callback' => sub { (); }, #XXX existing!
+                            { 'field'      => 'bill_dst_pkgpart',
+                              'type'       => 'select-part_pkg',
+                              'm2_label'   => 'Include line item(s) from package',
+                              'm2m_method' => 'bill_part_pkg_link',
+                              'm2m_dstcol' => 'dst_pkgpart',
+                              'm2_error_callback' =>
+                                &{$m2_error_callback_maker}('bill'),
                             },
 
                             { type  => 'tablebreak-tr-title',
                             },
                             { type => 'pkg_svc', },
 
-                            { 'field'    => 'svc_dst_pkgpart',
-                              'label'    => 'Also include services from package: ',
-                              'type'     => 'select-part_pkg',
-                              'm2_label' => 'Include services of package: ',
-                              'm2m_table'        => 'part_pkg_link',
-                              'm2m_target_table' => 'part_pkg', #XXX actually just the method name...
-                              'm2m_dstcol'       => 'dst_pkgpart',
-                              'm2m_static_or_something' => { 'link_type' => 'svc' }, #XXX
-                              'm2_error_callback' => sub { (); }, #XXX existing!
+                            { 'field'      => 'svc_dst_pkgpart',
+                              'label'      => 'Also include services from package: ',
+                              'type'       => 'select-part_pkg',
+                              'm2_label'   => 'Include services of package: ',
+                              'm2m_method' => 'svc_part_pkg_link',
+                              'm2m_dstcol' => 'dst_pkgpart',
+                              'm2_error_callback' =>
+                                &{$m2_error_callback_maker}('svc'),
                             },
 
                             { type  => 'tablebreak-tr-title',
@@ -148,16 +176,14 @@ die "access denied"
       || $curuser->access_right('Edit global package definitions')
       || ( $cgi->param('pkgnum') && $curuser->access_right('Customize customer package') );
 
+my $conf = new FS::Conf;
+my $taxproducts = $conf->exists('enable_taxproducts');
+
 #XXX
-# - part_pkg.pm bits (need separate access methods not just part_pkg_link)
 # - tr-part_pkg_freq: month_increments_only (from price plans)
-# - test editing 
-#   - write edit bits for m2ms
-# - display add-ons in browse... yeah
-# -QIS- thank goodness
 # - test cloning
+# - test errors cloning
 # - test custom pricing
-#recur_flat->recur_fee migration, ugh
 # - move the selectlayer divs away from lame layer_callback
 
 #my ($query) = $cgi->keywords;
@@ -165,16 +191,42 @@ 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' => '' });
+
+if ( $cgi->param('error') ) {  # oh well
+  foreach ($cgi->param) {
+    /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
+  }
+} elsif ( my $pkgpart = $cgi->keywords || $cgi->param('pkgpart') ) {
+  $pkgpart =~ /^(\d+)$/ or die "illegal pkgpart";
+  my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $pkgpart } );
+  die "no part_pkg for pkgpart $pkgpart" unless $pkgpart;
+  foreach ($part_pkg->options) {
+    /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
+  }
+  foreach ($part_pkg->part_pkg_taxoverride) {
+    $taxproductnums{$_->usage_class} = 1
+      if $_->usage_class;
+  }
+} else {
+  # do nothing
+}
+my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
+
 my %options = ();
 my $recur_disabled = 1;
 my $error_callback = sub {
-  my($cgi, $object, $fields) = @_;
+  my($cgi, $object, $fields, $opt ) = @_;
   (@agent_type) = $cgi->param('agent_type');
-  $tax_override = $cgi->param('tax_override');
+  $tax_override{''} = $cgi->param('tax_override');
+  $tax_override{$_} = $cgi->param('tax_override_$_')
+    foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
+  $opt->{action} = 'Custom' if $cgi->param('clone');
   $clone_part_pkg= qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
 
   $recur_disabled = $cgi->param('freq') ? 0 : 1;
@@ -192,7 +244,9 @@ my $error_callback = sub {
         }
         @options;
 
-  $cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
+  #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
+  $object->set($_ => scalar($cgi->param($_)) )
+    foreach (qw( setup_fee recur_fee ));
 
 };
 
@@ -201,29 +255,40 @@ 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') } );
-    my $part_pkg = $clone_part_pkg->clone;
+    $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;
+    $part_pkg->set($_ => $options{$_})
+      foreach (qw( setup_fee recur_fee ));
+    $recur_disabled = $part_pkg->freq ? 0 : 1;
   } else {
-    FS::part_pkg->new( $hashref );
+    $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 =
+  $tax_override{$_} =
     join (",", map {$_->taxclassnum}
-               qsearch( 'part_pkg_taxoverride', {'pkgpart' => $1} )
-         );
+               qsearch( 'part_pkg_taxoverride', { 'pkgpart' => $object->pkgpart,
+                                                  'usage_class' => $_,
+                                                }
+                      )
+         )
+    foreach ( '', @taxproductnums );
 
 #    join (",", map {$_->taxclassnum}
 #               $part_pkg->part_pkg_taxrate( 'cch', $conf->config('defaultloc')
@@ -232,6 +297,9 @@ my $edit_callback = sub {
 
   %options = $object->options;
 
+  $object->set($_ => $object->option($_))
+    foreach (qw( setup_fee recur_fee ));
+
 };
 
 my $new_callback = sub {
@@ -245,6 +313,23 @@ my $new_callback = sub {
 
 };
 
+my $m2_error_callback_maker = sub {
+  my $link_type = shift; #yay closures
+  return sub {
+    my( $cgi, $object ) = @_;
+      map  {
+             new FS::part_pkg_link {
+               'link_type'   => $link_type,
+               'src_pkgpart' => $object->pkgpart,
+               'dst_pkgpart' => $_,
+             };
+           }
+      grep $_,
+      map  $cgi->param($_),
+      grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
+  };
+};
+
 my $freq_changed = <<'END';
   <SCRIPT TYPE="text/javascript">
 
@@ -341,7 +426,7 @@ my $html_bottom = sub {
           ) {
             my $value = $record->getfield($href->{$field}{'select_key'});
             $html .= qq!<OPTION VALUE="$value"!.
-                     (  $options{$field} =~ /(^|, *)$value *(,|$)/ #XXX fix?
+                     (  $options{$field} =~ /(^|, *)$value *(,|$)/ #?
                           ? ' SELECTED'
                           : ''
                      ).
@@ -351,7 +436,7 @@ my $html_bottom = sub {
           foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
             my $label = $href->{$field}{'select_options'}{$key};
             $html .= qq!<OPTION VALUE="$key"!.
-                     ( $options{$field} =~ /(^|, *)$key *(,|$)/ #XXX fix?
+                     ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
                          ? ' SELECTED'
                          : ''
                      ).
@@ -373,7 +458,7 @@ my $html_bottom = sub {
         foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
           my $label = $href->{$field}{'options'}{$key};
           $html .= qq!$radio VALUE="$key"!.
-                   ( $options{$field} =~ /(^|, *)$key *(,|$)/ #XXX fix?
+                   ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
                        ? ' CHECKED'
                        : ''
                    ).
@@ -404,8 +489,57 @@ my $html_bottom = sub {
   include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
   '<SCRIPT TYPE="text/javascript">'.
     include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 ).
+    "taxproduct_selectchanged(document.getElementById('taxproduct_select'));".
   '</SCRIPT>';
 
 };
 
+my %usage_class = map { ($_->classnum => $_->classname) }
+                  qsearch('usage_class', {});
+$usage_class{setup} = 'Setup';
+$usage_class{recur} = 'Recurring';
+
+my %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>