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',
52 { field=>'clone', type=>'hidden',
53 curr_value_callback =>
54 sub { shift->param('clone') },
56 { field=>'pkgnum', type=>'hidden',
57 curr_value_callback =>
58 sub { shift->param('pkgnum') },
61 { type => 'columnstart' },
68 {field=>'comment', type=>'text', size=>40 }, #32
69 { field => 'agentnum',
70 type => 'select-agent',
71 disable_empty => ! $acl_edit_global,
72 empty_label => '(global)',
73 onchange => 'agent_changed',
75 {field=>'classnum', type=>'select-pkg_class' },
76 {field=>'disabled', type=>$disabled_type, value=>'Y'},
78 { type => 'tablebreak-tr-title',
79 value => 'Pricing', #better name?
82 type => 'selectlayers-select',
83 options => [ keys %plan_labels ],
84 labels => \%plan_labels,
86 { field => 'setup_fee',
90 type => 'part_pkg_freq',
91 onchange => 'freq_changed',
93 { field => 'recur_fee',
95 disabled => sub { $recur_disabled },
101 #recurring fee (auto-disable)
103 { type => 'columnnext' },
105 {type=>'justtitle', value=>'Taxation' },
106 {field=>'setuptax', type=>'checkbox', value=>'Y'},
107 {field=>'recurtax', type=>'checkbox', value=>'Y'},
108 {field=>'taxclass', type=>'select-taxclass' },
109 { field => 'taxproductnums',
111 value => join(',', @taxproductnums),
113 { field => 'taxproduct_select',
114 type => 'selectlayers',
115 options => [ '(default)', @taxproductnums ],
116 curr_value => '(default)',
117 labels => { ( '(default)' => '(default)' ),
118 map {($_=>$usage_class{$_})}
121 layer_fields => \%taxproduct_fields,
122 layer_values_callback => $taxproduct_values,
123 layers_only => !$taxproducts,
124 cell_style => ( !$taxproducts
130 { type => 'tablebreak-tr-title',
131 value => 'Promotions', #better name?
133 { field=>'promo_code', type=>'text', size=>15 },
135 { type => 'tablebreak-tr-title',
136 value => 'Cost tracking', #better name?
138 { field=>'setup_cost', type=>'money', },
139 { field=>'recur_cost', type=>'money', },
141 { type => 'columnnext' },
143 { field => 'agent_type',
144 type => 'select-agent_types',
145 disabled => ! $acl_edit_global,
146 curr_value_callback => sub {
147 my($cgi, $object, $field) = @_;
148 #in the other callbacks..? hmm.
153 { type => 'tablebreak-tr-title',
154 value => 'Line-item revenue recogition', #better name?
156 { field=>'pay_weight', type=>'text', size=>6 },
157 { field=>'credit_weight', type=>'text', size=>6 },
160 { type => 'columnend' },
162 { 'type' => 'tablebreak-tr-title',
163 'value' => 'Pricing add-ons',
165 { 'field' => 'bill_dst_pkgpart',
166 'type' => 'select-part_pkg',
167 'm2_label' => 'Include line item(s) from package',
168 'm2m_method' => 'bill_part_pkg_link',
169 'm2m_dstcol' => 'dst_pkgpart',
170 'm2_error_callback' =>
171 &{$m2_error_callback_maker}('bill'),
174 { type => 'tablebreak-tr-title',
177 { type => 'pkg_svc', },
179 { 'field' => 'svc_dst_pkgpart',
180 'label' => 'Also include services from package: ',
181 'type' => 'select-part_pkg',
182 'm2_label' => 'Include services of package: ',
183 'm2m_method' => 'svc_part_pkg_link',
184 'm2m_dstcol' => 'dst_pkgpart',
185 'm2_error_callback' =>
186 &{$m2_error_callback_maker}('svc'),
189 { type => 'tablebreak-tr-title',
190 value => 'Price plan options',
199 my $curuser = $FS::CurrentUser::CurrentUser;
201 my $edit_global = 'Edit global package definitions';
202 my $acl_edit = $curuser->access_right('Edit package definitions');
203 my $acl_edit_global = $curuser->access_right($edit_global);
205 my $acl_edit_either = $acl_edit || $acl_edit_global;
207 my $begin_callback = sub {
208 my( $cgi, $fields, $opt ) = @_;
210 unless $acl_edit_either
211 || ( $cgi->param('pkgnum')
212 && $curuser->access_right('Customize customer package')
216 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
218 my $agent_clone_extra_sql =
219 ' ( '. FS::part_pkg->curuser_pkgs_sql.
220 #kludge to clone custom customer packages you otherwise couldn't see
221 #really need a proper "CUSTOM" flag that's distinct from disabled
222 #" OR ( part_pkg.disabled = 'Y' AND part_pkg.comment LIKE '(CUSTOM)%' ) ".
223 " OR ( part_pkg.disabled = 'Y' ) ".
226 my $conf = new FS::Conf;
227 my $taxproducts = $conf->exists('enable_taxproducts');
230 # - tr-part_pkg_freq: month_increments_only (from price plans)
232 # - test errors cloning
233 # - test custom pricing
234 # - move the selectlayer divs away from lame layer_callback
236 #my ($query) = $cgi->keywords;
241 my %tax_override = ();
243 my %taxproductnums = map { ($_->classnum => 1) }
244 qsearch('usage_class', { 'disabled' => '' });
245 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
248 my $recur_disabled = 1;
250 my $error_callback = sub {
251 my($cgi, $object, $fields, $opt ) = @_;
253 (@agent_type) = $cgi->param('agent_type');
255 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
257 $recur_disabled = $cgi->param('freq') ? 0 : 1;
259 foreach ($cgi->param) {
260 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
262 $tax_override{''} = $cgi->param('tax_override');
263 $tax_override{$_} = $cgi->param('tax_override_$_')
264 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
266 #some false laziness w/process
267 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
269 my $options = $cgi->param($plan."__OPTIONS");
270 my @options = split(',', $options);
272 map { my $optionname = $_;
273 my $param = $plan."__$optionname";
274 my $value = join(', ', $cgi->param($param));
275 ( $optionname => $value );
279 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
280 $object->set($_ => scalar($cgi->param($_)) )
281 foreach (qw( setup_fee recur_fee ));
285 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
287 my $new_object_callback = sub {
288 my( $cgi, $hashref, $fields, $opt ) = @_;
290 my $part_pkg = FS::part_pkg->new( $hashref );
291 $part_pkg->set($_ => '0')
292 foreach (qw( setup_fee recur_fee ));
298 my $edit_callback = sub {
299 my( $cgi, $object, $fields, $opt ) = @_;
301 $recur_disabled = $object->freq ? 0 : 1;
303 (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
305 foreach ($object->options) {
306 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
308 foreach ($object->part_pkg_taxoverride) {
309 $taxproductnums{$_->usage_class} = 1
313 %options = $object->options;
315 $object->set($_ => $object->option($_))
316 foreach (qw( setup_fee recur_fee ));
320 my $new_callback = sub {
321 my( $cgi, $object, $fields ) = @_;
323 my $conf = new FS::Conf;
324 if ( $conf->exists('agent_defaultpkg') ) {
325 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
326 @agent_type = map {$_->typenum} qsearch('agent_type',{});
331 my $clone_callback = sub {
332 my( $cgi, $object, $fields, $opt ) = @_;
334 if ( $cgi->param('pkgnum') ) {
336 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
337 $object->agentnum( $cust_pkg->cust_main->agentnum );
339 $opt->{action} = 'Custom';
341 #my $part_pkg = $clone_part_pkg->clone;
342 #this is all clone did anyway
343 $object->comment( '(CUSTOM) '. $object->comment )
344 unless $object->comment =~ /^\(CUSTOM\) /;
346 $object->disabled('Y');
350 %options = $object->options;
352 $object->set($_ => $options{$_})
353 foreach (qw( setup_fee recur_fee ));
355 $recur_disabled = $object->freq ? 0 : 1;
358 my $m2_error_callback_maker = sub {
359 my $link_type = shift; #yay closures
361 my( $cgi, $object ) = @_;
363 new FS::part_pkg_link {
364 'link_type' => $link_type,
365 'src_pkgpart' => $object->pkgpart,
371 grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
375 my $javascript = <<'END';
376 <SCRIPT TYPE="text/javascript">
378 function freq_changed(what) {
379 var freq = what.options[what.selectedIndex].value;
382 what.form.recur_fee.disabled = true;
383 what.form.recur_fee.style.backgroundColor = '#dddddd';
385 what.form.recur_fee.disabled = false;
386 what.form.recur_fee.style.backgroundColor = '#ffffff';
391 function agent_changed(what) {
393 var agentnum = what.options[what.selectedIndex].value;
395 if ( agentnum == 0 ) {
396 what.form.agent_type.disabled = false;
397 //what.form.agent_type.style.backgroundColor = '#ffffff';
398 what.form.agent_type.style.visibility = '';
400 what.form.agent_type.disabled = true;
401 //what.form.agent_type.style.backgroundColor = '#dddddd';
402 what.form.agent_type.style.visibility = 'hidden';
410 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
412 tie my %plan_labels, 'Tie::IxHash',
413 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
416 my $html_bottom = sub {
419 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
421 my $layer_callback = sub {
424 my $html = ntable("#cccccc",2);
428 # <TD ALIGN="right">Recurring fee frequency </TD>
429 # <TD><SELECT NAME="freq">
432 #my @freq = keys %freq;
433 #@freq = grep { /^\d+$/ } @freq
434 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
435 #foreach my $freq ( @freq ) {
436 # $html .= qq(<OPTION VALUE="$freq");
437 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
438 # $html .= ">$freq{$freq}";
440 #$html .= '</SELECT></TD></TR>';
442 my $href = $plans{$layer}->{'fields'};
443 my @fields = exists($plans{$layer}->{'fieldorder'})
444 ? @{$plans{$layer}->{'fieldorder'}}
447 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
449 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
451 my $format = sub { shift };
452 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
454 #XXX these should use elements/ fields... (or this whole thing should
455 #just use layer_fields instead of layer_callback)
457 if ( ! exists($href->{$field}{'type'}) ) {
459 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
460 ( exists($options{$field})
461 ? &$format($options{$field})
462 : $href->{$field}{'default'} ).
465 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
467 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
468 ( exists($options{$field}) && $options{$field}
473 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
477 if $href->{$field}{'type'} eq 'select_multiple';
478 $html .= qq! NAME="${layer}__$field">!;
480 if ( $href->{$field}{'select_table'} ) {
482 qsearch( $href->{$field}{'select_table'},
483 $href->{$field}{'select_hash'} )
485 my $value = $record->getfield($href->{$field}{'select_key'});
486 $html .= qq!<OPTION VALUE="$value"!.
487 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
491 '>'. $record->getfield($href->{$field}{'select_label'});
493 } elsif ( $href->{$field}{'select_options'} ) {
494 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
495 my $label = $href->{$field}{'select_options'}{$key};
496 $html .= qq!<OPTION VALUE="$key"!.
497 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
505 $html .= '<font color="#ff0000">warning: '.
506 "don't know how to retreive options for $field select field".
509 $html .= '</SELECT>';
511 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
514 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
516 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
517 my $label = $href->{$field}{'options'}{$key};
518 $html .= qq!$radio VALUE="$key"!.
519 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
528 $html .= '</TD></TR>';
532 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
533 join(',', keys %{ $href } ). '">';
541 options => [ keys %plan_labels ],
542 labels => \%plan_labels,
543 curr_value => $object->plan,
544 layer_callback => $layer_callback,
548 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
549 '<SCRIPT TYPE="text/javascript">'.
550 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
553 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
556 $return .= '</SCRIPT>';
562 my %usage_class = map { ($_->classnum => $_->classname) }
563 qsearch('usage_class', {});
564 $usage_class{setup} = 'Setup';
565 $usage_class{recur} = 'Recurring';
567 my %taxproduct_fields = ();
568 my $end_callback = sub {
569 my( $cgi, $object, $fields, $opt ) = @_;
571 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
573 if ( $object->pkgpart ) {
574 foreach my $usage_class ( '', @taxproductnums ) {
575 $tax_override{$usage_class} =
576 join (",", map $_->taxclassnum,
577 qsearch( 'part_pkg_taxoverride', {
578 'pkgpart' => $object->pkgpart,
579 'usage_class' => $usage_class,
586 map { $_ => [ "taxproductnum_$_",
587 { type => 'select-taxproduct',
588 #label => "$usage_class{$_} tax product",
591 { type => 'select-taxoverride' }
596 $taxproduct_fields{'(default)'} =
597 [ 'taxproductnum', { type => 'select-taxproduct',
598 #label => 'Default tax product',
600 'tax_override', { type => 'select-taxoverride' },
604 my $taxproduct_values = sub {
605 my ($cgi, $object, $flags) = @_;
607 sub { my $layer = shift;
608 my @fields = @{$taxproduct_fields{$layer}};
611 my $field = shift @fields;
613 $field =~ /^taxproductnum_\w+$/ &&
614 push @values, ( $field => $options{"usage_$field"} );
615 $field =~ /^tax_override_(\w+)$/ &&
616 push @values, ( $field => $tax_override{$1} );
617 $field =~ /^taxproductnum$/ &&
618 push @values, ( $field => $object->taxproductnum );
619 $field =~ /^tax_override$/ &&
620 push @values, ( $field => $tax_override{''} );
626 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
631 my $field_callback = sub {
632 my ($cgi, $object, $fieldref) = @_;
634 my $field = $fieldref->{field};
635 if ($field eq 'taxproductnums') {
636 $fieldref->{value} = join(',', @taxproductnums);
637 } elsif ($field eq 'taxproduct_select') {
638 $fieldref->{options} = [ '(default)', @taxproductnums ];
639 $fieldref->{labels} = { ( '(default)' => '(default)' ),
640 map {( $_ => ($usage_class{$_} || $_) )}
643 $fieldref->{layer_fields} = \%taxproduct_fields;
644 $fieldref->{layer_values_callback} = $taxproduct_values;