1 <% include( 'elements/edit.html',
2 'post_url' => popurl(1).'process/part_pkg.cgi',
3 'name' => "Package definition",
7 'agent_null_right' => $edit_global,
8 'agent_clone_extra_sql' => FS::part_pkg->curuser_pkgs_sql,
10 #'viewall_dir' => 'browse',
11 'viewall_url' => $p.'browse/part_pkg.cgi',
12 'html_init' => include('/elements/init_overlib.html').
14 'html_bottom' => $html_bottom,
16 'onLoad="agent_changed(document.edit_topform.agentnum)"',
18 'begin_callback' => $begin_callback,
19 'end_callback' => $end_callback,
20 'new_hashref_callback' => $new_hashref_callback,
21 'new_object_callback' => $new_object_callback,
22 'new_callback' => $new_callback,
23 'clone_callback' => $clone_callback,
24 'edit_callback' => $edit_callback,
25 'error_callback' => $error_callback,
26 'field_callback' => $field_callback,
29 'pkgpart' => 'Package Definition',
30 'pkg' => 'Package (customer-visible)',
31 'comment' => 'Comment (customer-hidden)',
32 'classnum' => '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 'pay_weight' => 'Payment weight',
42 'credit_weight' => 'Credit weight',
43 'agentnum' => 'Agent',
44 'setup_fee' => 'Setup fee',
45 'recur_fee' => 'Recurring fee',
46 'bill_dst_pkgpart' => 'Include line item(s) from package',
47 'svc_dst_pkgpart' => 'Include services of package',
51 { field=>'clone', type=>'hidden',
52 curr_value_callback =>
53 sub { shift->param('clone') },
55 { field=>'pkgnum', type=>'hidden',
56 curr_value_callback =>
57 sub { shift->param('pkgnum') },
60 { type => 'columnstart' },
67 {field=>'comment', type=>'text', size=>40 }, #32
68 { field => 'agentnum',
69 type => 'select-agent',
70 disable_empty => ! $acl_edit_global,
71 empty_label => '(global)',
72 onchange => 'agent_changed',
74 {field=>'classnum', type=>'select-pkg_class' },
75 {field=>'disabled', type=>$disabled_type, value=>'Y'},
77 { type => 'tablebreak-tr-title',
78 value => 'Pricing', #better name?
81 type => 'selectlayers-select',
82 options => [ keys %plan_labels ],
83 labels => \%plan_labels,
85 { field => 'setup_fee',
89 type => 'part_pkg_freq',
90 onchange => 'freq_changed',
92 { field => 'recur_fee',
94 disabled => sub { $recur_disabled },
100 #recurring fee (auto-disable)
102 { type => 'columnnext' },
104 {type=>'justtitle', value=>'Taxation' },
105 {field=>'setuptax', type=>'checkbox', value=>'Y'},
106 {field=>'recurtax', type=>'checkbox', value=>'Y'},
107 {field=>'taxclass', type=>'select-taxclass' },
108 { field => 'taxproductnums',
110 value => join(',', @taxproductnums),
112 { field => 'taxproduct_select',
113 type => 'selectlayers',
114 options => [ '(default)', @taxproductnums ],
115 curr_value => '(default)',
116 labels => { ( '(default)' => '(default)' ),
117 map {($_=>$usage_class{$_})}
120 layer_fields => \%taxproduct_fields,
121 layer_values_callback => $taxproduct_values,
122 layers_only => !$taxproducts,
123 cell_style => ( !$taxproducts
129 { type => 'tablebreak-tr-title',
130 value => 'Promotions', #better name?
132 { field=>'promo_code', type=>'text', size=>15 },
134 { type => 'tablebreak-tr-title',
135 value => 'Line-item revenue recogition', #better name?
137 { field=>'pay_weight', type=>'text', size=>6 },
138 { field=>'credit_weight', type=>'text', size=>6 },
140 { type => 'columnnext' },
142 { field => 'agent_type',
143 type => 'select-agent_types',
144 disabled => ! $acl_edit_global,
145 curr_value_callback => sub {
146 my($cgi, $object, $field) = @_;
147 #in the other callbacks..? hmm.
152 { type => 'columnend' },
154 { 'type' => 'tablebreak-tr-title',
155 'value' => 'Pricing add-ons',
157 { 'field' => 'bill_dst_pkgpart',
158 'type' => 'select-part_pkg',
159 'm2_label' => 'Include line item(s) from package',
160 'm2m_method' => 'bill_part_pkg_link',
161 'm2m_dstcol' => 'dst_pkgpart',
162 'm2_error_callback' =>
163 &{$m2_error_callback_maker}('bill'),
166 { type => 'tablebreak-tr-title',
169 { type => 'pkg_svc', },
171 { 'field' => 'svc_dst_pkgpart',
172 'label' => 'Also include services from package: ',
173 'type' => 'select-part_pkg',
174 'm2_label' => 'Include services of package: ',
175 'm2m_method' => 'svc_part_pkg_link',
176 'm2m_dstcol' => 'dst_pkgpart',
177 'm2_error_callback' =>
178 &{$m2_error_callback_maker}('svc'),
181 { type => 'tablebreak-tr-title',
182 value => 'Price plan options',
191 my $curuser = $FS::CurrentUser::CurrentUser;
193 my $edit_global = 'Edit global package definitions';
194 my $acl_edit = $curuser->access_right('Edit package definitions');
195 my $acl_edit_global = $curuser->access_right($edit_global);
197 my $acl_edit_either = $acl_edit || $acl_edit_global;
199 my $begin_callback = sub {
200 my( $cgi, $fields, $opt ) = @_;
202 unless $acl_edit_either
203 || ( $cgi->param('pkgnum')
204 && $curuser->access_right('Customize customer package')
208 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
210 my $conf = new FS::Conf;
211 my $taxproducts = $conf->exists('enable_taxproducts');
214 # - tr-part_pkg_freq: month_increments_only (from price plans)
216 # - test errors cloning
217 # - test custom pricing
218 # - move the selectlayer divs away from lame layer_callback
220 #my ($query) = $cgi->keywords;
225 my %tax_override = ();
227 my %taxproductnums = map { ($_->classnum => 1) }
228 qsearch('usage_class', { 'disabled' => '' });
229 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
232 my $recur_disabled = 1;
234 my $error_callback = sub {
235 my($cgi, $object, $fields, $opt ) = @_;
237 (@agent_type) = $cgi->param('agent_type');
239 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
241 $recur_disabled = $cgi->param('freq') ? 0 : 1;
243 foreach ($cgi->param) {
244 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
246 $tax_override{''} = $cgi->param('tax_override');
247 $tax_override{$_} = $cgi->param('tax_override_$_')
248 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
250 #some false laziness w/process
251 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
253 my $options = $cgi->param($plan."__OPTIONS");
254 my @options = split(',', $options);
256 map { my $optionname = $_;
257 my $param = $plan."__$optionname";
258 my $value = join(', ', $cgi->param($param));
259 ( $optionname => $value );
263 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
264 $object->set($_ => scalar($cgi->param($_)) )
265 foreach (qw( setup_fee recur_fee ));
269 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
271 my $new_object_callback = sub {
272 my( $cgi, $hashref, $fields, $opt ) = @_;
274 my $part_pkg = FS::part_pkg->new( $hashref );
275 $part_pkg->set($_ => '0')
276 foreach (qw( setup_fee recur_fee ));
282 my $edit_callback = sub {
283 my( $cgi, $object, $fields, $opt ) = @_;
285 $recur_disabled = $object->freq ? 0 : 1;
287 (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
289 foreach ($object->options) {
290 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
292 foreach ($object->part_pkg_taxoverride) {
293 $taxproductnums{$_->usage_class} = 1
297 %options = $object->options;
299 $object->set($_ => $object->option($_))
300 foreach (qw( setup_fee recur_fee ));
304 my $new_callback = sub {
305 my( $cgi, $object, $fields ) = @_;
307 my $conf = new FS::Conf;
308 if ( $conf->exists('agent_defaultpkg') ) {
309 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
310 @agent_type = map {$_->typenum} qsearch('agent_type',{});
315 my $clone_callback = sub {
316 my( $cgi, $object, $fields, $opt ) = @_;
318 if ( $cgi->param('pkgnum') ) {
320 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
321 $object->agentnum( $cust_pkg->cust_main->agentnum );
323 $opt->{action} = 'Custom';
325 #my $part_pkg = $clone_part_pkg->clone;
326 #this is all clone did anyway
327 $object->comment( '(CUSTOM) '. $object->comment )
328 unless $object->comment =~ /^\(CUSTOM\) /;
330 $object->disabled('Y');
334 %options = $object->options;
336 $object->set($_ => $options{$_})
337 foreach (qw( setup_fee recur_fee ));
339 $recur_disabled = $object->freq ? 0 : 1;
342 my $m2_error_callback_maker = sub {
343 my $link_type = shift; #yay closures
345 my( $cgi, $object ) = @_;
347 new FS::part_pkg_link {
348 'link_type' => $link_type,
349 'src_pkgpart' => $object->pkgpart,
355 grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
359 my $javascript = <<'END';
360 <SCRIPT TYPE="text/javascript">
362 function freq_changed(what) {
363 var freq = what.options[what.selectedIndex].value;
366 what.form.recur_fee.disabled = true;
367 what.form.recur_fee.style.backgroundColor = '#dddddd';
369 what.form.recur_fee.disabled = false;
370 what.form.recur_fee.style.backgroundColor = '#ffffff';
375 function agent_changed(what) {
377 var agentnum = what.options[what.selectedIndex].value;
379 if ( agentnum == 0 ) {
380 what.form.agent_type.disabled = false;
381 //what.form.agent_type.style.backgroundColor = '#ffffff';
382 what.form.agent_type.style.visibility = '';
384 what.form.agent_type.disabled = true;
385 //what.form.agent_type.style.backgroundColor = '#dddddd';
386 what.form.agent_type.style.visibility = 'hidden';
394 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
396 tie my %plan_labels, 'Tie::IxHash',
397 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
400 my $html_bottom = sub {
403 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
405 my $layer_callback = sub {
408 my $html = ntable("#cccccc",2);
412 # <TD ALIGN="right">Recurring fee frequency </TD>
413 # <TD><SELECT NAME="freq">
416 #my @freq = keys %freq;
417 #@freq = grep { /^\d+$/ } @freq
418 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
419 #foreach my $freq ( @freq ) {
420 # $html .= qq(<OPTION VALUE="$freq");
421 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
422 # $html .= ">$freq{$freq}";
424 #$html .= '</SELECT></TD></TR>';
426 my $href = $plans{$layer}->{'fields'};
427 my @fields = exists($plans{$layer}->{'fieldorder'})
428 ? @{$plans{$layer}->{'fieldorder'}}
431 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
433 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
435 my $format = sub { shift };
436 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
438 #XXX these should use elements/ fields... (or this whole thing should
439 #just use layer_fields instead of layer_callback)
441 if ( ! exists($href->{$field}{'type'}) ) {
443 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
444 ( exists($options{$field})
445 ? &$format($options{$field})
446 : $href->{$field}{'default'} ).
449 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
451 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
452 ( exists($options{$field}) && $options{$field}
457 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
461 if $href->{$field}{'type'} eq 'select_multiple';
462 $html .= qq! NAME="${layer}__$field">!;
464 if ( $href->{$field}{'select_table'} ) {
466 qsearch( $href->{$field}{'select_table'},
467 $href->{$field}{'select_hash'} )
469 my $value = $record->getfield($href->{$field}{'select_key'});
470 $html .= qq!<OPTION VALUE="$value"!.
471 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
475 '>'. $record->getfield($href->{$field}{'select_label'});
477 } elsif ( $href->{$field}{'select_options'} ) {
478 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
479 my $label = $href->{$field}{'select_options'}{$key};
480 $html .= qq!<OPTION VALUE="$key"!.
481 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
489 $html .= '<font color="#ff0000">warning: '.
490 "don't know how to retreive options for $field select field".
493 $html .= '</SELECT>';
495 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
498 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
500 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
501 my $label = $href->{$field}{'options'}{$key};
502 $html .= qq!$radio VALUE="$key"!.
503 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
512 $html .= '</TD></TR>';
516 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
517 join(',', keys %{ $href } ). '">';
525 options => [ keys %plan_labels ],
526 labels => \%plan_labels,
527 curr_value => $object->plan,
528 layer_callback => $layer_callback,
532 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
533 '<SCRIPT TYPE="text/javascript">'.
534 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
537 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
540 $return .= '</SCRIPT>';
546 my %usage_class = map { ($_->classnum => $_->classname) }
547 qsearch('usage_class', {});
548 $usage_class{setup} = 'Setup';
549 $usage_class{recur} = 'Recurring';
551 my %taxproduct_fields = ();
552 my $end_callback = sub {
553 my( $cgi, $object, $fields, $opt ) = @_;
555 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
557 if ( $object->pkgpart ) {
558 foreach my $usage_class ( '', @taxproductnums ) {
559 $tax_override{$usage_class} =
560 join (",", map $_->taxclassnum,
561 qsearch( 'part_pkg_taxoverride', {
562 'pkgpart' => $object->pkgpart,
563 'usage_class' => $usage_class,
570 map { $_ => [ "taxproductnum_$_",
571 { type => 'select-taxproduct',
572 #label => "$usage_class{$_} tax product",
575 { type => 'select-taxoverride' }
580 $taxproduct_fields{'(default)'} =
581 [ 'taxproductnum', { type => 'select-taxproduct',
582 #label => 'Default tax product',
584 'tax_override', { type => 'select-taxoverride' },
588 my $taxproduct_values = sub {
589 my ($cgi, $object, $flags) = @_;
591 sub { my $layer = shift;
592 my @fields = @{$taxproduct_fields{$layer}};
595 my $field = shift @fields;
597 $field =~ /^taxproductnum_\w+$/ &&
598 push @values, ( $field => $options{"usage_$field"} );
599 $field =~ /^tax_override_(\w+)$/ &&
600 push @values, ( $field => $tax_override{$1} );
601 $field =~ /^taxproductnum$/ &&
602 push @values, ( $field => $object->taxproductnum );
603 $field =~ /^tax_override$/ &&
604 push @values, ( $field => $tax_override{''} );
610 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
615 my $field_callback = sub {
616 my ($cgi, $object, $fieldref) = @_;
618 my $field = $fieldref->{field};
619 if ($field eq 'taxproductnums') {
620 $fieldref->{value} = join(',', @taxproductnums);
621 } elsif ($field eq 'taxproduct_select') {
622 $fieldref->{options} = [ '(default)', @taxproductnums ];
623 $fieldref->{labels} = { ( '(default)' => '(default)' ),
624 map {( $_ => ($usage_class{$_} || $_) )}
627 $fieldref->{layer_fields} = \%taxproduct_fields;
628 $fieldref->{layer_values_callback} = $taxproduct_values;