1 <% include( 'elements/edit.html',
2 'post_url' => popurl(1).'process/part_pkg.cgi',
3 'name' => "Package definition",
5 #'viewall_dir' => 'browse',
6 'viewall_url' => $p.'browse/part_pkg.cgi',
7 'html_init' => include('/elements/init_overlib.html').
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,
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',
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',
39 { field=>'clone', type=>'hidden',
40 curr_value_callback =>
41 sub { shift->param('clone') },
43 { field=>'pkgnum', type=>'hidden',
44 curr_value_callback =>
45 sub { shift->param('pkgnum') },
48 { type => 'columnstart' },
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'},
55 { type => 'tablebreak-tr-title',
56 value => 'Pricing', #better name?
59 type => 'selectlayers-select',
60 options => [ keys %plan_labels ],
61 labels => \%plan_labels,
63 { field => 'setup_fee',
67 type => 'part_pkg_freq',
68 onchange => 'freq_changed',
70 { field => 'recur_fee',
72 disabled => sub { $recur_disabled },
78 #recurring fee (auto-disable)
80 { type => 'columnnext' },
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',
88 value => join(',', @taxproductnums),
90 { field => 'taxproduct_select',
91 type => 'selectlayers',
92 options => [ '(default)', @taxproductnums ],
93 curr_value => '(default)',
94 labels => { ( '(default)' => '(default)' ),
95 map {($_=>$usage_class{$_})}
98 layer_fields => \%taxproduct_fields,
99 layer_values_callback => $taxproduct_values,
100 layers_only => !$taxproducts,
101 cell_style => ( !$taxproducts
107 { type => 'tablebreak-tr-title',
108 value => 'Promotions', #better name?
110 { field=>'promo_code', type=>'text', size=>15 },
112 { type => 'tablebreak-tr-title',
113 value => 'Line-item revenue recogition', #better name?
115 { field=>'pay_weight', type=>'text', size=>6 },
116 { field=>'credit_weight', type=>'text', size=>6 },
118 { type => 'columnnext' },
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.
129 { type => 'columnend' },
131 { 'type' => 'tablebreak-tr-title',
132 'value' => 'Pricing add-ons',
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'),
143 { type => 'tablebreak-tr-title',
146 { type => 'pkg_svc', },
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'),
158 { type => 'tablebreak-tr-title',
159 value => 'Price plan options',
168 my $curuser = $FS::CurrentUser::CurrentUser;
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') );
175 my $conf = new FS::Conf;
176 my $taxproducts = $conf->exists('enable_taxproducts');
179 # - tr-part_pkg_freq: month_increments_only (from price plans)
181 # - test errors cloning
182 # - test custom pricing
183 # - move the selectlayer divs away from lame layer_callback
185 #my ($query) = $cgi->keywords;
190 my %tax_override = ();
192 my $clone_part_pkg = '';
194 my %taxproductnums = map { ($_->classnum => 1) }
195 qsearch('usage_class', { 'disabled' => '' });
197 if ( $cgi->param('error') ) { # oh well
198 foreach ($cgi->param) {
199 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
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);
208 foreach ($part_pkg->part_pkg_taxoverride) {
209 $taxproductnums{$_->usage_class} = 1
215 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
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') } );
228 $recur_disabled = $cgi->param('freq') ? 0 : 1;
230 #some false laziness w/process
231 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
233 my $options = $cgi->param($plan."__OPTIONS");
234 my @options = split(',', $options);
236 map { my $optionname = $_;
237 my $param = $plan."__$optionname";
238 my $value = join(', ', $cgi->param($param));
239 ( $optionname => $value );
243 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
244 $object->set($_ => scalar($cgi->param($_)) )
245 foreach (qw( setup_fee recur_fee ));
249 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
251 my $new_object_callback = sub {
252 my( $cgi, $hashref, $fields, $opt ) = @_;
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;
265 $part_pkg = FS::part_pkg->new( $hashref );
266 $part_pkg->set($_ => '0')
267 foreach (qw( setup_fee recur_fee ));
274 my $edit_callback = sub {
275 my( $cgi, $object, $fields, $opt ) = @_;
277 $recur_disabled = $object->freq ? 0 : 1;
279 (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
281 join (",", map {$_->taxclassnum}
282 qsearch( 'part_pkg_taxoverride', { 'pkgpart' => $object->pkgpart,
287 foreach ( '', @taxproductnums );
289 # join (",", map {$_->taxclassnum}
290 # $part_pkg->part_pkg_taxrate( 'cch', $conf->config('defaultloc')
292 # unless $tax_override;
294 %options = $object->options;
296 $object->set($_ => $object->option($_))
297 foreach (qw( setup_fee recur_fee ));
301 my $new_callback = sub {
302 my( $cgi, $object, $fields ) = @_;
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',{});
312 my $m2_error_callback_maker = sub {
313 my $link_type = shift; #yay closures
315 my( $cgi, $object ) = @_;
317 new FS::part_pkg_link {
318 'link_type' => $link_type,
319 'src_pkgpart' => $object->pkgpart,
325 grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
329 my $freq_changed = <<'END';
330 <SCRIPT TYPE="text/javascript">
332 function freq_changed(what) {
333 var freq = what.options[what.selectedIndex].value;
336 what.form.recur_fee.disabled = true;
337 what.form.recur_fee.style.backgroundColor = '#dddddd';
339 what.form.recur_fee.disabled = false;
340 what.form.recur_fee.style.backgroundColor = '#ffffff';
348 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
350 tie my %plan_labels, 'Tie::IxHash',
351 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
354 my $html_bottom = sub {
357 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
359 my $layer_callback = sub {
362 my $html = ntable("#cccccc",2);
366 # <TD ALIGN="right">Recurring fee frequency </TD>
367 # <TD><SELECT NAME="freq">
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}";
378 #$html .= '</SELECT></TD></TR>';
380 my $href = $plans{$layer}->{'fields'};
381 my @fields = exists($plans{$layer}->{'fieldorder'})
382 ? @{$plans{$layer}->{'fieldorder'}}
385 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
387 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
389 my $format = sub { shift };
390 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
392 #XXX these should use elements/ fields... (or this whole thing should
393 #just use layer_fields instead of layer_callback)
395 if ( ! exists($href->{$field}{'type'}) ) {
397 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
398 ( exists($options{$field})
399 ? &$format($options{$field})
400 : $href->{$field}{'default'} ).
403 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
405 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
406 ( exists($options{$field}) && $options{$field}
411 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
415 if $href->{$field}{'type'} eq 'select_multiple';
416 $html .= qq! NAME="${layer}__$field">!;
418 if ( $href->{$field}{'select_table'} ) {
420 qsearch( $href->{$field}{'select_table'},
421 $href->{$field}{'select_hash'} )
423 my $value = $record->getfield($href->{$field}{'select_key'});
424 $html .= qq!<OPTION VALUE="$value"!.
425 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
429 '>'. $record->getfield($href->{$field}{'select_label'});
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 *(,|$)/ #?
443 $html .= '<font color="#ff0000">warning: '.
444 "don't know how to retreive options for $field select field".
447 $html .= '</SELECT>';
449 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
452 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
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 *(,|$)/ #?
466 $html .= '</TD></TR>';
470 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
471 join(',', keys %{ $href } ). '">';
479 options => [ keys %plan_labels ],
480 labels => \%plan_labels,
481 curr_value => $object->plan,
482 layer_callback => $layer_callback,
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'));".
493 my %usage_class = map { ($_->classnum => $_->classname) }
494 qsearch('usage_class', {});
495 $usage_class{setup} = 'Setup';
496 $usage_class{recur} = 'Recurring';
498 my %taxproduct_fields = map { $_ => [ "taxproductnum_$_",
499 { type => 'select-taxproduct',
500 #label => "$usage_class{$_} tax product",
503 { type => 'select-taxoverride' }
507 $taxproduct_fields{'(default)'} =
508 [ 'taxproductnum', { type => 'select-taxproduct',
509 #label => 'Default tax product',
511 'tax_override', { type => 'select-taxoverride' },
514 my $taxproduct_values = sub {
515 my ($cgi, $object, $flags) = @_;
517 sub { my $layer = shift;
518 my @fields = @{$taxproduct_fields{$layer}};
521 my $field = 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{''} );
536 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );