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',
53 { field=>'clone', type=>'hidden',
54 curr_value_callback =>
55 sub { shift->param('clone') },
57 { field=>'pkgnum', type=>'hidden',
58 curr_value_callback =>
59 sub { shift->param('pkgnum') },
62 { field=>'custom', type=>'hidden' },
64 { type => 'columnstart' },
71 {field=>'comment', type=>'text', size=>40 }, #32
72 { field => 'agentnum',
73 type => 'select-agent',
74 disable_empty => ! $acl_edit_global,
75 empty_label => '(global)',
76 onchange => 'agent_changed',
78 {field=>'classnum', type=>'select-pkg_class' },
79 {field=>'disabled', type=>$disabled_type, value=>'Y'},
81 { type => 'tablebreak-tr-title',
82 value => 'Pricing', #better name?
85 type => 'selectlayers-select',
86 options => [ keys %plan_labels ],
87 labels => \%plan_labels,
89 { field => 'setup_fee',
93 type => 'part_pkg_freq',
94 onchange => 'freq_changed',
96 { field => 'recur_fee',
98 disabled => sub { $recur_disabled },
104 #recurring fee (auto-disable)
106 { type => 'columnnext' },
108 {type=>'justtitle', value=>'Taxation' },
109 {field=>'setuptax', type=>'checkbox', value=>'Y'},
110 {field=>'recurtax', type=>'checkbox', value=>'Y'},
111 {field=>'taxclass', type=>'select-taxclass' },
112 { field => 'taxproductnums',
114 value => join(',', @taxproductnums),
116 { field => 'taxproduct_select',
117 type => 'selectlayers',
118 options => [ '(default)', @taxproductnums ],
119 curr_value => '(default)',
120 labels => { ( '(default)' => '(default)' ),
121 map {($_=>$usage_class{$_})}
124 layer_fields => \%taxproduct_fields,
125 layer_values_callback => $taxproduct_values,
126 layers_only => !$taxproducts,
127 cell_style => ( !$taxproducts
133 { type => 'tablebreak-tr-title',
134 value => 'Promotions', #better name?
136 { field=>'promo_code', type=>'text', size=>15 },
138 { type => 'tablebreak-tr-title',
139 value => 'Cost tracking', #better name?
141 { field=>'setup_cost', type=>'money', },
142 { field=>'recur_cost', type=>'money', },
144 { type => 'columnnext' },
146 { field => 'agent_type',
147 type => 'select-agent_types',
148 disabled => ! $acl_edit_global,
149 curr_value_callback => sub {
150 my($cgi, $object, $field) = @_;
151 #in the other callbacks..? hmm.
156 { type => 'tablebreak-tr-title',
157 value => 'Line-item revenue recogition', #better name?
159 { field=>'pay_weight', type=>'text', size=>6 },
160 { field=>'credit_weight', type=>'text', size=>6 },
163 { type => 'columnend' },
165 { 'type' => $report_option ? 'tablebreak-tr-title'
167 'value' => 'Optional report classes',
168 'field' => 'census_title',
170 { 'field' => 'report_option',
171 'type' => $report_option ? 'select-table'
173 'table' => 'part_pkg_report_option',
174 'name_col' => 'name',
175 'hashref' => { 'disabled' => '' },
180 { 'type' => 'tablebreak-tr-title',
181 'value' => 'Pricing add-ons',
184 { 'field' => 'bill_dst_pkgpart',
185 'type' => 'select-part_pkg',
186 'm2_label' => 'Include line item(s) from package',
187 'm2m_method' => 'bill_part_pkg_link',
188 'm2m_dstcol' => 'dst_pkgpart',
189 'm2_error_callback' =>
190 &{$m2_error_callback_maker}('bill'),
191 'm2_fields' => [ { 'field' => 'hidden',
192 'type' => 'checkbox',
200 { type => 'tablebreak-tr-title',
203 { type => 'pkg_svc', },
205 { 'field' => 'svc_dst_pkgpart',
206 'label' => 'Also include services from package: ',
207 'type' => 'select-part_pkg',
208 'm2_label' => 'Include services of package: ',
209 'm2m_method' => 'svc_part_pkg_link',
210 'm2m_dstcol' => 'dst_pkgpart',
211 'm2_error_callback' =>
212 &{$m2_error_callback_maker}('svc'),
215 { type => 'tablebreak-tr-title',
216 value => 'Price plan options',
225 my $curuser = $FS::CurrentUser::CurrentUser;
227 my $edit_global = 'Edit global package definitions';
228 my $acl_edit = $curuser->access_right('Edit package definitions');
229 my $acl_edit_global = $curuser->access_right($edit_global);
231 my $acl_edit_either = $acl_edit || $acl_edit_global;
233 my $begin_callback = sub {
234 my( $cgi, $fields, $opt ) = @_;
236 unless $acl_edit_either
237 || ( $cgi->param('pkgnum')
238 && $curuser->access_right('Customize customer package')
242 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
244 #arg. access rights for cloning are Hard.
245 # on the one hand we don't really want cloning (customizing a package) to fail
246 # for want of finding the source package in normal usage
247 # on the other hand, we don't want people using the clone link to be able to
249 my $agent_clone_extra_sql =
250 ' ( '. FS::part_pkg->curuser_pkgs_sql.
251 " OR ( part_pkg.custom = 'Y' ) ".
254 my $conf = new FS::Conf;
255 my $taxproducts = $conf->exists('enable_taxproducts');
257 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
258 " WHERE disabled IS NULL OR disabled = '' ")
260 $sth->execute or die $sth->errstr;
261 my $report_option = $sth->fetchrow_arrayref->[0];
264 # - tr-part_pkg_freq: month_increments_only (from price plans)
266 # - test errors cloning
267 # - test custom pricing
268 # - move the selectlayer divs away from lame layer_callback
270 #my ($query) = $cgi->keywords;
275 my %tax_override = ();
277 my %taxproductnums = map { ($_->classnum => 1) }
278 qsearch('usage_class', { 'disabled' => '' });
279 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
282 my $recur_disabled = 1;
284 my $error_callback = sub {
285 my($cgi, $object, $fields, $opt ) = @_;
287 (@agent_type) = $cgi->param('agent_type');
289 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
291 $recur_disabled = $cgi->param('freq') ? 0 : 1;
293 foreach ($cgi->param) {
294 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
296 $tax_override{''} = $cgi->param('tax_override');
297 $tax_override{$_} = $cgi->param('tax_override_$_')
298 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
300 #some false laziness w/process
301 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
303 my $options = $cgi->param($plan."__OPTIONS");
304 my @options = split(',', $options);
306 map { my $optionname = $_;
307 my $param = $plan."__$optionname";
308 my $value = join(', ', $cgi->param($param));
309 ( $optionname => $value );
313 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
314 $object->set($_ => scalar($cgi->param($_)) )
315 foreach (qw( setup_fee recur_fee ));
319 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
321 my $new_object_callback = sub {
322 my( $cgi, $hashref, $fields, $opt ) = @_;
324 my $part_pkg = FS::part_pkg->new( $hashref );
325 $part_pkg->set($_ => '0')
326 foreach (qw( setup_fee recur_fee ));
332 my $edit_callback = sub {
333 my( $cgi, $object, $fields, $opt ) = @_;
335 $recur_disabled = $object->freq ? 0 : 1;
338 map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
340 my @report_option = ();
341 foreach ($object->options) {
342 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
343 /^report_option_(\d+)$/ && (push @report_option, $1);
345 foreach ($object->part_pkg_taxoverride) {
346 $taxproductnums{$_->usage_class} = 1
350 $cgi->param('report_option', join(',', @report_option));
351 foreach my $field ( @$fields ) {
353 ref($field) eq 'HASH' &&
355 $field->{field} eq 'report_option'
357 #$field->{curr_value} = join(',', @report_option);
358 $field->{value} = join(',', @report_option);
361 %options = $object->options;
363 $object->set($_ => $object->option($_))
364 foreach (qw( setup_fee recur_fee ));
368 my $new_callback = sub {
369 my( $cgi, $object, $fields ) = @_;
371 my $conf = new FS::Conf;
372 if ( $conf->exists('agent_defaultpkg') ) {
373 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
374 @agent_type = map {$_->typenum} qsearch('agent_type',{});
379 my $clone_callback = sub {
380 my( $cgi, $object, $fields, $opt ) = @_;
382 if ( $cgi->param('pkgnum') ) {
384 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
385 $object->agentnum( $cust_pkg->cust_main->agentnum );
387 $opt->{action} = 'Custom';
389 #my $part_pkg = $clone_part_pkg->clone;
390 #this is all clone does anyway
391 $object->custom('Y');
393 $object->disabled('Y');
395 } else { #not when cloning...
398 map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
402 %options = $object->options;
404 $object->set($_ => $options{$_})
405 foreach (qw( setup_fee recur_fee ));
407 $recur_disabled = $object->freq ? 0 : 1;
410 my $m2_error_callback_maker = sub {
411 my $link_type = shift; #yay closures
413 my( $cgi, $object ) = @_;
416 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
417 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
420 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
422 new FS::part_pkg_link {
423 'link_type' => $link_type,
424 'src_pkgpart' => $object->pkgpart,
425 'dst_pkgpart' => $dst,
436 my $javascript = <<'END';
437 <SCRIPT TYPE="text/javascript">
439 function freq_changed(what) {
440 var freq = what.options[what.selectedIndex].value;
443 what.form.recur_fee.disabled = true;
444 what.form.recur_fee.style.backgroundColor = '#dddddd';
446 what.form.recur_fee.disabled = false;
447 what.form.recur_fee.style.backgroundColor = '#ffffff';
452 function agent_changed(what) {
454 var agentnum = what.options[what.selectedIndex].value;
456 if ( agentnum == 0 ) {
457 what.form.agent_type.disabled = false;
458 //what.form.agent_type.style.backgroundColor = '#ffffff';
459 what.form.agent_type.style.visibility = '';
461 what.form.agent_type.disabled = true;
462 //what.form.agent_type.style.backgroundColor = '#dddddd';
463 what.form.agent_type.style.visibility = 'hidden';
471 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
473 tie my %plan_labels, 'Tie::IxHash',
474 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
477 my $html_bottom = sub {
480 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
482 my $layer_callback = sub {
485 my $html = ntable("#cccccc",2);
489 # <TD ALIGN="right">Recurring fee frequency </TD>
490 # <TD><SELECT NAME="freq">
493 #my @freq = keys %freq;
494 #@freq = grep { /^\d+$/ } @freq
495 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
496 #foreach my $freq ( @freq ) {
497 # $html .= qq(<OPTION VALUE="$freq");
498 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
499 # $html .= ">$freq{$freq}";
501 #$html .= '</SELECT></TD></TR>';
503 my $href = $plans{$layer}->{'fields'};
504 my @fields = exists($plans{$layer}->{'fieldorder'})
505 ? @{$plans{$layer}->{'fieldorder'}}
508 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
510 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
512 my $format = sub { shift };
513 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
515 #XXX these should use elements/ fields... (or this whole thing should
516 #just use layer_fields instead of layer_callback)
518 if ( ! exists($href->{$field}{'type'}) ) {
520 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
521 ( exists($options{$field})
522 ? &$format($options{$field})
523 : $href->{$field}{'default'} ).
526 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
528 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
529 ( exists($options{$field}) && $options{$field}
534 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
538 if $href->{$field}{'type'} eq 'select_multiple';
539 $html .= qq! NAME="${layer}__$field">!;
541 if ( $href->{$field}{'select_table'} ) {
543 qsearch( $href->{$field}{'select_table'},
544 $href->{$field}{'select_hash'} )
546 my $value = $record->getfield($href->{$field}{'select_key'});
547 $html .= qq!<OPTION VALUE="$value"!.
548 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
552 '>'. $record->getfield($href->{$field}{'select_label'});
554 } elsif ( $href->{$field}{'select_options'} ) {
555 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
556 my $label = $href->{$field}{'select_options'}{$key};
557 $html .= qq!<OPTION VALUE="$key"!.
558 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
566 $html .= '<font color="#ff0000">warning: '.
567 "don't know how to retreive options for $field select field".
570 $html .= '</SELECT>';
572 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
575 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
577 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
578 my $label = $href->{$field}{'options'}{$key};
579 $html .= qq!$radio VALUE="$key"!.
580 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
589 $html .= '</TD></TR>';
593 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
594 join(',', keys %{ $href } ). '">';
602 options => [ keys %plan_labels ],
603 labels => \%plan_labels,
604 curr_value => $object->plan,
605 layer_callback => $layer_callback,
609 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
610 '<SCRIPT TYPE="text/javascript">'.
611 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
614 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
617 $return .= '</SCRIPT>';
623 my %usage_class = map { ($_->classnum => $_->classname) }
624 qsearch('usage_class', {});
625 $usage_class{setup} = 'Setup';
626 $usage_class{recur} = 'Recurring';
628 my %taxproduct_fields = ();
629 my $end_callback = sub {
630 my( $cgi, $object, $fields, $opt ) = @_;
632 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
634 if ( $object->pkgpart ) {
635 foreach my $usage_class ( '', @taxproductnums ) {
636 $tax_override{$usage_class} =
637 join (",", map $_->taxclassnum,
638 qsearch( 'part_pkg_taxoverride', {
639 'pkgpart' => $object->pkgpart,
640 'usage_class' => $usage_class,
647 map { $_ => [ "taxproductnum_$_",
648 { type => 'select-taxproduct',
649 #label => "$usage_class{$_} tax product",
652 { type => 'select-taxoverride' }
657 $taxproduct_fields{'(default)'} =
658 [ 'taxproductnum', { type => 'select-taxproduct',
659 #label => 'Default tax product',
661 'tax_override', { type => 'select-taxoverride' },
665 my $taxproduct_values = sub {
666 my ($cgi, $object, $flags) = @_;
668 sub { my $layer = shift;
669 my @fields = @{$taxproduct_fields{$layer}};
672 my $field = shift @fields;
674 $field =~ /^taxproductnum_\w+$/ &&
675 push @values, ( $field => $options{"usage_$field"} );
676 $field =~ /^tax_override_(\w+)$/ &&
677 push @values, ( $field => $tax_override{$1} );
678 $field =~ /^taxproductnum$/ &&
679 push @values, ( $field => $object->taxproductnum );
680 $field =~ /^tax_override$/ &&
681 push @values, ( $field => $tax_override{''} );
687 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
692 my $field_callback = sub {
693 my ($cgi, $object, $fieldref) = @_;
695 my $field = $fieldref->{field};
696 if ($field eq 'taxproductnums') {
697 $fieldref->{value} = join(',', @taxproductnums);
698 } elsif ($field eq 'taxproduct_select') {
699 $fieldref->{options} = [ '(default)', @taxproductnums ];
700 $fieldref->{labels} = { ( '(default)' => '(default)' ),
701 map {( $_ => ($usage_class{$_} || $_) )}
704 $fieldref->{layer_fields} = \%taxproduct_fields;
705 $fieldref->{layer_values_callback} = $taxproduct_values;