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