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 'recur_show_zero' => 'Show zero recurring',
49 'discountnum' => 'Offer discounts for longer terms',
50 'bill_dst_pkgpart' => 'Include line item(s) from package',
51 'svc_dst_pkgpart' => 'Include services of package',
52 'report_option' => 'Report classes',
53 'fcc_ds0s' => 'Voice-grade equivalents',
57 { field=>'clone', type=>'hidden',
58 curr_value_callback =>
59 sub { shift->param('clone') },
61 { field=>'pkgnum', type=>'hidden',
62 curr_value_callback =>
63 sub { shift->param('pkgnum') },
66 { field=>'custom', type=>'hidden' },
68 { type => 'columnstart' },
75 {field=>'comment', type=>'text', size=>40 }, #32
76 { field => 'agentnum',
77 type => 'select-agent',
78 disable_empty => ! $acl_edit_global,
79 empty_label => '(global)',
80 onchange => 'agent_changed',
82 {field=>'classnum', type=>'select-pkg_class' },
83 ( $conf->exists('pkg-addon_classnum')
84 ? ( { field=>'addon_classnum',
85 type =>'select-pkg_class',
90 {field=>'disabled', type=>$disabled_type, value=>'Y'},
92 { type => 'tablebreak-tr-title',
93 value => 'Pricing', #better name?
96 type => 'selectlayers-select',
97 options => [ keys %plan_labels ],
98 labels => \%plan_labels,
99 onchange => 'aux_planchanged(what);',
101 { field => 'setup_fee',
105 type => 'part_pkg_freq',
106 onchange => 'freq_changed',
108 { field => 'recur_fee',
110 disabled => sub { $recur_disabled },
111 onchange => 'recur_changed',
114 { field => 'recur_show_zero',
117 disabled => sub { $recur_show_zero_disabled },
123 #recurring fee (auto-disable)
125 { type => 'columnnext' },
127 {type=>'justtitle', value=>'Taxation' },
128 {field=>'setuptax', type=>'checkbox', value=>'Y'},
129 {field=>'recurtax', type=>'checkbox', value=>'Y'},
130 {field=>'taxclass', type=>'select-taxclass' },
131 { field => 'taxproductnums',
133 value => join(',', @taxproductnums),
135 { field => 'taxproduct_select',
136 type => 'selectlayers',
137 options => [ '(default)', @taxproductnums ],
138 curr_value => '(default)',
139 labels => { ( '(default)' => '(default)' ),
140 map {($_=>$usage_class{$_})}
143 layer_fields => \%taxproduct_fields,
144 layer_values_callback => $taxproduct_values,
145 layers_only => !$taxproducts,
146 cell_style => ( !$taxproducts
152 { type => 'tablebreak-tr-title',
153 value => 'Promotions', #better name?
155 { field=>'promo_code', type=>'text', size=>15 },
157 { type => 'tablebreak-tr-title',
158 value => 'Cost tracking', #better name?
160 { field=>'setup_cost', type=>'money', },
161 { field=>'recur_cost', type=>'money', },
163 { type => 'columnnext' },
165 { field => 'agent_type',
166 type => 'select-agent_types',
167 disabled => ! $acl_edit_global,
168 curr_value_callback => sub {
169 my($cgi, $object, $field) = @_;
170 #in the other callbacks..? hmm.
175 { type => 'tablebreak-tr-title',
176 value => 'Line-item revenue recogition', #better name?
178 { field=>'pay_weight', type=>'text', size=>6 },
179 { field=>'credit_weight', type=>'text', size=>6 },
181 ( $conf->exists('cust_pkg-show_fcc_voice_grade_equivalent')
183 { type => 'tablebreak-tr-title',
184 value => 'FCC Form 477 information',
186 { field=>'fcc_ds0s', type=>'text', size=>6 },
192 { type => 'columnend' },
194 { 'type' => $report_option ? 'tablebreak-tr-title'
196 'value' => 'Optional report classes',
197 'field' => 'census_title',
199 { 'field' => 'report_option',
200 'type' => $report_option ? 'select-table'
202 'table' => 'part_pkg_report_option',
203 'name_col' => 'name',
204 'hashref' => { 'disabled' => '' },
208 { 'type' => 'tablebreak-tr-title',
209 'value' => 'Term discounts',
211 { 'field' => 'discountnum',
212 'type' => 'select-table',
213 'table' => 'discount',
214 'name_col' => 'name',
215 'hashref' => { %$discountnum_hashref },
216 #'extra_sql' => 'AND (months IS NOT NULL OR months != 0)',
217 'empty_label'=> 'Select discount',
218 'm2_label' => 'Offer discounts for longer terms',
219 'm2m_method' => 'part_pkg_discount',
220 'm2m_dstcol' => 'discountnum',
221 'm2_error_callback' => $discount_error_callback,
224 { 'type' => 'tablebreak-tr-title',
225 'value' => 'Pricing add-ons',
228 { 'field' => 'bill_dst_pkgpart',
229 'type' => 'select-part_pkg',
230 'extra_sql' => sub { $pkgpart
231 ? "AND pkgpart != $pkgpart"
234 'm2_label' => 'Include line item(s) from package',
235 'm2m_method' => 'bill_part_pkg_link',
236 'm2m_dstcol' => 'dst_pkgpart',
237 'm2_error_callback' =>
238 &{$m2_error_callback_maker}('bill'),
239 'm2_fields' => [ { 'field' => 'hidden',
240 'type' => 'checkbox',
248 { type => 'tablebreak-tr-title',
251 { type => 'pkg_svc', },
253 { 'field' => 'svc_dst_pkgpart',
254 'label' => 'Also include services from package: ',
255 'type' => 'select-part_pkg',
256 'extra_sql' => sub { $pkgpart
257 ? "AND pkgpart != $pkgpart"
260 'm2_label' => 'Include services of package: ',
261 'm2m_method' => 'svc_part_pkg_link',
262 'm2m_dstcol' => 'dst_pkgpart',
263 'm2_error_callback' =>
264 &{$m2_error_callback_maker}('svc'),
267 { type => 'tablebreak-tr-title',
268 value => 'Price plan options',
277 my $curuser = $FS::CurrentUser::CurrentUser;
279 my $edit_global = 'Edit global package definitions';
280 my $acl_edit = $curuser->access_right('Edit package definitions');
281 my $acl_edit_global = $curuser->access_right($edit_global);
283 my $acl_edit_either = $acl_edit || $acl_edit_global;
285 my $begin_callback = sub {
286 my( $cgi, $fields, $opt ) = @_;
288 unless $acl_edit_either
289 || ( $cgi->param('pkgnum')
290 && $curuser->access_right('Customize customer package')
294 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
296 #arg. access rights for cloning are Hard.
297 # on the one hand we don't really want cloning (customizing a package) to fail
298 # for want of finding the source package in normal usage
299 # on the other hand, we don't want people using the clone link to be able to
301 my $agent_clone_extra_sql =
302 ' ( '. FS::part_pkg->curuser_pkgs_sql.
303 " OR ( part_pkg.custom = 'Y' ) ".
306 my $conf = new FS::Conf;
307 my $taxproducts = $conf->exists('enable_taxproducts');
309 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
310 " WHERE disabled IS NULL OR disabled = '' ")
312 $sth->execute or die $sth->errstr;
313 my $report_option = $sth->fetchrow_arrayref->[0];
316 # - tr-part_pkg_freq: month_increments_only (from price plans)
318 # - test errors cloning
319 # - test custom pricing
320 # - move the selectlayer divs away from lame layer_callback
322 #my ($query) = $cgi->keywords;
327 my %tax_override = ();
329 my %taxproductnums = map { ($_->classnum => 1) }
330 qsearch('usage_class', { 'disabled' => '' });
331 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
334 my $recur_disabled = 1;
335 my $recur_show_zero_disabled = 1;
339 my $error_callback = sub {
340 my($cgi, $object, $fields, $opt ) = @_;
342 (@agent_type) = $cgi->param('agent_type');
344 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
346 $recur_disabled = $cgi->param('freq') ? 0 : 1;
347 $recur_show_zero_disabled =
349 ? $cgi->param('recur_fee') ? 0 : 1
352 foreach ($cgi->param) {
353 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
355 $tax_override{''} = $cgi->param('tax_override');
356 $tax_override{$_} = $cgi->param('tax_override_$_')
357 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
359 #some false laziness w/process
360 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
362 my $options = $cgi->param($plan."__OPTIONS");
363 my @options = split(',', $options);
365 map { my $optionname = $_;
366 my $param = $plan."__$optionname";
367 my $value = join(', ', $cgi->param($param));
368 ( $optionname => $value );
372 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
373 $object->set($_ => scalar($cgi->param($_)) )
374 foreach (qw( setup_fee recur_fee ));
376 $pkgpart = $object->pkgpart;
380 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
382 my $new_object_callback = sub {
383 my( $cgi, $hashref, $fields, $opt ) = @_;
385 my $part_pkg = FS::part_pkg->new( $hashref );
386 $part_pkg->set($_ => '0')
387 foreach (qw( setup_fee recur_fee ));
393 my $edit_callback = sub {
394 my( $cgi, $object, $fields, $opt ) = @_;
396 $recur_disabled = $object->freq ? 0 : 1;
399 map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
401 my @report_option = ();
402 foreach ($object->options) {
403 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
404 /^report_option_(\d+)$/ && (push @report_option, $1);
406 foreach ($object->part_pkg_taxoverride) {
407 $taxproductnums{$_->usage_class} = 1
411 $cgi->param('report_option', join(',', @report_option));
412 foreach my $field ( @$fields ) {
414 ref($field) eq 'HASH' &&
416 $field->{field} eq 'report_option'
418 #$field->{curr_value} = join(',', @report_option);
419 $field->{value} = join(',', @report_option);
422 %options = $object->options;
424 $object->set($_ => $object->option($_))
425 foreach (qw( setup_fee recur_fee ));
427 $pkgpart = $object->pkgpart;
431 my $new_callback = sub {
432 my( $cgi, $object, $fields ) = @_;
434 my $conf = new FS::Conf;
436 if ( $conf->exists('agent_defaultpkg') ) {
437 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
438 @agent_type = map {$_->typenum} qsearch('agent_type',{});
441 $options{'suspend_bill'}=1 if $conf->exists('part_pkg-default_suspend_bill');
445 my $clone_callback = sub {
446 my( $cgi, $object, $fields, $opt ) = @_;
448 if ( $cgi->param('pkgnum') ) {
450 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
451 $object->agentnum( $cust_pkg->cust_main->agentnum );
453 $opt->{action} = 'Custom';
455 #my $part_pkg = $clone_part_pkg->clone;
456 #this is all clone does anyway
457 $object->custom('Y');
459 $object->disabled('Y');
461 } else { #not when cloning...
464 map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
468 %options = $object->options;
470 $object->set($_ => $options{$_})
471 foreach (qw( setup_fee recur_fee ));
473 $recur_disabled = $object->freq ? 0 : 1;
476 my $discount_error_callback = sub {
477 my( $cgi, $object ) = @_;
479 if ( /^discountnum(\d+)$/ &&
480 ( my $discountnum = $cgi->param("discountnum$1") ) )
482 new FS::part_pkg_discount {
483 'pkgpart' => $object->pkgpart,
484 'discountnum' => $discountnum,
493 my $m2_error_callback_maker = sub {
494 my $link_type = shift; #yay closures
496 my( $cgi, $object ) = @_;
499 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
500 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
503 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
505 new FS::part_pkg_link {
506 'link_type' => $link_type,
507 'src_pkgpart' => $object->pkgpart,
508 'dst_pkgpart' => $dst,
519 my $javascript = <<'END';
520 <SCRIPT TYPE="text/javascript">
522 function freq_changed(what) {
523 var freq = what.options[what.selectedIndex].value;
526 what.form.recur_fee.disabled = true;
527 what.form.recur_fee.style.backgroundColor = '#dddddd';
528 what.form.recur_show_zero.disabled = true;
529 //what.form.recur_show_zero.style.backgroundColor= '#dddddd';
531 what.form.recur_fee.disabled = false;
532 what.form.recur_fee.style.backgroundColor = '#ffffff';
533 what.form.recur_show_zero.disabled = false;
534 //what.form.recur_show_zero.style.backgroundColor= '#ffffff';
539 function recur_changed(what) {
540 var recur = what.value;
542 what.form.recur_show_zero.disabled = false;
544 what.form.recur_show_zero.disabled = true;
548 function agent_changed(what) {
551 if ( what.type == 'select-one' ) {
552 agentnum = what.options[what.selectedIndex].value;
554 agentnum = what.value;
557 if ( agentnum == 0 ) {
558 what.form.agent_type.disabled = false;
559 //what.form.agent_type.style.backgroundColor = '#ffffff';
560 what.form.agent_type.style.visibility = '';
562 what.form.agent_type.disabled = true;
563 //what.form.agent_type.style.backgroundColor = '#dddddd';
564 what.form.agent_type.style.visibility = 'hidden';
569 function aux_planchanged(what) {
572 var plan = what.options[what.selectedIndex].value;
573 var table = document.getElementById('TableNumber7') // XXX NOT ROBUST
575 if ( plan == 'flat' || plan == 'prorate' || plan == 'subscription' ) {
576 //table.disabled = false;
577 table.style.visibility = '';
579 //table.disabled = true;
580 table.style.visibility = 'hidden';
588 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
590 tie my %plan_labels, 'Tie::IxHash',
591 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
594 my $html_bottom = sub {
597 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
599 my $layer_callback = sub {
602 my $html = ntable("#cccccc",2);
606 # <TD ALIGN="right">Recurring fee frequency </TD>
607 # <TD><SELECT NAME="freq">
610 #my @freq = keys %freq;
611 #@freq = grep { /^\d+$/ } @freq
612 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
613 #foreach my $freq ( @freq ) {
614 # $html .= qq(<OPTION VALUE="$freq");
615 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
616 # $html .= ">$freq{$freq}";
619 #$html .= '</SELECT></TD></TR>';
621 my $href = $plans{$layer}->{'fields'};
622 my @fields = exists($plans{$layer}->{'fieldorder'})
623 ? @{$plans{$layer}->{'fieldorder'}}
626 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
628 if(!exists($href->{$field})) {
630 warn "nonexistent part_pkg option: '$field'\n";
634 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
636 my $format = sub { shift };
637 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
639 #XXX these should use elements/ fields... (or this whole thing should
640 #just use layer_fields instead of layer_callback)
642 if ( ! exists($href->{$field}{'type'}) ) {
644 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
645 ( exists($options{$field})
646 ? &$format($options{$field})
647 : $href->{$field}{'default'} ).
650 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
652 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
653 ( exists($options{$field}) && $options{$field}
658 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
662 if $href->{$field}{'type'} eq 'select_multiple';
663 $html .= qq! NAME="${layer}__$field">!;
665 $html .= '<OPTION VALUE="">'. $href->{$field}{'empty_label'}
666 if exists($href->{$field}{'disable_empty'})
667 && ! $href->{$field}{'disable_empty'};
669 if ( $href->{$field}{'select_table'} ) {
671 qsearch( $href->{$field}{'select_table'},
672 $href->{$field}{'select_hash'} )
674 my $value = $record->getfield($href->{$field}{'select_key'});
675 $html .= qq!<OPTION VALUE="$value"!.
676 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
680 '>'. $record->getfield($href->{$field}{'select_label'});
682 } elsif ( $href->{$field}{'select_options'} ) {
683 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
684 my $label = $href->{$field}{'select_options'}{$key};
685 $html .= qq!<OPTION VALUE="$key"!.
686 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
694 $html .= '<font color="#ff0000">warning: '.
695 "don't know how to retreive options for $field select field".
698 $html .= '</SELECT>';
700 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
703 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
705 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
706 my $label = $href->{$field}{'options'}{$key};
707 $html .= qq!$radio VALUE="$key"!.
708 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
717 $html .= '</TD></TR>';
721 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
722 join(',', keys %{ $href } ). '">';
730 options => [ keys %plan_labels ],
731 labels => \%plan_labels,
732 curr_value => $object->plan,
733 layer_callback => $layer_callback,
737 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
738 '<SCRIPT TYPE="text/javascript">'.
739 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
742 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
745 $return .= '</SCRIPT>';
751 my %usage_class = map { ($_->classnum => $_->classname) }
752 qsearch('usage_class', {});
753 $usage_class{setup} = 'Setup';
754 $usage_class{recur} = 'Recurring';
756 my %taxproduct_fields = ();
757 my $end_callback = sub {
758 my( $cgi, $object, $fields, $opt ) = @_;
760 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
762 if ( $object->pkgpart ) {
763 foreach my $usage_class ( '', @taxproductnums ) {
764 $tax_override{$usage_class} =
765 join (",", map $_->taxclassnum,
766 qsearch( 'part_pkg_taxoverride', {
767 'pkgpart' => $object->pkgpart,
768 'usage_class' => $usage_class,
775 map { $_ => [ "taxproductnum_$_",
776 { type => 'select-taxproduct',
777 #label => "$usage_class{$_} tax product",
780 { type => 'select-taxoverride' }
785 $taxproduct_fields{'(default)'} =
786 [ 'taxproductnum', { type => 'select-taxproduct',
787 #label => 'Default tax product',
789 'tax_override', { type => 'select-taxoverride' },
793 my $taxproduct_values = sub {
794 my ($cgi, $object, $flags) = @_;
796 sub { my $layer = shift;
797 my @fields = @{$taxproduct_fields{$layer}};
800 my $field = shift @fields;
802 $field =~ /^taxproductnum_\w+$/ &&
803 push @values, ( $field => $options{"usage_$field"} );
804 $field =~ /^tax_override_(\w+)$/ &&
805 push @values, ( $field => $tax_override{$1} );
806 $field =~ /^taxproductnum$/ &&
807 push @values, ( $field => $object->taxproductnum );
808 $field =~ /^tax_override$/ &&
809 push @values, ( $field => $tax_override{''} );
815 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
820 my $field_callback = sub {
821 my ($cgi, $object, $fieldref) = @_;
823 my $field = $fieldref->{field};
824 if ($field eq 'taxproductnums') {
825 $fieldref->{value} = join(',', @taxproductnums);
826 } elsif ($field eq 'taxproduct_select') {
827 $fieldref->{options} = [ '(default)', @taxproductnums ];
828 $fieldref->{labels} = { ( '(default)' => '(default)' ),
829 map {( $_ => ($usage_class{$_} || $_) )}
832 $fieldref->{layer_fields} = \%taxproduct_fields;
833 $fieldref->{layer_values_callback} = $taxproduct_values;
837 my $discountnum_hashref = {
839 'months' => { 'op' => '>', 'value' => 1 },