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 'addon_classnum' => 'Restrict additional orders to 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 'setup_cost' => 'Setup cost',
42 'recur_cost' => 'Recur cost',
43 'pay_weight' => 'Payment weight',
44 'credit_weight' => 'Credit weight',
45 'agentnum' => 'Agent',
46 'setup_fee' => 'Setup fee',
47 'recur_fee' => 'Recurring fee',
48 'bill_dst_pkgpart' => 'Include line item(s) from package',
49 'svc_dst_pkgpart' => 'Include services of package',
50 'report_option' => 'Report classes',
51 'fcc_ds0s' => 'Voice-grade eqivalents',
55 { field=>'clone', type=>'hidden',
56 curr_value_callback =>
57 sub { shift->param('clone') },
59 { field=>'pkgnum', type=>'hidden',
60 curr_value_callback =>
61 sub { shift->param('pkgnum') },
64 { field=>'custom', type=>'hidden' },
66 { type => 'columnstart' },
73 {field=>'comment', type=>'text', size=>40 }, #32
74 { field => 'agentnum',
75 type => 'select-agent',
76 disable_empty => ! $acl_edit_global,
77 empty_label => '(global)',
78 onchange => 'agent_changed',
80 {field=>'classnum', type=>'select-pkg_class' },
81 ( $conf->exists('pkg-addon_classnum')
82 ? ( { field=>'addon_classnum',
83 type =>'select-pkg_class',
88 {field=>'disabled', type=>$disabled_type, value=>'Y'},
90 { type => 'tablebreak-tr-title',
91 value => 'Pricing', #better name?
94 type => 'selectlayers-select',
95 options => [ keys %plan_labels ],
96 labels => \%plan_labels,
98 { field => 'setup_fee',
102 type => 'part_pkg_freq',
103 onchange => 'freq_changed',
105 { field => 'recur_fee',
107 disabled => sub { $recur_disabled },
113 #recurring fee (auto-disable)
115 { type => 'columnnext' },
117 {type=>'justtitle', value=>'Taxation' },
118 {field=>'setuptax', type=>'checkbox', value=>'Y'},
119 {field=>'recurtax', type=>'checkbox', value=>'Y'},
120 {field=>'taxclass', type=>'select-taxclass' },
121 { field => 'taxproductnums',
123 value => join(',', @taxproductnums),
125 { field => 'taxproduct_select',
126 type => 'selectlayers',
127 options => [ '(default)', @taxproductnums ],
128 curr_value => '(default)',
129 labels => { ( '(default)' => '(default)' ),
130 map {($_=>$usage_class{$_})}
133 layer_fields => \%taxproduct_fields,
134 layer_values_callback => $taxproduct_values,
135 layers_only => !$taxproducts,
136 cell_style => ( !$taxproducts
142 { type => 'tablebreak-tr-title',
143 value => 'Promotions', #better name?
145 { field=>'promo_code', type=>'text', size=>15 },
147 { type => 'tablebreak-tr-title',
148 value => 'Cost tracking', #better name?
150 { field=>'setup_cost', type=>'money', },
151 { field=>'recur_cost', type=>'money', },
153 { type => 'columnnext' },
155 { field => 'agent_type',
156 type => 'select-agent_types',
157 disabled => ! $acl_edit_global,
158 curr_value_callback => sub {
159 my($cgi, $object, $field) = @_;
160 #in the other callbacks..? hmm.
165 { type => 'tablebreak-tr-title',
166 value => 'Line-item revenue recogition', #better name?
168 { field=>'pay_weight', type=>'text', size=>6 },
169 { field=>'credit_weight', type=>'text', size=>6 },
171 ( $conf->exists('cust_pkg-show_fcc_voice_grade_equivalent')
173 { type => 'tablebreak-tr-title',
174 value => 'FCC Form 477 information',
176 { field=>'fcc_ds0s', type=>'text', size=>6 },
182 { type => 'columnend' },
184 { 'type' => $report_option ? 'tablebreak-tr-title'
186 'value' => 'Optional report classes',
187 'field' => 'census_title',
189 { 'field' => 'report_option',
190 'type' => $report_option ? 'select-table'
192 'table' => 'part_pkg_report_option',
193 'name_col' => 'name',
194 'hashref' => { 'disabled' => '' },
199 { 'type' => 'tablebreak-tr-title',
200 'value' => 'Pricing add-ons',
203 { 'field' => 'bill_dst_pkgpart',
204 'type' => 'select-part_pkg',
205 'm2_label' => 'Include line item(s) from package',
206 'm2m_method' => 'bill_part_pkg_link',
207 'm2m_dstcol' => 'dst_pkgpart',
208 'm2_error_callback' =>
209 &{$m2_error_callback_maker}('bill'),
210 'm2_fields' => [ { 'field' => 'hidden',
211 'type' => 'checkbox',
219 { type => 'tablebreak-tr-title',
222 { type => 'pkg_svc', },
224 { 'field' => 'svc_dst_pkgpart',
225 'label' => 'Also include services from package: ',
226 'type' => 'select-part_pkg',
227 'm2_label' => 'Include services of package: ',
228 'm2m_method' => 'svc_part_pkg_link',
229 'm2m_dstcol' => 'dst_pkgpart',
230 'm2_error_callback' =>
231 &{$m2_error_callback_maker}('svc'),
234 { type => 'tablebreak-tr-title',
235 value => 'Price plan options',
244 my $curuser = $FS::CurrentUser::CurrentUser;
246 my $edit_global = 'Edit global package definitions';
247 my $acl_edit = $curuser->access_right('Edit package definitions');
248 my $acl_edit_global = $curuser->access_right($edit_global);
250 my $acl_edit_either = $acl_edit || $acl_edit_global;
252 my $begin_callback = sub {
253 my( $cgi, $fields, $opt ) = @_;
255 unless $acl_edit_either
256 || ( $cgi->param('pkgnum')
257 && $curuser->access_right('Customize customer package')
261 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
263 #arg. access rights for cloning are Hard.
264 # on the one hand we don't really want cloning (customizing a package) to fail
265 # for want of finding the source package in normal usage
266 # on the other hand, we don't want people using the clone link to be able to
268 my $agent_clone_extra_sql =
269 ' ( '. FS::part_pkg->curuser_pkgs_sql.
270 " OR ( part_pkg.custom = 'Y' ) ".
273 my $conf = new FS::Conf;
274 my $taxproducts = $conf->exists('enable_taxproducts');
276 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
277 " WHERE disabled IS NULL OR disabled = '' ")
279 $sth->execute or die $sth->errstr;
280 my $report_option = $sth->fetchrow_arrayref->[0];
283 # - tr-part_pkg_freq: month_increments_only (from price plans)
285 # - test errors cloning
286 # - test custom pricing
287 # - move the selectlayer divs away from lame layer_callback
289 #my ($query) = $cgi->keywords;
294 my %tax_override = ();
296 my %taxproductnums = map { ($_->classnum => 1) }
297 qsearch('usage_class', { 'disabled' => '' });
298 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
301 my $recur_disabled = 1;
303 my $error_callback = sub {
304 my($cgi, $object, $fields, $opt ) = @_;
306 (@agent_type) = $cgi->param('agent_type');
308 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
310 $recur_disabled = $cgi->param('freq') ? 0 : 1;
312 foreach ($cgi->param) {
313 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
315 $tax_override{''} = $cgi->param('tax_override');
316 $tax_override{$_} = $cgi->param('tax_override_$_')
317 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
319 #some false laziness w/process
320 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
322 my $options = $cgi->param($plan."__OPTIONS");
323 my @options = split(',', $options);
325 map { my $optionname = $_;
326 my $param = $plan."__$optionname";
327 my $value = join(', ', $cgi->param($param));
328 ( $optionname => $value );
332 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
333 $object->set($_ => scalar($cgi->param($_)) )
334 foreach (qw( setup_fee recur_fee ));
338 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
340 my $new_object_callback = sub {
341 my( $cgi, $hashref, $fields, $opt ) = @_;
343 my $part_pkg = FS::part_pkg->new( $hashref );
344 $part_pkg->set($_ => '0')
345 foreach (qw( setup_fee recur_fee ));
351 my $edit_callback = sub {
352 my( $cgi, $object, $fields, $opt ) = @_;
354 $recur_disabled = $object->freq ? 0 : 1;
357 map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
359 my @report_option = ();
360 foreach ($object->options) {
361 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
362 /^report_option_(\d+)$/ && (push @report_option, $1);
364 foreach ($object->part_pkg_taxoverride) {
365 $taxproductnums{$_->usage_class} = 1
369 $cgi->param('report_option', join(',', @report_option));
370 foreach my $field ( @$fields ) {
372 ref($field) eq 'HASH' &&
374 $field->{field} eq 'report_option'
376 #$field->{curr_value} = join(',', @report_option);
377 $field->{value} = join(',', @report_option);
380 %options = $object->options;
382 $object->set($_ => $object->option($_))
383 foreach (qw( setup_fee recur_fee ));
387 my $new_callback = sub {
388 my( $cgi, $object, $fields ) = @_;
390 my $conf = new FS::Conf;
391 if ( $conf->exists('agent_defaultpkg') ) {
392 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
393 @agent_type = map {$_->typenum} qsearch('agent_type',{});
398 my $clone_callback = sub {
399 my( $cgi, $object, $fields, $opt ) = @_;
401 if ( $cgi->param('pkgnum') ) {
403 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
404 $object->agentnum( $cust_pkg->cust_main->agentnum );
406 $opt->{action} = 'Custom';
408 #my $part_pkg = $clone_part_pkg->clone;
409 #this is all clone does anyway
410 $object->custom('Y');
412 $object->disabled('Y');
414 } else { #not when cloning...
417 map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
421 %options = $object->options;
423 $object->set($_ => $options{$_})
424 foreach (qw( setup_fee recur_fee ));
426 $recur_disabled = $object->freq ? 0 : 1;
429 my $m2_error_callback_maker = sub {
430 my $link_type = shift; #yay closures
432 my( $cgi, $object ) = @_;
435 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
436 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
439 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
441 new FS::part_pkg_link {
442 'link_type' => $link_type,
443 'src_pkgpart' => $object->pkgpart,
444 'dst_pkgpart' => $dst,
455 my $javascript = <<'END';
456 <SCRIPT TYPE="text/javascript">
458 function freq_changed(what) {
459 var freq = what.options[what.selectedIndex].value;
462 what.form.recur_fee.disabled = true;
463 what.form.recur_fee.style.backgroundColor = '#dddddd';
465 what.form.recur_fee.disabled = false;
466 what.form.recur_fee.style.backgroundColor = '#ffffff';
471 function agent_changed(what) {
473 var agentnum = what.options[what.selectedIndex].value;
475 if ( agentnum == 0 ) {
476 what.form.agent_type.disabled = false;
477 //what.form.agent_type.style.backgroundColor = '#ffffff';
478 what.form.agent_type.style.visibility = '';
480 what.form.agent_type.disabled = true;
481 //what.form.agent_type.style.backgroundColor = '#dddddd';
482 what.form.agent_type.style.visibility = 'hidden';
490 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
492 tie my %plan_labels, 'Tie::IxHash',
493 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
496 my $html_bottom = sub {
499 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
501 my $layer_callback = sub {
504 my $html = ntable("#cccccc",2);
508 # <TD ALIGN="right">Recurring fee frequency </TD>
509 # <TD><SELECT NAME="freq">
512 #my @freq = keys %freq;
513 #@freq = grep { /^\d+$/ } @freq
514 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
515 #foreach my $freq ( @freq ) {
516 # $html .= qq(<OPTION VALUE="$freq");
517 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
518 # $html .= ">$freq{$freq}";
521 #$html .= '</SELECT></TD></TR>';
523 my $href = $plans{$layer}->{'fields'};
524 my @fields = exists($plans{$layer}->{'fieldorder'})
525 ? @{$plans{$layer}->{'fieldorder'}}
528 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
530 if(!exists($href->{$field})) {
532 warn "nonexistent part_pkg option: '$field'\n";
536 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
538 my $format = sub { shift };
539 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
541 #XXX these should use elements/ fields... (or this whole thing should
542 #just use layer_fields instead of layer_callback)
544 if ( ! exists($href->{$field}{'type'}) ) {
546 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
547 ( exists($options{$field})
548 ? &$format($options{$field})
549 : $href->{$field}{'default'} ).
552 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
554 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
555 ( exists($options{$field}) && $options{$field}
560 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
564 if $href->{$field}{'type'} eq 'select_multiple';
565 $html .= qq! NAME="${layer}__$field">!;
567 if ( $href->{$field}{'select_table'} ) {
569 qsearch( $href->{$field}{'select_table'},
570 $href->{$field}{'select_hash'} )
572 my $value = $record->getfield($href->{$field}{'select_key'});
573 $html .= qq!<OPTION VALUE="$value"!.
574 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
578 '>'. $record->getfield($href->{$field}{'select_label'});
580 } elsif ( $href->{$field}{'select_options'} ) {
581 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
582 my $label = $href->{$field}{'select_options'}{$key};
583 $html .= qq!<OPTION VALUE="$key"!.
584 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
592 $html .= '<font color="#ff0000">warning: '.
593 "don't know how to retreive options for $field select field".
596 $html .= '</SELECT>';
598 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
601 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
603 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
604 my $label = $href->{$field}{'options'}{$key};
605 $html .= qq!$radio VALUE="$key"!.
606 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
615 $html .= '</TD></TR>';
619 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
620 join(',', keys %{ $href } ). '">';
628 options => [ keys %plan_labels ],
629 labels => \%plan_labels,
630 curr_value => $object->plan,
631 layer_callback => $layer_callback,
635 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
636 '<SCRIPT TYPE="text/javascript">'.
637 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
640 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
643 $return .= '</SCRIPT>';
649 my %usage_class = map { ($_->classnum => $_->classname) }
650 qsearch('usage_class', {});
651 $usage_class{setup} = 'Setup';
652 $usage_class{recur} = 'Recurring';
654 my %taxproduct_fields = ();
655 my $end_callback = sub {
656 my( $cgi, $object, $fields, $opt ) = @_;
658 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
660 if ( $object->pkgpart ) {
661 foreach my $usage_class ( '', @taxproductnums ) {
662 $tax_override{$usage_class} =
663 join (",", map $_->taxclassnum,
664 qsearch( 'part_pkg_taxoverride', {
665 'pkgpart' => $object->pkgpart,
666 'usage_class' => $usage_class,
673 map { $_ => [ "taxproductnum_$_",
674 { type => 'select-taxproduct',
675 #label => "$usage_class{$_} tax product",
678 { type => 'select-taxoverride' }
683 $taxproduct_fields{'(default)'} =
684 [ 'taxproductnum', { type => 'select-taxproduct',
685 #label => 'Default tax product',
687 'tax_override', { type => 'select-taxoverride' },
691 my $taxproduct_values = sub {
692 my ($cgi, $object, $flags) = @_;
694 sub { my $layer = shift;
695 my @fields = @{$taxproduct_fields{$layer}};
698 my $field = shift @fields;
700 $field =~ /^taxproductnum_\w+$/ &&
701 push @values, ( $field => $options{"usage_$field"} );
702 $field =~ /^tax_override_(\w+)$/ &&
703 push @values, ( $field => $tax_override{$1} );
704 $field =~ /^taxproductnum$/ &&
705 push @values, ( $field => $object->taxproductnum );
706 $field =~ /^tax_override$/ &&
707 push @values, ( $field => $tax_override{''} );
713 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
718 my $field_callback = sub {
719 my ($cgi, $object, $fieldref) = @_;
721 my $field = $fieldref->{field};
722 if ($field eq 'taxproductnums') {
723 $fieldref->{value} = join(',', @taxproductnums);
724 } elsif ($field eq 'taxproduct_select') {
725 $fieldref->{options} = [ '(default)', @taxproductnums ];
726 $fieldref->{labels} = { ( '(default)' => '(default)' ),
727 map {( $_ => ($usage_class{$_} || $_) )}
730 $fieldref->{layer_fields} = \%taxproduct_fields;
731 $fieldref->{layer_values_callback} = $taxproduct_values;