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 'bill_dst_pkgpart' => 'Include line item(s) from package',
49 'svc_dst_pkgpart' => 'Include services of package',
50 'report_option' => 'Report classes',
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 ( $conf->exists('pkg-addon_classnum')
81 ? ( { field=>'addon_classnum',
82 type =>'select-pkg_class',
87 {field=>'disabled', type=>$disabled_type, value=>'Y'},
89 { type => 'tablebreak-tr-title',
90 value => 'Pricing', #better name?
93 type => 'selectlayers-select',
94 options => [ keys %plan_labels ],
95 labels => \%plan_labels,
97 { field => 'setup_fee',
101 type => 'part_pkg_freq',
102 onchange => 'freq_changed',
104 { field => 'recur_fee',
106 disabled => sub { $recur_disabled },
112 #recurring fee (auto-disable)
114 { type => 'columnnext' },
116 {type=>'justtitle', value=>'Taxation' },
117 {field=>'setuptax', type=>'checkbox', value=>'Y'},
118 {field=>'recurtax', type=>'checkbox', value=>'Y'},
119 {field=>'taxclass', type=>'select-taxclass' },
120 { field => 'taxproductnums',
122 value => join(',', @taxproductnums),
124 { field => 'taxproduct_select',
125 type => 'selectlayers',
126 options => [ '(default)', @taxproductnums ],
127 curr_value => '(default)',
128 labels => { ( '(default)' => '(default)' ),
129 map {($_=>$usage_class{$_})}
132 layer_fields => \%taxproduct_fields,
133 layer_values_callback => $taxproduct_values,
134 layers_only => !$taxproducts,
135 cell_style => ( !$taxproducts
141 { type => 'tablebreak-tr-title',
142 value => 'Promotions', #better name?
144 { field=>'promo_code', type=>'text', size=>15 },
146 { type => 'tablebreak-tr-title',
147 value => 'Cost tracking', #better name?
149 { field=>'setup_cost', type=>'money', },
150 { field=>'recur_cost', type=>'money', },
152 { type => 'columnnext' },
154 { field => 'agent_type',
155 type => 'select-agent_types',
156 disabled => ! $acl_edit_global,
157 curr_value_callback => sub {
158 my($cgi, $object, $field) = @_;
159 #in the other callbacks..? hmm.
164 { type => 'tablebreak-tr-title',
165 value => 'Line-item revenue recogition', #better name?
167 { field=>'pay_weight', type=>'text', size=>6 },
168 { field=>'credit_weight', type=>'text', size=>6 },
171 { type => 'columnend' },
173 { 'type' => $census ? 'tablebreak-tr-title'
175 'value' => 'Optional report classes',
176 'field' => 'census_title',
178 { 'field' => 'report_option',
179 'type' => $census ? 'select-table' : 'hidden',
180 'table' => 'part_pkg_report_option',
181 'name_col' => 'name',
186 { 'type' => 'tablebreak-tr-title',
187 'value' => 'Pricing add-ons',
190 { 'field' => 'bill_dst_pkgpart',
191 'type' => 'select-part_pkg',
192 'm2_label' => 'Include line item(s) from package',
193 'm2m_method' => 'bill_part_pkg_link',
194 'm2m_dstcol' => 'dst_pkgpart',
195 'm2_error_callback' =>
196 &{$m2_error_callback_maker}('bill'),
197 'm2_fields' => [ { 'field' => 'hidden',
198 'type' => 'checkbox',
206 { type => 'tablebreak-tr-title',
209 { type => 'pkg_svc', },
211 { 'field' => 'svc_dst_pkgpart',
212 'label' => 'Also include services from package: ',
213 'type' => 'select-part_pkg',
214 'm2_label' => 'Include services of package: ',
215 'm2m_method' => 'svc_part_pkg_link',
216 'm2m_dstcol' => 'dst_pkgpart',
217 'm2_error_callback' =>
218 &{$m2_error_callback_maker}('svc'),
221 { type => 'tablebreak-tr-title',
222 value => 'Price plan options',
231 my $curuser = $FS::CurrentUser::CurrentUser;
233 my $edit_global = 'Edit global package definitions';
234 my $acl_edit = $curuser->access_right('Edit package definitions');
235 my $acl_edit_global = $curuser->access_right($edit_global);
237 my $acl_edit_either = $acl_edit || $acl_edit_global;
239 my $begin_callback = sub {
240 my( $cgi, $fields, $opt ) = @_;
242 unless $acl_edit_either
243 || ( $cgi->param('pkgnum')
244 && $curuser->access_right('Customize customer package')
248 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
250 my $agent_clone_extra_sql =
251 ' ( '. FS::part_pkg->curuser_pkgs_sql.
252 " OR ( part_pkg.custom = 'Y' ) ".
255 my $conf = new FS::Conf;
256 my $taxproducts = $conf->exists('enable_taxproducts');
257 my $census = scalar( qsearch( 'part_pkg_report_option', {} ) );
260 # - tr-part_pkg_freq: month_increments_only (from price plans)
262 # - test errors cloning
263 # - test custom pricing
264 # - move the selectlayer divs away from lame layer_callback
266 #my ($query) = $cgi->keywords;
271 my %tax_override = ();
273 my %taxproductnums = map { ($_->classnum => 1) }
274 qsearch('usage_class', { 'disabled' => '' });
275 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
278 my $recur_disabled = 1;
280 my $error_callback = sub {
281 my($cgi, $object, $fields, $opt ) = @_;
283 (@agent_type) = $cgi->param('agent_type');
285 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
287 $recur_disabled = $cgi->param('freq') ? 0 : 1;
289 foreach ($cgi->param) {
290 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
292 $tax_override{''} = $cgi->param('tax_override');
293 $tax_override{$_} = $cgi->param('tax_override_$_')
294 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
296 #some false laziness w/process
297 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
299 my $options = $cgi->param($plan."__OPTIONS");
300 my @options = split(',', $options);
302 map { my $optionname = $_;
303 my $param = $plan."__$optionname";
304 my $value = join(', ', $cgi->param($param));
305 ( $optionname => $value );
309 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
310 $object->set($_ => scalar($cgi->param($_)) )
311 foreach (qw( setup_fee recur_fee ));
315 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
317 my $new_object_callback = sub {
318 my( $cgi, $hashref, $fields, $opt ) = @_;
320 my $part_pkg = FS::part_pkg->new( $hashref );
321 $part_pkg->set($_ => '0')
322 foreach (qw( setup_fee recur_fee ));
328 my $edit_callback = sub {
329 my( $cgi, $object, $fields, $opt ) = @_;
331 $recur_disabled = $object->freq ? 0 : 1;
333 (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
335 my @report_option = ();
336 foreach ($object->options) {
337 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
338 /^report_option_(\d+)$/ && (push @report_option, $1);
340 foreach ($object->part_pkg_taxoverride) {
341 $taxproductnums{$_->usage_class} = 1
345 $cgi->param('report_option', join(',', @report_option));
346 foreach my $field ( @$fields ) {
348 ref($field) eq 'HASH' &&
350 $field->{field} eq 'report_option'
352 #$field->{curr_value} = join(',', @report_option);
353 $field->{value} = join(',', @report_option);
356 %options = $object->options;
358 $object->set($_ => $object->option($_))
359 foreach (qw( setup_fee recur_fee ));
363 my $new_callback = sub {
364 my( $cgi, $object, $fields ) = @_;
366 my $conf = new FS::Conf;
367 if ( $conf->exists('agent_defaultpkg') ) {
368 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
369 @agent_type = map {$_->typenum} qsearch('agent_type',{});
374 my $clone_callback = sub {
375 my( $cgi, $object, $fields, $opt ) = @_;
377 if ( $cgi->param('pkgnum') ) {
379 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
380 $object->agentnum( $cust_pkg->cust_main->agentnum );
382 $opt->{action} = 'Custom';
384 #my $part_pkg = $clone_part_pkg->clone;
385 #this is all clone does anyway
386 $object->custom('Y');
388 $object->disabled('Y');
392 %options = $object->options;
394 $object->set($_ => $options{$_})
395 foreach (qw( setup_fee recur_fee ));
397 $recur_disabled = $object->freq ? 0 : 1;
400 my $m2_error_callback_maker = sub {
401 my $link_type = shift; #yay closures
403 my( $cgi, $object ) = @_;
407 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
408 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
411 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
413 new FS::part_pkg_link {
414 'link_type' => $link_type,
415 'src_pkgpart' => $object->pkgpart,
416 'dst_pkgpart' => $dst,
427 my $javascript = <<'END';
428 <SCRIPT TYPE="text/javascript">
430 function freq_changed(what) {
431 var freq = what.options[what.selectedIndex].value;
434 what.form.recur_fee.disabled = true;
435 what.form.recur_fee.style.backgroundColor = '#dddddd';
437 what.form.recur_fee.disabled = false;
438 what.form.recur_fee.style.backgroundColor = '#ffffff';
443 function agent_changed(what) {
445 var agentnum = what.options[what.selectedIndex].value;
447 if ( agentnum == 0 ) {
448 what.form.agent_type.disabled = false;
449 //what.form.agent_type.style.backgroundColor = '#ffffff';
450 what.form.agent_type.style.visibility = '';
452 what.form.agent_type.disabled = true;
453 //what.form.agent_type.style.backgroundColor = '#dddddd';
454 what.form.agent_type.style.visibility = 'hidden';
462 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
464 tie my %plan_labels, 'Tie::IxHash',
465 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
468 my $html_bottom = sub {
471 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
473 my $layer_callback = sub {
476 my $html = ntable("#cccccc",2);
480 # <TD ALIGN="right">Recurring fee frequency </TD>
481 # <TD><SELECT NAME="freq">
484 #my @freq = keys %freq;
485 #@freq = grep { /^\d+$/ } @freq
486 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
487 #foreach my $freq ( @freq ) {
488 # $html .= qq(<OPTION VALUE="$freq");
489 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
490 # $html .= ">$freq{$freq}";
492 #$html .= '</SELECT></TD></TR>';
494 my $href = $plans{$layer}->{'fields'};
495 my @fields = exists($plans{$layer}->{'fieldorder'})
496 ? @{$plans{$layer}->{'fieldorder'}}
499 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
501 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
503 my $format = sub { shift };
504 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
506 #XXX these should use elements/ fields... (or this whole thing should
507 #just use layer_fields instead of layer_callback)
509 if ( ! exists($href->{$field}{'type'}) ) {
511 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
512 ( exists($options{$field})
513 ? &$format($options{$field})
514 : $href->{$field}{'default'} ).
517 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
519 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
520 ( exists($options{$field}) && $options{$field}
525 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
529 if $href->{$field}{'type'} eq 'select_multiple';
530 $html .= qq! NAME="${layer}__$field">!;
532 if ( $href->{$field}{'select_table'} ) {
534 qsearch( $href->{$field}{'select_table'},
535 $href->{$field}{'select_hash'} )
537 my $value = $record->getfield($href->{$field}{'select_key'});
538 $html .= qq!<OPTION VALUE="$value"!.
539 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
543 '>'. $record->getfield($href->{$field}{'select_label'});
545 } elsif ( $href->{$field}{'select_options'} ) {
546 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
547 my $label = $href->{$field}{'select_options'}{$key};
548 $html .= qq!<OPTION VALUE="$key"!.
549 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
557 $html .= '<font color="#ff0000">warning: '.
558 "don't know how to retreive options for $field select field".
561 $html .= '</SELECT>';
563 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
566 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
568 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
569 my $label = $href->{$field}{'options'}{$key};
570 $html .= qq!$radio VALUE="$key"!.
571 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
580 $html .= '</TD></TR>';
584 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
585 join(',', keys %{ $href } ). '">';
593 options => [ keys %plan_labels ],
594 labels => \%plan_labels,
595 curr_value => $object->plan,
596 layer_callback => $layer_callback,
600 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
601 '<SCRIPT TYPE="text/javascript">'.
602 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
605 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
608 $return .= '</SCRIPT>';
614 my %usage_class = map { ($_->classnum => $_->classname) }
615 qsearch('usage_class', {});
616 $usage_class{setup} = 'Setup';
617 $usage_class{recur} = 'Recurring';
619 my %taxproduct_fields = ();
620 my $end_callback = sub {
621 my( $cgi, $object, $fields, $opt ) = @_;
623 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
625 if ( $object->pkgpart ) {
626 foreach my $usage_class ( '', @taxproductnums ) {
627 $tax_override{$usage_class} =
628 join (",", map $_->taxclassnum,
629 qsearch( 'part_pkg_taxoverride', {
630 'pkgpart' => $object->pkgpart,
631 'usage_class' => $usage_class,
638 map { $_ => [ "taxproductnum_$_",
639 { type => 'select-taxproduct',
640 #label => "$usage_class{$_} tax product",
643 { type => 'select-taxoverride' }
648 $taxproduct_fields{'(default)'} =
649 [ 'taxproductnum', { type => 'select-taxproduct',
650 #label => 'Default tax product',
652 'tax_override', { type => 'select-taxoverride' },
656 my $taxproduct_values = sub {
657 my ($cgi, $object, $flags) = @_;
659 sub { my $layer = shift;
660 my @fields = @{$taxproduct_fields{$layer}};
663 my $field = shift @fields;
665 $field =~ /^taxproductnum_\w+$/ &&
666 push @values, ( $field => $options{"usage_$field"} );
667 $field =~ /^tax_override_(\w+)$/ &&
668 push @values, ( $field => $tax_override{$1} );
669 $field =~ /^taxproductnum$/ &&
670 push @values, ( $field => $object->taxproductnum );
671 $field =~ /^tax_override$/ &&
672 push @values, ( $field => $tax_override{''} );
678 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
683 my $field_callback = sub {
684 my ($cgi, $object, $fieldref) = @_;
686 my $field = $fieldref->{field};
687 if ($field eq 'taxproductnums') {
688 $fieldref->{value} = join(',', @taxproductnums);
689 } elsif ($field eq 'taxproduct_select') {
690 $fieldref->{options} = [ '(default)', @taxproductnums ];
691 $fieldref->{labels} = { ( '(default)' => '(default)' ),
692 map {( $_ => ($usage_class{$_} || $_) )}
695 $fieldref->{layer_fields} = \%taxproduct_fields;
696 $fieldref->{layer_values_callback} = $taxproduct_values;