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' => $census ? 'tablebreak-tr-title'
167 'value' => 'Optional report classes',
168 'field' => 'census_title',
170 { 'field' => 'report_option',
171 'type' => $census ? 'select-table' : 'hidden',
172 'table' => 'part_pkg_report_option',
173 'name_col' => 'name',
178 { 'type' => 'tablebreak-tr-title',
179 'value' => 'Pricing add-ons',
182 { 'field' => 'bill_dst_pkgpart',
183 'type' => 'select-part_pkg',
184 'm2_label' => 'Include line item(s) from package',
185 'm2m_method' => 'bill_part_pkg_link',
186 'm2m_dstcol' => 'dst_pkgpart',
187 'm2_error_callback' =>
188 &{$m2_error_callback_maker}('bill'),
189 'm2_fields' => [ { 'field' => 'hidden',
190 'type' => 'checkbox',
198 { type => 'tablebreak-tr-title',
201 { type => 'pkg_svc', },
203 { 'field' => 'svc_dst_pkgpart',
204 'label' => 'Also include services from package: ',
205 'type' => 'select-part_pkg',
206 'm2_label' => 'Include services of package: ',
207 'm2m_method' => 'svc_part_pkg_link',
208 'm2m_dstcol' => 'dst_pkgpart',
209 'm2_error_callback' =>
210 &{$m2_error_callback_maker}('svc'),
213 { type => 'tablebreak-tr-title',
214 value => 'Price plan options',
223 my $curuser = $FS::CurrentUser::CurrentUser;
225 my $edit_global = 'Edit global package definitions';
226 my $acl_edit = $curuser->access_right('Edit package definitions');
227 my $acl_edit_global = $curuser->access_right($edit_global);
229 my $acl_edit_either = $acl_edit || $acl_edit_global;
231 my $begin_callback = sub {
232 my( $cgi, $fields, $opt ) = @_;
234 unless $acl_edit_either
235 || ( $cgi->param('pkgnum')
236 && $curuser->access_right('Customize customer package')
240 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
242 #arg. access rights for cloning are Hard.
243 # on the one hand we don't really want cloning (customizing a package) to fail
244 # for want of finding the source package in normal usage
245 # on the other hand, we don't want people using the clone link to be able to
247 my $agent_clone_extra_sql =
248 ' ( '. FS::part_pkg->curuser_pkgs_sql.
249 " OR ( part_pkg.custom = 'Y' ) ".
252 my $conf = new FS::Conf;
253 my $taxproducts = $conf->exists('enable_taxproducts');
255 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
256 " WHERE disabled IS NULL OR disabled = '' ")
258 $sth->execute or die $sth->errstr;
259 my $census = $sth->fetchrow_arrayref->[0];
262 # - tr-part_pkg_freq: month_increments_only (from price plans)
264 # - test errors cloning
265 # - test custom pricing
266 # - move the selectlayer divs away from lame layer_callback
268 #my ($query) = $cgi->keywords;
273 my %tax_override = ();
275 my %taxproductnums = map { ($_->classnum => 1) }
276 qsearch('usage_class', { 'disabled' => '' });
277 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
280 my $recur_disabled = 1;
282 my $error_callback = sub {
283 my($cgi, $object, $fields, $opt ) = @_;
285 (@agent_type) = $cgi->param('agent_type');
287 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
289 $recur_disabled = $cgi->param('freq') ? 0 : 1;
291 foreach ($cgi->param) {
292 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
294 $tax_override{''} = $cgi->param('tax_override');
295 $tax_override{$_} = $cgi->param('tax_override_$_')
296 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
298 #some false laziness w/process
299 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
301 my $options = $cgi->param($plan."__OPTIONS");
302 my @options = split(',', $options);
304 map { my $optionname = $_;
305 my $param = $plan."__$optionname";
306 my $value = join(', ', $cgi->param($param));
307 ( $optionname => $value );
311 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
312 $object->set($_ => scalar($cgi->param($_)) )
313 foreach (qw( setup_fee recur_fee ));
317 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
319 my $new_object_callback = sub {
320 my( $cgi, $hashref, $fields, $opt ) = @_;
322 my $part_pkg = FS::part_pkg->new( $hashref );
323 $part_pkg->set($_ => '0')
324 foreach (qw( setup_fee recur_fee ));
330 my $edit_callback = sub {
331 my( $cgi, $object, $fields, $opt ) = @_;
333 $recur_disabled = $object->freq ? 0 : 1;
336 map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
338 my @report_option = ();
339 foreach ($object->options) {
340 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
341 /^report_option_(\d+)$/ && (push @report_option, $1);
343 foreach ($object->part_pkg_taxoverride) {
344 $taxproductnums{$_->usage_class} = 1
348 $cgi->param('report_option', join(',', @report_option));
349 foreach my $field ( @$fields ) {
351 ref($field) eq 'HASH' &&
353 $field->{field} eq 'report_option'
355 #$field->{curr_value} = join(',', @report_option);
356 $field->{value} = join(',', @report_option);
359 %options = $object->options;
361 $object->set($_ => $object->option($_))
362 foreach (qw( setup_fee recur_fee ));
366 my $new_callback = sub {
367 my( $cgi, $object, $fields ) = @_;
369 my $conf = new FS::Conf;
370 if ( $conf->exists('agent_defaultpkg') ) {
371 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
372 @agent_type = map {$_->typenum} qsearch('agent_type',{});
377 my $clone_callback = sub {
378 my( $cgi, $object, $fields, $opt ) = @_;
380 if ( $cgi->param('pkgnum') ) {
382 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
383 $object->agentnum( $cust_pkg->cust_main->agentnum );
385 $opt->{action} = 'Custom';
387 #my $part_pkg = $clone_part_pkg->clone;
388 #this is all clone does anyway
389 $object->custom('Y');
391 $object->disabled('Y');
393 } else { #not when cloning...
396 map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
400 %options = $object->options;
402 $object->set($_ => $options{$_})
403 foreach (qw( setup_fee recur_fee ));
405 $recur_disabled = $object->freq ? 0 : 1;
408 my $m2_error_callback_maker = sub {
409 my $link_type = shift; #yay closures
411 my( $cgi, $object ) = @_;
415 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
416 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
419 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
421 new FS::part_pkg_link {
422 'link_type' => $link_type,
423 'src_pkgpart' => $object->pkgpart,
424 'dst_pkgpart' => $dst,
435 my $javascript = <<'END';
436 <SCRIPT TYPE="text/javascript">
438 function freq_changed(what) {
439 var freq = what.options[what.selectedIndex].value;
442 what.form.recur_fee.disabled = true;
443 what.form.recur_fee.style.backgroundColor = '#dddddd';
445 what.form.recur_fee.disabled = false;
446 what.form.recur_fee.style.backgroundColor = '#ffffff';
451 function agent_changed(what) {
453 var agentnum = what.options[what.selectedIndex].value;
455 if ( agentnum == 0 ) {
456 what.form.agent_type.disabled = false;
457 //what.form.agent_type.style.backgroundColor = '#ffffff';
458 what.form.agent_type.style.visibility = '';
460 what.form.agent_type.disabled = true;
461 //what.form.agent_type.style.backgroundColor = '#dddddd';
462 what.form.agent_type.style.visibility = 'hidden';
470 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
472 tie my %plan_labels, 'Tie::IxHash',
473 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
476 my $html_bottom = sub {
479 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
481 my $layer_callback = sub {
484 my $html = ntable("#cccccc",2);
488 # <TD ALIGN="right">Recurring fee frequency </TD>
489 # <TD><SELECT NAME="freq">
492 #my @freq = keys %freq;
493 #@freq = grep { /^\d+$/ } @freq
494 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
495 #foreach my $freq ( @freq ) {
496 # $html .= qq(<OPTION VALUE="$freq");
497 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
498 # $html .= ">$freq{$freq}";
500 #$html .= '</SELECT></TD></TR>';
502 my $href = $plans{$layer}->{'fields'};
503 my @fields = exists($plans{$layer}->{'fieldorder'})
504 ? @{$plans{$layer}->{'fieldorder'}}
507 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
509 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
511 my $format = sub { shift };
512 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
514 #XXX these should use elements/ fields... (or this whole thing should
515 #just use layer_fields instead of layer_callback)
517 if ( ! exists($href->{$field}{'type'}) ) {
519 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
520 ( exists($options{$field})
521 ? &$format($options{$field})
522 : $href->{$field}{'default'} ).
525 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
527 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
528 ( exists($options{$field}) && $options{$field}
533 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
537 if $href->{$field}{'type'} eq 'select_multiple';
538 $html .= qq! NAME="${layer}__$field">!;
540 if ( $href->{$field}{'select_table'} ) {
542 qsearch( $href->{$field}{'select_table'},
543 $href->{$field}{'select_hash'} )
545 my $value = $record->getfield($href->{$field}{'select_key'});
546 $html .= qq!<OPTION VALUE="$value"!.
547 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
551 '>'. $record->getfield($href->{$field}{'select_label'});
553 } elsif ( $href->{$field}{'select_options'} ) {
554 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
555 my $label = $href->{$field}{'select_options'}{$key};
556 $html .= qq!<OPTION VALUE="$key"!.
557 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
565 $html .= '<font color="#ff0000">warning: '.
566 "don't know how to retreive options for $field select field".
569 $html .= '</SELECT>';
571 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
574 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
576 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
577 my $label = $href->{$field}{'options'}{$key};
578 $html .= qq!$radio VALUE="$key"!.
579 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
588 $html .= '</TD></TR>';
592 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
593 join(',', keys %{ $href } ). '">';
601 options => [ keys %plan_labels ],
602 labels => \%plan_labels,
603 curr_value => $object->plan,
604 layer_callback => $layer_callback,
608 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
609 '<SCRIPT TYPE="text/javascript">'.
610 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
613 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
616 $return .= '</SCRIPT>';
622 my %usage_class = map { ($_->classnum => $_->classname) }
623 qsearch('usage_class', {});
624 $usage_class{setup} = 'Setup';
625 $usage_class{recur} = 'Recurring';
627 my %taxproduct_fields = ();
628 my $end_callback = sub {
629 my( $cgi, $object, $fields, $opt ) = @_;
631 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
633 if ( $object->pkgpart ) {
634 foreach my $usage_class ( '', @taxproductnums ) {
635 $tax_override{$usage_class} =
636 join (",", map $_->taxclassnum,
637 qsearch( 'part_pkg_taxoverride', {
638 'pkgpart' => $object->pkgpart,
639 'usage_class' => $usage_class,
646 map { $_ => [ "taxproductnum_$_",
647 { type => 'select-taxproduct',
648 #label => "$usage_class{$_} tax product",
651 { type => 'select-taxoverride' }
656 $taxproduct_fields{'(default)'} =
657 [ 'taxproductnum', { type => 'select-taxproduct',
658 #label => 'Default tax product',
660 'tax_override', { type => 'select-taxoverride' },
664 my $taxproduct_values = sub {
665 my ($cgi, $object, $flags) = @_;
667 sub { my $layer = shift;
668 my @fields = @{$taxproduct_fields{$layer}};
671 my $field = shift @fields;
673 $field =~ /^taxproductnum_\w+$/ &&
674 push @values, ( $field => $options{"usage_$field"} );
675 $field =~ /^tax_override_(\w+)$/ &&
676 push @values, ( $field => $tax_override{$1} );
677 $field =~ /^taxproductnum$/ &&
678 push @values, ( $field => $object->taxproductnum );
679 $field =~ /^tax_override$/ &&
680 push @values, ( $field => $tax_override{''} );
686 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
691 my $field_callback = sub {
692 my ($cgi, $object, $fieldref) = @_;
694 my $field = $fieldref->{field};
695 if ($field eq 'taxproductnums') {
696 $fieldref->{value} = join(',', @taxproductnums);
697 } elsif ($field eq 'taxproduct_select') {
698 $fieldref->{options} = [ '(default)', @taxproductnums ];
699 $fieldref->{labels} = { ( '(default)' => '(default)' ),
700 map {( $_ => ($usage_class{$_} || $_) )}
703 $fieldref->{layer_fields} = \%taxproduct_fields;
704 $fieldref->{layer_values_callback} = $taxproduct_values;