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