suspend_bill package option to continue recurring billing while suspended and part_pk...
[freeside.git] / httemplate / edit / part_pkg.cgi
1 <% include( 'elements/edit.html',
2               'post_url'              => popurl(1).'process/part_pkg.cgi',
3               'name'                  => "Package definition",
4               'table'                 => 'part_pkg',
5
6               'agent_virt'            => 1,
7               'agent_null_right'      => $edit_global,
8               'agent_clone_extra_sql' => $agent_clone_extra_sql,
9               #'viewall_dir'           => 'browse',
10               'viewall_url'           => $p.'browse/part_pkg.cgi',
11               'html_init'             => include('/elements/init_overlib.html').
12                                          $javascript,
13               'html_bottom'           => $html_bottom,
14               'body_etc'              =>
15                 'onLoad="agent_changed(document.edit_topform.agentnum)"',
16
17               'begin_callback'        => $begin_callback,
18               'end_callback'          => $end_callback,
19               'new_hashref_callback'  => $new_hashref_callback,
20               'new_object_callback'   => $new_object_callback,
21               'new_callback'          => $new_callback,
22               'clone_callback'        => $clone_callback,
23               'edit_callback'         => $edit_callback,
24               'error_callback'        => $error_callback,
25               'field_callback'        => $field_callback,
26
27               'labels' => { 
28                             'pkgpart'          => 'Package Definition',
29                             'pkg'              => 'Package (customer-visible)',
30                             'comment'          => 'Comment (customer-hidden)',
31                             'classnum'         => 'Package class',
32                             'addon_classnum'   => 'Restrict additional orders to package class',
33                             'promo_code'       => 'Promotional code',
34                             'freq'             => 'Recurring fee frequency',
35                             'setuptax'         => 'Setup fee tax exempt',
36                             'recurtax'         => 'Recurring fee tax exempt',
37                             'taxclass'         => 'Tax class',
38                             'taxproduct_select'=> 'Tax products',
39                             'plan'             => 'Price plan',
40                             'disabled'         => 'Disable new orders',
41                             'setup_cost'       => 'Setup cost',
42                             'recur_cost'       => 'Recur cost',
43                             'pay_weight'       => 'Payment weight',
44                             'credit_weight'    => 'Credit weight',
45                             'agentnum'         => 'Agent',
46                             'setup_fee'        => 'Setup fee',
47                             'recur_fee'        => 'Recurring fee',
48                             'discountnum'      => 'Offer discounts for longer terms',
49                             'bill_dst_pkgpart' => 'Include line item(s) from package',
50                             'svc_dst_pkgpart'  => 'Include services of package',
51                             'report_option'    => 'Report classes',
52                             'fcc_ds0s'         => 'Voice-grade eqivalents',
53                           },
54
55               'fields' => [
56                             { field=>'clone',  type=>'hidden',
57                               curr_value_callback =>
58                                 sub { shift->param('clone') },
59                             },
60                             { field=>'pkgnum', type=>'hidden',
61                               curr_value_callback =>
62                                 sub { shift->param('pkgnum') },
63                             },
64
65                             { field=>'custom',  type=>'hidden' },
66
67                             { type => 'columnstart' },
68                             
69                               { field     => 'pkg',
70                                 type      => 'text',
71                                 size      => 40, #32
72                                 maxlength => 50,
73                               },
74                               {field=>'comment',  type=>'text', size=>40 }, #32
75                               { field         => 'agentnum',
76                                 type          => 'select-agent',
77                                 disable_empty => ! $acl_edit_global,
78                                 empty_label   => '(global)',
79                                 onchange      => 'agent_changed',
80                               },
81                               {field=>'classnum', type=>'select-pkg_class' },
82                               ( $conf->exists('pkg-addon_classnum')
83                                   ? ( { field=>'addon_classnum',
84                                         type =>'select-pkg_class',
85                                       }
86                                     )
87                                    : ()
88                               ),
89                               {field=>'disabled', type=>$disabled_type, value=>'Y'},
90
91                               { type  => 'tablebreak-tr-title',
92                                 value => 'Pricing', #better name?
93                               },
94                               { field => 'plan',
95                                 type  => 'selectlayers-select',
96                                 options => [ keys %plan_labels ],
97                                 labels  => \%plan_labels,
98                                 onchange => 'aux_planchanged(what);',
99                               },
100                               { field => 'setup_fee',
101                                 type  => 'money',
102                               },
103                               { field    => 'freq',
104                                 type     => 'part_pkg_freq',
105                                 onchange => 'freq_changed',
106                               },
107                               { field    => 'recur_fee',
108                                 type     => 'money',
109                                 disabled => sub { $recur_disabled },
110                               },
111                                 
112                               #price plan
113                               #setup fee
114                               #recurring frequency
115                               #recurring fee (auto-disable)
116
117                             { type => 'columnnext' },
118
119                               {type=>'justtitle', value=>'Taxation' },
120                               {field=>'setuptax', type=>'checkbox', value=>'Y'},
121                               {field=>'recurtax', type=>'checkbox', value=>'Y'},
122                               {field=>'taxclass', type=>'select-taxclass' },
123                               { field => 'taxproductnums',
124                                 type  => 'hidden',
125                                 value => join(',', @taxproductnums),
126                               },
127                               { field => 'taxproduct_select',
128                                 type  => 'selectlayers',
129                                 options => [ '(default)', @taxproductnums ],
130                                 curr_value => '(default)',
131                                 labels  => { ( '(default)' => '(default)' ),
132                                              map {($_=>$usage_class{$_})}
133                                              @taxproductnums
134                                            },
135                                 layer_fields => \%taxproduct_fields,
136                                 layer_values_callback => $taxproduct_values,
137                                 layers_only  =>   !$taxproducts,
138                                 cell_style   => ( !$taxproducts
139                                                   ? 'display:none'
140                                                   : ''
141                                                 ),
142                               },
143
144                               { type  => 'tablebreak-tr-title',
145                                 value => 'Promotions', #better name?
146                               },
147                               { field=>'promo_code', type=>'text', size=>15 },
148
149                               { type  => 'tablebreak-tr-title',
150                                 value => 'Cost tracking', #better name?
151                               },
152                               { field=>'setup_cost', type=>'money', },
153                               { field=>'recur_cost', type=>'money', },
154
155                             { type => 'columnnext' },
156
157                               { field    => 'agent_type',
158                                 type     => 'select-agent_types',
159                                 disabled => ! $acl_edit_global,
160                                 curr_value_callback => sub {
161                                   my($cgi, $object, $field) = @_;
162                                   #in the other callbacks..?  hmm.
163                                   \@agent_type;
164                                 },
165                               },
166
167                               { type  => 'tablebreak-tr-title',
168                                 value => 'Line-item revenue recogition', #better name?
169                               },
170                               { field=>'pay_weight',    type=>'text', size=>6 },
171                               { field=>'credit_weight', type=>'text', size=>6 },
172
173                               ( $conf->exists('cust_pkg-show_fcc_voice_grade_equivalent')
174                                 ? ( 
175                                     { type  => 'tablebreak-tr-title',
176                                       value => 'FCC Form 477 information',
177                                     },
178                                     { field=>'fcc_ds0s', type=>'text', size=>6 },
179                                   )
180                                  : ()
181                               ),
182
183
184                             { type => 'columnend' },
185
186                             { 'type'  => $report_option ? 'tablebreak-tr-title'
187                                                         : 'hidden',
188                               'value' => 'Optional report classes',
189                               'field' => 'census_title',
190                             },
191                             { 'field'    => 'report_option',
192                               'type'     => $report_option ? 'select-table'
193                                                            : 'hidden',
194                               'table'    => 'part_pkg_report_option',
195                               'name_col' => 'name',
196                               'hashref'  => { 'disabled' => '' },
197                               'multiple' => 1,
198                             },
199
200                             { 'type'    => 'tablebreak-tr-title',
201                               'value'   => 'Term discounts',
202                             },
203                             { 'field'      => 'discountnum',
204                               'type'       => 'select-table',
205                               'table'      => 'discount',
206                               'name_col'   => 'name',
207                               'hashref'    => { %$discountnum_hashref },
208                               #'extra_sql'  => 'AND (months IS NOT NULL OR months != 0)',
209                               'empty_label'=> 'Select discount',
210                               'm2_label'   => 'Offer discounts for longer terms',
211                               'm2m_method' => 'part_pkg_discount',
212                               'm2m_dstcol' => 'discountnum',
213                               'm2_error_callback' => $discount_error_callback,
214                             },
215
216                             { 'type'    => 'tablebreak-tr-title',
217                               'value'   => 'Pricing add-ons',
218                               'colspan' => 4,
219                             },
220                             { 'field'      => 'bill_dst_pkgpart',
221                               'type'       => 'select-part_pkg',
222                               'extra_sql'  => sub { $pkgpart
223                                                      ? "AND pkgpart != $pkgpart"
224                                                      : ''
225                                                   },
226                               'm2_label'   => 'Include line item(s) from package',
227                               'm2m_method' => 'bill_part_pkg_link',
228                               'm2m_dstcol' => 'dst_pkgpart',
229                               'm2_error_callback' =>
230                                 &{$m2_error_callback_maker}('bill'),
231                               'm2_fields' => [ { 'field' => 'hidden',
232                                                  'type'  => 'checkbox',
233                                                  'value' => 'Y',
234                                                  'curr_value' => '',
235                                                  'label' => 'Bundle',
236                                                },
237                                              ],
238                             },
239
240                             { type  => 'tablebreak-tr-title',
241                               value => 'Services',
242                             },
243                             { type => 'pkg_svc', },
244
245                             { 'field'      => 'svc_dst_pkgpart',
246                               'label'      => 'Also include services from package: ',
247                               'type'       => 'select-part_pkg',
248                               'extra_sql'  => sub { $pkgpart
249                                                      ? "AND pkgpart != $pkgpart"
250                                                      : ''
251                                                   },
252                               'm2_label'   => 'Include services of package: ',
253                               'm2m_method' => 'svc_part_pkg_link',
254                               'm2m_dstcol' => 'dst_pkgpart',
255                               'm2_error_callback' =>
256                                 &{$m2_error_callback_maker}('svc'),
257                             },
258
259                             { type  => 'tablebreak-tr-title',
260                               value => 'Price plan options',
261                             },
262
263                           ],
264
265            )
266 %>
267 <%init>
268
269 my $curuser = $FS::CurrentUser::CurrentUser;
270
271 my $edit_global = 'Edit global package definitions';
272 my $acl_edit        = $curuser->access_right('Edit package definitions');
273 my $acl_edit_global = $curuser->access_right($edit_global);
274
275 my $acl_edit_either = $acl_edit || $acl_edit_global;
276
277 my $begin_callback = sub {
278   my( $cgi, $fields, $opt ) = @_;
279   die "access denied"
280     unless $acl_edit_either
281         || ( $cgi->param('pkgnum')
282              && $curuser->access_right('Customize customer package')
283            );
284 };
285
286 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
287
288 #arg.  access rights for cloning are Hard.
289 # on the one hand we don't really want cloning (customizing a package) to fail 
290 #  for want of finding the source package in normal usage
291 # on the other hand, we don't want people using the clone link to be able to
292 #  see 
293 my $agent_clone_extra_sql = 
294   ' ( '. FS::part_pkg->curuser_pkgs_sql.
295   "   OR ( part_pkg.custom = 'Y' ) ".
296   ' ) ';
297
298 my $conf = new FS::Conf;
299 my $taxproducts = $conf->exists('enable_taxproducts');
300
301 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
302                        "  WHERE disabled IS NULL OR disabled = ''  ")
303   or die dbh->errstr;
304 $sth->execute or die $sth->errstr;
305 my $report_option = $sth->fetchrow_arrayref->[0];
306
307 #XXX
308 # - tr-part_pkg_freq: month_increments_only (from price plans)
309 # - test cloning
310 # - test errors cloning
311 # - test custom pricing
312 # - move the selectlayer divs away from lame layer_callback
313
314 #my ($query) = $cgi->keywords;
315 #
316 #my $part_pkg = '';
317
318 my @agent_type = ();
319 my %tax_override = ();
320
321 my %taxproductnums = map { ($_->classnum => 1) }
322                      qsearch('usage_class', { 'disabled' => '' });
323 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
324
325 my %options = ();
326 my $recur_disabled = 1;
327
328 my $pkgpart = '';
329
330 my $error_callback = sub {
331   my($cgi, $object, $fields, $opt ) = @_;
332
333   (@agent_type) = $cgi->param('agent_type');
334
335   $opt->{action} = 'Custom' if $cgi->param('pkgnum');
336
337   $recur_disabled = $cgi->param('freq') ? 0 : 1;
338
339   foreach ($cgi->param) {
340     /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
341   }
342   $tax_override{''} = $cgi->param('tax_override');
343   $tax_override{$_} = $cgi->param('tax_override_$_')
344     foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
345
346   #some false laziness w/process
347   $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
348   my $plan = $1;
349   my $options = $cgi->param($plan."__OPTIONS");
350   my @options = split(',', $options);
351   %options =
352     map { my $optionname = $_;
353           my $param = $plan."__$optionname";
354           my $value = join(', ', $cgi->param($param));
355           ( $optionname => $value );
356         }
357         @options;
358
359   #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
360   $object->set($_ => scalar($cgi->param($_)) )
361     foreach (qw( setup_fee recur_fee ));
362
363   $pkgpart = $object->pkgpart;
364
365 };
366
367 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
368
369 my $new_object_callback = sub {
370   my( $cgi, $hashref, $fields, $opt ) = @_;
371
372   my $part_pkg = FS::part_pkg->new( $hashref );
373   $part_pkg->set($_ => '0')
374     foreach (qw( setup_fee recur_fee ));
375
376   $part_pkg;
377
378 };
379
380 my $edit_callback = sub {
381   my( $cgi, $object, $fields, $opt ) = @_;
382
383   $recur_disabled = $object->freq ? 0 : 1;
384
385   (@agent_type) =
386     map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
387
388   my @report_option = ();
389   foreach ($object->options) {
390     /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
391     /^report_option_(\d+)$/ && (push @report_option, $1);
392   }
393   foreach ($object->part_pkg_taxoverride) {
394     $taxproductnums{$_->usage_class} = 1
395       if $_->usage_class;
396   }
397
398   $cgi->param('report_option', join(',', @report_option));
399   foreach my $field ( @$fields ) {
400     next unless ( 
401       ref($field) eq 'HASH' &&
402       $field->{field} &&
403       $field->{field} eq 'report_option'
404     );
405     #$field->{curr_value} = join(',', @report_option);
406     $field->{value} = join(',', @report_option);
407   }
408
409   %options = $object->options;
410
411   $object->set($_ => $object->option($_))
412     foreach (qw( setup_fee recur_fee ));
413
414   $pkgpart = $object->pkgpart;
415
416 };
417
418 my $new_callback = sub {
419   my( $cgi, $object, $fields ) = @_;
420
421   my $conf = new FS::Conf; 
422
423   if ( $conf->exists('agent_defaultpkg') ) {
424     #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
425     @agent_type = map {$_->typenum} qsearch('agent_type',{});
426   }
427
428   $options{'suspend_bill'}=1 if $conf->exists('part_pkg-default_suspend_bill');
429
430 };
431
432 my $clone_callback = sub {
433   my( $cgi, $object, $fields, $opt ) = @_;
434
435   if ( $cgi->param('pkgnum') ) {
436
437     my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
438     $object->agentnum( $cust_pkg->cust_main->agentnum );
439
440     $opt->{action} = 'Custom';
441
442     #my $part_pkg = $clone_part_pkg->clone;
443     #this is all clone does anyway
444     $object->custom('Y');
445
446     $object->disabled('Y');
447
448   } else { #not when cloning...
449
450     (@agent_type) =
451       map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
452
453   }
454
455   %options = $object->options;
456
457   $object->set($_ => $options{$_})
458     foreach (qw( setup_fee recur_fee ));
459
460   $recur_disabled = $object->freq ? 0 : 1;
461 };
462
463 my $discount_error_callback = sub {
464   my( $cgi, $object ) = @_;
465   map {
466         if ( /^discountnum(\d+)$/ &&
467              ( my $discountnum = $cgi->param("discountnum$1") ) )
468         {
469           new FS::part_pkg_discount {
470             'pkgpart'     => $object->pkgpart,
471             'discountnum' => $discountnum,
472           };
473         } else {
474           ();
475         }
476       }
477   $cgi->param;
478 };
479
480 my $m2_error_callback_maker = sub {
481   my $link_type = shift; #yay closures
482   return sub {
483     my( $cgi, $object ) = @_;
484     map {
485
486           if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
487                ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
488           {
489
490             my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
491                          || '';
492             new FS::part_pkg_link {
493               'link_type'   => $link_type,
494               'src_pkgpart' => $object->pkgpart,
495               'dst_pkgpart' => $dst,
496               'hidden'      => $hidden,
497             };
498           } else {
499             ();
500           }
501         }
502     $cgi->param;
503   };
504 };
505
506 my $javascript = <<'END';
507   <SCRIPT TYPE="text/javascript">
508
509     function freq_changed(what) {
510       var freq = what.options[what.selectedIndex].value;
511
512       if ( freq == '0' ) {
513         what.form.recur_fee.disabled = true;
514         what.form.recur_fee.style.backgroundColor = '#dddddd';
515       } else {
516         what.form.recur_fee.disabled = false;
517         what.form.recur_fee.style.backgroundColor = '#ffffff';
518       }
519
520     }
521
522     function agent_changed(what) {
523
524       var agentnum = what.options[what.selectedIndex].value;
525
526       if ( agentnum == 0 ) {
527         what.form.agent_type.disabled = false;
528         //what.form.agent_type.style.backgroundColor = '#ffffff';
529         what.form.agent_type.style.visibility = '';
530       } else {
531         what.form.agent_type.disabled = true;
532         //what.form.agent_type.style.backgroundColor = '#dddddd';
533         what.form.agent_type.style.visibility = 'hidden';
534       }
535
536     }
537
538     function aux_planchanged(what) {
539
540       alert('called!');
541       var plan = what.options[what.selectedIndex].value;
542       var table = document.getElementById('TableNumber7') // XXX NOT ROBUST
543
544       if ( plan == 'flat' || plan == 'prorate' || plan == 'subscription' ) {
545         //table.disabled = false;
546         table.style.visibility = '';
547       } else {
548         //table.disabled = true;
549         table.style.visibility = 'hidden';
550       }
551
552     }
553
554   </SCRIPT>
555 END
556
557 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
558
559 tie my %plan_labels, 'Tie::IxHash',
560   map {  $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
561       keys %plans;
562
563 my $html_bottom = sub {
564   my( $object ) = @_;
565
566   #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
567
568   my $layer_callback = sub {
569   
570     my $layer = shift;
571     my $html = ntable("#cccccc",2);
572   
573     #$html .= '
574     #  <TR>
575     #    <TD ALIGN="right">Recurring fee frequency </TD>
576     #    <TD><SELECT NAME="freq">
577     #';
578     #
579     #my @freq = keys %freq;
580     #@freq = grep { /^\d+$/ } @freq
581   #XXX this bit#  #  if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
582     #foreach my $freq ( @freq ) {
583     #  $html .= qq(<OPTION VALUE="$freq");
584     #  $html .= ' SELECTED' if $freq eq $part_pkg->freq;
585     #  $html .= ">$freq{$freq}";
586     #}
587
588    #$html .= '</SELECT></TD></TR>';
589   
590     my $href = $plans{$layer}->{'fields'};
591     my @fields = exists($plans{$layer}->{'fieldorder'})
592                    ? @{$plans{$layer}->{'fieldorder'}}
593                    : keys %{ $href };
594   
595     foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
596   
597        if(!exists($href->{$field})) {
598         # shouldn't happen
599         warn "nonexistent part_pkg option: '$field'\n";
600         next;
601       }
602
603       $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
604   
605       my $format = sub { shift };
606       $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
607
608       #XXX these should use elements/ fields... (or this whole thing should
609       #just use layer_fields instead of layer_callback)
610   
611       if ( ! exists($href->{$field}{'type'}) ) {
612   
613         $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
614                  ( exists($options{$field})
615                      ? &$format($options{$field})
616                      : $href->{$field}{'default'} ).
617                  qq!">!;
618   
619       } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
620   
621         $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
622                  ( exists($options{$field}) && $options{$field}
623                    ? ' CHECKED'
624                    : ''
625                  ). '>';
626   
627       } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
628   
629         $html .= '<SELECT';
630         $html .= ' MULTIPLE'
631           if $href->{$field}{'type'} eq 'select_multiple';
632         $html .= qq! NAME="${layer}__$field">!;
633
634         $html .= '<OPTION VALUE="">'. $href->{$field}{'empty_label'}
635           if exists($href->{$field}{'disable_empty'})
636                && ! $href->{$field}{'disable_empty'};
637   
638         if ( $href->{$field}{'select_table'} ) {
639           foreach my $record (
640             qsearch( $href->{$field}{'select_table'},
641                      $href->{$field}{'select_hash'}   )
642           ) {
643             my $value = $record->getfield($href->{$field}{'select_key'});
644             $html .= qq!<OPTION VALUE="$value"!.
645                      (  $options{$field} =~ /(^|, *)$value *(,|$)/ #?
646                           ? ' SELECTED'
647                           : ''
648                      ).
649                      '>'. $record->getfield($href->{$field}{'select_label'});
650           }
651         } elsif ( $href->{$field}{'select_options'} ) {
652           foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
653             my $label = $href->{$field}{'select_options'}{$key};
654             $html .= qq!<OPTION VALUE="$key"!.
655                      ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
656                          ? ' SELECTED'
657                          : ''
658                      ).
659                      '>'. $label;
660           }
661   
662         } else {
663           $html .= '<font color="#ff0000">warning: '.
664                    "don't know how to retreive options for $field select field".
665                    '</font>';
666         }
667         $html .= '</SELECT>';
668   
669       } elsif ( $href->{$field}{'type'} eq 'radio' ) {
670   
671         my $radio =
672           qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
673   
674         foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
675           my $label = $href->{$field}{'options'}{$key};
676           $html .= qq!$radio VALUE="$key"!.
677                    ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
678                        ? ' CHECKED'
679                        : ''
680                    ).
681                    "> $label<BR>";
682         }
683   
684       }
685   
686       $html .= '</TD></TR>';
687     }
688     $html .= '</TABLE>';
689   
690     $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
691              join(',', keys %{ $href } ). '">';
692   
693     $html;
694   
695   };
696
697   my %selectlayers = (
698     field          => 'plan',
699     options        => [ keys %plan_labels ],
700     labels         => \%plan_labels,
701     curr_value     => $object->plan,
702     layer_callback => $layer_callback,
703   );
704
705   my $return =
706     include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
707     '<SCRIPT TYPE="text/javascript">'.
708       include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
709
710   $return .=
711     "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
712       if $taxproducts;
713
714   $return .= '</SCRIPT>';
715
716   $return;
717
718 };
719
720 my %usage_class = map { ($_->classnum => $_->classname) }
721                   qsearch('usage_class', {});
722 $usage_class{setup} = 'Setup';
723 $usage_class{recur} = 'Recurring';
724
725 my %taxproduct_fields = ();
726 my $end_callback = sub {
727   my( $cgi, $object, $fields, $opt ) = @_;
728
729   @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
730
731   if ( $object->pkgpart ) {
732     foreach my $usage_class ( '', @taxproductnums ) {
733       $tax_override{$usage_class} =
734         join (",", map $_->taxclassnum,
735                        qsearch( 'part_pkg_taxoverride', {
736                                   'pkgpart'     => $object->pkgpart,
737                                   'usage_class' => $usage_class,
738                               })
739              );
740     }
741   }
742
743   %taxproduct_fields =
744     map { $_ => [ "taxproductnum_$_", 
745                   { type  => 'select-taxproduct',
746                     #label => "$usage_class{$_} tax product",
747                   },
748                   "tax_override_$_", 
749                   { type  => 'select-taxoverride' }
750                 ]
751         }
752         @taxproductnums;
753
754   $taxproduct_fields{'(default)'} =
755     [ 'taxproductnum', { type => 'select-taxproduct',
756                          #label => 'Default tax product',
757                        },
758       'tax_override',  { type => 'select-taxoverride' },
759     ];
760 };
761
762 my $taxproduct_values = sub {
763   my ($cgi, $object, $flags) = @_;
764   my $routine =
765     sub { my $layer = shift;
766           my @fields = @{$taxproduct_fields{$layer}};
767           my @values = ();
768           while( @fields ) {
769             my $field = shift @fields;
770             shift @fields;
771             $field =~ /^taxproductnum_\w+$/ &&
772               push @values, ( $field => $options{"usage_$field"} );
773             $field =~ /^tax_override_(\w+)$/ &&
774               push @values, ( $field => $tax_override{$1} );
775             $field =~ /^taxproductnum$/ &&
776               push @values, ( $field => $object->taxproductnum );
777             $field =~ /^tax_override$/ &&
778               push @values, ( $field => $tax_override{''} );
779           }
780           { (@values) };
781         };
782   
783   my @result = 
784     map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
785   return({ @result });
786   
787 };
788
789 my $field_callback = sub {
790   my ($cgi, $object, $fieldref) = @_;
791
792   my $field = $fieldref->{field};
793   if ($field eq 'taxproductnums') {
794     $fieldref->{value} = join(',', @taxproductnums);
795   } elsif ($field eq 'taxproduct_select') {
796     $fieldref->{options} = [ '(default)', @taxproductnums ];
797     $fieldref->{labels}  = { ( '(default)' => '(default)' ),
798                              map {( $_ => ($usage_class{$_} || $_) )}
799                                @taxproductnums
800                            };
801     $fieldref->{layer_fields} = \%taxproduct_fields;
802     $fieldref->{layer_values_callback} = $taxproduct_values;
803   }
804 };
805
806 my $discountnum_hashref = {
807                             'disabled' => '',
808                             'months' => { 'op' => '>', 'value' => 1 },
809                           };
810
811 </%init>