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