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 'disable_line_item_date_ranges' => 'Disable line item date ranges',
42 'setup_cost' => 'Setup cost',
43 'recur_cost' => 'Recur cost',
44 'pay_weight' => 'Payment weight',
45 'credit_weight' => 'Credit weight',
46 'agentnum' => 'Agent',
47 'setup_fee' => 'Setup fee',
48 'recur_fee' => 'Recurring fee',
49 'recur_show_zero' => 'Show zero recurring',
50 'discountnum' => 'Offer discounts for longer terms',
51 'bill_dst_pkgpart' => 'Include line item(s) from package',
52 'svc_dst_pkgpart' => 'Include services of package',
53 'report_option' => 'Report classes',
54 'fcc_ds0s' => 'Voice-grade equivalents',
58 { field=>'clone', type=>'hidden',
59 curr_value_callback =>
60 sub { shift->param('clone') },
62 { field=>'pkgnum', type=>'hidden',
63 curr_value_callback =>
64 sub { shift->param('pkgnum') },
67 { field=>'custom', type=>'hidden' },
69 { type => 'columnstart' },
76 {field=>'comment', type=>'text', size=>40 }, #32
77 { field => 'agentnum',
78 type => 'select-agent',
79 disable_empty => ! $acl_edit_global,
80 empty_label => '(global)',
81 onchange => 'agent_changed',
83 {field=>'classnum', type=>'select-pkg_class' },
84 ( $conf->exists('pkg-addon_classnum')
85 ? ( { field=>'addon_classnum',
86 type =>'select-pkg_class',
91 {field=>'disabled', type=>$disabled_type, value=>'Y'},
92 {field=>'disable_line_item_date_ranges', type=>$disabled_type, value=>'Y'},
94 { type => 'tablebreak-tr-title',
95 value => 'Pricing', #better name?
98 type => 'selectlayers-select',
99 options => [ keys %plan_labels ],
100 labels => \%plan_labels,
101 onchange => 'aux_planchanged(what);',
103 { field => 'setup_fee',
107 type => 'part_pkg_freq',
108 onchange => 'freq_changed',
110 { field => 'recur_fee',
112 disabled => sub { $recur_disabled },
113 onchange => 'recur_changed',
116 { field => 'recur_show_zero',
119 disabled => sub { $recur_show_zero_disabled },
125 #recurring fee (auto-disable)
127 { type => 'columnnext' },
129 {type=>'justtitle', value=>'Taxation' },
130 {field=>'setuptax', type=>'checkbox', value=>'Y'},
131 {field=>'recurtax', type=>'checkbox', value=>'Y'},
132 {field=>'taxclass', type=>'select-taxclass' },
133 { field => 'taxproductnums',
135 value => join(',', @taxproductnums),
137 { field => 'taxproduct_select',
138 type => 'selectlayers',
139 options => [ '(default)', @taxproductnums ],
140 curr_value => '(default)',
141 labels => { ( '(default)' => '(default)' ),
142 map {($_=>$usage_class{$_})}
145 layer_fields => \%taxproduct_fields,
146 layer_values_callback => $taxproduct_values,
147 layers_only => !$taxproducts,
148 cell_style => ( !$taxproducts
154 { type => 'tablebreak-tr-title',
155 value => 'Promotions', #better name?
157 { field=>'promo_code', type=>'text', size=>15 },
159 { type => 'tablebreak-tr-title',
160 value => 'Cost tracking', #better name?
162 { field=>'setup_cost', type=>'money', },
163 { field=>'recur_cost', type=>'money', },
165 { type => 'columnnext' },
167 { field => 'agent_type',
168 type => 'select-agent_types',
169 disabled => ! $acl_edit_global,
170 curr_value_callback => sub {
171 my($cgi, $object, $field) = @_;
172 #in the other callbacks..? hmm.
177 { type => 'tablebreak-tr-title',
178 value => 'Line-item revenue recogition', #better name?
180 { field=>'pay_weight', type=>'text', size=>6 },
181 { field=>'credit_weight', type=>'text', size=>6 },
183 ( $conf->exists('cust_pkg-show_fcc_voice_grade_equivalent')
185 { type => 'tablebreak-tr-title',
186 value => 'FCC Form 477 information',
188 { field=>'fcc_ds0s', type=>'text', size=>6 },
194 { type => 'columnend' },
196 { 'type' => $report_option ? 'tablebreak-tr-title'
198 'value' => 'Optional report classes',
199 'field' => 'census_title',
201 { 'field' => 'report_option',
202 'type' => $report_option ? 'select-table'
204 'table' => 'part_pkg_report_option',
205 'name_col' => 'name',
206 'hashref' => { 'disabled' => '' },
210 { 'type' => 'tablebreak-tr-title',
211 'value' => 'Term discounts',
213 { 'field' => 'discountnum',
214 'type' => 'select-table',
215 'table' => 'discount',
216 'name_col' => 'name',
217 'hashref' => { %$discountnum_hashref },
218 #'extra_sql' => 'AND (months IS NOT NULL OR months != 0)',
219 'empty_label'=> 'Select discount',
220 'm2_label' => 'Offer discounts for longer terms',
221 'm2m_method' => 'part_pkg_discount',
222 'm2m_dstcol' => 'discountnum',
223 'm2_error_callback' => $discount_error_callback,
226 { 'type' => 'tablebreak-tr-title',
227 'value' => 'Pricing add-ons',
230 { 'field' => 'bill_dst_pkgpart',
231 'type' => 'select-part_pkg',
232 'extra_sql' => sub { $pkgpart
233 ? "AND pkgpart != $pkgpart"
236 'm2_label' => 'Include line item(s) from package',
237 'm2m_method' => 'bill_part_pkg_link',
238 'm2m_dstcol' => 'dst_pkgpart',
239 'm2_error_callback' =>
240 &{$m2_error_callback_maker}('bill'),
241 'm2_fields' => [ { 'field' => 'hidden',
242 'type' => 'checkbox',
250 { type => 'tablebreak-tr-title',
253 { type => 'pkg_svc', },
255 { 'field' => 'svc_dst_pkgpart',
256 'label' => 'Also include services from package: ',
257 'type' => 'select-part_pkg',
258 'extra_sql' => sub { $pkgpart
259 ? "AND pkgpart != $pkgpart"
262 'm2_label' => 'Include services of package: ',
263 'm2m_method' => 'svc_part_pkg_link',
264 'm2m_dstcol' => 'dst_pkgpart',
265 'm2_error_callback' =>
266 &{$m2_error_callback_maker}('svc'),
269 { type => 'tablebreak-tr-title',
270 value => 'Price plan options',
279 my $curuser = $FS::CurrentUser::CurrentUser;
281 my $edit_global = 'Edit global package definitions';
282 my $acl_edit = $curuser->access_right('Edit package definitions');
283 my $acl_edit_global = $curuser->access_right($edit_global);
285 my $acl_edit_either = $acl_edit || $acl_edit_global;
287 my $begin_callback = sub {
288 my( $cgi, $fields, $opt ) = @_;
290 unless $acl_edit_either
291 || ( $cgi->param('pkgnum')
292 && $curuser->access_right('Customize customer package')
296 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
298 #arg. access rights for cloning are Hard.
299 # on the one hand we don't really want cloning (customizing a package) to fail
300 # for want of finding the source package in normal usage
301 # on the other hand, we don't want people using the clone link to be able to
303 my $agent_clone_extra_sql =
304 ' ( '. FS::part_pkg->curuser_pkgs_sql.
305 " OR ( part_pkg.custom = 'Y' ) ".
308 my $conf = new FS::Conf;
309 my $taxproducts = $conf->exists('enable_taxproducts');
311 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
312 " WHERE disabled IS NULL OR disabled = '' ")
314 $sth->execute or die $sth->errstr;
315 my $report_option = $sth->fetchrow_arrayref->[0];
318 # - tr-part_pkg_freq: month_increments_only (from price plans)
320 # - test errors cloning
321 # - test custom pricing
322 # - move the selectlayer divs away from lame layer_callback
324 #my ($query) = $cgi->keywords;
329 my %tax_override = ();
331 my %taxproductnums = map { ($_->classnum => 1) }
332 qsearch('usage_class', { 'disabled' => '' });
333 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
336 my $recur_disabled = 1;
337 my $recur_show_zero_disabled = 1;
341 my $error_callback = sub {
342 my($cgi, $object, $fields, $opt ) = @_;
344 (@agent_type) = $cgi->param('agent_type');
346 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
348 $recur_disabled = $cgi->param('freq') ? 0 : 1;
349 $recur_show_zero_disabled =
351 ? $cgi->param('recur_fee') ? 0 : 1
354 foreach ($cgi->param) {
355 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
357 $tax_override{''} = $cgi->param('tax_override');
358 $tax_override{$_} = $cgi->param('tax_override_$_')
359 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
361 #some false laziness w/process
362 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
364 my $options = $cgi->param($plan."__OPTIONS");
365 my @options = split(',', $options);
367 map { my $optionname = $_;
368 my $param = $plan."__$optionname";
369 my $value = join(', ', $cgi->param($param));
370 ( $optionname => $value );
374 $object->set($_ => scalar($cgi->param($_)) )
375 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
377 $pkgpart = $object->pkgpart;
381 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
383 my $new_object_callback = sub {
384 my( $cgi, $hashref, $fields, $opt ) = @_;
386 my $part_pkg = FS::part_pkg->new( $hashref );
387 $part_pkg->set($_ => '0')
388 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
394 my $edit_callback = sub {
395 my( $cgi, $object, $fields, $opt ) = @_;
397 $recur_disabled = $object->freq ? 0 : 1;
400 map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
402 my @report_option = ();
403 foreach ($object->options) {
404 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
405 /^report_option_(\d+)$/ && (push @report_option, $1);
407 foreach ($object->part_pkg_taxoverride) {
408 $taxproductnums{$_->usage_class} = 1
412 $cgi->param('report_option', join(',', @report_option));
413 foreach my $field ( @$fields ) {
415 ref($field) eq 'HASH' &&
417 $field->{field} eq 'report_option'
419 #$field->{curr_value} = join(',', @report_option);
420 $field->{value} = join(',', @report_option);
423 %options = $object->options;
425 $object->set($_ => $object->option($_))
426 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
428 $pkgpart = $object->pkgpart;
432 my $new_callback = sub {
433 my( $cgi, $object, $fields ) = @_;
435 my $conf = new FS::Conf;
437 if ( $conf->exists('agent_defaultpkg') ) {
438 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
439 @agent_type = map {$_->typenum} qsearch('agent_type',{});
442 $options{'suspend_bill'}=1 if $conf->exists('part_pkg-default_suspend_bill');
446 my $clone_callback = sub {
447 my( $cgi, $object, $fields, $opt ) = @_;
449 if ( $cgi->param('pkgnum') ) {
451 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
452 $object->agentnum( $cust_pkg->cust_main->agentnum );
454 $opt->{action} = 'Custom';
456 #my $part_pkg = $clone_part_pkg->clone;
457 #this is all clone does anyway
458 $object->custom('Y');
460 $object->disabled('Y');
462 } else { #not when cloning...
465 map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
469 %options = $object->options;
471 $object->set($_ => $options{$_})
472 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
474 $recur_disabled = $object->freq ? 0 : 1;
477 my $discount_error_callback = sub {
478 my( $cgi, $object ) = @_;
480 if ( /^discountnum(\d+)$/ &&
481 ( my $discountnum = $cgi->param("discountnum$1") ) )
483 new FS::part_pkg_discount {
484 'pkgpart' => $object->pkgpart,
485 'discountnum' => $discountnum,
494 my $m2_error_callback_maker = sub {
495 my $link_type = shift; #yay closures
497 my( $cgi, $object ) = @_;
500 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
501 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
504 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
506 new FS::part_pkg_link {
507 'link_type' => $link_type,
508 'src_pkgpart' => $object->pkgpart,
509 'dst_pkgpart' => $dst,
520 my $javascript = <<'END';
521 <SCRIPT TYPE="text/javascript">
523 function freq_changed(what) {
524 var freq = what.options[what.selectedIndex].value;
527 what.form.recur_fee.disabled = true;
528 what.form.recur_fee.style.backgroundColor = '#dddddd';
529 what.form.recur_show_zero.disabled = true;
530 //what.form.recur_show_zero.style.backgroundColor= '#dddddd';
532 what.form.recur_fee.disabled = false;
533 what.form.recur_fee.style.backgroundColor = '#ffffff';
534 what.form.recur_show_zero.disabled = false;
535 //what.form.recur_show_zero.style.backgroundColor= '#ffffff';
540 function recur_changed(what) {
541 var recur = what.value;
543 what.form.recur_show_zero.disabled = false;
545 what.form.recur_show_zero.disabled = true;
549 function agent_changed(what) {
552 if ( what.type == 'select-one' ) {
553 agentnum = what.options[what.selectedIndex].value;
555 agentnum = what.value;
558 if ( agentnum == 0 ) {
559 what.form.agent_type.disabled = false;
560 //what.form.agent_type.style.backgroundColor = '#ffffff';
561 what.form.agent_type.style.visibility = '';
563 what.form.agent_type.disabled = true;
564 //what.form.agent_type.style.backgroundColor = '#dddddd';
565 what.form.agent_type.style.visibility = 'hidden';
570 function aux_planchanged(what) {
573 var plan = what.options[what.selectedIndex].value;
574 var table = document.getElementById('TableNumber7') // XXX NOT ROBUST
576 if ( plan == 'flat' || plan == 'prorate' || plan == 'subscription' ) {
577 //table.disabled = false;
578 table.style.visibility = '';
580 //table.disabled = true;
581 table.style.visibility = 'hidden';
589 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
591 tie my %plan_labels, 'Tie::IxHash',
592 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
595 my $html_bottom = sub {
598 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
600 my $layer_callback = sub {
603 my $html = ntable("#cccccc",2);
607 # <TD ALIGN="right">Recurring fee frequency </TD>
608 # <TD><SELECT NAME="freq">
611 #my @freq = keys %freq;
612 #@freq = grep { /^\d+$/ } @freq
613 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
614 #foreach my $freq ( @freq ) {
615 # $html .= qq(<OPTION VALUE="$freq");
616 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
617 # $html .= ">$freq{$freq}";
620 #$html .= '</SELECT></TD></TR>';
622 my $href = $plans{$layer}->{'fields'};
623 my @fields = exists($plans{$layer}->{'fieldorder'})
624 ? @{$plans{$layer}->{'fieldorder'}}
627 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
629 if(!exists($href->{$field})) {
631 warn "nonexistent part_pkg option: '$field'\n";
635 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
637 my $format = sub { shift };
638 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
640 #XXX these should use elements/ fields... (or this whole thing should
641 #just use layer_fields instead of layer_callback)
643 if ( ! exists($href->{$field}{'type'}) ) {
645 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
646 ( exists($options{$field})
647 ? &$format($options{$field})
648 : $href->{$field}{'default'} ).
651 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
653 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
654 ( exists($options{$field}) && $options{$field}
659 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
663 if $href->{$field}{'type'} eq 'select_multiple';
664 $html .= qq! NAME="${layer}__$field">!;
666 $html .= '<OPTION VALUE="">'. $href->{$field}{'empty_label'}
667 if exists($href->{$field}{'disable_empty'})
668 && ! $href->{$field}{'disable_empty'};
670 if ( $href->{$field}{'select_table'} ) {
672 qsearch( $href->{$field}{'select_table'},
673 $href->{$field}{'select_hash'} )
675 my $value = $record->getfield($href->{$field}{'select_key'});
676 $html .= qq!<OPTION VALUE="$value"!.
677 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
681 '>'. $record->getfield($href->{$field}{'select_label'});
683 } elsif ( $href->{$field}{'select_options'} ) {
684 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
685 my $label = $href->{$field}{'select_options'}{$key};
686 $html .= qq!<OPTION VALUE="$key"!.
687 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
695 $html .= '<font color="#ff0000">warning: '.
696 "don't know how to retreive options for $field select field".
699 $html .= '</SELECT>';
701 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
704 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
706 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
707 my $label = $href->{$field}{'options'}{$key};
708 $html .= qq!$radio VALUE="$key"!.
709 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
718 $html .= '</TD></TR>';
722 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
723 join(',', keys %{ $href } ). '">';
731 options => [ keys %plan_labels ],
732 labels => \%plan_labels,
733 curr_value => $object->plan,
734 layer_callback => $layer_callback,
738 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
739 '<SCRIPT TYPE="text/javascript">'.
740 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
743 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
746 $return .= '</SCRIPT>';
752 my %usage_class = map { ($_->classnum => $_->classname) }
753 qsearch('usage_class', {});
754 $usage_class{setup} = 'Setup';
755 $usage_class{recur} = 'Recurring';
757 my %taxproduct_fields = ();
758 my $end_callback = sub {
759 my( $cgi, $object, $fields, $opt ) = @_;
761 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
763 if ( $object->pkgpart ) {
764 foreach my $usage_class ( '', @taxproductnums ) {
765 $tax_override{$usage_class} =
766 join (",", map $_->taxclassnum,
767 qsearch( 'part_pkg_taxoverride', {
768 'pkgpart' => $object->pkgpart,
769 'usage_class' => $usage_class,
776 map { $_ => [ "taxproductnum_$_",
777 { type => 'select-taxproduct',
778 #label => "$usage_class{$_} tax product",
781 { type => 'select-taxoverride' }
786 $taxproduct_fields{'(default)'} =
787 [ 'taxproductnum', { type => 'select-taxproduct',
788 #label => 'Default tax product',
790 'tax_override', { type => 'select-taxoverride' },
794 my $taxproduct_values = sub {
795 my ($cgi, $object, $flags) = @_;
797 sub { my $layer = shift;
798 my @fields = @{$taxproduct_fields{$layer}};
801 my $field = shift @fields;
803 $field =~ /^taxproductnum_\w+$/ &&
804 push @values, ( $field => $options{"usage_$field"} );
805 $field =~ /^tax_override_(\w+)$/ &&
806 push @values, ( $field => $tax_override{$1} );
807 $field =~ /^taxproductnum$/ &&
808 push @values, ( $field => $object->taxproductnum );
809 $field =~ /^tax_override$/ &&
810 push @values, ( $field => $tax_override{''} );
816 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
821 my $field_callback = sub {
822 my ($cgi, $object, $fieldref) = @_;
824 my $field = $fieldref->{field};
825 if ($field eq 'taxproductnums') {
826 $fieldref->{value} = join(',', @taxproductnums);
827 } elsif ($field eq 'taxproduct_select') {
828 $fieldref->{options} = [ '(default)', @taxproductnums ];
829 $fieldref->{labels} = { ( '(default)' => '(default)' ),
830 map {( $_ => ($usage_class{$_} || $_) )}
833 $fieldref->{layer_fields} = \%taxproduct_fields;
834 $fieldref->{layer_values_callback} = $taxproduct_values;
838 my $discountnum_hashref = {
840 'months' => { 'op' => '>', 'value' => 1 },