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