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 'pay_weight' => 'Payment weight',
41 'credit_weight' => 'Credit weight',
42 'agentnum' => 'Agent',
43 'setup_fee' => 'Setup fee',
44 'recur_fee' => 'Recurring fee',
45 'bill_dst_pkgpart' => 'Include line item(s) from package',
46 'svc_dst_pkgpart' => 'Include services of package',
50 { field=>'clone', type=>'hidden',
51 curr_value_callback =>
52 sub { shift->param('clone') },
54 { field=>'pkgnum', type=>'hidden',
55 curr_value_callback =>
56 sub { shift->param('pkgnum') },
59 { type => 'columnstart' },
66 {field=>'comment', type=>'text', size=>40 }, #32
67 { field => 'agentnum',
68 type => 'select-agent',
69 disable_empty => ! $acl_edit_global,
70 empty_label => '(global)',
71 onchange => 'agent_changed',
73 {field=>'classnum', type=>'select-pkg_class' },
74 {field=>'disabled', type=>$disabled_type, value=>'Y'},
76 { type => 'tablebreak-tr-title',
77 value => 'Pricing', #better name?
80 type => 'selectlayers-select',
81 options => [ keys %plan_labels ],
82 labels => \%plan_labels,
84 { field => 'setup_fee',
88 type => 'part_pkg_freq',
89 onchange => 'freq_changed',
91 { field => 'recur_fee',
93 disabled => sub { $recur_disabled },
99 #recurring fee (auto-disable)
101 { type => 'columnnext' },
103 {type=>'justtitle', value=>'Taxation' },
104 {field=>'setuptax', type=>'checkbox', value=>'Y'},
105 {field=>'recurtax', type=>'checkbox', value=>'Y'},
106 {field=>'taxclass', type=>'select-taxclass' },
107 { field => 'taxproductnums',
109 value => join(',', @taxproductnums),
111 { field => 'taxproduct_select',
112 type => 'selectlayers',
113 options => [ '(default)', @taxproductnums ],
114 curr_value => '(default)',
115 labels => { ( '(default)' => '(default)' ),
116 map {($_=>$usage_class{$_})}
119 layer_fields => \%taxproduct_fields,
120 layer_values_callback => $taxproduct_values,
121 layers_only => !$taxproducts,
122 cell_style => ( !$taxproducts
128 { type => 'tablebreak-tr-title',
129 value => 'Promotions', #better name?
131 { field=>'promo_code', type=>'text', size=>15 },
133 { type => 'tablebreak-tr-title',
134 value => 'Line-item revenue recogition', #better name?
136 { field=>'pay_weight', type=>'text', size=>6 },
137 { field=>'credit_weight', type=>'text', size=>6 },
139 { type => 'columnnext' },
141 { field => 'agent_type',
142 type => 'select-agent_types',
143 disabled => ! $acl_edit_global,
144 curr_value_callback => sub {
145 my($cgi, $object, $field) = @_;
146 #in the other callbacks..? hmm.
151 { type => 'columnend' },
153 { 'type' => 'tablebreak-tr-title',
154 'value' => 'Pricing add-ons',
156 { 'field' => 'bill_dst_pkgpart',
157 'type' => 'select-part_pkg',
158 'm2_label' => 'Include line item(s) from package',
159 'm2m_method' => 'bill_part_pkg_link',
160 'm2m_dstcol' => 'dst_pkgpart',
161 'm2_error_callback' =>
162 &{$m2_error_callback_maker}('bill'),
165 { type => 'tablebreak-tr-title',
168 { type => 'pkg_svc', },
170 { 'field' => 'svc_dst_pkgpart',
171 'label' => 'Also include services from package: ',
172 'type' => 'select-part_pkg',
173 'm2_label' => 'Include services of package: ',
174 'm2m_method' => 'svc_part_pkg_link',
175 'm2m_dstcol' => 'dst_pkgpart',
176 'm2_error_callback' =>
177 &{$m2_error_callback_maker}('svc'),
180 { type => 'tablebreak-tr-title',
181 value => 'Price plan options',
190 my $curuser = $FS::CurrentUser::CurrentUser;
192 my $edit_global = 'Edit global package definitions';
193 my $acl_edit = $curuser->access_right('Edit package definitions');
194 my $acl_edit_global = $curuser->access_right($edit_global);
196 my $acl_edit_either = $acl_edit || $acl_edit_global;
198 my $begin_callback = sub {
199 my( $cgi, $fields, $opt ) = @_;
201 unless $acl_edit_either
202 || ( $cgi->param('pkgnum')
203 && $curuser->access_right('Customize customer package')
207 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
209 my $agent_clone_extra_sql =
210 ' ( '. FS::part_pkg->curuser_pkgs_sql.
211 #kludge to clone custom customer packages you otherwise couldn't see
212 #really need a proper "CUSTOM" flag that's distinct from disabled
213 #" OR ( part_pkg.disabled = 'Y' AND part_pkg.comment LIKE '(CUSTOM)%' ) ".
214 " OR ( part_pkg.disabled = 'Y' ) ".
217 my $conf = new FS::Conf;
218 my $taxproducts = $conf->exists('enable_taxproducts');
221 # - tr-part_pkg_freq: month_increments_only (from price plans)
223 # - test errors cloning
224 # - test custom pricing
225 # - move the selectlayer divs away from lame layer_callback
227 #my ($query) = $cgi->keywords;
232 my %tax_override = ();
234 my %taxproductnums = map { ($_->classnum => 1) }
235 qsearch('usage_class', { 'disabled' => '' });
236 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
239 my $recur_disabled = 1;
241 my $error_callback = sub {
242 my($cgi, $object, $fields, $opt ) = @_;
244 (@agent_type) = $cgi->param('agent_type');
246 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
248 $recur_disabled = $cgi->param('freq') ? 0 : 1;
250 foreach ($cgi->param) {
251 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
253 $tax_override{''} = $cgi->param('tax_override');
254 $tax_override{$_} = $cgi->param('tax_override_$_')
255 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
257 #some false laziness w/process
258 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
260 my $options = $cgi->param($plan."__OPTIONS");
261 my @options = split(',', $options);
263 map { my $optionname = $_;
264 my $param = $plan."__$optionname";
265 my $value = join(', ', $cgi->param($param));
266 ( $optionname => $value );
270 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
271 $object->set($_ => scalar($cgi->param($_)) )
272 foreach (qw( setup_fee recur_fee ));
276 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
278 my $new_object_callback = sub {
279 my( $cgi, $hashref, $fields, $opt ) = @_;
281 my $part_pkg = FS::part_pkg->new( $hashref );
282 $part_pkg->set($_ => '0')
283 foreach (qw( setup_fee recur_fee ));
289 my $edit_callback = sub {
290 my( $cgi, $object, $fields, $opt ) = @_;
292 $recur_disabled = $object->freq ? 0 : 1;
294 (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
296 foreach ($object->options) {
297 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
299 foreach ($object->part_pkg_taxoverride) {
300 $taxproductnums{$_->usage_class} = 1
304 %options = $object->options;
306 $object->set($_ => $object->option($_))
307 foreach (qw( setup_fee recur_fee ));
311 my $new_callback = sub {
312 my( $cgi, $object, $fields ) = @_;
314 my $conf = new FS::Conf;
315 if ( $conf->exists('agent_defaultpkg') ) {
316 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
317 @agent_type = map {$_->typenum} qsearch('agent_type',{});
322 my $clone_callback = sub {
323 my( $cgi, $object, $fields, $opt ) = @_;
325 if ( $cgi->param('pkgnum') ) {
327 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
328 $object->agentnum( $cust_pkg->cust_main->agentnum );
330 $opt->{action} = 'Custom';
332 #my $part_pkg = $clone_part_pkg->clone;
333 #this is all clone did anyway
334 $object->comment( '(CUSTOM) '. $object->comment )
335 unless $object->comment =~ /^\(CUSTOM\) /;
337 $object->disabled('Y');
341 %options = $object->options;
343 $object->set($_ => $options{$_})
344 foreach (qw( setup_fee recur_fee ));
346 $recur_disabled = $object->freq ? 0 : 1;
349 my $m2_error_callback_maker = sub {
350 my $link_type = shift; #yay closures
352 my( $cgi, $object ) = @_;
354 new FS::part_pkg_link {
355 'link_type' => $link_type,
356 'src_pkgpart' => $object->pkgpart,
362 grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
366 my $javascript = <<'END';
367 <SCRIPT TYPE="text/javascript">
369 function freq_changed(what) {
370 var freq = what.options[what.selectedIndex].value;
373 what.form.recur_fee.disabled = true;
374 what.form.recur_fee.style.backgroundColor = '#dddddd';
376 what.form.recur_fee.disabled = false;
377 what.form.recur_fee.style.backgroundColor = '#ffffff';
382 function agent_changed(what) {
384 var agentnum = what.options[what.selectedIndex].value;
386 if ( agentnum == 0 ) {
387 what.form.agent_type.disabled = false;
388 //what.form.agent_type.style.backgroundColor = '#ffffff';
389 what.form.agent_type.style.visibility = '';
391 what.form.agent_type.disabled = true;
392 //what.form.agent_type.style.backgroundColor = '#dddddd';
393 what.form.agent_type.style.visibility = 'hidden';
401 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
403 tie my %plan_labels, 'Tie::IxHash',
404 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
407 my $html_bottom = sub {
410 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
412 my $layer_callback = sub {
415 my $html = ntable("#cccccc",2);
419 # <TD ALIGN="right">Recurring fee frequency </TD>
420 # <TD><SELECT NAME="freq">
423 #my @freq = keys %freq;
424 #@freq = grep { /^\d+$/ } @freq
425 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
426 #foreach my $freq ( @freq ) {
427 # $html .= qq(<OPTION VALUE="$freq");
428 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
429 # $html .= ">$freq{$freq}";
431 #$html .= '</SELECT></TD></TR>';
433 my $href = $plans{$layer}->{'fields'};
434 my @fields = exists($plans{$layer}->{'fieldorder'})
435 ? @{$plans{$layer}->{'fieldorder'}}
438 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
440 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
442 my $format = sub { shift };
443 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
445 #XXX these should use elements/ fields... (or this whole thing should
446 #just use layer_fields instead of layer_callback)
448 if ( ! exists($href->{$field}{'type'}) ) {
450 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
451 ( exists($options{$field})
452 ? &$format($options{$field})
453 : $href->{$field}{'default'} ).
456 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
458 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
459 ( exists($options{$field}) && $options{$field}
464 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
468 if $href->{$field}{'type'} eq 'select_multiple';
469 $html .= qq! NAME="${layer}__$field">!;
471 if ( $href->{$field}{'select_table'} ) {
473 qsearch( $href->{$field}{'select_table'},
474 $href->{$field}{'select_hash'} )
476 my $value = $record->getfield($href->{$field}{'select_key'});
477 $html .= qq!<OPTION VALUE="$value"!.
478 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
482 '>'. $record->getfield($href->{$field}{'select_label'});
484 } elsif ( $href->{$field}{'select_options'} ) {
485 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
486 my $label = $href->{$field}{'select_options'}{$key};
487 $html .= qq!<OPTION VALUE="$key"!.
488 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
496 $html .= '<font color="#ff0000">warning: '.
497 "don't know how to retreive options for $field select field".
500 $html .= '</SELECT>';
502 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
505 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
507 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
508 my $label = $href->{$field}{'options'}{$key};
509 $html .= qq!$radio VALUE="$key"!.
510 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
519 $html .= '</TD></TR>';
523 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
524 join(',', keys %{ $href } ). '">';
532 options => [ keys %plan_labels ],
533 labels => \%plan_labels,
534 curr_value => $object->plan,
535 layer_callback => $layer_callback,
539 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
540 '<SCRIPT TYPE="text/javascript">'.
541 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
544 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
547 $return .= '</SCRIPT>';
553 my %usage_class = map { ($_->classnum => $_->classname) }
554 qsearch('usage_class', {});
555 $usage_class{setup} = 'Setup';
556 $usage_class{recur} = 'Recurring';
558 my %taxproduct_fields = ();
559 my $end_callback = sub {
560 my( $cgi, $object, $fields, $opt ) = @_;
562 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
564 if ( $object->pkgpart ) {
565 foreach my $usage_class ( '', @taxproductnums ) {
566 $tax_override{$usage_class} =
567 join (",", map $_->taxclassnum,
568 qsearch( 'part_pkg_taxoverride', {
569 'pkgpart' => $object->pkgpart,
570 'usage_class' => $usage_class,
577 map { $_ => [ "taxproductnum_$_",
578 { type => 'select-taxproduct',
579 #label => "$usage_class{$_} tax product",
582 { type => 'select-taxoverride' }
587 $taxproduct_fields{'(default)'} =
588 [ 'taxproductnum', { type => 'select-taxproduct',
589 #label => 'Default tax product',
591 'tax_override', { type => 'select-taxoverride' },
595 my $taxproduct_values = sub {
596 my ($cgi, $object, $flags) = @_;
598 sub { my $layer = shift;
599 my @fields = @{$taxproduct_fields{$layer}};
602 my $field = shift @fields;
604 $field =~ /^taxproductnum_\w+$/ &&
605 push @values, ( $field => $options{"usage_$field"} );
606 $field =~ /^tax_override_(\w+)$/ &&
607 push @values, ( $field => $tax_override{$1} );
608 $field =~ /^taxproductnum$/ &&
609 push @values, ( $field => $object->taxproductnum );
610 $field =~ /^tax_override$/ &&
611 push @values, ( $field => $tax_override{''} );
617 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
622 my $field_callback = sub {
623 my ($cgi, $object, $fieldref) = @_;
625 my $field = $fieldref->{field};
626 if ($field eq 'taxproductnums') {
627 $fieldref->{value} = join(',', @taxproductnums);
628 } elsif ($field eq 'taxproduct_select') {
629 $fieldref->{options} = [ '(default)', @taxproductnums ];
630 $fieldref->{labels} = { ( '(default)' => '(default)' ),
631 map {( $_ => ($usage_class{$_} || $_) )}
634 $fieldref->{layer_fields} = \%taxproduct_fields;
635 $fieldref->{layer_values_callback} = $taxproduct_values;