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 { field=>'custom', type=>'hidden' },
63 { type => 'columnstart' },
70 {field=>'comment', type=>'text', size=>40 }, #32
71 { field => 'agentnum',
72 type => 'select-agent',
73 disable_empty => ! $acl_edit_global,
74 empty_label => '(global)',
75 onchange => 'agent_changed',
77 {field=>'classnum', type=>'select-pkg_class' },
78 {field=>'disabled', type=>$disabled_type, value=>'Y'},
80 { type => 'tablebreak-tr-title',
81 value => 'Pricing', #better name?
84 type => 'selectlayers-select',
85 options => [ keys %plan_labels ],
86 labels => \%plan_labels,
88 { field => 'setup_fee',
92 type => 'part_pkg_freq',
93 onchange => 'freq_changed',
95 { field => 'recur_fee',
97 disabled => sub { $recur_disabled },
103 #recurring fee (auto-disable)
105 { type => 'columnnext' },
107 {type=>'justtitle', value=>'Taxation' },
108 {field=>'setuptax', type=>'checkbox', value=>'Y'},
109 {field=>'recurtax', type=>'checkbox', value=>'Y'},
110 {field=>'taxclass', type=>'select-taxclass' },
111 { field => 'taxproductnums',
113 value => join(',', @taxproductnums),
115 { field => 'taxproduct_select',
116 type => 'selectlayers',
117 options => [ '(default)', @taxproductnums ],
118 curr_value => '(default)',
119 labels => { ( '(default)' => '(default)' ),
120 map {($_=>$usage_class{$_})}
123 layer_fields => \%taxproduct_fields,
124 layer_values_callback => $taxproduct_values,
125 layers_only => !$taxproducts,
126 cell_style => ( !$taxproducts
132 { type => 'tablebreak-tr-title',
133 value => 'Promotions', #better name?
135 { field=>'promo_code', type=>'text', size=>15 },
137 { type => 'tablebreak-tr-title',
138 value => 'Cost tracking', #better name?
140 { field=>'setup_cost', type=>'money', },
141 { field=>'recur_cost', type=>'money', },
143 { type => 'columnnext' },
145 { field => 'agent_type',
146 type => 'select-agent_types',
147 disabled => ! $acl_edit_global,
148 curr_value_callback => sub {
149 my($cgi, $object, $field) = @_;
150 #in the other callbacks..? hmm.
155 { type => 'tablebreak-tr-title',
156 value => 'Line-item revenue recogition', #better name?
158 { field=>'pay_weight', type=>'text', size=>6 },
159 { field=>'credit_weight', type=>'text', size=>6 },
162 { type => 'columnend' },
164 { 'type' => 'tablebreak-tr-title',
165 'value' => 'Pricing add-ons',
167 { 'field' => 'bill_dst_pkgpart',
168 'type' => 'select-part_pkg',
169 'm2_label' => 'Include line item(s) from package',
170 'm2m_method' => 'bill_part_pkg_link',
171 'm2m_dstcol' => 'dst_pkgpart',
172 'm2_error_callback' =>
173 &{$m2_error_callback_maker}('bill'),
176 { type => 'tablebreak-tr-title',
179 { type => 'pkg_svc', },
181 { 'field' => 'svc_dst_pkgpart',
182 'label' => 'Also include services from package: ',
183 'type' => 'select-part_pkg',
184 'm2_label' => 'Include services of package: ',
185 'm2m_method' => 'svc_part_pkg_link',
186 'm2m_dstcol' => 'dst_pkgpart',
187 'm2_error_callback' =>
188 &{$m2_error_callback_maker}('svc'),
191 { type => 'tablebreak-tr-title',
192 value => 'Price plan options',
201 my $curuser = $FS::CurrentUser::CurrentUser;
203 my $edit_global = 'Edit global package definitions';
204 my $acl_edit = $curuser->access_right('Edit package definitions');
205 my $acl_edit_global = $curuser->access_right($edit_global);
207 my $acl_edit_either = $acl_edit || $acl_edit_global;
209 my $begin_callback = sub {
210 my( $cgi, $fields, $opt ) = @_;
212 unless $acl_edit_either
213 || ( $cgi->param('pkgnum')
214 && $curuser->access_right('Customize customer package')
218 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
220 my $agent_clone_extra_sql =
221 ' ( '. FS::part_pkg->curuser_pkgs_sql.
222 " OR ( part_pkg.custom = 'Y' ) ".
225 my $conf = new FS::Conf;
226 my $taxproducts = $conf->exists('enable_taxproducts');
229 # - tr-part_pkg_freq: month_increments_only (from price plans)
231 # - test errors cloning
232 # - test custom pricing
233 # - move the selectlayer divs away from lame layer_callback
235 #my ($query) = $cgi->keywords;
240 my %tax_override = ();
242 my %taxproductnums = map { ($_->classnum => 1) }
243 qsearch('usage_class', { 'disabled' => '' });
244 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
247 my $recur_disabled = 1;
249 my $error_callback = sub {
250 my($cgi, $object, $fields, $opt ) = @_;
252 (@agent_type) = $cgi->param('agent_type');
254 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
256 $recur_disabled = $cgi->param('freq') ? 0 : 1;
258 foreach ($cgi->param) {
259 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
261 $tax_override{''} = $cgi->param('tax_override');
262 $tax_override{$_} = $cgi->param('tax_override_$_')
263 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
265 #some false laziness w/process
266 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
268 my $options = $cgi->param($plan."__OPTIONS");
269 my @options = split(',', $options);
271 map { my $optionname = $_;
272 my $param = $plan."__$optionname";
273 my $value = join(', ', $cgi->param($param));
274 ( $optionname => $value );
278 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
279 $object->set($_ => scalar($cgi->param($_)) )
280 foreach (qw( setup_fee recur_fee ));
284 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
286 my $new_object_callback = sub {
287 my( $cgi, $hashref, $fields, $opt ) = @_;
289 my $part_pkg = FS::part_pkg->new( $hashref );
290 $part_pkg->set($_ => '0')
291 foreach (qw( setup_fee recur_fee ));
297 my $edit_callback = sub {
298 my( $cgi, $object, $fields, $opt ) = @_;
300 $recur_disabled = $object->freq ? 0 : 1;
302 (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
304 foreach ($object->options) {
305 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
307 foreach ($object->part_pkg_taxoverride) {
308 $taxproductnums{$_->usage_class} = 1
312 %options = $object->options;
314 $object->set($_ => $object->option($_))
315 foreach (qw( setup_fee recur_fee ));
319 my $new_callback = sub {
320 my( $cgi, $object, $fields ) = @_;
322 my $conf = new FS::Conf;
323 if ( $conf->exists('agent_defaultpkg') ) {
324 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
325 @agent_type = map {$_->typenum} qsearch('agent_type',{});
330 my $clone_callback = sub {
331 my( $cgi, $object, $fields, $opt ) = @_;
333 if ( $cgi->param('pkgnum') ) {
335 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
336 $object->agentnum( $cust_pkg->cust_main->agentnum );
338 $opt->{action} = 'Custom';
340 #my $part_pkg = $clone_part_pkg->clone;
341 #this is all clone does anyway
342 $object->custom('Y');
344 $object->disabled('Y');
348 %options = $object->options;
350 $object->set($_ => $options{$_})
351 foreach (qw( setup_fee recur_fee ));
353 $recur_disabled = $object->freq ? 0 : 1;
356 my $m2_error_callback_maker = sub {
357 my $link_type = shift; #yay closures
359 my( $cgi, $object ) = @_;
361 new FS::part_pkg_link {
362 'link_type' => $link_type,
363 'src_pkgpart' => $object->pkgpart,
369 grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
373 my $javascript = <<'END';
374 <SCRIPT TYPE="text/javascript">
376 function freq_changed(what) {
377 var freq = what.options[what.selectedIndex].value;
380 what.form.recur_fee.disabled = true;
381 what.form.recur_fee.style.backgroundColor = '#dddddd';
383 what.form.recur_fee.disabled = false;
384 what.form.recur_fee.style.backgroundColor = '#ffffff';
389 function agent_changed(what) {
391 var agentnum = what.options[what.selectedIndex].value;
393 if ( agentnum == 0 ) {
394 what.form.agent_type.disabled = false;
395 //what.form.agent_type.style.backgroundColor = '#ffffff';
396 what.form.agent_type.style.visibility = '';
398 what.form.agent_type.disabled = true;
399 //what.form.agent_type.style.backgroundColor = '#dddddd';
400 what.form.agent_type.style.visibility = 'hidden';
408 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
410 tie my %plan_labels, 'Tie::IxHash',
411 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
414 my $html_bottom = sub {
417 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
419 my $layer_callback = sub {
422 my $html = ntable("#cccccc",2);
426 # <TD ALIGN="right">Recurring fee frequency </TD>
427 # <TD><SELECT NAME="freq">
430 #my @freq = keys %freq;
431 #@freq = grep { /^\d+$/ } @freq
432 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
433 #foreach my $freq ( @freq ) {
434 # $html .= qq(<OPTION VALUE="$freq");
435 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
436 # $html .= ">$freq{$freq}";
438 #$html .= '</SELECT></TD></TR>';
440 my $href = $plans{$layer}->{'fields'};
441 my @fields = exists($plans{$layer}->{'fieldorder'})
442 ? @{$plans{$layer}->{'fieldorder'}}
445 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
447 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
449 my $format = sub { shift };
450 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
452 #XXX these should use elements/ fields... (or this whole thing should
453 #just use layer_fields instead of layer_callback)
455 if ( ! exists($href->{$field}{'type'}) ) {
457 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
458 ( exists($options{$field})
459 ? &$format($options{$field})
460 : $href->{$field}{'default'} ).
463 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
465 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
466 ( exists($options{$field}) && $options{$field}
471 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
475 if $href->{$field}{'type'} eq 'select_multiple';
476 $html .= qq! NAME="${layer}__$field">!;
478 if ( $href->{$field}{'select_table'} ) {
480 qsearch( $href->{$field}{'select_table'},
481 $href->{$field}{'select_hash'} )
483 my $value = $record->getfield($href->{$field}{'select_key'});
484 $html .= qq!<OPTION VALUE="$value"!.
485 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
489 '>'. $record->getfield($href->{$field}{'select_label'});
491 } elsif ( $href->{$field}{'select_options'} ) {
492 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
493 my $label = $href->{$field}{'select_options'}{$key};
494 $html .= qq!<OPTION VALUE="$key"!.
495 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
503 $html .= '<font color="#ff0000">warning: '.
504 "don't know how to retreive options for $field select field".
507 $html .= '</SELECT>';
509 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
512 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
514 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
515 my $label = $href->{$field}{'options'}{$key};
516 $html .= qq!$radio VALUE="$key"!.
517 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
526 $html .= '</TD></TR>';
530 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
531 join(',', keys %{ $href } ). '">';
539 options => [ keys %plan_labels ],
540 labels => \%plan_labels,
541 curr_value => $object->plan,
542 layer_callback => $layer_callback,
546 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
547 '<SCRIPT TYPE="text/javascript">'.
548 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
551 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
554 $return .= '</SCRIPT>';
560 my %usage_class = map { ($_->classnum => $_->classname) }
561 qsearch('usage_class', {});
562 $usage_class{setup} = 'Setup';
563 $usage_class{recur} = 'Recurring';
565 my %taxproduct_fields = ();
566 my $end_callback = sub {
567 my( $cgi, $object, $fields, $opt ) = @_;
569 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
571 if ( $object->pkgpart ) {
572 foreach my $usage_class ( '', @taxproductnums ) {
573 $tax_override{$usage_class} =
574 join (",", map $_->taxclassnum,
575 qsearch( 'part_pkg_taxoverride', {
576 'pkgpart' => $object->pkgpart,
577 'usage_class' => $usage_class,
584 map { $_ => [ "taxproductnum_$_",
585 { type => 'select-taxproduct',
586 #label => "$usage_class{$_} tax product",
589 { type => 'select-taxoverride' }
594 $taxproduct_fields{'(default)'} =
595 [ 'taxproductnum', { type => 'select-taxproduct',
596 #label => 'Default tax product',
598 'tax_override', { type => 'select-taxoverride' },
602 my $taxproduct_values = sub {
603 my ($cgi, $object, $flags) = @_;
605 sub { my $layer = shift;
606 my @fields = @{$taxproduct_fields{$layer}};
609 my $field = shift @fields;
611 $field =~ /^taxproductnum_\w+$/ &&
612 push @values, ( $field => $options{"usage_$field"} );
613 $field =~ /^tax_override_(\w+)$/ &&
614 push @values, ( $field => $tax_override{$1} );
615 $field =~ /^taxproductnum$/ &&
616 push @values, ( $field => $object->taxproductnum );
617 $field =~ /^tax_override$/ &&
618 push @values, ( $field => $tax_override{''} );
624 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
629 my $field_callback = sub {
630 my ($cgi, $object, $fieldref) = @_;
632 my $field = $fieldref->{field};
633 if ($field eq 'taxproductnums') {
634 $fieldref->{value} = join(',', @taxproductnums);
635 } elsif ($field eq 'taxproduct_select') {
636 $fieldref->{options} = [ '(default)', @taxproductnums ];
637 $fieldref->{labels} = { ( '(default)' => '(default)' ),
638 map {( $_ => ($usage_class{$_} || $_) )}
641 $fieldref->{layer_fields} = \%taxproduct_fields;
642 $fieldref->{layer_values_callback} = $taxproduct_values;