fix recurring box graying out on package customize
[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               #'viewall_dir' => 'browse',
6               'viewall_url' => $p.'browse/part_pkg.cgi',
7               'html_init'   => include('/elements/init_overlib.html').
8                                $freq_changed,
9               'html_bottom' => $html_bottom,
10               'new_hashref_callback' => $new_hashref_callback,
11               'new_object_callback'  => $new_object_callback,
12               'new_callback'         => $new_callback,
13               'edit_callback'        => $edit_callback,
14               'error_callback'       => $error_callback,
15
16               'labels' => { 
17                             'pkgpart'          => 'Package Definition',
18                             'pkg'              => 'Package (customer-visible)',
19                             'comment'          => 'Comment (customer-hidden)',
20                             'classnum'         => 'Package class',
21                             'promo_code'       => 'Promotional code',
22                             'freq'             => 'Recurring fee frequency',
23                             'setuptax'         => 'Setup fee tax exempt',
24                             'recurtax'         => 'Recurring fee tax exempt',
25                             'taxclass'         => 'Tax class',
26                             'taxproduct_select'=> 'Tax products',
27                             'plan'             => 'Price plan',
28                             'disabled'         => 'Disable new orders',
29                             'pay_weight'       => 'Payment weight',
30                             'credit_weight'    => 'Credit weight',
31                             'agentnum'         => '',
32                             'setup_fee'        => 'Setup fee',
33                             'recur_fee'        => 'Recurring fee',
34                             'bill_dst_pkgpart' => 'Include line item(s) from package',
35                             'svc_dst_pkgpart'  => 'Include services of package',
36                           },
37
38               'fields' => [
39                             { field=>'clone',  type=>'hidden',
40                               curr_value_callback =>
41                                 sub { shift->param('clone') },
42                             },
43                             { field=>'pkgnum', type=>'hidden',
44                               curr_value_callback =>
45                                 sub { shift->param('pkgnum') },
46                             },
47
48                             { type => 'columnstart' },
49                             
50                               {field=>'pkg',      type=>'text', size=>40 }, #32
51                               {field=>'comment',  type=>'text', size=>40 }, #32
52                               {field=>'classnum', type=>'select-pkg_class' },
53                               {field=>'disabled', type=>'checkbox', value=>'Y'},
54
55                               { type  => 'tablebreak-tr-title',
56                                 value => 'Pricing', #better name?
57                               },
58                               { field => 'plan',
59                                 type  => 'selectlayers-select',
60                                 options => [ keys %plan_labels ],
61                                 labels  => \%plan_labels,
62                               },
63                               { field => 'setup_fee',
64                                 type  => 'money',
65                               },
66                               { field    => 'freq',
67                                 type     => 'part_pkg_freq',
68                                 onchange => 'freq_changed',
69                               },
70                               { field    => 'recur_fee',
71                                 type     => 'money',
72                                 disabled => sub { $recur_disabled },
73                               },
74                                 
75                               #price plan
76                               #setup fee
77                               #recurring frequency
78                               #recurring fee (auto-disable)
79
80                             { type => 'columnnext' },
81
82                               {type=>'justtitle', value=>'Taxation' },
83                               {field=>'setuptax', type=>'checkbox', value=>'Y'},
84                               {field=>'recurtax', type=>'checkbox', value=>'Y'},
85                               {field=>'taxclass', type=>'select-taxclass' },
86                               { field => 'taxproductnums',
87                                 type  => 'hidden',
88                                 value => join(',', @taxproductnums),
89                               },
90                               { field => 'taxproduct_select',
91                                 type  => 'selectlayers',
92                                 options => [ '(default)', @taxproductnums ],
93                                 curr_value => '(default)',
94                                 labels  => { ( '(default)' => '(default)' ),
95                                              map {($_=>$usage_class{$_})}
96                                              @taxproductnums
97                                            },
98                                 layer_fields => \%taxproduct_fields,
99                                 layer_values_callback => $taxproduct_values,
100                                 layers_only  =>   !$taxproducts,
101                                 cell_style   => ( !$taxproducts
102                                                   ? 'display:none'
103                                                   : ''
104                                                 ),
105                               },
106
107                               { type  => 'tablebreak-tr-title',
108                                 value => 'Promotions', #better name?
109                               },
110                               { field=>'promo_code', type=>'text', size=>15 },
111
112                               { type  => 'tablebreak-tr-title',
113                                 value => 'Line-item revenue recogition', #better name?
114                               },
115                               { field=>'pay_weight',    type=>'text', size=>6 },
116                               { field=>'credit_weight', type=>'text', size=>6 },
117
118                             { type => 'columnnext' },
119
120                               { field=>'agent_type',
121                                 type => 'select-agent_types',
122                                 curr_value_callback => sub {
123                                   my($cgi, $object, $field) = @_;
124                                   #in the other callbacks..?  hmm.
125                                   \@agent_type;
126                                 },
127                               },
128
129                             { type => 'columnend' },
130
131                             { 'type'  => 'tablebreak-tr-title',
132                               'value' => 'Pricing add-ons',
133                             },
134                             { 'field'      => 'bill_dst_pkgpart',
135                               'type'       => 'select-part_pkg',
136                               'm2_label'   => 'Include line item(s) from package',
137                               'm2m_method' => 'bill_part_pkg_link',
138                               'm2m_dstcol' => 'dst_pkgpart',
139                               'm2_error_callback' =>
140                                 &{$m2_error_callback_maker}('bill'),
141                             },
142
143                             { type  => 'tablebreak-tr-title',
144                               value => 'Services',
145                             },
146                             { type => 'pkg_svc', },
147
148                             { 'field'      => 'svc_dst_pkgpart',
149                               'label'      => 'Also include services from package: ',
150                               'type'       => 'select-part_pkg',
151                               'm2_label'   => 'Include services of package: ',
152                               'm2m_method' => 'svc_part_pkg_link',
153                               'm2m_dstcol' => 'dst_pkgpart',
154                               'm2_error_callback' =>
155                                 &{$m2_error_callback_maker}('svc'),
156                             },
157
158                             { type  => 'tablebreak-tr-title',
159                               value => 'Price plan options',
160                             },
161
162                           ],
163
164            )
165 %>
166 <%init>
167
168 my $curuser = $FS::CurrentUser::CurrentUser;
169
170 die "access denied"
171   unless $curuser->access_right('Edit package definitions')
172       || $curuser->access_right('Edit global package definitions')
173       || ( $cgi->param('pkgnum') && $curuser->access_right('Customize customer package') );
174
175 my $conf = new FS::Conf;
176 my $taxproducts = $conf->exists('enable_taxproducts');
177
178 #XXX
179 # - tr-part_pkg_freq: month_increments_only (from price plans)
180 # - test cloning
181 # - test errors cloning
182 # - test custom pricing
183 # - move the selectlayer divs away from lame layer_callback
184
185 #my ($query) = $cgi->keywords;
186 #
187 #my $part_pkg = '';
188
189 my @agent_type = ();
190 my %tax_override = ();
191
192 my $clone_part_pkg = '';
193
194 my %taxproductnums = map { ($_->classnum => 1) }
195                      qsearch('usage_class', { 'disabled' => '' });
196
197 if ( $cgi->param('error') ) {  # oh well
198   foreach ($cgi->param) {
199     /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
200   }
201 } elsif ( my $pkgpart = $cgi->keywords || $cgi->param('pkgpart') ) {
202   $pkgpart =~ /^(\d+)$/ or die "illegal pkgpart";
203   my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $pkgpart } );
204   die "no part_pkg for pkgpart $pkgpart" unless $pkgpart;
205   foreach ($part_pkg->options) {
206     /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
207   }
208   foreach ($part_pkg->part_pkg_taxoverride) {
209     $taxproductnums{$_->usage_class} = 1
210       if $_->usage_class;
211   }
212 } else {
213   # do nothing
214 }
215 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
216
217 my %options = ();
218 my $recur_disabled = 1;
219 my $error_callback = sub {
220   my($cgi, $object, $fields, $opt ) = @_;
221   (@agent_type) = $cgi->param('agent_type');
222   $tax_override{''} = $cgi->param('tax_override');
223   $tax_override{$_} = $cgi->param('tax_override_$_')
224     foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
225   $opt->{action} = 'Custom' if $cgi->param('clone');
226   $clone_part_pkg= qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
227
228   $recur_disabled = $cgi->param('freq') ? 0 : 1;
229
230   #some false laziness w/process
231   $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
232   my $plan = $1;
233   my $options = $cgi->param($plan."__OPTIONS");
234   my @options = split(',', $options);
235   %options =
236     map { my $optionname = $_;
237           my $param = $plan."__$optionname";
238           my $value = join(', ', $cgi->param($param));
239           ( $optionname => $value );
240         }
241         @options;
242
243   #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
244   $object->set($_ => scalar($cgi->param($_)) )
245     foreach (qw( setup_fee recur_fee ));
246
247 };
248
249 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
250
251 my $new_object_callback = sub {
252   my( $cgi, $hashref, $fields, $opt ) = @_;
253
254   my $part_pkg = '';
255   if ( $cgi->param('clone') ) {
256     $opt->{action} = 'Custom';
257     $clone_part_pkg = qsearchs('part_pkg', { pkgpart=>$cgi->param('clone') } );
258     $part_pkg = $clone_part_pkg->clone;
259     $part_pkg->disabled('Y');
260     %options = $clone_part_pkg->options;
261     $part_pkg->set($_ => $options{$_})
262       foreach (qw( setup_fee recur_fee ));
263     $recur_disabled = $part_pkg->freq ? 0 : 1;
264   } else {
265     $part_pkg = FS::part_pkg->new( $hashref );
266     $part_pkg->set($_ => '0')
267       foreach (qw( setup_fee recur_fee ));
268   }
269
270   $part_pkg;
271
272 };
273
274 my $edit_callback = sub {
275   my( $cgi, $object, $fields, $opt ) = @_;
276
277   $recur_disabled = $object->freq ? 0 : 1;
278
279   (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
280   $tax_override{$_} =
281     join (",", map {$_->taxclassnum}
282                qsearch( 'part_pkg_taxoverride', { 'pkgpart' => $object->pkgpart,
283                                                   'usage_class' => $_,
284                                                 }
285                       )
286          )
287     foreach ( '', @taxproductnums );
288
289 #    join (",", map {$_->taxclassnum}
290 #               $part_pkg->part_pkg_taxrate( 'cch', $conf->config('defaultloc')
291 #         );
292 #      unless $tax_override;
293
294   %options = $object->options;
295
296   $object->set($_ => $object->option($_))
297     foreach (qw( setup_fee recur_fee ));
298
299 };
300
301 my $new_callback = sub {
302   my( $cgi, $object, $fields ) = @_;
303
304   my $conf = new FS::Conf; 
305   if ( $conf->exists('agent_defaultpkg') ) {
306     #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
307     @agent_type = map {$_->typenum} qsearch('agent_type',{});
308   }
309
310 };
311
312 my $m2_error_callback_maker = sub {
313   my $link_type = shift; #yay closures
314   return sub {
315     my( $cgi, $object ) = @_;
316       map  {
317              new FS::part_pkg_link {
318                'link_type'   => $link_type,
319                'src_pkgpart' => $object->pkgpart,
320                'dst_pkgpart' => $_,
321              };
322            }
323       grep $_,
324       map  $cgi->param($_),
325       grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
326   };
327 };
328
329 my $freq_changed = <<'END';
330   <SCRIPT TYPE="text/javascript">
331
332     function freq_changed(what) {
333       var freq = what.options[what.selectedIndex].value;
334
335       if ( freq == '0' ) {
336         what.form.recur_fee.disabled = true;
337         what.form.recur_fee.style.backgroundColor = '#dddddd';
338       } else {
339         what.form.recur_fee.disabled = false;
340         what.form.recur_fee.style.backgroundColor = '#ffffff';
341       }
342
343     }
344
345   </SCRIPT>
346 END
347
348 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
349
350 tie my %plan_labels, 'Tie::IxHash',
351   map {  $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
352       keys %plans;
353
354 my $html_bottom = sub {
355   my( $object ) = @_;
356
357   #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
358
359   my $layer_callback = sub {
360   
361     my $layer = shift;
362     my $html = ntable("#cccccc",2);
363   
364     #$html .= '
365     #  <TR>
366     #    <TD ALIGN="right">Recurring fee frequency </TD>
367     #    <TD><SELECT NAME="freq">
368     #';
369     #
370     #my @freq = keys %freq;
371     #@freq = grep { /^\d+$/ } @freq
372   #XXX this bit#  #  if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
373     #foreach my $freq ( @freq ) {
374     #  $html .= qq(<OPTION VALUE="$freq");
375     #  $html .= ' SELECTED' if $freq eq $part_pkg->freq;
376     #  $html .= ">$freq{$freq}";
377     #}
378     #$html .= '</SELECT></TD></TR>';
379   
380     my $href = $plans{$layer}->{'fields'};
381     my @fields = exists($plans{$layer}->{'fieldorder'})
382                    ? @{$plans{$layer}->{'fieldorder'}}
383                    : keys %{ $href };
384   
385     foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
386   
387       $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
388   
389       my $format = sub { shift };
390       $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
391
392       #XXX these should use elements/ fields... (or this whole thing should
393       #just use layer_fields instead of layer_callback)
394   
395       if ( ! exists($href->{$field}{'type'}) ) {
396   
397         $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
398                  ( exists($options{$field})
399                      ? &$format($options{$field})
400                      : $href->{$field}{'default'} ).
401                  qq!">!;
402   
403       } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
404   
405         $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
406                  ( exists($options{$field}) && $options{$field}
407                    ? ' CHECKED'
408                    : ''
409                  ). '>';
410   
411       } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
412   
413         $html .= '<SELECT';
414         $html .= ' MULTIPLE'
415           if $href->{$field}{'type'} eq 'select_multiple';
416         $html .= qq! NAME="${layer}__$field">!;
417   
418         if ( $href->{$field}{'select_table'} ) {
419           foreach my $record (
420             qsearch( $href->{$field}{'select_table'},
421                      $href->{$field}{'select_hash'}   )
422           ) {
423             my $value = $record->getfield($href->{$field}{'select_key'});
424             $html .= qq!<OPTION VALUE="$value"!.
425                      (  $options{$field} =~ /(^|, *)$value *(,|$)/ #?
426                           ? ' SELECTED'
427                           : ''
428                      ).
429                      '>'. $record->getfield($href->{$field}{'select_label'});
430           }
431         } elsif ( $href->{$field}{'select_options'} ) {
432           foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
433             my $label = $href->{$field}{'select_options'}{$key};
434             $html .= qq!<OPTION VALUE="$key"!.
435                      ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
436                          ? ' SELECTED'
437                          : ''
438                      ).
439                      '>'. $label;
440           }
441   
442         } else {
443           $html .= '<font color="#ff0000">warning: '.
444                    "don't know how to retreive options for $field select field".
445                    '</font>';
446         }
447         $html .= '</SELECT>';
448   
449       } elsif ( $href->{$field}{'type'} eq 'radio' ) {
450   
451         my $radio =
452           qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
453   
454         foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
455           my $label = $href->{$field}{'options'}{$key};
456           $html .= qq!$radio VALUE="$key"!.
457                    ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
458                        ? ' CHECKED'
459                        : ''
460                    ).
461                    "> $label<BR>";
462         }
463   
464       }
465   
466       $html .= '</TD></TR>';
467     }
468     $html .= '</TABLE>';
469   
470     $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
471              join(',', keys %{ $href } ). '">';
472   
473     $html;
474   
475   };
476
477   my %selectlayers = (
478     field          => 'plan',
479     options        => [ keys %plan_labels ],
480     labels         => \%plan_labels,
481     curr_value     => $object->plan,
482     layer_callback => $layer_callback,
483   );
484
485   include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
486   '<SCRIPT TYPE="text/javascript">'.
487     include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 ).
488     "taxproduct_selectchanged(document.getElementById('taxproduct_select'));".
489   '</SCRIPT>';
490
491 };
492
493 my %usage_class = map { ($_->classnum => $_->classname) }
494                   qsearch('usage_class', {});
495 $usage_class{setup} = 'Setup';
496 $usage_class{recur} = 'Recurring';
497
498 my %taxproduct_fields = map { $_ => [ "taxproductnum_$_", 
499                                       { type  => 'select-taxproduct',
500                                         #label => "$usage_class{$_} tax product",
501                                       },
502                                       "tax_override_$_", 
503                                       { type  => 'select-taxoverride' }
504                                     ]
505                             }
506                          @taxproductnums;
507 $taxproduct_fields{'(default)'} =
508   [ 'taxproductnum', { type => 'select-taxproduct',
509                        #label => 'Default tax product',
510                      },
511     'tax_override',  { type => 'select-taxoverride' },
512   ];
513
514 my $taxproduct_values = sub {
515   my ($cgi, $object, $flags) = @_;
516   my $routine =
517     sub { my $layer = shift;
518           my @fields = @{$taxproduct_fields{$layer}};
519           my @values = ();
520           while( @fields ) {
521             my $field = shift @fields;
522             shift @fields;
523             $field =~ /^taxproductnum_\w+$/ &&
524               push @values, ( $field => $options{"usage_$field"} );
525             $field =~ /^tax_override_(\w+)$/ &&
526               push @values, ( $field => $tax_override{$1} );
527             $field =~ /^taxproductnum$/ &&
528               push @values, ( $field => $object->taxproductnum );
529             $field =~ /^tax_override$/ &&
530               push @values, ( $field => $tax_override{''} );
531           }
532           { (@values) };
533         };
534   
535   my @result = 
536     map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
537   return({ @result });
538   
539 };
540
541 </%init>