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 'promo_code' => 'Promotional code',
33 'freq' => 'Recurring fee frequency',
34 'setuptax' => 'Setup fee tax exempt',
35 'recurtax' => 'Recurring fee tax exempt',
36 'taxclass' => 'Tax class',
37 'taxproduct_select'=> 'Tax products',
38 'plan' => 'Price plan',
39 'disabled' => 'Disable new orders',
40 'setup_cost' => 'Setup cost',
41 'recur_cost' => 'Recur cost',
42 'pay_weight' => 'Payment weight',
43 'credit_weight' => 'Credit weight',
44 'agentnum' => 'Agent',
45 'setup_fee' => 'Setup fee',
46 'recur_fee' => 'Recurring fee',
47 'bill_dst_pkgpart' => 'Include line item(s) from package',
48 'svc_dst_pkgpart' => 'Include services of package',
49 'report_option' => 'Report classes',
50 'fcc_ds0s' => 'Voice-grade eqivalents',
54 { field=>'clone', type=>'hidden',
55 curr_value_callback =>
56 sub { shift->param('clone') },
58 { field=>'pkgnum', type=>'hidden',
59 curr_value_callback =>
60 sub { shift->param('pkgnum') },
63 { field=>'custom', type=>'hidden' },
65 { type => 'columnstart' },
72 {field=>'comment', type=>'text', size=>40 }, #32
73 { field => 'agentnum',
74 type => 'select-agent',
75 disable_empty => ! $acl_edit_global,
76 empty_label => '(global)',
77 onchange => 'agent_changed',
79 {field=>'classnum', type=>'select-pkg_class' },
80 {field=>'disabled', type=>$disabled_type, value=>'Y'},
82 { type => 'tablebreak-tr-title',
83 value => 'Pricing', #better name?
86 type => 'selectlayers-select',
87 options => [ keys %plan_labels ],
88 labels => \%plan_labels,
90 { field => 'setup_fee',
94 type => 'part_pkg_freq',
95 onchange => 'freq_changed',
97 { field => 'recur_fee',
99 disabled => sub { $recur_disabled },
105 #recurring fee (auto-disable)
107 { type => 'columnnext' },
109 {type=>'justtitle', value=>'Taxation' },
110 {field=>'setuptax', type=>'checkbox', value=>'Y'},
111 {field=>'recurtax', type=>'checkbox', value=>'Y'},
112 {field=>'taxclass', type=>'select-taxclass' },
113 { field => 'taxproductnums',
115 value => join(',', @taxproductnums),
117 { field => 'taxproduct_select',
118 type => 'selectlayers',
119 options => [ '(default)', @taxproductnums ],
120 curr_value => '(default)',
121 labels => { ( '(default)' => '(default)' ),
122 map {($_=>$usage_class{$_})}
125 layer_fields => \%taxproduct_fields,
126 layer_values_callback => $taxproduct_values,
127 layers_only => !$taxproducts,
128 cell_style => ( !$taxproducts
134 { type => 'tablebreak-tr-title',
135 value => 'Promotions', #better name?
137 { field=>'promo_code', type=>'text', size=>15 },
139 { type => 'tablebreak-tr-title',
140 value => 'Cost tracking', #better name?
142 { field=>'setup_cost', type=>'money', },
143 { field=>'recur_cost', type=>'money', },
145 { type => 'columnnext' },
147 { field => 'agent_type',
148 type => 'select-agent_types',
149 disabled => ! $acl_edit_global,
150 curr_value_callback => sub {
151 my($cgi, $object, $field) = @_;
152 #in the other callbacks..? hmm.
157 { type => 'tablebreak-tr-title',
158 value => 'Line-item revenue recogition', #better name?
160 { field=>'pay_weight', type=>'text', size=>6 },
161 { field=>'credit_weight', type=>'text', size=>6 },
163 ( $conf->exists('cust_pkg-show_fcc_voice_grade_equivalent')
165 { type => 'tablebreak-tr-title',
166 value => 'FCC Form 477 information',
168 { field=>'fcc_ds0s', type=>'text', size=>6 },
174 { type => 'columnend' },
176 { 'type' => $report_option ? 'tablebreak-tr-title'
178 'value' => 'Optional report classes',
179 'field' => 'census_title',
181 { 'field' => 'report_option',
182 'type' => $report_option ? 'select-table'
184 'table' => 'part_pkg_report_option',
185 'name_col' => 'name',
186 'hashref' => { 'disabled' => '' },
191 { 'type' => 'tablebreak-tr-title',
192 'value' => 'Pricing add-ons',
195 { 'field' => 'bill_dst_pkgpart',
196 'type' => 'select-part_pkg',
197 'm2_label' => 'Include line item(s) from package',
198 'm2m_method' => 'bill_part_pkg_link',
199 'm2m_dstcol' => 'dst_pkgpart',
200 'm2_error_callback' =>
201 &{$m2_error_callback_maker}('bill'),
202 'm2_fields' => [ { 'field' => 'hidden',
203 'type' => 'checkbox',
211 { type => 'tablebreak-tr-title',
214 { type => 'pkg_svc', },
216 { 'field' => 'svc_dst_pkgpart',
217 'label' => 'Also include services from package: ',
218 'type' => 'select-part_pkg',
219 'm2_label' => 'Include services of package: ',
220 'm2m_method' => 'svc_part_pkg_link',
221 'm2m_dstcol' => 'dst_pkgpart',
222 'm2_error_callback' =>
223 &{$m2_error_callback_maker}('svc'),
226 { type => 'tablebreak-tr-title',
227 value => 'Price plan options',
236 my $curuser = $FS::CurrentUser::CurrentUser;
238 my $edit_global = 'Edit global package definitions';
239 my $acl_edit = $curuser->access_right('Edit package definitions');
240 my $acl_edit_global = $curuser->access_right($edit_global);
242 my $acl_edit_either = $acl_edit || $acl_edit_global;
244 my $begin_callback = sub {
245 my( $cgi, $fields, $opt ) = @_;
247 unless $acl_edit_either
248 || ( $cgi->param('pkgnum')
249 && $curuser->access_right('Customize customer package')
253 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
255 #arg. access rights for cloning are Hard.
256 # on the one hand we don't really want cloning (customizing a package) to fail
257 # for want of finding the source package in normal usage
258 # on the other hand, we don't want people using the clone link to be able to
260 my $agent_clone_extra_sql =
261 ' ( '. FS::part_pkg->curuser_pkgs_sql.
262 " OR ( part_pkg.custom = 'Y' ) ".
265 my $conf = new FS::Conf;
266 my $taxproducts = $conf->exists('enable_taxproducts');
268 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
269 " WHERE disabled IS NULL OR disabled = '' ")
271 $sth->execute or die $sth->errstr;
272 my $report_option = $sth->fetchrow_arrayref->[0];
275 # - tr-part_pkg_freq: month_increments_only (from price plans)
277 # - test errors cloning
278 # - test custom pricing
279 # - move the selectlayer divs away from lame layer_callback
281 #my ($query) = $cgi->keywords;
286 my %tax_override = ();
288 my %taxproductnums = map { ($_->classnum => 1) }
289 qsearch('usage_class', { 'disabled' => '' });
290 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
293 my $recur_disabled = 1;
295 my $error_callback = sub {
296 my($cgi, $object, $fields, $opt ) = @_;
298 (@agent_type) = $cgi->param('agent_type');
300 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
302 $recur_disabled = $cgi->param('freq') ? 0 : 1;
304 foreach ($cgi->param) {
305 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
307 $tax_override{''} = $cgi->param('tax_override');
308 $tax_override{$_} = $cgi->param('tax_override_$_')
309 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
311 #some false laziness w/process
312 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
314 my $options = $cgi->param($plan."__OPTIONS");
315 my @options = split(',', $options);
317 map { my $optionname = $_;
318 my $param = $plan."__$optionname";
319 my $value = join(', ', $cgi->param($param));
320 ( $optionname => $value );
324 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
325 $object->set($_ => scalar($cgi->param($_)) )
326 foreach (qw( setup_fee recur_fee ));
330 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
332 my $new_object_callback = sub {
333 my( $cgi, $hashref, $fields, $opt ) = @_;
335 my $part_pkg = FS::part_pkg->new( $hashref );
336 $part_pkg->set($_ => '0')
337 foreach (qw( setup_fee recur_fee ));
343 my $edit_callback = sub {
344 my( $cgi, $object, $fields, $opt ) = @_;
346 $recur_disabled = $object->freq ? 0 : 1;
349 map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
351 my @report_option = ();
352 foreach ($object->options) {
353 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
354 /^report_option_(\d+)$/ && (push @report_option, $1);
356 foreach ($object->part_pkg_taxoverride) {
357 $taxproductnums{$_->usage_class} = 1
361 $cgi->param('report_option', join(',', @report_option));
362 foreach my $field ( @$fields ) {
364 ref($field) eq 'HASH' &&
366 $field->{field} eq 'report_option'
368 #$field->{curr_value} = join(',', @report_option);
369 $field->{value} = join(',', @report_option);
372 %options = $object->options;
374 $object->set($_ => $object->option($_))
375 foreach (qw( setup_fee recur_fee ));
379 my $new_callback = sub {
380 my( $cgi, $object, $fields ) = @_;
382 my $conf = new FS::Conf;
383 if ( $conf->exists('agent_defaultpkg') ) {
384 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
385 @agent_type = map {$_->typenum} qsearch('agent_type',{});
390 my $clone_callback = sub {
391 my( $cgi, $object, $fields, $opt ) = @_;
393 if ( $cgi->param('pkgnum') ) {
395 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
396 $object->agentnum( $cust_pkg->cust_main->agentnum );
398 $opt->{action} = 'Custom';
400 #my $part_pkg = $clone_part_pkg->clone;
401 #this is all clone does anyway
402 $object->custom('Y');
404 $object->disabled('Y');
406 } else { #not when cloning...
409 map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
413 %options = $object->options;
415 $object->set($_ => $options{$_})
416 foreach (qw( setup_fee recur_fee ));
418 $recur_disabled = $object->freq ? 0 : 1;
421 my $m2_error_callback_maker = sub {
422 my $link_type = shift; #yay closures
424 my( $cgi, $object ) = @_;
427 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
428 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
431 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
433 new FS::part_pkg_link {
434 'link_type' => $link_type,
435 'src_pkgpart' => $object->pkgpart,
436 'dst_pkgpart' => $dst,
447 my $javascript = <<'END';
448 <SCRIPT TYPE="text/javascript">
450 function freq_changed(what) {
451 var freq = what.options[what.selectedIndex].value;
454 what.form.recur_fee.disabled = true;
455 what.form.recur_fee.style.backgroundColor = '#dddddd';
457 what.form.recur_fee.disabled = false;
458 what.form.recur_fee.style.backgroundColor = '#ffffff';
463 function agent_changed(what) {
465 var agentnum = what.options[what.selectedIndex].value;
467 if ( agentnum == 0 ) {
468 what.form.agent_type.disabled = false;
469 //what.form.agent_type.style.backgroundColor = '#ffffff';
470 what.form.agent_type.style.visibility = '';
472 what.form.agent_type.disabled = true;
473 //what.form.agent_type.style.backgroundColor = '#dddddd';
474 what.form.agent_type.style.visibility = 'hidden';
482 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
484 tie my %plan_labels, 'Tie::IxHash',
485 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
488 my $html_bottom = sub {
491 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
493 my $layer_callback = sub {
496 my $html = ntable("#cccccc",2);
500 # <TD ALIGN="right">Recurring fee frequency </TD>
501 # <TD><SELECT NAME="freq">
504 #my @freq = keys %freq;
505 #@freq = grep { /^\d+$/ } @freq
506 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
507 #foreach my $freq ( @freq ) {
508 # $html .= qq(<OPTION VALUE="$freq");
509 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
510 # $html .= ">$freq{$freq}";
512 #$html .= '</SELECT></TD></TR>';
514 my $href = $plans{$layer}->{'fields'};
515 my @fields = exists($plans{$layer}->{'fieldorder'})
516 ? @{$plans{$layer}->{'fieldorder'}}
519 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
521 if(!exists($href->{$field})) {
523 warn "nonexistent part_pkg option: '$field'\n";
527 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
529 my $format = sub { shift };
530 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
532 #XXX these should use elements/ fields... (or this whole thing should
533 #just use layer_fields instead of layer_callback)
535 if ( ! exists($href->{$field}{'type'}) ) {
537 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
538 ( exists($options{$field})
539 ? &$format($options{$field})
540 : $href->{$field}{'default'} ).
543 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
545 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
546 ( exists($options{$field}) && $options{$field}
551 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
555 if $href->{$field}{'type'} eq 'select_multiple';
556 $html .= qq! NAME="${layer}__$field">!;
558 if ( $href->{$field}{'select_table'} ) {
560 qsearch( $href->{$field}{'select_table'},
561 $href->{$field}{'select_hash'} )
563 my $value = $record->getfield($href->{$field}{'select_key'});
564 $html .= qq!<OPTION VALUE="$value"!.
565 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
569 '>'. $record->getfield($href->{$field}{'select_label'});
571 } elsif ( $href->{$field}{'select_options'} ) {
572 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
573 my $label = $href->{$field}{'select_options'}{$key};
574 $html .= qq!<OPTION VALUE="$key"!.
575 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
583 $html .= '<font color="#ff0000">warning: '.
584 "don't know how to retreive options for $field select field".
587 $html .= '</SELECT>';
589 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
592 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
594 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
595 my $label = $href->{$field}{'options'}{$key};
596 $html .= qq!$radio VALUE="$key"!.
597 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
606 $html .= '</TD></TR>';
610 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
611 join(',', keys %{ $href } ). '">';
619 options => [ keys %plan_labels ],
620 labels => \%plan_labels,
621 curr_value => $object->plan,
622 layer_callback => $layer_callback,
626 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
627 '<SCRIPT TYPE="text/javascript">'.
628 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
631 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
634 $return .= '</SCRIPT>';
640 my %usage_class = map { ($_->classnum => $_->classname) }
641 qsearch('usage_class', {});
642 $usage_class{setup} = 'Setup';
643 $usage_class{recur} = 'Recurring';
645 my %taxproduct_fields = ();
646 my $end_callback = sub {
647 my( $cgi, $object, $fields, $opt ) = @_;
649 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
651 if ( $object->pkgpart ) {
652 foreach my $usage_class ( '', @taxproductnums ) {
653 $tax_override{$usage_class} =
654 join (",", map $_->taxclassnum,
655 qsearch( 'part_pkg_taxoverride', {
656 'pkgpart' => $object->pkgpart,
657 'usage_class' => $usage_class,
664 map { $_ => [ "taxproductnum_$_",
665 { type => 'select-taxproduct',
666 #label => "$usage_class{$_} tax product",
669 { type => 'select-taxoverride' }
674 $taxproduct_fields{'(default)'} =
675 [ 'taxproductnum', { type => 'select-taxproduct',
676 #label => 'Default tax product',
678 'tax_override', { type => 'select-taxoverride' },
682 my $taxproduct_values = sub {
683 my ($cgi, $object, $flags) = @_;
685 sub { my $layer = shift;
686 my @fields = @{$taxproduct_fields{$layer}};
689 my $field = shift @fields;
691 $field =~ /^taxproductnum_\w+$/ &&
692 push @values, ( $field => $options{"usage_$field"} );
693 $field =~ /^tax_override_(\w+)$/ &&
694 push @values, ( $field => $tax_override{$1} );
695 $field =~ /^taxproductnum$/ &&
696 push @values, ( $field => $object->taxproductnum );
697 $field =~ /^tax_override$/ &&
698 push @values, ( $field => $tax_override{''} );
704 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
709 my $field_callback = sub {
710 my ($cgi, $object, $fieldref) = @_;
712 my $field = $fieldref->{field};
713 if ($field eq 'taxproductnums') {
714 $fieldref->{value} = join(',', @taxproductnums);
715 } elsif ($field eq 'taxproduct_select') {
716 $fieldref->{options} = [ '(default)', @taxproductnums ];
717 $fieldref->{labels} = { ( '(default)' => '(default)' ),
718 map {( $_ => ($usage_class{$_} || $_) )}
721 $fieldref->{layer_fields} = \%taxproduct_fields;
722 $fieldref->{layer_values_callback} = $taxproduct_values;