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,
11 'begin_callback' => $begin_callback,
12 'end_callback' => $end_callback,
13 'new_hashref_callback' => $new_hashref_callback,
14 'new_object_callback' => $new_object_callback,
15 'new_callback' => $new_callback,
16 'clone_callback' => $clone_callback,
17 'edit_callback' => $edit_callback,
18 'error_callback' => $error_callback,
21 'pkgpart' => 'Package Definition',
22 'pkg' => 'Package (customer-visible)',
23 'comment' => 'Comment (customer-hidden)',
24 'classnum' => 'Package class',
25 'promo_code' => 'Promotional code',
26 'freq' => 'Recurring fee frequency',
27 'setuptax' => 'Setup fee tax exempt',
28 'recurtax' => 'Recurring fee tax exempt',
29 'taxclass' => 'Tax class',
30 'taxproduct_select'=> 'Tax products',
31 'plan' => 'Price plan',
32 'disabled' => 'Disable new orders',
33 'pay_weight' => 'Payment weight',
34 'credit_weight' => 'Credit weight',
36 'setup_fee' => 'Setup fee',
37 'recur_fee' => 'Recurring fee',
38 'bill_dst_pkgpart' => 'Include line item(s) from package',
39 'svc_dst_pkgpart' => 'Include services of package',
43 { field=>'clone', type=>'hidden',
44 curr_value_callback =>
45 sub { shift->param('clone') },
47 { field=>'pkgnum', type=>'hidden',
48 curr_value_callback =>
49 sub { shift->param('pkgnum') },
52 { type => 'columnstart' },
59 {field=>'comment', type=>'text', size=>40 }, #32
60 {field=>'classnum', type=>'select-pkg_class' },
61 {field=>'disabled', type=>$disabled_type, value=>'Y'},
63 { type => 'tablebreak-tr-title',
64 value => 'Pricing', #better name?
67 type => 'selectlayers-select',
68 options => [ keys %plan_labels ],
69 labels => \%plan_labels,
71 { field => 'setup_fee',
75 type => 'part_pkg_freq',
76 onchange => 'freq_changed',
78 { field => 'recur_fee',
80 disabled => sub { $recur_disabled },
86 #recurring fee (auto-disable)
88 { type => 'columnnext' },
90 {type=>'justtitle', value=>'Taxation' },
91 {field=>'setuptax', type=>'checkbox', value=>'Y'},
92 {field=>'recurtax', type=>'checkbox', value=>'Y'},
93 {field=>'taxclass', type=>'select-taxclass' },
94 { field => 'taxproductnums',
96 value => join(',', @taxproductnums),
98 { field => 'taxproduct_select',
99 type => 'selectlayers',
100 options => [ '(default)', @taxproductnums ],
101 curr_value => '(default)',
102 labels => { ( '(default)' => '(default)' ),
103 map {($_=>$usage_class{$_})}
106 layer_fields => \%taxproduct_fields,
107 layer_values_callback => $taxproduct_values,
108 layers_only => !$taxproducts,
109 cell_style => ( !$taxproducts
115 { type => 'tablebreak-tr-title',
116 value => 'Promotions', #better name?
118 { field=>'promo_code', type=>'text', size=>15 },
120 { type => 'tablebreak-tr-title',
121 value => 'Line-item revenue recogition', #better name?
123 { field=>'pay_weight', type=>'text', size=>6 },
124 { field=>'credit_weight', type=>'text', size=>6 },
126 { type => 'columnnext' },
128 { field=>'agent_type',
129 type => 'select-agent_types',
130 curr_value_callback => sub {
131 my($cgi, $object, $field) = @_;
132 #in the other callbacks..? hmm.
137 { type => 'columnend' },
139 { 'type' => 'tablebreak-tr-title',
140 'value' => 'Pricing add-ons',
142 { 'field' => 'bill_dst_pkgpart',
143 'type' => 'select-part_pkg',
144 'm2_label' => 'Include line item(s) from package',
145 'm2m_method' => 'bill_part_pkg_link',
146 'm2m_dstcol' => 'dst_pkgpart',
147 'm2_error_callback' =>
148 &{$m2_error_callback_maker}('bill'),
151 { type => 'tablebreak-tr-title',
154 { type => 'pkg_svc', },
156 { 'field' => 'svc_dst_pkgpart',
157 'label' => 'Also include services from package: ',
158 'type' => 'select-part_pkg',
159 'm2_label' => 'Include services of package: ',
160 'm2m_method' => 'svc_part_pkg_link',
161 'm2m_dstcol' => 'dst_pkgpart',
162 'm2_error_callback' =>
163 &{$m2_error_callback_maker}('svc'),
166 { type => 'tablebreak-tr-title',
167 value => 'Price plan options',
176 my $curuser = $FS::CurrentUser::CurrentUser;
178 my $edit_right = $curuser->access_right('Edit package definitions')
179 || $curuser->access_right('Edit global package definitions');
181 my $begin_callback = sub {
182 my( $cgi, $fields, $opt ) = @_;
185 || ( $cgi->param('pkgnum')
186 && $curuser->access_right('Customize customer package')
190 my $disabled_type = $edit_right ? 'checkbox' : 'hidden';
192 my $conf = new FS::Conf;
193 my $taxproducts = $conf->exists('enable_taxproducts');
196 # - tr-part_pkg_freq: month_increments_only (from price plans)
198 # - test errors cloning
199 # - test custom pricing
200 # - move the selectlayer divs away from lame layer_callback
202 #my ($query) = $cgi->keywords;
207 my %tax_override = ();
209 my %taxproductnums = map { ($_->classnum => 1) }
210 qsearch('usage_class', { 'disabled' => '' });
213 my $recur_disabled = 1;
215 my $error_callback = sub {
216 my($cgi, $object, $fields, $opt ) = @_;
218 (@agent_type) = $cgi->param('agent_type');
220 $opt->{action} = 'Custom' if $cgi->param('clone');
222 $recur_disabled = $cgi->param('freq') ? 0 : 1;
224 foreach ($cgi->param) {
225 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
227 $tax_override{''} = $cgi->param('tax_override');
228 $tax_override{$_} = $cgi->param('tax_override_$_')
229 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
231 #some false laziness w/process
232 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
234 my $options = $cgi->param($plan."__OPTIONS");
235 my @options = split(',', $options);
237 map { my $optionname = $_;
238 my $param = $plan."__$optionname";
239 my $value = join(', ', $cgi->param($param));
240 ( $optionname => $value );
244 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
245 $object->set($_ => scalar($cgi->param($_)) )
246 foreach (qw( setup_fee recur_fee ));
250 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
252 my $new_object_callback = sub {
253 my( $cgi, $hashref, $fields, $opt ) = @_;
255 my $part_pkg = FS::part_pkg->new( $hashref );
256 $part_pkg->set($_ => '0')
257 foreach (qw( setup_fee recur_fee ));
263 my $edit_callback = sub {
264 my( $cgi, $object, $fields, $opt ) = @_;
266 $recur_disabled = $object->freq ? 0 : 1;
268 (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
270 foreach ($object->options) {
271 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
273 foreach ($object->part_pkg_taxoverride) {
274 $taxproductnums{$_->usage_class} = 1
278 %options = $object->options;
280 $object->set($_ => $object->option($_))
281 foreach (qw( setup_fee recur_fee ));
285 my $new_callback = sub {
286 my( $cgi, $object, $fields ) = @_;
288 my $conf = new FS::Conf;
289 if ( $conf->exists('agent_defaultpkg') ) {
290 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
291 @agent_type = map {$_->typenum} qsearch('agent_type',{});
296 my $clone_callback = sub {
297 my( $cgi, $object, $fields, $opt ) = @_;
299 $opt->{action} = 'Custom';
301 #my $part_pkg = $clone_part_pkg->clone;
302 #this is all clone did anyway
303 $object->comment( '(CUSTOM) '. $object->comment )
304 unless $object->comment =~ /^\(CUSTOM\) /;
306 $object->disabled('Y');
308 %options = $object->options;
310 $object->set($_ => $options{$_})
311 foreach (qw( setup_fee recur_fee ));
313 $recur_disabled = $object->freq ? 0 : 1;
316 my $m2_error_callback_maker = sub {
317 my $link_type = shift; #yay closures
319 my( $cgi, $object ) = @_;
321 new FS::part_pkg_link {
322 'link_type' => $link_type,
323 'src_pkgpart' => $object->pkgpart,
329 grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
333 my $freq_changed = <<'END';
334 <SCRIPT TYPE="text/javascript">
336 function freq_changed(what) {
337 var freq = what.options[what.selectedIndex].value;
340 what.form.recur_fee.disabled = true;
341 what.form.recur_fee.style.backgroundColor = '#dddddd';
343 what.form.recur_fee.disabled = false;
344 what.form.recur_fee.style.backgroundColor = '#ffffff';
352 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
354 tie my %plan_labels, 'Tie::IxHash',
355 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
358 my $html_bottom = sub {
361 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
363 my $layer_callback = sub {
366 my $html = ntable("#cccccc",2);
370 # <TD ALIGN="right">Recurring fee frequency </TD>
371 # <TD><SELECT NAME="freq">
374 #my @freq = keys %freq;
375 #@freq = grep { /^\d+$/ } @freq
376 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
377 #foreach my $freq ( @freq ) {
378 # $html .= qq(<OPTION VALUE="$freq");
379 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
380 # $html .= ">$freq{$freq}";
382 #$html .= '</SELECT></TD></TR>';
384 my $href = $plans{$layer}->{'fields'};
385 my @fields = exists($plans{$layer}->{'fieldorder'})
386 ? @{$plans{$layer}->{'fieldorder'}}
389 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
391 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
393 my $format = sub { shift };
394 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
396 #XXX these should use elements/ fields... (or this whole thing should
397 #just use layer_fields instead of layer_callback)
399 if ( ! exists($href->{$field}{'type'}) ) {
401 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
402 ( exists($options{$field})
403 ? &$format($options{$field})
404 : $href->{$field}{'default'} ).
407 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
409 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
410 ( exists($options{$field}) && $options{$field}
415 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
419 if $href->{$field}{'type'} eq 'select_multiple';
420 $html .= qq! NAME="${layer}__$field">!;
422 if ( $href->{$field}{'select_table'} ) {
424 qsearch( $href->{$field}{'select_table'},
425 $href->{$field}{'select_hash'} )
427 my $value = $record->getfield($href->{$field}{'select_key'});
428 $html .= qq!<OPTION VALUE="$value"!.
429 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
433 '>'. $record->getfield($href->{$field}{'select_label'});
435 } elsif ( $href->{$field}{'select_options'} ) {
436 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
437 my $label = $href->{$field}{'select_options'}{$key};
438 $html .= qq!<OPTION VALUE="$key"!.
439 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
447 $html .= '<font color="#ff0000">warning: '.
448 "don't know how to retreive options for $field select field".
451 $html .= '</SELECT>';
453 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
456 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
458 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
459 my $label = $href->{$field}{'options'}{$key};
460 $html .= qq!$radio VALUE="$key"!.
461 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
470 $html .= '</TD></TR>';
474 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
475 join(',', keys %{ $href } ). '">';
483 options => [ keys %plan_labels ],
484 labels => \%plan_labels,
485 curr_value => $object->plan,
486 layer_callback => $layer_callback,
490 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
491 '<SCRIPT TYPE="text/javascript">'.
492 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
495 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
498 $return .= '</SCRIPT>';
504 my %usage_class = map { ($_->classnum => $_->classname) }
505 qsearch('usage_class', {});
506 $usage_class{setup} = 'Setup';
507 $usage_class{recur} = 'Recurring';
509 my @taxproductnums = ();
510 my %taxproduct_fields = ();
511 my $end_callback = sub {
512 my( $cgi, $object, $fields, $opt ) = @_;
514 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
516 if ( $object->pkgpart ) {
517 foreach my $usage_class ( '', @taxproductnums ) {
518 $tax_override{$usage_class} =
519 join (",", map $_->taxclassnum,
520 qsearch( 'part_pkg_taxoverride', {
521 'pkgpart' => $object->pkgpart,
522 'usage_class' => $usage_class,
529 map { $_ => [ "taxproductnum_$_",
530 { type => 'select-taxproduct',
531 #label => "$usage_class{$_} tax product",
534 { type => 'select-taxoverride' }
539 $taxproduct_fields{'(default)'} =
540 [ 'taxproductnum', { type => 'select-taxproduct',
541 #label => 'Default tax product',
543 'tax_override', { type => 'select-taxoverride' },
547 my $taxproduct_values = sub {
548 my ($cgi, $object, $flags) = @_;
550 sub { my $layer = shift;
551 my @fields = @{$taxproduct_fields{$layer}};
554 my $field = shift @fields;
556 $field =~ /^taxproductnum_\w+$/ &&
557 push @values, ( $field => $options{"usage_$field"} );
558 $field =~ /^tax_override_(\w+)$/ &&
559 push @values, ( $field => $tax_override{$1} );
560 $field =~ /^taxproductnum$/ &&
561 push @values, ( $field => $object->taxproductnum );
562 $field =~ /^tax_override$/ &&
563 push @values, ( $field => $tax_override{''} );
569 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );