1 <% include( 'elements/edit.html',
2 'post_url' => popurl(1).'process/part_pkg.cgi',
3 'name' => "Package definition",
5 #'viewall_dir' => 'browse',
6 'viewall_url' => $p.'browse/part_pkg.cgi',
7 'html_init' => include('/elements/init_overlib.html').
9 'html_bottom' => $html_bottom,
10 'new_hashref_callback' => $new_hashref_callback,
11 'new_object_callback' => $new_object_callback,
12 'new_callback' => $new_callback,
13 'edit_callback' => $edit_callback,
14 'error_callback' => $error_callback,
17 'pkgpart' => 'Package Definition',
18 'pkg' => 'Package (customer-visible)',
19 'comment' => 'Comment (customer-hidden)',
20 'classnum' => 'Package class',
21 'promo_code' => 'Promotional code',
22 'freq' => 'Recurring fee frequency',
23 'setuptax' => 'Setup fee tax exempt',
24 'recurtax' => 'Recurring fee tax exempt',
25 'taxclass' => 'Tax class',
26 'plan' => 'Price plan',
27 'disabled' => 'Disable new orders',
28 'pay_weight' => 'Payment weight',
29 'credit_weight' => 'Credit weight',
31 'setup_fee' => 'Setup fee',
32 'recur_fee' => 'Recurring fee',
33 'bill_dst_pkgpart' => 'Include line item(s) from package',
34 'svc_dst_pkgpart' => 'Include services of package',
38 { field=>'clone', type=>'hidden',
39 curr_value_callback =>
40 sub { shift->param('clone') },
42 { field=>'pkgnum', type=>'hidden',
43 curr_value_callback =>
44 sub { shift->param('pkgnum') },
47 { type => 'columnstart' },
49 {field=>'pkg', type=>'text', size=>40 }, #32
50 {field=>'comment', type=>'text', size=>40 }, #32
51 {field=>'classnum', type=>'select-pkg_class' },
52 {field=>'disabled', type=>'checkbox', value=>'Y'},
54 { type => 'tablebreak-tr-title',
55 value => 'Pricing', #better name?
58 type => 'selectlayers-select',
59 options => [ keys %plan_labels ],
60 labels => \%plan_labels,
62 { field => 'setup_fee',
66 type => 'part_pkg_freq',
67 onchange => 'freq_changed',
69 { field => 'recur_fee',
71 disabled => sub { $recur_disabled },
77 #recurring fee (auto-disable)
79 { type => 'columnnext' },
81 {type=>'justtitle', value=>'Taxation' },
82 {field=>'setuptax', type=>'checkbox', value=>'Y'},
83 {field=>'recurtax', type=>'checkbox', value=>'Y'},
84 {field=>'taxclass', type=>'select-taxclass' },
85 {field=>'taxproductnum', type=>'select-taxproduct' },
87 { type => 'tablebreak-tr-title',
88 value => 'Promotions', #better name?
90 { field=>'promo_code', type=>'text', size=>15 },
92 { type => 'tablebreak-tr-title',
93 value => 'Line-item revenue recogition', #better name?
95 { field=>'pay_weight', type=>'text', size=>6 },
96 { field=>'credit_weight', type=>'text', size=>6 },
98 { type => 'columnnext' },
100 { field=>'agent_type',
101 type => 'select-agent_types',
102 curr_value_callback => sub {
103 my($cgi, $object, $field) = @_;
104 #in the other callbacks..? hmm.
109 { type => 'columnend' },
111 { 'type' => 'tablebreak-tr-title',
112 'value' => 'Pricing add-ons',
114 { 'field' => 'bill_dst_pkgpart',
115 'type' => 'select-part_pkg',
116 'm2_label' => 'Include line item(s) from package',
117 'm2m_method' => 'bill_part_pkg_link',
118 'm2m_dstcol' => 'dst_pkgpart',
119 'm2_error_callback' =>
120 &{$m2_error_callback_maker}('bill'),
123 { type => 'tablebreak-tr-title',
126 { type => 'pkg_svc', },
128 { 'field' => 'svc_dst_pkgpart',
129 'label' => 'Also include services from package: ',
130 'type' => 'select-part_pkg',
131 'm2_label' => 'Include services of package: ',
132 'm2m_method' => 'svc_part_pkg_link',
133 'm2m_dstcol' => 'dst_pkgpart',
134 'm2_error_callback' =>
135 &{$m2_error_callback_maker}('svc'),
138 { type => 'tablebreak-tr-title',
139 value => 'Price plan options',
148 my $curuser = $FS::CurrentUser::CurrentUser;
151 unless $curuser->access_right('Edit package definitions')
152 || $curuser->access_right('Edit global package definitions')
153 || ( $cgi->param('pkgnum') && $curuser->access_right('Customize customer package') );
156 # - tr-part_pkg_freq: month_increments_only (from price plans)
158 # - test errors cloning
159 # - test custom pricing
160 # - move the selectlayer divs away from lame layer_callback
162 #my ($query) = $cgi->keywords;
169 my $clone_part_pkg = '';
172 my $recur_disabled = 1;
173 my $error_callback = sub {
174 my($cgi, $object, $fields, $opt ) = @_;
175 (@agent_type) = $cgi->param('agent_type');
176 $tax_override = $cgi->param('tax_override');
177 $opt->{action} = 'Custom' if $cgi->param('clone');
178 $clone_part_pkg= qsearchs('part_pkg', { 'pkgpart' => $cgi->param('clone') } );
180 $recur_disabled = $cgi->param('freq') ? 0 : 1;
182 #some false laziness w/process
183 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
185 my $options = $cgi->param($plan."__OPTIONS");
186 my @options = split(',', $options);
188 map { my $optionname = $_;
189 my $param = $plan."__$optionname";
190 my $value = join(', ', $cgi->param($param));
191 ( $optionname => $value );
195 #$cgi->param($_, $options{$_}) foreach (qw( setup_fee recur_fee ));
196 $object->set($_ => scalar($cgi->param($_)) )
197 foreach (qw( setup_fee recur_fee ));
201 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
203 my $new_object_callback = sub {
204 my( $cgi, $hashref, $fields, $opt ) = @_;
207 if ( $cgi->param('clone') ) {
208 $opt->{action} = 'Custom';
209 $clone_part_pkg = qsearchs('part_pkg', { pkgpart=>$cgi->param('clone') } );
210 $part_pkg = $clone_part_pkg->clone;
211 $part_pkg->disabled('Y');
212 %options = $clone_part_pkg->options;
214 $part_pkg = FS::part_pkg->new( $hashref );
217 $part_pkg->set($_ => '0')
218 foreach (qw( setup_fee recur_fee ));
224 my $edit_callback = sub {
225 my( $cgi, $object, $fields ) = @_;
227 $recur_disabled = $object->freq ? 0 : 1;
229 (@agent_type) = map {$_->typenum} qsearch('type_pkgs',{'pkgpart'=>$1});
231 join (",", map {$_->taxclassnum}
232 qsearch( 'part_pkg_taxoverride', {'pkgpart' => $1} )
235 # join (",", map {$_->taxclassnum}
236 # $part_pkg->part_pkg_taxrate( 'cch', $conf->config('defaultloc')
238 # unless $tax_override;
240 %options = $object->options;
242 $object->set($_ => $object->option($_))
243 foreach (qw( setup_fee recur_fee ));
247 my $new_callback = sub {
248 my( $cgi, $object, $fields ) = @_;
250 my $conf = new FS::Conf;
251 if ( $conf->exists('agent_defaultpkg') ) {
252 #my @all_agent_types = map {$_->typenum} qsearch('agent_type',{});
253 @agent_type = map {$_->typenum} qsearch('agent_type',{});
258 my $m2_error_callback_maker = sub {
259 my $link_type = shift; #yay closures
261 my( $cgi, $object ) = @_;
263 new FS::part_pkg_link {
264 'link_type' => $link_type,
265 'src_pkgpart' => $object->pkgpart,
271 grep /^${link_type}_dst_pkgpart(\d+)$/, $cgi->param;
275 my $freq_changed = <<'END';
276 <SCRIPT TYPE="text/javascript">
278 function freq_changed(what) {
279 var freq = what.options[what.selectedIndex].value;
282 what.form.recur_fee.disabled = true;
283 what.form.recur_fee.style.backgroundColor = '#dddddd';
285 what.form.recur_fee.disabled = false;
286 what.form.recur_fee.style.backgroundColor = '#ffffff';
294 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
296 tie my %plan_labels, 'Tie::IxHash',
297 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
300 my $html_bottom = sub {
303 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
305 my $layer_callback = sub {
308 my $html = ntable("#cccccc",2);
312 # <TD ALIGN="right">Recurring fee frequency </TD>
313 # <TD><SELECT NAME="freq">
316 #my @freq = keys %freq;
317 #@freq = grep { /^\d+$/ } @freq
318 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
319 #foreach my $freq ( @freq ) {
320 # $html .= qq(<OPTION VALUE="$freq");
321 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
322 # $html .= ">$freq{$freq}";
324 #$html .= '</SELECT></TD></TR>';
326 my $href = $plans{$layer}->{'fields'};
327 my @fields = exists($plans{$layer}->{'fieldorder'})
328 ? @{$plans{$layer}->{'fieldorder'}}
331 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
333 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
335 my $format = sub { shift };
336 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
338 #XXX these should use elements/ fields... (or this whole thing should
339 #just use layer_fields instead of layer_callback)
341 if ( ! exists($href->{$field}{'type'}) ) {
343 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
344 ( exists($options{$field})
345 ? &$format($options{$field})
346 : $href->{$field}{'default'} ).
349 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
351 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
352 ( exists($options{$field}) && $options{$field}
357 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
361 if $href->{$field}{'type'} eq 'select_multiple';
362 $html .= qq! NAME="${layer}__$field">!;
364 if ( $href->{$field}{'select_table'} ) {
366 qsearch( $href->{$field}{'select_table'},
367 $href->{$field}{'select_hash'} )
369 my $value = $record->getfield($href->{$field}{'select_key'});
370 $html .= qq!<OPTION VALUE="$value"!.
371 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
375 '>'. $record->getfield($href->{$field}{'select_label'});
377 } elsif ( $href->{$field}{'select_options'} ) {
378 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
379 my $label = $href->{$field}{'select_options'}{$key};
380 $html .= qq!<OPTION VALUE="$key"!.
381 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
389 $html .= '<font color="#ff0000">warning: '.
390 "don't know how to retreive options for $field select field".
393 $html .= '</SELECT>';
395 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
398 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
400 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
401 my $label = $href->{$field}{'options'}{$key};
402 $html .= qq!$radio VALUE="$key"!.
403 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
412 $html .= '</TD></TR>';
416 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
417 join(',', keys %{ $href } ). '">';
425 options => [ keys %plan_labels ],
426 labels => \%plan_labels,
427 curr_value => $object->plan,
428 layer_callback => $layer_callback,
431 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
432 '<SCRIPT TYPE="text/javascript">'.
433 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 ).