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);
16 aux_planchanged(document.edit_topform.plan);
19 'begin_callback' => $begin_callback,
20 'end_callback' => $end_callback,
21 'new_hashref_callback' => $new_hashref_callback,
22 'new_object_callback' => $new_object_callback,
23 'new_callback' => $new_callback,
24 'clone_callback' => $clone_callback,
25 'edit_callback' => $edit_callback,
26 'error_callback' => $error_callback,
27 'field_callback' => $field_callback,
29 'onsubmit' => 'confirm_submit',
32 'pkgpart' => 'Package Definition',
35 'comment' => 'Comment (customer-hidden)',
36 'classnum' => 'Package class',
37 'addon_classnum' => 'Restrict additional orders to package class',
38 'promo_code' => 'Promotional code',
39 'freq' => 'Recurring fee frequency',
40 'setuptax' => 'Setup fee tax exempt',
41 'recurtax' => 'Recurring fee tax exempt',
42 'taxclass' => 'Tax class',
43 'taxproduct_select'=> 'Tax products',
44 'plan' => 'Price plan',
45 'disabled' => 'Disable new orders',
46 'disable_line_item_date_ranges' => 'Disable line item date ranges',
47 'start_on_hold' => 'Start on hold',
48 'setup_cost' => 'Setup cost',
49 'recur_cost' => 'Recur cost',
50 'pay_weight' => 'Payment weight',
51 'credit_weight' => 'Credit weight',
52 'agent_pkgpartid' => 'External ID',
53 'agentnum' => 'Agent',
54 'agent_type' => ' ', #just its title headingn is fine
55 'setup_fee' => 'Setup fee',
56 'setup_show_zero' => 'Show zero setup',
57 'recur_fee' => 'Recurring fee',
58 'recur_show_zero' => 'Show zero recurring',
59 ( map { ( "setup_fee_$_" => "Setup fee $_",
60 "recur_fee_$_" => "Recurring fee $_",
63 $conf->config('currencies')
65 'usagepricepart' => ' ',
66 'discountnum' => 'Offer discounts for longer terms',
67 'bill_dst_pkgpart' => 'Include line item(s) from package',
68 'svc_dst_pkgpart' => 'Include services of package',
69 'supp_dst_pkgpart' => 'When ordering package, also order',
70 'report_option' => 'Report classes',
71 'delay_start' => 'Default delay (days)',
75 { field=>'clone', type=>'hidden',
76 curr_value_callback =>
77 sub { shift->param('clone') },
79 { field=>'pkgnum', type=>'hidden',
80 curr_value_callback =>
81 sub { shift->param('pkgnum') },
84 { field=>'custom', type=>'hidden' },
85 { field=>'family_pkgpart', type=>'hidden' },
86 { field=>'successor', type=>'hidden' },
88 { type => 'columnstart' },
96 {field=>'comment', type=>'text', size=>40 }, #32
97 { field => 'agentnum',
98 type => 'select-agent',
99 disable_empty => ! $acl_edit_global,
100 empty_label => '(global)',
101 onchange => 'agent_changed',
103 {field=>'classnum', type=>'select-pkg_class' },
104 ( $conf->exists('pkg-addon_classnum')
105 ? ( { field=>'addon_classnum',
106 type =>'select-pkg_class',
111 {field=>'disabled', type=>$disabled_type, value=>'Y'},
112 {field=>'disable_line_item_date_ranges', type=>$disabled_type, value=>'Y'},
113 { field => 'start_on_hold',
118 { type => 'tablebreak-tr-title',
119 value => 'Pricing', #better name?
122 type => 'selectlayers-select',
123 options => [ keys %plan_labels ],
124 labels => \%plan_labels,
125 onchange => 'aux_planchanged(what);',
127 { field => 'setup_fee',
129 onchange => 'setup_changed',
131 { field => 'setup_show_zero',
134 disabled => sub { $setup_show_zero_disabled },
136 ( map { +{ field => "setup_fee_$_",
138 prefix=> currency_symbol($_, SYM_HTML),
142 sort $conf->config('currencies')
145 type => 'part_pkg_freq',
146 onchange => 'freq_changed',
148 { field => 'recur_fee',
150 disabled => sub { $recur_disabled },
151 onchange => 'recur_changed',
153 { field => 'recur_show_zero',
156 disabled => sub { $recur_show_zero_disabled },
158 ( map { +{ field => "recur_fee_$_",
160 prefix=> currency_symbol($_, SYM_HTML),
164 sort $conf->config('currencies')
170 #recurring fee (auto-disable)
172 { type => 'columnnext' },
174 {type=>'justtitle', value=>'Taxation' },
175 {field=>'setuptax', type=>'checkbox', value=>'Y'},
176 {field=>'recurtax', type=>'checkbox', value=>'Y'},
177 {field=>'taxclass', type=>'select-taxclass' },
178 { field => 'taxproductnums',
180 value => join(',', @taxproductnums),
182 { field => 'taxproduct_select',
183 type => 'selectlayers',
184 options => [ '(default)', @taxproductnums ],
185 curr_value => '(default)',
186 labels => { ( '(default)' => '(default)' ),
187 map {($_=>$usage_class{$_})}
190 layer_fields => \%taxproduct_fields,
191 layer_values_callback => $taxproduct_values,
192 layers_only => !$taxproducts,
193 cell_style => ( !$taxproducts
199 { type => 'tablebreak-tr-title',
200 value => 'Promotions', #better name?
202 { field=>'promo_code', type=>'text', size=>15 },
204 { type => 'tablebreak-tr-title',
205 value => 'Cost tracking', #better name?
208 ( $curuser->access_right('Edit package definition costs')
209 ? ( { field=>'setup_cost', type=>'money', },
210 { field=>'recur_cost', type=>'money', },
212 : ( { field=>'setup_cost', type=>'fixed', },
213 { field=>'recur_cost', type=>'fixed', },
217 ( $conf->exists('part_pkg-delay_start')
218 ? ( { type => 'tablebreak-tr-title',
219 value => 'Delayed start',
221 { field => 'delay_start',
222 type => 'text', size => 6 },
227 { type => 'columnnext' },
229 {type=>'justtitle', value=>'Agent (reseller) types' },
231 { field => 'agent_type',
232 type => 'select-agent_type',
233 disabled => ! $acl_edit_global,
234 element_etc => 'size="10"',
235 multiple => '1', #cause edit.html is dum
236 curr_value_callback => sub {
237 my($cgi, $object, $field) = @_;
238 #in the other callbacks..? hmm.
244 { type => 'tablebreak-tr-title',
245 value => 'FCC Form 477 information',
247 { field => 'fcc_options_string',
248 type => 'input-fcc_options',
249 curr_value_callback => sub {
250 my ($cgi, $part_pkg, $fref) = @_;
251 if ( $cgi->param('fcc_options_string') ) {
253 return $cgi->param('fcc_options_string');
256 %hash = $part_pkg->fcc_options
257 if ($part_pkg->pkgpart);
258 return encode_json(\%hash);
264 { type => 'tablebreak-tr-title',
265 value => 'External Links', #better name?
267 { field=>'agent_pkgpartid', type=>'text', size=>21 },
269 { type => 'tablebreak-tr-title',
270 value => 'Line-item revenue recogition', #better name?
272 { field=>'pay_weight', type=>'text', size=>6 },
273 { field=>'credit_weight', type=>'text', size=>6 },
275 { type => 'columnend' },
277 { type => 'tablebreak-tr-title',
278 value => 'Usage pricing add-ons', #better name? just 'Usage pricing' ? there's also CDR usage pricing, RADIUS usage pricing, etc :/
280 { 'field' => 'usagepricepart',
281 'type' => 'part_pkg_usageprice',
282 'o2m_table' => 'part_pkg_usageprice',
284 'm2_error_callback' => $usageprice_error_callback,
287 { 'type' => $report_option ? 'tablebreak-tr-title'
289 'value' => 'Optional report classes',
290 'field' => 'census_title',
292 { 'field' => 'report_option',
293 'type' => $report_option ? 'select-table'
295 'table' => 'part_pkg_report_option',
296 'name_col' => 'name',
297 'hashref' => { 'disabled' => '' },
301 { 'type' => 'tablebreak-tr-title',
302 'value' => 'Term discounts',
304 { 'field' => 'discountnum',
305 'type' => 'select-table',
306 'table' => 'discount',
307 'name_col' => 'name',
308 'hashref' => { %$discountnum_hashref },
309 #'extra_sql' => 'AND (months IS NOT NULL OR months != 0)',
310 'empty_label'=> 'Select discount',
311 'm2_label' => 'Offer discounts for longer terms',
312 'm2m_method' => 'part_pkg_discount',
313 'm2m_dstcol' => 'discountnum',
314 'm2_error_callback' => $discount_error_callback,
317 { 'type' => 'tablebreak-tr-title',
318 'value' => 'Pricing add-ons',
321 { 'field' => 'bill_dst_pkgpart',
322 'type' => 'select-part_pkg',
323 'extra_sql' => sub { $pkgpart
324 ? "AND pkgpart != $pkgpart"
327 'label_callback' => sub { shift->pkg_comment_only },
328 'm2_label' => 'Include line item(s) from package',
329 'm2m_method' => 'bill_part_pkg_link',
330 'm2m_dstcol' => 'dst_pkgpart',
331 'm2_error_callback' =>
332 &{$m2_error_callback_maker}('bill'),
333 'm2_fields' => [ { 'field' => 'hidden',
334 'type' => 'checkbox',
342 { type => 'tablebreak-tr-title',
345 { type => 'pkg_svc', },
347 { 'field' => 'svc_dst_pkgpart',
348 'label' => 'Also include services from package: ',
349 'type' => 'select-part_pkg',
350 'extra_sql' => sub { $pkgpart
351 ? "AND pkgpart != $pkgpart"
354 'label_callback' => sub { shift->pkg_comment_only },
355 'm2_label' => 'Include services of package: ',
356 'm2m_method' => 'svc_part_pkg_link',
357 'm2m_dstcol' => 'dst_pkgpart',
358 'm2_error_callback' =>
359 &{$m2_error_callback_maker}('svc'),
362 { 'type' => 'tablebreak-tr-title',
363 'value' => 'Supplemental packages',
365 'include_opt_callback' => sub {
366 'id' => 'show_supp_pkgs',
369 { 'field' => 'supp_dst_pkgpart',
370 'type' => 'select-part_pkg',
371 'label_callback' => sub { shift->pkg_comment_only },
372 'm2_label' => 'When ordering package, also order',
373 'm2m_method' => 'supp_part_pkg_link',
374 'm2m_dstcol' => 'dst_pkgpart',
375 'm2_error_callback' =>
376 &{$m2_error_callback_maker}('supp'),
379 { type => 'tablebreak-tr-title',
380 value => 'Price plan options',
387 my $curuser = $FS::CurrentUser::CurrentUser;
389 my $edit_global = 'Edit global package definitions';
390 my $acl_edit = $curuser->access_right('Edit package definitions');
391 my $acl_edit_global = $curuser->access_right($edit_global);
393 my $acl_edit_either = $acl_edit || $acl_edit_global;
395 my $begin_callback = sub {
396 my( $cgi, $fields, $opt ) = @_;
398 unless $acl_edit_either
399 || ( $cgi->param('pkgnum')
400 && $curuser->access_right('Customize customer package')
404 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
406 #arg. access rights for cloning are Hard.
407 # on the one hand we don't really want cloning (customizing a package) to fail
408 # for want of finding the source package in normal usage
409 # on the other hand, we don't want people using the clone link to be able to
411 my $agent_clone_extra_sql =
412 ' ( '. FS::part_pkg->curuser_pkgs_sql.
413 " OR ( part_pkg.custom = 'Y' ) ".
416 my $conf = new FS::Conf;
417 my $taxproducts = $conf->exists('enable_taxproducts');
419 my $fcc_opts = $conf->exists('part_pkg-show_fcc_options');
421 my @locales = grep { ! /^en_/i } $conf->config('available-locales'); #should filter from the default locale lang instead of en_
422 my %locale_labels = map {
423 ( $_ => 'Package -- '. FS::Locales->description($_) )
426 sort { $locale_labels{$a} cmp $locale_labels{$b} }
430 my %locale_field_labels = (
432 ( 'pkgpartmsgnum'. $n++. '_pkg' => $locale_labels{$_} );
437 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
438 " WHERE disabled IS NULL OR disabled = '' ")
440 $sth->execute or die $sth->errstr;
441 my $report_option = $sth->fetchrow_arrayref->[0];
444 # - tr-part_pkg_freq: month_increments_only (from price plans)
446 # - test errors cloning
447 # - test custom pricing
448 # - move the selectlayer divs away from lame layer_callback
450 #my ($query) = $cgi->keywords;
455 my %tax_override = ();
457 my %taxproductnums = map { ($_->classnum => 1) }
458 qsearch('usage_class', { 'disabled' => '' });
459 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
462 my $recur_disabled = 1;
463 my $setup_show_zero_disabled = 0;
464 my $recur_show_zero_disabled = 1;
468 my $splice_locale_fields = sub {
469 my( $fields, $pkey_value_callback, $pkg_value_callback ) = @_;
472 my @locale_fields = (
474 my $pkey_value= $pkey_value_callback ? &$pkey_value_callback($_) : '';
475 my $pkg_value = $pkg_value_callback
476 ? $pkg_value_callback eq 'cgiparam'
477 ? $cgi->param('pkgpartmsgnum'. $n. '_pkg')
478 : &$pkg_value_callback($_)
481 { field => 'pkgpartmsgnum'. $n,
483 value => $pkey_value,
485 { field => 'pkgpartmsgnum'. $n. '_locale',
489 { field => 'pkgpartmsgnum'. $n++. '_pkg',
500 splice(@$fields, 7, 0, @locale_fields); #XXX 7 is arbitrary above
504 my $error_callback = sub {
505 my($cgi, $object, $fields, $opt ) = @_;
507 (@agent_type) = $cgi->param('agent_type');
509 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
511 $setup_show_zero_disabled = ($cgi->param('setup_fee') > 0) ? 1 : 0;
513 $recur_disabled = $cgi->param('freq') ? 0 : 1;
514 $recur_show_zero_disabled =
516 ? $cgi->param('recur_fee') > 0 ? 1 : 0
519 foreach ($cgi->param) {
520 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
522 $tax_override{''} = $cgi->param('tax_override');
523 $tax_override{$_} = $cgi->param('tax_override_$_')
524 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
526 #some false laziness w/process
527 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
529 my $options = $cgi->param($plan."__OPTIONS");
530 my @options = split(',', $options);
532 map { my $optionname = $_;
533 my $param = $plan."__$optionname";
534 my $value = join(', ', $cgi->param($param));
535 ( $optionname => $value );
539 $object->set($_ => scalar($cgi->param($_)) )
540 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
542 foreach my $currency ( $conf->config('currencies') ) {
543 my %part_pkg_currency = $object->part_pkg_currency_options($currency);
544 foreach (qw( setup_fee recur_fee )) {
545 my $param = $_.'_'.$currency;
546 $object->set( $param, $cgi->param($param) );
550 $pkgpart = $object->pkgpart;
552 &$splice_locale_fields(
556 my $part_pkg_msgcat = $object->part_pkg_msgcat($locale);
557 $part_pkg_msgcat ? $part_pkg_msgcat->pkgpartmsgnum : '';
564 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
566 my $new_object_callback = sub {
567 my( $cgi, $hashref, $fields, $opt ) = @_;
569 my $part_pkg = FS::part_pkg->new( $hashref );
570 $part_pkg->set($_ => '0')
571 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
577 sub set_report_option {
578 my($cgi, $object, $fields ) = @_; #, $opt
580 my @report_option = ();
581 foreach ($object->options) {
582 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
583 /^report_option_(\d+)$/ && (push @report_option, $1);
585 foreach ($object->part_pkg_taxoverride) {
586 $taxproductnums{$_->usage_class} = 1
590 $cgi->param('report_option', join(',', @report_option));
591 foreach my $field ( @$fields ) {
593 ref($field) eq 'HASH' &&
595 $field->{field} eq 'report_option'
597 #$field->{curr_value} = join(',', @report_option);
598 $field->{value} = join(',', @report_option);
603 my $edit_callback = sub {
604 my( $cgi, $object, $fields, $opt ) = @_;
606 $setup_show_zero_disabled = ($object->option('setup_fee') > 0) ? 1 : 0;
608 $recur_disabled = $object->freq ? 0 : 1;
610 $recur_show_zero_disabled =
612 ? $object->option('recur_fee') > 0 ? 1 : 0
616 map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
618 set_report_option( $cgi, $object, $fields);
620 %options = $object->options;
622 $object->set($_ => $object->option($_, 1))
623 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
625 foreach my $currency ( $conf->config('currencies') ) {
626 my %part_pkg_currency = $object->part_pkg_currency_options($currency);
627 $object->set( $_.'_'.$currency, $part_pkg_currency{$_} )
628 foreach keys %part_pkg_currency;
631 $pkgpart = $object->pkgpart;
633 &$splice_locale_fields(
637 my $part_pkg_msgcat = $object->part_pkg_msgcat($locale);
638 $part_pkg_msgcat ? $part_pkg_msgcat->pkgpartmsgnum : '';
642 my $part_pkg_msgcat = $object->part_pkg_msgcat($locale);
643 $part_pkg_msgcat ? $part_pkg_msgcat->pkg : '';
649 my $new_callback = sub {
650 my( $cgi, $object, $fields ) = @_;
652 my $conf = new FS::Conf;
654 if ( $conf->exists('agent_defaultpkg') ) {
655 @agent_type = map {$_->typenum} qsearch('agent_type', { 'disabled'=>'' });
658 $options{'suspend_bill'}=1 if $conf->exists('part_pkg-default_suspend_bill');
660 &$splice_locale_fields($fields, '', '');
664 my $clone_callback = sub {
665 my( $cgi, $object, $fields, $opt ) = @_;
667 if ( $cgi->param('pkgnum') ) {
669 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
670 $object->agentnum( $cust_pkg->cust_main->agentnum );
672 $opt->{action} = 'Custom';
674 #my $part_pkg = $clone_part_pkg->clone;
675 #this is all clone does anyway
676 $object->custom('Y');
678 $object->disabled('Y');
680 } else { #when explicitly cloning, not customizing
683 map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
687 set_report_option( $cgi, $object, $fields);
689 %options = $object->options;
691 $object->set($_ => $options{$_})
692 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
694 foreach my $currency ( $conf->config('currencies') ) {
695 my %part_pkg_currency = $object->part_pkg_currency_options($currency);
696 $object->set( $_.'_'.$currency, $part_pkg_currency{$_} )
697 foreach keys %part_pkg_currency;
700 $recur_disabled = $object->freq ? 0 : 1;
702 &$splice_locale_fields(
707 my $part_pkg_msgcat = $object->part_pkg_msgcat($locale);
708 $part_pkg_msgcat ? $part_pkg_msgcat->pkg : '';
713 my $discount_error_callback = sub {
714 my( $cgi, $object ) = @_;
716 if ( /^discountnum(\d+)$/ &&
717 ( my $discountnum = $cgi->param("discountnum$1") ) )
719 new FS::part_pkg_discount {
720 'pkgpart' => $object->pkgpart,
721 'discountnum' => $discountnum,
730 my $usageprice_error_callback = sub {
731 my( $cgi, $object ) = @_;
733 if ( /^usagepricepart(\d+)_price$/
734 && $cgi->param("usagepricepart$1_price") )
736 new FS::part_pkg_usageprice {
737 'usagepricepart' => $cgi->param("usagepricepart$1"),
738 'pkgpart' => $object->pkgpart,
739 'price' => scalar($cgi->param("usagepricepart$1_price")),
741 'action' => scalar($cgi->param("usagepricepart$1_action")),
742 'target' => scalar($cgi->param("usagepricepart$1_target")),
743 'amount' => scalar($cgi->param("usagepricepart$1_amount")),
752 my $m2_error_callback_maker = sub {
753 my $link_type = shift; #yay closures
755 my( $cgi, $object ) = @_;
758 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
759 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
762 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
764 new FS::part_pkg_link {
765 'link_type' => $link_type,
766 'src_pkgpart' => $object->pkgpart,
767 'dst_pkgpart' => $dst,
778 my $javascript = <<'END';
779 <SCRIPT TYPE="text/javascript">
781 function freq_changed(what) {
782 var freq = what.options[what.selectedIndex].value;
785 what.form.recur_fee.disabled = true;
786 what.form.recur_fee.style.backgroundColor = '#dddddd';
787 what.form.recur_show_zero.disabled = true;
788 //what.form.recur_show_zero.style.backgroundColor= '#dddddd';
790 what.form.recur_fee.disabled = false;
791 what.form.recur_fee.style.backgroundColor = '#ffffff';
792 recur_changed( what.form.recur_fee );
793 //what.form.recur_show_zero.style.backgroundColor= '#ffffff';
798 function setup_changed(what) {
799 var setup = what.value;
800 if ( parseFloat(setup) == 0 ) {
801 what.form.setup_show_zero.disabled = false;
803 what.form.setup_show_zero.disabled = true;
807 function recur_changed(what) {
808 var recur = what.value;
809 if ( parseFloat(recur) == 0 ) {
810 what.form.recur_show_zero.disabled = false;
812 what.form.recur_show_zero.disabled = true;
816 function agent_changed(what) {
819 if ( what.type == 'select-one' ) {
820 agentnum = what.options[what.selectedIndex].value;
822 agentnum = what.value;
825 if ( agentnum == 0 ) {
826 what.form.agent_type.disabled = false;
827 //what.form.agent_type.style.backgroundColor = '#ffffff';
828 what.form.agent_type.style.visibility = '';
830 what.form.agent_type.disabled = true;
831 //what.form.agent_type.style.backgroundColor = '#dddddd';
832 what.form.agent_type.style.visibility = 'hidden';
837 function aux_planchanged(what) { //?
839 var plan = what.options[what.selectedIndex].value;
841 var term_table = document.getElementById('TableNumber8') // XXX NOT ROBUST
842 if ( plan == 'flat' || plan == 'prorate' || plan == 'subscription' ) {
843 //term_table.disabled = false;
844 //term_table.style.visibility = '';
845 term_table.style.display = '';
847 //term_table.disabled = true;
848 //term_table.style.visibility = 'hidden';
849 term_table.style.display = 'none';
852 var currency_regex = /^(setup|recur)_fee_[A-Z]{3}$/;
855 for ( var i=0; i < form.length; i++ ) {
856 if ( currency_regex.test(form[i].name) ) {
857 if ( plan == 'currency_fixed' ) {
858 form[i].disabled = false;
860 form[i].disabled = true;
867 // some magic to make "supplemental packages" less obvious
868 var supp_pkg_rows = [];
869 function show_supp_pkgs_click() {
870 supp_pkg_rows[0].style.display = '';
872 this.style.backgroundColor = '';
873 this.style.border = '';
874 this.style.padding = '';
877 function hide_supp_pkgs() {
878 var all_selects = document.getElementsByTagName('select');
879 for (var i=0; i < all_selects.length; i++) {
880 if ( all_selects[i].id.match(/^supp_dst_pkgpart/) ) {
881 supp_pkg_rows.push( all_selects[i].parentNode.parentNode );
884 if ( supp_pkg_rows.length == 1 ) {
885 // there are none configured, so hide the row to create a new one
886 supp_pkg_rows[0].style.display = 'none';
887 var button = document.getElementById('show_supp_pkgs');
888 button.onclick = show_supp_pkgs_click;
889 button.style.backgroundColor = '#cccccc';
890 button.style.border = '1px solid #7e0079';
891 button.style.padding = '1px';
895 function finish_edit_fcc(id) {
897 show_fcc_options(id); // refresh the display
903 'Changing the setup or recurring fee will create a new package definition. '.
906 $javascript .= "function confirm_submit(f) {";
907 if ( $conf->exists('part_pkg-lineage') ) {
910 var fields = Array('setup_fee','recur_fee');
911 for(var i=0; i < fields.length; i++) {
912 if ( f[fields[i]].value != f[fields[i]].defaultValue ) {
913 return confirm('$warning');
923 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
925 tie my %plan_labels, 'Tie::IxHash',
926 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
929 my $html_bottom = sub {
932 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
934 my $layer_callback = sub {
937 my $html = ntable("#cccccc",2);
941 # <TD ALIGN="right">Recurring fee frequency </TD>
942 # <TD><SELECT NAME="freq">
945 #my @freq = keys %freq;
946 #@freq = grep { /^\d+$/ } @freq
947 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
948 #foreach my $freq ( @freq ) {
949 # $html .= qq(<OPTION VALUE="$freq");
950 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
951 # $html .= ">$freq{$freq}";
954 #$html .= '</SELECT></TD></TR>';
956 my $href = $plans{$layer}->{'fields'};
957 my @fields = exists($plans{$layer}->{'fieldorder'})
958 ? @{$plans{$layer}->{'fieldorder'}}
961 # hash of dependencies for each of the Pricing Plan fields.
962 # make sure NOT to use double-quotes inside the 'msg' value.
964 'unused_credit_suspend' => {
965 'msg' => q|You must set the 'suspend_credit_type' option in Configuration->Settings to gain access to this option.|,
967 my $conf = new FS::conf;
968 my @conf_info = qsearch('conf', { 'name' => 'suspend_credit_type' } );
969 return 1 if (exists($conf_info[0]) && $conf_info[0]->{Hash}{value});
973 'unused_credit_cancel' => {
974 'msg' => q|You must set the 'cancel_credit_type' option in Configuration->Settings to gain access to this option.|,
976 my $conf = new FS::conf;
977 my @conf_info = qsearch('conf', { 'name' => 'cancel_credit_type' } );
978 return 1 if (exists($conf_info[0]) && $conf_info[0]->{Hash}{value});
984 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
986 if(!exists($href->{$field})) {
988 warn "nonexistent part_pkg option: '$field'\n";
991 if ( exists($href->{$field}->{display_if}) ) {
992 my %args = ( 'plan' => $layer ); # anything else?
993 my $display = &{ $href->{$field}->{display_if} }(%args);
997 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
999 my $format = sub { shift };
1000 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
1002 #XXX these should use elements/ fields... (or this whole thing should
1003 #just use layer_fields instead of layer_callback)
1005 if (exists($dependencies->{$field}) && !$dependencies->{$field}{'are_met'}()) {
1006 $html .= q!<span title="!.$dependencies->{$field}{'msg'}.q!">N/A</span>!;
1008 } elsif ( ! exists($href->{$field}{'type'}) ) {
1010 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
1011 ( exists($options{$field})
1012 ? &$format($options{$field})
1013 : $href->{$field}{'default'} ).
1016 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
1018 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
1019 ( exists($options{$field}) && $options{$field}
1024 } elsif ( $href->{$field}{'type'} eq 'select-rate' ) {
1026 $html .= include('/elements/select-rate.html',
1027 'field' => $layer.'__'.$field,
1028 'curr_value' => $options{$field},
1029 map { $_ => $href->{$field}{$_} }
1030 grep { $_ !~ /^(name|type)$/ }
1031 keys %{ $href->{$field} }
1034 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
1037 $html .= ' MULTIPLE'
1038 if $href->{$field}{'type'} eq 'select_multiple';
1039 $html .= qq! NAME="${layer}__$field">!;
1041 $html .= '<OPTION VALUE="">'. $href->{$field}{'empty_label'}
1042 if exists($href->{$field}{'disable_empty'})
1043 && ! $href->{$field}{'disable_empty'};
1045 if ( $href->{$field}{'select_table'} ) {
1046 foreach my $record (
1047 qsearch( $href->{$field}{'select_table'},
1048 $href->{$field}{'select_hash'} )
1050 my $value = $record->getfield($href->{$field}{'select_key'});
1051 $html .= qq!<OPTION VALUE="$value"!.
1052 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
1056 '>'. $record->getfield($href->{$field}{'select_label'});
1058 } elsif ( $href->{$field}{'select_options'} ) {
1059 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
1060 my $label = $href->{$field}{'select_options'}{$key};
1061 $html .= qq!<OPTION VALUE="$key"!.
1062 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
1070 $html .= '<font color="#ff0000">warning: '.
1071 "don't know how to retreive options for $field select field".
1074 $html .= '</SELECT>';
1076 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
1079 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
1081 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
1082 my $label = $href->{$field}{'options'}{$key};
1083 $html .= qq!$radio VALUE="$key"!.
1084 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
1093 $html .= '</TD></TR>';
1095 $html .= '</TABLE>';
1097 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
1098 join(',', keys %{ $href } ). '">';
1104 my %selectlayers = (
1106 options => [ keys %plan_labels ],
1107 labels => \%plan_labels,
1108 curr_value => $object->plan,
1109 layer_callback => $layer_callback,
1110 onchange => 'aux_planchanged(what);',
1114 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
1115 '<SCRIPT TYPE="text/javascript">'.
1116 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
1119 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
1122 $return .= '</SCRIPT>';
1128 my %usage_class = map { ($_->classnum => $_->classname) }
1129 qsearch('usage_class', {});
1130 $usage_class{setup} = 'Setup';
1131 $usage_class{recur} = 'Recurring';
1133 my %taxproduct_fields = ();
1134 my $end_callback = sub {
1135 my( $cgi, $object, $fields, $opt ) = @_;
1137 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
1139 if ( $object->pkgpart ) {
1140 foreach my $usage_class ( '', @taxproductnums ) {
1141 $tax_override{$usage_class} =
1142 join (",", map $_->taxclassnum,
1143 qsearch( 'part_pkg_taxoverride', {
1144 'pkgpart' => $object->pkgpart,
1145 'usage_class' => $usage_class,
1151 %taxproduct_fields =
1152 map { $_ => [ "taxproductnum_$_",
1153 { type => 'select-taxproduct',
1154 #label => "$usage_class{$_} tax product",
1157 { type => 'select-taxoverride' }
1162 $taxproduct_fields{'(default)'} =
1163 [ 'taxproductnum', { type => 'select-taxproduct',
1164 #label => 'Default tax product',
1166 'tax_override', { type => 'select-taxoverride' },
1170 my $taxproduct_values = sub {
1171 my ($cgi, $object, $flags) = @_;
1173 sub { my $layer = shift;
1174 my @fields = @{$taxproduct_fields{$layer}};
1177 my $field = shift @fields;
1179 $field =~ /^taxproductnum_\w+$/ &&
1180 push @values, ( $field => $options{"usage_$field"} );
1181 $field =~ /^tax_override_(\w+)$/ &&
1182 push @values, ( $field => $tax_override{$1} );
1183 $field =~ /^taxproductnum$/ &&
1184 push @values, ( $field => $object->taxproductnum );
1185 $field =~ /^tax_override$/ &&
1186 push @values, ( $field => $tax_override{''} );
1192 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
1193 return({ @result });
1197 my $field_callback = sub {
1198 my ($cgi, $object, $fieldref) = @_;
1200 my $field = $fieldref->{field};
1201 if ($field eq 'taxproductnums') {
1202 $fieldref->{value} = join(',', @taxproductnums);
1203 } elsif ($field eq 'taxproduct_select') {
1204 $fieldref->{options} = [ '(default)', @taxproductnums ];
1205 $fieldref->{labels} = { ( '(default)' => '(default)' ),
1206 map {( $_ => ($usage_class{$_} || $_) )}
1209 $fieldref->{layer_fields} = \%taxproduct_fields;
1210 $fieldref->{layer_values_callback} = $taxproduct_values;
1214 my $discountnum_hashref = {
1216 'months' => { 'op' => '>', 'value' => 1 },