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' => $agent_clone_extra_sql,
9 #'viewall_dir' => 'browse',
10 'viewall_url' => $p.'browse/part_pkg.cgi',
11 'html_init' => include('/elements/init_overlib.html').
13 'html_bottom' => $html_bottom,
15 'onLoad="agent_changed(document.edit_topform.agentnum)"',
17 'begin_callback' => $begin_callback,
18 'end_callback' => $end_callback,
19 'new_hashref_callback' => $new_hashref_callback,
20 'new_object_callback' => $new_object_callback,
21 'new_callback' => $new_callback,
22 'clone_callback' => $clone_callback,
23 'edit_callback' => $edit_callback,
24 'error_callback' => $error_callback,
25 'field_callback' => $field_callback,
28 'pkgpart' => 'Package Definition',
29 'pkg' => 'Package (customer-visible)',
30 'comment' => 'Comment (customer-hidden)',
31 'classnum' => 'Package class',
32 'promo_code' => 'Promotional code',
33 'freq' => 'Recurring fee frequency',
34 'setuptax' => 'Setup fee tax exempt',
35 'recurtax' => 'Recurring fee tax exempt',
36 'taxclass' => 'Tax class',
37 'taxproduct_select'=> 'Tax products',
38 'plan' => 'Price plan',
39 'disabled' => 'Disable new orders',
40 'setup_cost' => 'Setup cost',
41 'recur_cost' => 'Recur cost',
42 'pay_weight' => 'Payment weight',
43 'credit_weight' => 'Credit weight',
44 'agentnum' => 'Agent',
45 'setup_fee' => 'Setup fee',
46 'recur_fee' => 'Recurring fee',
47 'bill_dst_pkgpart' => 'Include line item(s) from package',
48 'svc_dst_pkgpart' => 'Include services of package',
49 'report_option' => 'Report classes',
53 { field=>'clone', type=>'hidden',
54 curr_value_callback =>
55 sub { shift->param('clone') },
57 { field=>'pkgnum', type=>'hidden',
58 curr_value_callback =>
59 sub { shift->param('pkgnum') },
62 { field=>'custom', type=>'hidden' },
64 { type => 'columnstart' },
71 {field=>'comment', type=>'text', size=>40 }, #32
72 { field => 'agentnum',
73 type => 'select-agent',
74 disable_empty => ! $acl_edit_global,
75 empty_label => '(global)',
76 onchange => 'agent_changed',
78 {field=>'classnum', type=>'select-pkg_class' },
79 {field=>'disabled', type=>$disabled_type, value=>'Y'},
81 { type => 'tablebreak-tr-title',
82 value => 'Pricing', #better name?
85 type => 'selectlayers-select',
86 options => [ keys %plan_labels ],
87 labels => \%plan_labels,
89 { field => 'setup_fee',
93 type => 'part_pkg_freq',
94 onchange => 'freq_changed',
96 { field => 'recur_fee',
98 disabled => sub { $recur_disabled },
104 #recurring fee (auto-disable)
106 { type => 'columnnext' },
108 {type=>'justtitle', value=>'Taxation' },
109 {field=>'setuptax', type=>'checkbox', value=>'Y'},
110 {field=>'recurtax', type=>'checkbox', value=>'Y'},
111 {field=>'taxclass', type=>'select-taxclass' },
112 { field => 'taxproductnums',
114 value => join(',', @taxproductnums),
116 { field => 'taxproduct_select',
117 type => 'selectlayers',
118 options => [ '(default)', @taxproductnums ],
119 curr_value => '(default)',
120 labels => { ( '(default)' => '(default)' ),
121 map {($_=>$usage_class{$_})}
124 layer_fields => \%taxproduct_fields,
125 layer_values_callback => $taxproduct_values,
126 layers_only => !$taxproducts,
127 cell_style => ( !$taxproducts
133 { type => 'tablebreak-tr-title',
134 value => 'Promotions', #better name?
136 { field=>'promo_code', type=>'text', size=>15 },
138 { type => 'tablebreak-tr-title',
139 value => 'Cost tracking', #better name?
141 { field=>'setup_cost', type=>'money', },
142 { field=>'recur_cost', type=>'money', },
144 { type => 'columnnext' },
146 { field => 'agent_type',
147 type => 'select-agent_types',
148 disabled => ! $acl_edit_global,
149 curr_value_callback => sub {
150 my($cgi, $object, $field) = @_;
151 #in the other callbacks..? hmm.
156 { type => 'tablebreak-tr-title',
157 value => 'Line-item revenue recogition', #better name?
159 { field=>'pay_weight', type=>'text', size=>6 },
160 { field=>'credit_weight', type=>'text', size=>6 },
163 { type => 'columnend' },
165 { 'type' => $census ? 'tablebreak-tr-title'
167 'value' => 'Optional report classes',
168 'field' => 'census_title',
170 { 'field' => 'report_option',
171 'type' => $census ? 'select-table' : 'hidden',
172 'table' => 'part_pkg_report_option',
173 'name_col' => 'name',
178 { 'type' => 'tablebreak-tr-title',
179 'value' => 'Pricing add-ons',
182 { 'field' => 'bill_dst_pkgpart',
183 'type' => 'select-part_pkg',
184 'm2_label' => 'Include line item(s) from package',
185 'm2m_method' => 'bill_part_pkg_link',
186 'm2m_dstcol' => 'dst_pkgpart',
187 'm2_error_callback' =>
188 &{$m2_error_callback_maker}('bill'),
189 'm2_fields' => [ { 'field' => 'hidden',
190 'type' => 'checkbox',
198 { type => 'tablebreak-tr-title',
201 { type => 'pkg_svc', },
203 { 'field' => 'svc_dst_pkgpart',
204 'label' => 'Also include services from package: ',
205 'type' => 'select-part_pkg',
206 'm2_label' => 'Include services of package: ',
207 'm2m_method' => 'svc_part_pkg_link',
208 'm2m_dstcol' => 'dst_pkgpart',
209 'm2_error_callback' =>
210 &{$m2_error_callback_maker}('svc'),
213 { type => 'tablebreak-tr-title',
214 value => 'Price plan options',
223 my $curuser = $FS::CurrentUser::CurrentUser;
225 my $edit_global = 'Edit global package definitions';
226 my $acl_edit = $curuser->access_right('Edit package definitions');
227 my $acl_edit_global = $curuser->access_right($edit_global);
229 my $acl_edit_either = $acl_edit || $acl_edit_global;
231 my $begin_callback = sub {
232 my( $cgi, $fields, $opt ) = @_;
234 unless $acl_edit_either
235 || ( $cgi->param('pkgnum')
236 && $curuser->access_right('Customize customer package')
240 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
242 #arg. access rights for cloning are Hard.
243 # on the one hand we don't really want cloning (customizing a package) to fail
244 # for want of finding the source package in normal usage
245 # on the other hand, we don't want people using the clone link to be able to
247 my $agent_clone_extra_sql =
248 ' ( '. FS::part_pkg->curuser_pkgs_sql.
249 " OR ( part_pkg.custom = 'Y' ) ".
252 my $conf = new FS::Conf;
253 my $taxproducts = $conf->exists('enable_taxproducts');
255 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
256 " WHERE disabled IS NULL OR disabled = '' ")
258 $sth->execute or die $sth->errstr;
259 my $census = $sth->fetchrow_arrayref->[0];
262 # - tr-part_pkg_freq: month_increments_only (from price plans)
264 # - test errors cloning
265 # - test custom pricing
266 # - move the selectlayer divs away from lame layer_callback
268 #my ($query) = $cgi->keywords;
273 my %tax_override = ();
275 my %taxproductnums = map { ($_->classnum => 1) }
276 qsearch('usage_class', { 'disabled' => '' });
277 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
280 my $recur_disabled = 1;
282 my $error_callback = sub {
283 my($cgi, $object, $fields, $opt ) = @_;
285 (@agent_type) = $cgi->param('agent_type');
287 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
289 $recur_disabled = $cgi->param('freq') ? 0 : 1;
291 foreach ($cgi->param) {
292 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
294 $tax_override{''} = $cgi->param('tax_override');
295 $tax_override{$_} = $cgi->param('tax_override_$_')
296 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
298 #some false laziness w/process
299 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
301 my $options = $cgi->param($plan."__OPTIONS");
302 my @options = split(',', $options);
304 map { my $optionname = $_;
305 my $param = $plan."__$optionname";
306 my $value = join(', ', $cgi->param($param));
307 ( $optionname => $value );
311 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
312 $object->set($_ => scalar($cgi->param($_)) )
313 foreach (qw( setup_fee recur_fee ));
317 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
319 my $new_object_callback = sub {
320 my( $cgi, $hashref, $fields, $opt ) = @_;
322 my $part_pkg = FS::part_pkg->new( $hashref );
323 $part_pkg->set($_ => '0')
324 foreach (qw( setup_fee recur_fee ));
330 my $edit_callback = sub {
331 my( $cgi, $object, $fields, $opt ) = @_;
333 $recur_disabled = $object->freq ? 0 : 1;
335 (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
337 my @report_option = ();
338 foreach ($object->options) {
339 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
340 /^report_option_(\d+)$/ && (push @report_option, $1);
342 foreach ($object->part_pkg_taxoverride) {
343 $taxproductnums{$_->usage_class} = 1
347 $cgi->param('report_option', join(',', @report_option));
348 foreach my $field ( @$fields ) {
350 ref($field) eq 'HASH' &&
352 $field->{field} eq 'report_option'
354 #$field->{curr_value} = join(',', @report_option);
355 $field->{value} = join(',', @report_option);
358 %options = $object->options;
360 $object->set($_ => $object->option($_))
361 foreach (qw( setup_fee recur_fee ));
365 my $new_callback = sub {
366 my( $cgi, $object, $fields ) = @_;
368 my $conf = new FS::Conf;
369 if ( $conf->exists('agent_defaultpkg') ) {
370 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
371 @agent_type = map {$_->typenum} qsearch('agent_type',{});
376 my $clone_callback = sub {
377 my( $cgi, $object, $fields, $opt ) = @_;
379 if ( $cgi->param('pkgnum') ) {
381 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
382 $object->agentnum( $cust_pkg->cust_main->agentnum );
384 $opt->{action} = 'Custom';
386 #my $part_pkg = $clone_part_pkg->clone;
387 #this is all clone does anyway
388 $object->custom('Y');
390 $object->disabled('Y');
394 %options = $object->options;
396 $object->set($_ => $options{$_})
397 foreach (qw( setup_fee recur_fee ));
399 $recur_disabled = $object->freq ? 0 : 1;
402 my $m2_error_callback_maker = sub {
403 my $link_type = shift; #yay closures
405 my( $cgi, $object ) = @_;
409 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
410 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
413 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
415 new FS::part_pkg_link {
416 'link_type' => $link_type,
417 'src_pkgpart' => $object->pkgpart,
418 'dst_pkgpart' => $dst,
429 my $javascript = <<'END';
430 <SCRIPT TYPE="text/javascript">
432 function freq_changed(what) {
433 var freq = what.options[what.selectedIndex].value;
436 what.form.recur_fee.disabled = true;
437 what.form.recur_fee.style.backgroundColor = '#dddddd';
439 what.form.recur_fee.disabled = false;
440 what.form.recur_fee.style.backgroundColor = '#ffffff';
445 function agent_changed(what) {
447 var agentnum = what.options[what.selectedIndex].value;
449 if ( agentnum == 0 ) {
450 what.form.agent_type.disabled = false;
451 //what.form.agent_type.style.backgroundColor = '#ffffff';
452 what.form.agent_type.style.visibility = '';
454 what.form.agent_type.disabled = true;
455 //what.form.agent_type.style.backgroundColor = '#dddddd';
456 what.form.agent_type.style.visibility = 'hidden';
464 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
466 tie my %plan_labels, 'Tie::IxHash',
467 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
470 my $html_bottom = sub {
473 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
475 my $layer_callback = sub {
478 my $html = ntable("#cccccc",2);
482 # <TD ALIGN="right">Recurring fee frequency </TD>
483 # <TD><SELECT NAME="freq">
486 #my @freq = keys %freq;
487 #@freq = grep { /^\d+$/ } @freq
488 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
489 #foreach my $freq ( @freq ) {
490 # $html .= qq(<OPTION VALUE="$freq");
491 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
492 # $html .= ">$freq{$freq}";
494 #$html .= '</SELECT></TD></TR>';
496 my $href = $plans{$layer}->{'fields'};
497 my @fields = exists($plans{$layer}->{'fieldorder'})
498 ? @{$plans{$layer}->{'fieldorder'}}
501 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
503 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
505 my $format = sub { shift };
506 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
508 #XXX these should use elements/ fields... (or this whole thing should
509 #just use layer_fields instead of layer_callback)
511 if ( ! exists($href->{$field}{'type'}) ) {
513 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
514 ( exists($options{$field})
515 ? &$format($options{$field})
516 : $href->{$field}{'default'} ).
519 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
521 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
522 ( exists($options{$field}) && $options{$field}
527 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
531 if $href->{$field}{'type'} eq 'select_multiple';
532 $html .= qq! NAME="${layer}__$field">!;
534 if ( $href->{$field}{'select_table'} ) {
536 qsearch( $href->{$field}{'select_table'},
537 $href->{$field}{'select_hash'} )
539 my $value = $record->getfield($href->{$field}{'select_key'});
540 $html .= qq!<OPTION VALUE="$value"!.
541 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
545 '>'. $record->getfield($href->{$field}{'select_label'});
547 } elsif ( $href->{$field}{'select_options'} ) {
548 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
549 my $label = $href->{$field}{'select_options'}{$key};
550 $html .= qq!<OPTION VALUE="$key"!.
551 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
559 $html .= '<font color="#ff0000">warning: '.
560 "don't know how to retreive options for $field select field".
563 $html .= '</SELECT>';
565 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
568 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
570 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
571 my $label = $href->{$field}{'options'}{$key};
572 $html .= qq!$radio VALUE="$key"!.
573 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
582 $html .= '</TD></TR>';
586 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
587 join(',', keys %{ $href } ). '">';
595 options => [ keys %plan_labels ],
596 labels => \%plan_labels,
597 curr_value => $object->plan,
598 layer_callback => $layer_callback,
602 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
603 '<SCRIPT TYPE="text/javascript">'.
604 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
607 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
610 $return .= '</SCRIPT>';
616 my %usage_class = map { ($_->classnum => $_->classname) }
617 qsearch('usage_class', {});
618 $usage_class{setup} = 'Setup';
619 $usage_class{recur} = 'Recurring';
621 my %taxproduct_fields = ();
622 my $end_callback = sub {
623 my( $cgi, $object, $fields, $opt ) = @_;
625 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
627 if ( $object->pkgpart ) {
628 foreach my $usage_class ( '', @taxproductnums ) {
629 $tax_override{$usage_class} =
630 join (",", map $_->taxclassnum,
631 qsearch( 'part_pkg_taxoverride', {
632 'pkgpart' => $object->pkgpart,
633 'usage_class' => $usage_class,
640 map { $_ => [ "taxproductnum_$_",
641 { type => 'select-taxproduct',
642 #label => "$usage_class{$_} tax product",
645 { type => 'select-taxoverride' }
650 $taxproduct_fields{'(default)'} =
651 [ 'taxproductnum', { type => 'select-taxproduct',
652 #label => 'Default tax product',
654 'tax_override', { type => 'select-taxoverride' },
658 my $taxproduct_values = sub {
659 my ($cgi, $object, $flags) = @_;
661 sub { my $layer = shift;
662 my @fields = @{$taxproduct_fields{$layer}};
665 my $field = shift @fields;
667 $field =~ /^taxproductnum_\w+$/ &&
668 push @values, ( $field => $options{"usage_$field"} );
669 $field =~ /^tax_override_(\w+)$/ &&
670 push @values, ( $field => $tax_override{$1} );
671 $field =~ /^taxproductnum$/ &&
672 push @values, ( $field => $object->taxproductnum );
673 $field =~ /^tax_override$/ &&
674 push @values, ( $field => $tax_override{''} );
680 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
685 my $field_callback = sub {
686 my ($cgi, $object, $fieldref) = @_;
688 my $field = $fieldref->{field};
689 if ($field eq 'taxproductnums') {
690 $fieldref->{value} = join(',', @taxproductnums);
691 } elsif ($field eq 'taxproduct_select') {
692 $fieldref->{options} = [ '(default)', @taxproductnums ];
693 $fieldref->{labels} = { ( '(default)' => '(default)' ),
694 map {( $_ => ($usage_class{$_} || $_) )}
697 $fieldref->{layer_fields} = \%taxproduct_fields;
698 $fieldref->{layer_values_callback} = $taxproduct_values;