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 'clone_callback' => $clone_callback,
14 'edit_callback' => $edit_callback,
15 'error_callback' => $error_callback,
18 'pkgpart' => 'Package Definition',
19 'pkg' => 'Package (customer-visible)',
20 'comment' => 'Comment (customer-hidden)',
21 'classnum' => 'Package class',
22 'promo_code' => 'Promotional code',
23 'freq' => 'Recurring fee frequency',
24 'setuptax' => 'Setup fee tax exempt',
25 'recurtax' => 'Recurring fee tax exempt',
26 'taxclass' => 'Tax class',
27 'taxproduct_select'=> 'Tax products',
28 'plan' => 'Price plan',
29 'disabled' => 'Disable new orders',
30 'pay_weight' => 'Payment weight',
31 'credit_weight' => 'Credit weight',
33 'setup_fee' => 'Setup fee',
34 'recur_fee' => 'Recurring fee',
35 'bill_dst_pkgpart' => 'Include line item(s) from package',
36 'svc_dst_pkgpart' => 'Include services of package',
40 { field=>'clone', type=>'hidden',
41 curr_value_callback =>
42 sub { shift->param('clone') },
44 { field=>'pkgnum', type=>'hidden',
45 curr_value_callback =>
46 sub { shift->param('pkgnum') },
49 { type => 'columnstart' },
56 {field=>'comment', type=>'text', size=>40 }, #32
57 {field=>'classnum', type=>'select-pkg_class' },
58 {field=>'disabled', type=>'checkbox', value=>'Y'},
60 { type => 'tablebreak-tr-title',
61 value => 'Pricing', #better name?
64 type => 'selectlayers-select',
65 options => [ keys %plan_labels ],
66 labels => \%plan_labels,
68 { field => 'setup_fee',
72 type => 'part_pkg_freq',
73 onchange => 'freq_changed',
75 { field => 'recur_fee',
77 disabled => sub { $recur_disabled },
83 #recurring fee (auto-disable)
85 { type => 'columnnext' },
87 {type=>'justtitle', value=>'Taxation' },
88 {field=>'setuptax', type=>'checkbox', value=>'Y'},
89 {field=>'recurtax', type=>'checkbox', value=>'Y'},
90 {field=>'taxclass', type=>'select-taxclass' },
91 { field => 'taxproductnums',
93 value => join(',', @taxproductnums),
95 { field => 'taxproduct_select',
96 type => 'selectlayers',
97 options => [ '(default)', @taxproductnums ],
98 curr_value => '(default)',
99 labels => { ( '(default)' => '(default)' ),
100 map {($_=>$usage_class{$_})}
103 layer_fields => \%taxproduct_fields,
104 layer_values_callback => $taxproduct_values,
105 layers_only => !$taxproducts,
106 cell_style => ( !$taxproducts
112 { type => 'tablebreak-tr-title',
113 value => 'Promotions', #better name?
115 { field=>'promo_code', type=>'text', size=>15 },
117 { type => 'tablebreak-tr-title',
118 value => 'Line-item revenue recogition', #better name?
120 { field=>'pay_weight', type=>'text', size=>6 },
121 { field=>'credit_weight', type=>'text', size=>6 },
123 { type => 'columnnext' },
125 { field=>'agent_type',
126 type => 'select-agent_types',
127 curr_value_callback => sub {
128 my($cgi, $object, $field) = @_;
129 #in the other callbacks..? hmm.
134 { type => 'columnend' },
136 { 'type' => 'tablebreak-tr-title',
137 'value' => 'Pricing add-ons',
139 { 'field' => 'bill_dst_pkgpart',
140 'type' => 'select-part_pkg',
141 'm2_label' => 'Include line item(s) from package',
142 'm2m_method' => 'bill_part_pkg_link',
143 'm2m_dstcol' => 'dst_pkgpart',
144 'm2_error_callback' =>
145 &{$m2_error_callback_maker}('bill'),
148 { type => 'tablebreak-tr-title',
151 { type => 'pkg_svc', },
153 { 'field' => 'svc_dst_pkgpart',
154 'label' => 'Also include services from package: ',
155 'type' => 'select-part_pkg',
156 'm2_label' => 'Include services of package: ',
157 'm2m_method' => 'svc_part_pkg_link',
158 'm2m_dstcol' => 'dst_pkgpart',
159 'm2_error_callback' =>
160 &{$m2_error_callback_maker}('svc'),
163 { type => 'tablebreak-tr-title',
164 value => 'Price plan options',
173 my $curuser = $FS::CurrentUser::CurrentUser;
176 unless $curuser->access_right('Edit package definitions')
177 || $curuser->access_right('Edit global package definitions')
178 || ( $cgi->param('pkgnum') && $curuser->access_right('Customize customer package') );
180 my $conf = new FS::Conf;
181 my $taxproducts = $conf->exists('enable_taxproducts');
184 # - tr-part_pkg_freq: month_increments_only (from price plans)
186 # - test errors cloning
187 # - test custom pricing
188 # - move the selectlayer divs away from lame layer_callback
190 #my ($query) = $cgi->keywords;
195 my %tax_override = ();
197 my %taxproductnums = map { ($_->classnum => 1) }
198 qsearch('usage_class', { 'disabled' => '' });
200 if ( $cgi->param('error') ) { # oh well
201 foreach ($cgi->param) {
202 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
204 } elsif ( my $pkgpart = $cgi->keywords || $cgi->param('pkgpart') ) {
205 $pkgpart =~ /^(\d+)$/ or die "illegal pkgpart";
206 my $part_pkg = qsearchs( 'part_pkg', { pkgpart => $pkgpart } );
207 die "no part_pkg for pkgpart $pkgpart" unless $pkgpart;
208 foreach ($part_pkg->options) {
209 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
211 foreach ($part_pkg->part_pkg_taxoverride) {
212 $taxproductnums{$_->usage_class} = 1
218 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
221 my $recur_disabled = 1;
222 my $error_callback = sub {
223 my($cgi, $object, $fields, $opt ) = @_;
224 (@agent_type) = $cgi->param('agent_type');
225 $tax_override{''} = $cgi->param('tax_override');
226 $tax_override{$_} = $cgi->param('tax_override_$_')
227 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
228 $opt->{action} = 'Custom' if $cgi->param('clone');
230 $recur_disabled = $cgi->param('freq') ? 0 : 1;
232 #some false laziness w/process
233 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
235 my $options = $cgi->param($plan."__OPTIONS");
236 my @options = split(',', $options);
238 map { my $optionname = $_;
239 my $param = $plan."__$optionname";
240 my $value = join(', ', $cgi->param($param));
241 ( $optionname => $value );
245 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
246 $object->set($_ => scalar($cgi->param($_)) )
247 foreach (qw( setup_fee recur_fee ));
251 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
253 my $new_object_callback = sub {
254 my( $cgi, $hashref, $fields, $opt ) = @_;
256 my $part_pkg = FS::part_pkg->new( $hashref );
257 $part_pkg->set($_ => '0')
258 foreach (qw( setup_fee recur_fee ));
264 my $edit_callback = sub {
265 my( $cgi, $object, $fields, $opt ) = @_;
267 $recur_disabled = $object->freq ? 0 : 1;
269 (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
271 join (",", map {$_->taxclassnum}
272 qsearch( 'part_pkg_taxoverride', { 'pkgpart' => $object->pkgpart,
277 foreach ( '', @taxproductnums );
279 # join (",", map {$_->taxclassnum}
280 # $part_pkg->part_pkg_taxrate( 'cch', $conf->config('defaultloc')
282 # unless $tax_override;
284 %options = $object->options;
286 $object->set($_ => $object->option($_))
287 foreach (qw( setup_fee recur_fee ));
291 my $new_callback = sub {
292 my( $cgi, $object, $fields ) = @_;
294 my $conf = new FS::Conf;
295 if ( $conf->exists('agent_defaultpkg') ) {
296 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
297 @agent_type = map {$_->typenum} qsearch('agent_type',{});
302 my $clone_callback = sub {
303 my( $cgi, $object, $fields, $opt ) = @_;
305 $opt->{action} = 'Custom';
307 #my $part_pkg = $clone_part_pkg->clone;
308 #this is all clone did anyway
309 $object->comment( '(CUSTOM) '. $object->comment )
310 unless $object->comment =~ /^\(CUSTOM\) /;
312 $object->disabled('Y');
314 %options = $object->options;
316 $object->set($_ => $options{$_})
317 foreach (qw( setup_fee recur_fee ));
319 $recur_disabled = $object->freq ? 0 : 1;
322 my $m2_error_callback_maker = sub {
323 my $link_type = shift; #yay closures
325 my( $cgi, $object ) = @_;
327 new FS::part_pkg_link {
328 'link_type' => $link_type,
329 'src_pkgpart' => $object->pkgpart,
335 grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
339 my $freq_changed = <<'END';
340 <SCRIPT TYPE="text/javascript">
342 function freq_changed(what) {
343 var freq = what.options[what.selectedIndex].value;
346 what.form.recur_fee.disabled = true;
347 what.form.recur_fee.style.backgroundColor = '#dddddd';
349 what.form.recur_fee.disabled = false;
350 what.form.recur_fee.style.backgroundColor = '#ffffff';
358 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
360 tie my %plan_labels, 'Tie::IxHash',
361 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
364 my $html_bottom = sub {
367 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
369 my $layer_callback = sub {
372 my $html = ntable("#cccccc",2);
376 # <TD ALIGN="right">Recurring fee frequency </TD>
377 # <TD><SELECT NAME="freq">
380 #my @freq = keys %freq;
381 #@freq = grep { /^\d+$/ } @freq
382 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
383 #foreach my $freq ( @freq ) {
384 # $html .= qq(<OPTION VALUE="$freq");
385 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
386 # $html .= ">$freq{$freq}";
388 #$html .= '</SELECT></TD></TR>';
390 my $href = $plans{$layer}->{'fields'};
391 my @fields = exists($plans{$layer}->{'fieldorder'})
392 ? @{$plans{$layer}->{'fieldorder'}}
395 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
397 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
399 my $format = sub { shift };
400 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
402 #XXX these should use elements/ fields... (or this whole thing should
403 #just use layer_fields instead of layer_callback)
405 if ( ! exists($href->{$field}{'type'}) ) {
407 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
408 ( exists($options{$field})
409 ? &$format($options{$field})
410 : $href->{$field}{'default'} ).
413 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
415 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
416 ( exists($options{$field}) && $options{$field}
421 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
425 if $href->{$field}{'type'} eq 'select_multiple';
426 $html .= qq! NAME="${layer}__$field">!;
428 if ( $href->{$field}{'select_table'} ) {
430 qsearch( $href->{$field}{'select_table'},
431 $href->{$field}{'select_hash'} )
433 my $value = $record->getfield($href->{$field}{'select_key'});
434 $html .= qq!<OPTION VALUE="$value"!.
435 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
439 '>'. $record->getfield($href->{$field}{'select_label'});
441 } elsif ( $href->{$field}{'select_options'} ) {
442 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
443 my $label = $href->{$field}{'select_options'}{$key};
444 $html .= qq!<OPTION VALUE="$key"!.
445 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
453 $html .= '<font color="#ff0000">warning: '.
454 "don't know how to retreive options for $field select field".
457 $html .= '</SELECT>';
459 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
462 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
464 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
465 my $label = $href->{$field}{'options'}{$key};
466 $html .= qq!$radio VALUE="$key"!.
467 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
476 $html .= '</TD></TR>';
480 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
481 join(',', keys %{ $href } ). '">';
489 options => [ keys %plan_labels ],
490 labels => \%plan_labels,
491 curr_value => $object->plan,
492 layer_callback => $layer_callback,
496 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
497 '<SCRIPT TYPE="text/javascript">'.
498 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
501 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
504 $return .= '</SCRIPT>';
510 my %usage_class = map { ($_->classnum => $_->classname) }
511 qsearch('usage_class', {});
512 $usage_class{setup} = 'Setup';
513 $usage_class{recur} = 'Recurring';
515 my %taxproduct_fields = map { $_ => [ "taxproductnum_$_",
516 { type => 'select-taxproduct',
517 #label => "$usage_class{$_} tax product",
520 { type => 'select-taxoverride' }
524 $taxproduct_fields{'(default)'} =
525 [ 'taxproductnum', { type => 'select-taxproduct',
526 #label => 'Default tax product',
528 'tax_override', { type => 'select-taxoverride' },
531 my $taxproduct_values = sub {
532 my ($cgi, $object, $flags) = @_;
534 sub { my $layer = shift;
535 my @fields = @{$taxproduct_fields{$layer}};
538 my $field = shift @fields;
540 $field =~ /^taxproductnum_\w+$/ &&
541 push @values, ( $field => $options{"usage_$field"} );
542 $field =~ /^tax_override_(\w+)$/ &&
543 push @values, ( $field => $tax_override{$1} );
544 $field =~ /^taxproductnum$/ &&
545 push @values, ( $field => $object->taxproductnum );
546 $field =~ /^tax_override$/ &&
547 push @values, ( $field => $tax_override{''} );
553 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );