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 'm2_label' => 'Include line item(s) from package',
223 'm2m_method' => 'bill_part_pkg_link',
224 'm2m_dstcol' => 'dst_pkgpart',
225 'm2_error_callback' =>
226 &{$m2_error_callback_maker}('bill'),
227 'm2_fields' => [ { 'field' => 'hidden',
228 'type' => 'checkbox',
236 { type => 'tablebreak-tr-title',
239 { type => 'pkg_svc', },
241 { 'field' => 'svc_dst_pkgpart',
242 'label' => 'Also include services from package: ',
243 'type' => 'select-part_pkg',
244 'm2_label' => 'Include services of package: ',
245 'm2m_method' => 'svc_part_pkg_link',
246 'm2m_dstcol' => 'dst_pkgpart',
247 'm2_error_callback' =>
248 &{$m2_error_callback_maker}('svc'),
251 { type => 'tablebreak-tr-title',
252 value => 'Price plan options',
261 my $curuser = $FS::CurrentUser::CurrentUser;
263 my $edit_global = 'Edit global package definitions';
264 my $acl_edit = $curuser->access_right('Edit package definitions');
265 my $acl_edit_global = $curuser->access_right($edit_global);
267 my $acl_edit_either = $acl_edit || $acl_edit_global;
269 my $begin_callback = sub {
270 my( $cgi, $fields, $opt ) = @_;
272 unless $acl_edit_either
273 || ( $cgi->param('pkgnum')
274 && $curuser->access_right('Customize customer package')
278 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
280 #arg. access rights for cloning are Hard.
281 # on the one hand we don't really want cloning (customizing a package) to fail
282 # for want of finding the source package in normal usage
283 # on the other hand, we don't want people using the clone link to be able to
285 my $agent_clone_extra_sql =
286 ' ( '. FS::part_pkg->curuser_pkgs_sql.
287 " OR ( part_pkg.custom = 'Y' ) ".
290 my $conf = new FS::Conf;
291 my $taxproducts = $conf->exists('enable_taxproducts');
293 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
294 " WHERE disabled IS NULL OR disabled = '' ")
296 $sth->execute or die $sth->errstr;
297 my $report_option = $sth->fetchrow_arrayref->[0];
300 # - tr-part_pkg_freq: month_increments_only (from price plans)
302 # - test errors cloning
303 # - test custom pricing
304 # - move the selectlayer divs away from lame layer_callback
306 #my ($query) = $cgi->keywords;
311 my %tax_override = ();
313 my %taxproductnums = map { ($_->classnum => 1) }
314 qsearch('usage_class', { 'disabled' => '' });
315 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
318 my $recur_disabled = 1;
320 my $error_callback = sub {
321 my($cgi, $object, $fields, $opt ) = @_;
323 (@agent_type) = $cgi->param('agent_type');
325 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
327 $recur_disabled = $cgi->param('freq') ? 0 : 1;
329 foreach ($cgi->param) {
330 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
332 $tax_override{''} = $cgi->param('tax_override');
333 $tax_override{$_} = $cgi->param('tax_override_$_')
334 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
336 #some false laziness w/process
337 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
339 my $options = $cgi->param($plan."__OPTIONS");
340 my @options = split(',', $options);
342 map { my $optionname = $_;
343 my $param = $plan."__$optionname";
344 my $value = join(', ', $cgi->param($param));
345 ( $optionname => $value );
349 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
350 $object->set($_ => scalar($cgi->param($_)) )
351 foreach (qw( setup_fee recur_fee ));
355 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
357 my $new_object_callback = sub {
358 my( $cgi, $hashref, $fields, $opt ) = @_;
360 my $part_pkg = FS::part_pkg->new( $hashref );
361 $part_pkg->set($_ => '0')
362 foreach (qw( setup_fee recur_fee ));
368 my $edit_callback = sub {
369 my( $cgi, $object, $fields, $opt ) = @_;
371 $recur_disabled = $object->freq ? 0 : 1;
374 map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
376 my @report_option = ();
377 foreach ($object->options) {
378 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
379 /^report_option_(\d+)$/ && (push @report_option, $1);
381 foreach ($object->part_pkg_taxoverride) {
382 $taxproductnums{$_->usage_class} = 1
386 $cgi->param('report_option', join(',', @report_option));
387 foreach my $field ( @$fields ) {
389 ref($field) eq 'HASH' &&
391 $field->{field} eq 'report_option'
393 #$field->{curr_value} = join(',', @report_option);
394 $field->{value} = join(',', @report_option);
397 %options = $object->options;
399 $object->set($_ => $object->option($_))
400 foreach (qw( setup_fee recur_fee ));
404 my $new_callback = sub {
405 my( $cgi, $object, $fields ) = @_;
407 my $conf = new FS::Conf;
408 if ( $conf->exists('agent_defaultpkg') ) {
409 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
410 @agent_type = map {$_->typenum} qsearch('agent_type',{});
415 my $clone_callback = sub {
416 my( $cgi, $object, $fields, $opt ) = @_;
418 if ( $cgi->param('pkgnum') ) {
420 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
421 $object->agentnum( $cust_pkg->cust_main->agentnum );
423 $opt->{action} = 'Custom';
425 #my $part_pkg = $clone_part_pkg->clone;
426 #this is all clone does anyway
427 $object->custom('Y');
429 $object->disabled('Y');
431 } else { #not when cloning...
434 map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
438 %options = $object->options;
440 $object->set($_ => $options{$_})
441 foreach (qw( setup_fee recur_fee ));
443 $recur_disabled = $object->freq ? 0 : 1;
446 my $discount_error_callback = sub {
447 my( $cgi, $object ) = @_;
449 if ( /^discountnum(\d+)$/ &&
450 ( my $discountnum = $cgi->param("discountnum$1") ) )
452 new FS::part_pkg_discount {
453 'pkgpart' => $object->pkgpart,
454 'discountnum' => $discountnum,
463 my $m2_error_callback_maker = sub {
464 my $link_type = shift; #yay closures
466 my( $cgi, $object ) = @_;
469 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
470 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
473 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
475 new FS::part_pkg_link {
476 'link_type' => $link_type,
477 'src_pkgpart' => $object->pkgpart,
478 'dst_pkgpart' => $dst,
489 my $javascript = <<'END';
490 <SCRIPT TYPE="text/javascript">
492 function freq_changed(what) {
493 var freq = what.options[what.selectedIndex].value;
496 what.form.recur_fee.disabled = true;
497 what.form.recur_fee.style.backgroundColor = '#dddddd';
499 what.form.recur_fee.disabled = false;
500 what.form.recur_fee.style.backgroundColor = '#ffffff';
505 function agent_changed(what) {
507 var agentnum = what.options[what.selectedIndex].value;
509 if ( agentnum == 0 ) {
510 what.form.agent_type.disabled = false;
511 //what.form.agent_type.style.backgroundColor = '#ffffff';
512 what.form.agent_type.style.visibility = '';
514 what.form.agent_type.disabled = true;
515 //what.form.agent_type.style.backgroundColor = '#dddddd';
516 what.form.agent_type.style.visibility = 'hidden';
521 function aux_planchanged(what) {
524 var plan = what.options[what.selectedIndex].value;
525 var table = document.getElementById('TableNumber7') // XXX NOT ROBUST
527 if ( plan == 'flat' || plan == 'prorate' || plan == 'subscription' ) {
528 //table.disabled = false;
529 table.style.visibility = '';
531 //table.disabled = true;
532 table.style.visibility = 'hidden';
540 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
542 tie my %plan_labels, 'Tie::IxHash',
543 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
546 my $html_bottom = sub {
549 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
551 my $layer_callback = sub {
554 my $html = ntable("#cccccc",2);
558 # <TD ALIGN="right">Recurring fee frequency </TD>
559 # <TD><SELECT NAME="freq">
562 #my @freq = keys %freq;
563 #@freq = grep { /^\d+$/ } @freq
564 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
565 #foreach my $freq ( @freq ) {
566 # $html .= qq(<OPTION VALUE="$freq");
567 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
568 # $html .= ">$freq{$freq}";
571 #$html .= '</SELECT></TD></TR>';
573 my $href = $plans{$layer}->{'fields'};
574 my @fields = exists($plans{$layer}->{'fieldorder'})
575 ? @{$plans{$layer}->{'fieldorder'}}
578 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
580 if(!exists($href->{$field})) {
582 warn "nonexistent part_pkg option: '$field'\n";
586 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
588 my $format = sub { shift };
589 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
591 #XXX these should use elements/ fields... (or this whole thing should
592 #just use layer_fields instead of layer_callback)
594 if ( ! exists($href->{$field}{'type'}) ) {
596 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
597 ( exists($options{$field})
598 ? &$format($options{$field})
599 : $href->{$field}{'default'} ).
602 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
604 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
605 ( exists($options{$field}) && $options{$field}
610 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
614 if $href->{$field}{'type'} eq 'select_multiple';
615 $html .= qq! NAME="${layer}__$field">!;
617 $html .= '<OPTION VALUE="">'. $href->{$field}{'empty_label'}
618 if exists($href->{$field}{'disable_empty'})
619 && ! $href->{$field}{'disable_empty'};
621 if ( $href->{$field}{'select_table'} ) {
623 qsearch( $href->{$field}{'select_table'},
624 $href->{$field}{'select_hash'} )
626 my $value = $record->getfield($href->{$field}{'select_key'});
627 $html .= qq!<OPTION VALUE="$value"!.
628 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
632 '>'. $record->getfield($href->{$field}{'select_label'});
634 } elsif ( $href->{$field}{'select_options'} ) {
635 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
636 my $label = $href->{$field}{'select_options'}{$key};
637 $html .= qq!<OPTION VALUE="$key"!.
638 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
646 $html .= '<font color="#ff0000">warning: '.
647 "don't know how to retreive options for $field select field".
650 $html .= '</SELECT>';
652 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
655 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
657 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
658 my $label = $href->{$field}{'options'}{$key};
659 $html .= qq!$radio VALUE="$key"!.
660 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
669 $html .= '</TD></TR>';
673 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
674 join(',', keys %{ $href } ). '">';
682 options => [ keys %plan_labels ],
683 labels => \%plan_labels,
684 curr_value => $object->plan,
685 layer_callback => $layer_callback,
689 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
690 '<SCRIPT TYPE="text/javascript">'.
691 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
694 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
697 $return .= '</SCRIPT>';
703 my %usage_class = map { ($_->classnum => $_->classname) }
704 qsearch('usage_class', {});
705 $usage_class{setup} = 'Setup';
706 $usage_class{recur} = 'Recurring';
708 my %taxproduct_fields = ();
709 my $end_callback = sub {
710 my( $cgi, $object, $fields, $opt ) = @_;
712 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
714 if ( $object->pkgpart ) {
715 foreach my $usage_class ( '', @taxproductnums ) {
716 $tax_override{$usage_class} =
717 join (",", map $_->taxclassnum,
718 qsearch( 'part_pkg_taxoverride', {
719 'pkgpart' => $object->pkgpart,
720 'usage_class' => $usage_class,
727 map { $_ => [ "taxproductnum_$_",
728 { type => 'select-taxproduct',
729 #label => "$usage_class{$_} tax product",
732 { type => 'select-taxoverride' }
737 $taxproduct_fields{'(default)'} =
738 [ 'taxproductnum', { type => 'select-taxproduct',
739 #label => 'Default tax product',
741 'tax_override', { type => 'select-taxoverride' },
745 my $taxproduct_values = sub {
746 my ($cgi, $object, $flags) = @_;
748 sub { my $layer = shift;
749 my @fields = @{$taxproduct_fields{$layer}};
752 my $field = shift @fields;
754 $field =~ /^taxproductnum_\w+$/ &&
755 push @values, ( $field => $options{"usage_$field"} );
756 $field =~ /^tax_override_(\w+)$/ &&
757 push @values, ( $field => $tax_override{$1} );
758 $field =~ /^taxproductnum$/ &&
759 push @values, ( $field => $object->taxproductnum );
760 $field =~ /^tax_override$/ &&
761 push @values, ( $field => $tax_override{''} );
767 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
772 my $field_callback = sub {
773 my ($cgi, $object, $fieldref) = @_;
775 my $field = $fieldref->{field};
776 if ($field eq 'taxproductnums') {
777 $fieldref->{value} = join(',', @taxproductnums);
778 } elsif ($field eq 'taxproduct_select') {
779 $fieldref->{options} = [ '(default)', @taxproductnums ];
780 $fieldref->{labels} = { ( '(default)' => '(default)' ),
781 map {( $_ => ($usage_class{$_} || $_) )}
784 $fieldref->{layer_fields} = \%taxproduct_fields;
785 $fieldref->{layer_values_callback} = $taxproduct_values;
789 my $discountnum_hashref = {
791 'months' => { 'op' => '>', 'value' => 1 },