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 'discountnum' => 'Offer discounts for longer terms',
49 'bill_dst_pkgpart' => 'Include line item(s) from package',
50 'svc_dst_pkgpart' => 'Include services of package',
51 'report_option' => 'Report classes',
52 'fcc_ds0s' => 'Voice-grade eqivalents',
56 { field=>'clone', type=>'hidden',
57 curr_value_callback =>
58 sub { shift->param('clone') },
60 { field=>'pkgnum', type=>'hidden',
61 curr_value_callback =>
62 sub { shift->param('pkgnum') },
65 { field=>'custom', type=>'hidden' },
67 { type => 'columnstart' },
74 {field=>'comment', type=>'text', size=>40 }, #32
75 { field => 'agentnum',
76 type => 'select-agent',
77 disable_empty => ! $acl_edit_global,
78 empty_label => '(global)',
79 onchange => 'agent_changed',
81 {field=>'classnum', type=>'select-pkg_class' },
82 ( $conf->exists('pkg-addon_classnum')
83 ? ( { field=>'addon_classnum',
84 type =>'select-pkg_class',
89 {field=>'disabled', type=>$disabled_type, value=>'Y'},
91 { type => 'tablebreak-tr-title',
92 value => 'Pricing', #better name?
95 type => 'selectlayers-select',
96 options => [ keys %plan_labels ],
97 labels => \%plan_labels,
98 onchange => 'aux_planchanged(what);',
100 { field => 'setup_fee',
104 type => 'part_pkg_freq',
105 onchange => 'freq_changed',
107 { field => 'recur_fee',
109 disabled => sub { $recur_disabled },
115 #recurring fee (auto-disable)
117 { type => 'columnnext' },
119 {type=>'justtitle', value=>'Taxation' },
120 {field=>'setuptax', type=>'checkbox', value=>'Y'},
121 {field=>'recurtax', type=>'checkbox', value=>'Y'},
122 {field=>'taxclass', type=>'select-taxclass' },
123 { field => 'taxproductnums',
125 value => join(',', @taxproductnums),
127 { field => 'taxproduct_select',
128 type => 'selectlayers',
129 options => [ '(default)', @taxproductnums ],
130 curr_value => '(default)',
131 labels => { ( '(default)' => '(default)' ),
132 map {($_=>$usage_class{$_})}
135 layer_fields => \%taxproduct_fields,
136 layer_values_callback => $taxproduct_values,
137 layers_only => !$taxproducts,
138 cell_style => ( !$taxproducts
144 { type => 'tablebreak-tr-title',
145 value => 'Promotions', #better name?
147 { field=>'promo_code', type=>'text', size=>15 },
149 { type => 'tablebreak-tr-title',
150 value => 'Cost tracking', #better name?
152 { field=>'setup_cost', type=>'money', },
153 { field=>'recur_cost', type=>'money', },
155 { type => 'columnnext' },
157 { field => 'agent_type',
158 type => 'select-agent_types',
159 disabled => ! $acl_edit_global,
160 curr_value_callback => sub {
161 my($cgi, $object, $field) = @_;
162 #in the other callbacks..? hmm.
167 { type => 'tablebreak-tr-title',
168 value => 'Line-item revenue recogition', #better name?
170 { field=>'pay_weight', type=>'text', size=>6 },
171 { field=>'credit_weight', type=>'text', size=>6 },
173 ( $conf->exists('cust_pkg-show_fcc_voice_grade_equivalent')
175 { type => 'tablebreak-tr-title',
176 value => 'FCC Form 477 information',
178 { field=>'fcc_ds0s', type=>'text', size=>6 },
184 { type => 'columnend' },
186 { 'type' => $report_option ? 'tablebreak-tr-title'
188 'value' => 'Optional report classes',
189 'field' => 'census_title',
191 { 'field' => 'report_option',
192 'type' => $report_option ? 'select-table'
194 'table' => 'part_pkg_report_option',
195 'name_col' => 'name',
196 'hashref' => { 'disabled' => '' },
200 { 'type' => 'tablebreak-tr-title',
201 'value' => 'Term discounts',
203 { 'field' => 'discountnum',
204 'type' => 'select-table',
205 'table' => 'discount',
206 'name_col' => 'name',
207 'hashref' => { %$discountnum_hashref },
208 #'extra_sql' => 'AND (months IS NOT NULL OR months != 0)',
209 'empty_label'=> 'Select discount',
210 'm2_label' => 'Offer discounts for longer terms',
211 'm2m_method' => 'part_pkg_discount',
212 'm2m_dstcol' => 'discountnum',
213 'm2_error_callback' => $discount_error_callback,
216 { 'type' => 'tablebreak-tr-title',
217 'value' => 'Pricing add-ons',
220 { 'field' => 'bill_dst_pkgpart',
221 'type' => 'select-part_pkg',
222 'extra_sql' => sub { $pkgpart
223 ? "AND pkgpart != $pkgpart"
226 'm2_label' => 'Include line item(s) from package',
227 'm2m_method' => 'bill_part_pkg_link',
228 'm2m_dstcol' => 'dst_pkgpart',
229 'm2_error_callback' =>
230 &{$m2_error_callback_maker}('bill'),
231 'm2_fields' => [ { 'field' => 'hidden',
232 'type' => 'checkbox',
240 { type => 'tablebreak-tr-title',
243 { type => 'pkg_svc', },
245 { 'field' => 'svc_dst_pkgpart',
246 'label' => 'Also include services from package: ',
247 'type' => 'select-part_pkg',
248 'extra_sql' => sub { $pkgpart
249 ? "AND pkgpart != $pkgpart"
252 'm2_label' => 'Include services of package: ',
253 'm2m_method' => 'svc_part_pkg_link',
254 'm2m_dstcol' => 'dst_pkgpart',
255 'm2_error_callback' =>
256 &{$m2_error_callback_maker}('svc'),
259 { type => 'tablebreak-tr-title',
260 value => 'Price plan options',
269 my $curuser = $FS::CurrentUser::CurrentUser;
271 my $edit_global = 'Edit global package definitions';
272 my $acl_edit = $curuser->access_right('Edit package definitions');
273 my $acl_edit_global = $curuser->access_right($edit_global);
275 my $acl_edit_either = $acl_edit || $acl_edit_global;
277 my $begin_callback = sub {
278 my( $cgi, $fields, $opt ) = @_;
280 unless $acl_edit_either
281 || ( $cgi->param('pkgnum')
282 && $curuser->access_right('Customize customer package')
286 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
288 #arg. access rights for cloning are Hard.
289 # on the one hand we don't really want cloning (customizing a package) to fail
290 # for want of finding the source package in normal usage
291 # on the other hand, we don't want people using the clone link to be able to
293 my $agent_clone_extra_sql =
294 ' ( '. FS::part_pkg->curuser_pkgs_sql.
295 " OR ( part_pkg.custom = 'Y' ) ".
298 my $conf = new FS::Conf;
299 my $taxproducts = $conf->exists('enable_taxproducts');
301 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
302 " WHERE disabled IS NULL OR disabled = '' ")
304 $sth->execute or die $sth->errstr;
305 my $report_option = $sth->fetchrow_arrayref->[0];
308 # - tr-part_pkg_freq: month_increments_only (from price plans)
310 # - test errors cloning
311 # - test custom pricing
312 # - move the selectlayer divs away from lame layer_callback
314 #my ($query) = $cgi->keywords;
319 my %tax_override = ();
321 my %taxproductnums = map { ($_->classnum => 1) }
322 qsearch('usage_class', { 'disabled' => '' });
323 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
326 my $recur_disabled = 1;
330 my $error_callback = sub {
331 my($cgi, $object, $fields, $opt ) = @_;
333 (@agent_type) = $cgi->param('agent_type');
335 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
337 $recur_disabled = $cgi->param('freq') ? 0 : 1;
339 foreach ($cgi->param) {
340 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
342 $tax_override{''} = $cgi->param('tax_override');
343 $tax_override{$_} = $cgi->param('tax_override_$_')
344 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
346 #some false laziness w/process
347 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
349 my $options = $cgi->param($plan."__OPTIONS");
350 my @options = split(',', $options);
352 map { my $optionname = $_;
353 my $param = $plan."__$optionname";
354 my $value = join(', ', $cgi->param($param));
355 ( $optionname => $value );
359 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
360 $object->set($_ => scalar($cgi->param($_)) )
361 foreach (qw( setup_fee recur_fee ));
363 $pkgpart = $object->pkgpart;
367 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
369 my $new_object_callback = sub {
370 my( $cgi, $hashref, $fields, $opt ) = @_;
372 my $part_pkg = FS::part_pkg->new( $hashref );
373 $part_pkg->set($_ => '0')
374 foreach (qw( setup_fee recur_fee ));
380 my $edit_callback = sub {
381 my( $cgi, $object, $fields, $opt ) = @_;
383 $recur_disabled = $object->freq ? 0 : 1;
386 map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
388 my @report_option = ();
389 foreach ($object->options) {
390 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
391 /^report_option_(\d+)$/ && (push @report_option, $1);
393 foreach ($object->part_pkg_taxoverride) {
394 $taxproductnums{$_->usage_class} = 1
398 $cgi->param('report_option', join(',', @report_option));
399 foreach my $field ( @$fields ) {
401 ref($field) eq 'HASH' &&
403 $field->{field} eq 'report_option'
405 #$field->{curr_value} = join(',', @report_option);
406 $field->{value} = join(',', @report_option);
409 %options = $object->options;
411 $object->set($_ => $object->option($_))
412 foreach (qw( setup_fee recur_fee ));
414 $pkgpart = $object->pkgpart;
418 my $new_callback = sub {
419 my( $cgi, $object, $fields ) = @_;
421 my $conf = new FS::Conf;
423 if ( $conf->exists('agent_defaultpkg') ) {
424 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
425 @agent_type = map {$_->typenum} qsearch('agent_type',{});
428 $options{'suspend_bill'}=1 if $conf->exists('part_pkg-default_suspend_bill');
432 my $clone_callback = sub {
433 my( $cgi, $object, $fields, $opt ) = @_;
435 if ( $cgi->param('pkgnum') ) {
437 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
438 $object->agentnum( $cust_pkg->cust_main->agentnum );
440 $opt->{action} = 'Custom';
442 #my $part_pkg = $clone_part_pkg->clone;
443 #this is all clone does anyway
444 $object->custom('Y');
446 $object->disabled('Y');
448 } else { #not when cloning...
451 map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
455 %options = $object->options;
457 $object->set($_ => $options{$_})
458 foreach (qw( setup_fee recur_fee ));
460 $recur_disabled = $object->freq ? 0 : 1;
463 my $discount_error_callback = sub {
464 my( $cgi, $object ) = @_;
466 if ( /^discountnum(\d+)$/ &&
467 ( my $discountnum = $cgi->param("discountnum$1") ) )
469 new FS::part_pkg_discount {
470 'pkgpart' => $object->pkgpart,
471 'discountnum' => $discountnum,
480 my $m2_error_callback_maker = sub {
481 my $link_type = shift; #yay closures
483 my( $cgi, $object ) = @_;
486 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
487 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
490 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
492 new FS::part_pkg_link {
493 'link_type' => $link_type,
494 'src_pkgpart' => $object->pkgpart,
495 'dst_pkgpart' => $dst,
506 my $javascript = <<'END';
507 <SCRIPT TYPE="text/javascript">
509 function freq_changed(what) {
510 var freq = what.options[what.selectedIndex].value;
513 what.form.recur_fee.disabled = true;
514 what.form.recur_fee.style.backgroundColor = '#dddddd';
516 what.form.recur_fee.disabled = false;
517 what.form.recur_fee.style.backgroundColor = '#ffffff';
522 function agent_changed(what) {
524 var agentnum = what.options[what.selectedIndex].value;
526 if ( agentnum == 0 ) {
527 what.form.agent_type.disabled = false;
528 //what.form.agent_type.style.backgroundColor = '#ffffff';
529 what.form.agent_type.style.visibility = '';
531 what.form.agent_type.disabled = true;
532 //what.form.agent_type.style.backgroundColor = '#dddddd';
533 what.form.agent_type.style.visibility = 'hidden';
538 function aux_planchanged(what) {
541 var plan = what.options[what.selectedIndex].value;
542 var table = document.getElementById('TableNumber7') // XXX NOT ROBUST
544 if ( plan == 'flat' || plan == 'prorate' || plan == 'subscription' ) {
545 //table.disabled = false;
546 table.style.visibility = '';
548 //table.disabled = true;
549 table.style.visibility = 'hidden';
557 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
559 tie my %plan_labels, 'Tie::IxHash',
560 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
563 my $html_bottom = sub {
566 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
568 my $layer_callback = sub {
571 my $html = ntable("#cccccc",2);
575 # <TD ALIGN="right">Recurring fee frequency </TD>
576 # <TD><SELECT NAME="freq">
579 #my @freq = keys %freq;
580 #@freq = grep { /^\d+$/ } @freq
581 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
582 #foreach my $freq ( @freq ) {
583 # $html .= qq(<OPTION VALUE="$freq");
584 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
585 # $html .= ">$freq{$freq}";
588 #$html .= '</SELECT></TD></TR>';
590 my $href = $plans{$layer}->{'fields'};
591 my @fields = exists($plans{$layer}->{'fieldorder'})
592 ? @{$plans{$layer}->{'fieldorder'}}
595 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
597 if(!exists($href->{$field})) {
599 warn "nonexistent part_pkg option: '$field'\n";
603 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
605 my $format = sub { shift };
606 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
608 #XXX these should use elements/ fields... (or this whole thing should
609 #just use layer_fields instead of layer_callback)
611 if ( ! exists($href->{$field}{'type'}) ) {
613 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
614 ( exists($options{$field})
615 ? &$format($options{$field})
616 : $href->{$field}{'default'} ).
619 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
621 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
622 ( exists($options{$field}) && $options{$field}
627 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
631 if $href->{$field}{'type'} eq 'select_multiple';
632 $html .= qq! NAME="${layer}__$field">!;
634 $html .= '<OPTION VALUE="">'. $href->{$field}{'empty_label'}
635 if exists($href->{$field}{'disable_empty'})
636 && ! $href->{$field}{'disable_empty'};
638 if ( $href->{$field}{'select_table'} ) {
640 qsearch( $href->{$field}{'select_table'},
641 $href->{$field}{'select_hash'} )
643 my $value = $record->getfield($href->{$field}{'select_key'});
644 $html .= qq!<OPTION VALUE="$value"!.
645 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
649 '>'. $record->getfield($href->{$field}{'select_label'});
651 } elsif ( $href->{$field}{'select_options'} ) {
652 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
653 my $label = $href->{$field}{'select_options'}{$key};
654 $html .= qq!<OPTION VALUE="$key"!.
655 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
663 $html .= '<font color="#ff0000">warning: '.
664 "don't know how to retreive options for $field select field".
667 $html .= '</SELECT>';
669 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
672 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
674 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
675 my $label = $href->{$field}{'options'}{$key};
676 $html .= qq!$radio VALUE="$key"!.
677 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
686 $html .= '</TD></TR>';
690 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
691 join(',', keys %{ $href } ). '">';
699 options => [ keys %plan_labels ],
700 labels => \%plan_labels,
701 curr_value => $object->plan,
702 layer_callback => $layer_callback,
706 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
707 '<SCRIPT TYPE="text/javascript">'.
708 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
711 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
714 $return .= '</SCRIPT>';
720 my %usage_class = map { ($_->classnum => $_->classname) }
721 qsearch('usage_class', {});
722 $usage_class{setup} = 'Setup';
723 $usage_class{recur} = 'Recurring';
725 my %taxproduct_fields = ();
726 my $end_callback = sub {
727 my( $cgi, $object, $fields, $opt ) = @_;
729 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
731 if ( $object->pkgpart ) {
732 foreach my $usage_class ( '', @taxproductnums ) {
733 $tax_override{$usage_class} =
734 join (",", map $_->taxclassnum,
735 qsearch( 'part_pkg_taxoverride', {
736 'pkgpart' => $object->pkgpart,
737 'usage_class' => $usage_class,
744 map { $_ => [ "taxproductnum_$_",
745 { type => 'select-taxproduct',
746 #label => "$usage_class{$_} tax product",
749 { type => 'select-taxoverride' }
754 $taxproduct_fields{'(default)'} =
755 [ 'taxproductnum', { type => 'select-taxproduct',
756 #label => 'Default tax product',
758 'tax_override', { type => 'select-taxoverride' },
762 my $taxproduct_values = sub {
763 my ($cgi, $object, $flags) = @_;
765 sub { my $layer = shift;
766 my @fields = @{$taxproduct_fields{$layer}};
769 my $field = shift @fields;
771 $field =~ /^taxproductnum_\w+$/ &&
772 push @values, ( $field => $options{"usage_$field"} );
773 $field =~ /^tax_override_(\w+)$/ &&
774 push @values, ( $field => $tax_override{$1} );
775 $field =~ /^taxproductnum$/ &&
776 push @values, ( $field => $object->taxproductnum );
777 $field =~ /^tax_override$/ &&
778 push @values, ( $field => $tax_override{''} );
784 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
789 my $field_callback = sub {
790 my ($cgi, $object, $fieldref) = @_;
792 my $field = $fieldref->{field};
793 if ($field eq 'taxproductnums') {
794 $fieldref->{value} = join(',', @taxproductnums);
795 } elsif ($field eq 'taxproduct_select') {
796 $fieldref->{options} = [ '(default)', @taxproductnums ];
797 $fieldref->{labels} = { ( '(default)' => '(default)' ),
798 map {( $_ => ($usage_class{$_} || $_) )}
801 $fieldref->{layer_fields} = \%taxproduct_fields;
802 $fieldref->{layer_values_callback} = $taxproduct_values;
806 my $discountnum_hashref = {
808 'months' => { 'op' => '>', 'value' => 1 },