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