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 : '';
562 if ( $cgi->param('error') =~ / is suggested with / ) {
563 #yeah, detection is a shitty kludge, but we don't have exception objects
564 $opt->{form_init} = '<INPUT TYPE="checkbox" NAME="part_pkg_restrict_soft_override" VALUE="Y"> Override suggestion<BR><BR>';
569 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
571 my $new_object_callback = sub {
572 my( $cgi, $hashref, $fields, $opt ) = @_;
574 my $part_pkg = FS::part_pkg->new( $hashref );
575 $part_pkg->set($_ => '0')
576 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
582 sub set_report_option {
583 my($cgi, $object, $fields ) = @_; #, $opt
585 my @report_option = ();
586 foreach ($object->options) {
587 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
588 /^report_option_(\d+)$/ && (push @report_option, $1);
590 foreach ($object->part_pkg_taxoverride) {
591 $taxproductnums{$_->usage_class} = 1
595 $cgi->param('report_option', join(',', @report_option));
596 foreach my $field ( @$fields ) {
598 ref($field) eq 'HASH' &&
600 $field->{field} eq 'report_option'
602 #$field->{curr_value} = join(',', @report_option);
603 $field->{value} = join(',', @report_option);
608 my $edit_callback = sub {
609 my( $cgi, $object, $fields, $opt ) = @_;
611 $setup_show_zero_disabled = ($object->option('setup_fee') > 0) ? 1 : 0;
613 $recur_disabled = $object->freq ? 0 : 1;
615 $recur_show_zero_disabled =
617 ? $object->option('recur_fee') > 0 ? 1 : 0
621 map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
623 set_report_option( $cgi, $object, $fields);
625 %options = $object->options;
627 $object->set($_ => $object->option($_, 1))
628 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
630 foreach my $currency ( $conf->config('currencies') ) {
631 my %part_pkg_currency = $object->part_pkg_currency_options($currency);
632 $object->set( $_.'_'.$currency, $part_pkg_currency{$_} )
633 foreach keys %part_pkg_currency;
636 $pkgpart = $object->pkgpart;
638 &$splice_locale_fields(
642 my $part_pkg_msgcat = $object->part_pkg_msgcat($locale);
643 $part_pkg_msgcat ? $part_pkg_msgcat->pkgpartmsgnum : '';
647 my $part_pkg_msgcat = $object->part_pkg_msgcat($locale);
648 $part_pkg_msgcat ? $part_pkg_msgcat->pkg : '';
654 my $new_callback = sub {
655 my( $cgi, $object, $fields ) = @_;
657 my $conf = new FS::Conf;
659 if ( $conf->exists('agent_defaultpkg') ) {
660 @agent_type = map {$_->typenum} qsearch('agent_type', { 'disabled'=>'' });
663 $options{'suspend_bill'}=1 if $conf->exists('part_pkg-default_suspend_bill');
665 &$splice_locale_fields($fields, '', '');
669 my $clone_callback = sub {
670 my( $cgi, $object, $fields, $opt ) = @_;
672 if ( $cgi->param('pkgnum') ) {
674 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
675 $object->agentnum( $cust_pkg->cust_main->agentnum );
677 $opt->{action} = 'Custom';
679 #my $part_pkg = $clone_part_pkg->clone;
680 #this is all clone does anyway
681 $object->custom('Y');
683 $object->disabled('Y');
685 } else { #when explicitly cloning, not customizing
688 map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
692 set_report_option( $cgi, $object, $fields);
694 %options = $object->options;
696 $object->set($_ => $options{$_})
697 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
699 foreach my $currency ( $conf->config('currencies') ) {
700 my %part_pkg_currency = $object->part_pkg_currency_options($currency);
701 $object->set( $_.'_'.$currency, $part_pkg_currency{$_} )
702 foreach keys %part_pkg_currency;
705 $recur_disabled = $object->freq ? 0 : 1;
707 &$splice_locale_fields(
712 my $part_pkg_msgcat = $object->part_pkg_msgcat($locale);
713 $part_pkg_msgcat ? $part_pkg_msgcat->pkg : '';
718 my $discount_error_callback = sub {
719 my( $cgi, $object ) = @_;
721 if ( /^discountnum(\d+)$/ &&
722 ( my $discountnum = $cgi->param("discountnum$1") ) )
724 new FS::part_pkg_discount {
725 'pkgpart' => $object->pkgpart,
726 'discountnum' => $discountnum,
735 my $usageprice_error_callback = sub {
736 my( $cgi, $object ) = @_;
738 if ( /^usagepricepart(\d+)_price$/
739 && $cgi->param("usagepricepart$1_price") )
741 new FS::part_pkg_usageprice {
742 'usagepricepart' => $cgi->param("usagepricepart$1"),
743 'pkgpart' => $object->pkgpart,
744 'price' => scalar($cgi->param("usagepricepart$1_price")),
746 'action' => scalar($cgi->param("usagepricepart$1_action")),
747 'target' => scalar($cgi->param("usagepricepart$1_target")),
748 'amount' => scalar($cgi->param("usagepricepart$1_amount")),
757 my $m2_error_callback_maker = sub {
758 my $link_type = shift; #yay closures
760 my( $cgi, $object ) = @_;
763 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
764 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
767 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
769 new FS::part_pkg_link {
770 'link_type' => $link_type,
771 'src_pkgpart' => $object->pkgpart,
772 'dst_pkgpart' => $dst,
783 my $javascript = <<'END';
784 <SCRIPT TYPE="text/javascript">
786 function freq_changed(what) {
787 var freq = what.options[what.selectedIndex].value;
790 what.form.recur_fee.disabled = true;
791 what.form.recur_fee.style.backgroundColor = '#dddddd';
792 what.form.recur_show_zero.disabled = true;
793 //what.form.recur_show_zero.style.backgroundColor= '#dddddd';
795 what.form.recur_fee.disabled = false;
796 what.form.recur_fee.style.backgroundColor = '#ffffff';
797 recur_changed( what.form.recur_fee );
798 //what.form.recur_show_zero.style.backgroundColor= '#ffffff';
803 function setup_changed(what) {
804 var setup = what.value;
805 if ( parseFloat(setup) == 0 ) {
806 what.form.setup_show_zero.disabled = false;
808 what.form.setup_show_zero.disabled = true;
812 function recur_changed(what) {
813 var recur = what.value;
814 if ( parseFloat(recur) == 0 ) {
815 what.form.recur_show_zero.disabled = false;
817 what.form.recur_show_zero.disabled = true;
821 function agent_changed(what) {
824 if ( what.type == 'select-one' ) {
825 agentnum = what.options[what.selectedIndex].value;
827 agentnum = what.value;
830 if ( agentnum == 0 ) {
831 what.form.agent_type.disabled = false;
832 //what.form.agent_type.style.backgroundColor = '#ffffff';
833 what.form.agent_type.style.visibility = '';
835 what.form.agent_type.disabled = true;
836 //what.form.agent_type.style.backgroundColor = '#dddddd';
837 what.form.agent_type.style.visibility = 'hidden';
842 function aux_planchanged(what) { //?
844 var plan = what.options[what.selectedIndex].value;
846 var term_table = document.getElementById('TableNumber8') // XXX NOT ROBUST
847 if ( plan == 'flat' || plan == 'prorate' || plan == 'subscription' ) {
848 //term_table.disabled = false;
849 //term_table.style.visibility = '';
850 term_table.style.display = '';
852 //term_table.disabled = true;
853 //term_table.style.visibility = 'hidden';
854 term_table.style.display = 'none';
857 var currency_regex = /^(setup|recur)_fee_[A-Z]{3}$/;
860 for ( var i=0; i < form.length; i++ ) {
861 if ( currency_regex.test(form[i].name) ) {
862 if ( plan == 'currency_fixed' ) {
863 form[i].disabled = false;
865 form[i].disabled = true;
872 // some magic to make "supplemental packages" less obvious
873 var supp_pkg_rows = [];
874 function show_supp_pkgs_click() {
875 supp_pkg_rows[0].style.display = '';
877 this.style.backgroundColor = '';
878 this.style.border = '';
879 this.style.padding = '';
882 function hide_supp_pkgs() {
883 var all_selects = document.getElementsByTagName('select');
884 for (var i=0; i < all_selects.length; i++) {
885 if ( all_selects[i].id.match(/^supp_dst_pkgpart/) ) {
886 supp_pkg_rows.push( all_selects[i].parentNode.parentNode );
889 if ( supp_pkg_rows.length == 1 ) {
890 // there are none configured, so hide the row to create a new one
891 supp_pkg_rows[0].style.display = 'none';
892 var button = document.getElementById('show_supp_pkgs');
893 button.onclick = show_supp_pkgs_click;
894 button.style.backgroundColor = '#cccccc';
895 button.style.border = '1px solid #7e0079';
896 button.style.padding = '1px';
900 function finish_edit_fcc(id) {
902 show_fcc_options(id); // refresh the display
908 'Changing the setup or recurring fee will create a new package definition. '.
911 $javascript .= "function confirm_submit(f) {";
912 if ( $conf->exists('part_pkg-lineage') ) {
915 var fields = Array('setup_fee','recur_fee');
916 for(var i=0; i < fields.length; i++) {
917 if ( f[fields[i]].value != f[fields[i]].defaultValue ) {
918 return confirm('$warning');
928 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
930 tie my %plan_labels, 'Tie::IxHash',
931 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
934 my $html_bottom = sub {
937 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
939 my $layer_callback = sub {
942 my $html = ntable("#cccccc",2);
946 # <TD ALIGN="right">Recurring fee frequency </TD>
947 # <TD><SELECT NAME="freq">
950 #my @freq = keys %freq;
951 #@freq = grep { /^\d+$/ } @freq
952 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
953 #foreach my $freq ( @freq ) {
954 # $html .= qq(<OPTION VALUE="$freq");
955 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
956 # $html .= ">$freq{$freq}";
959 #$html .= '</SELECT></TD></TR>';
961 my $href = $plans{$layer}->{'fields'};
962 my @fields = exists($plans{$layer}->{'fieldorder'})
963 ? @{$plans{$layer}->{'fieldorder'}}
966 # hash of dependencies for each of the Pricing Plan fields.
967 # make sure NOT to use double-quotes inside the 'msg' value.
969 'unused_credit_suspend' => {
970 'msg' => q|You must set the 'suspend_credit_type' option in Configuration->Settings to gain access to this option.|,
972 my $conf = new FS::conf;
973 my @conf_info = qsearch('conf', { 'name' => 'suspend_credit_type' } );
974 return 1 if (exists($conf_info[0]) && $conf_info[0]->{Hash}{value});
978 'unused_credit_cancel' => {
979 'msg' => q|You must set the 'cancel_credit_type' option in Configuration->Settings to gain access to this option.|,
981 my $conf = new FS::conf;
982 my @conf_info = qsearch('conf', { 'name' => 'cancel_credit_type' } );
983 return 1 if (exists($conf_info[0]) && $conf_info[0]->{Hash}{value});
989 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
991 if(!exists($href->{$field})) {
993 warn "nonexistent part_pkg option: '$field'\n";
996 if ( exists($href->{$field}->{display_if}) ) {
997 my %args = ( 'plan' => $layer ); # anything else?
998 my $display = &{ $href->{$field}->{display_if} }(%args);
1002 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>';
1004 my $format = sub { shift };
1005 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
1007 #XXX these should use elements/ fields... (or this whole thing should
1008 #just use layer_fields instead of layer_callback)
1010 if (exists($dependencies->{$field}) && !$dependencies->{$field}{'are_met'}()) {
1011 $html .= q!<span title="!.$dependencies->{$field}{'msg'}.q!">N/A</span>!;
1013 } elsif ( ! exists($href->{$field}{'type'}) ) {
1015 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
1016 ( exists($options{$field})
1017 ? &$format($options{$field})
1018 : $href->{$field}{'default'} ).
1021 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
1023 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
1024 ( exists($options{$field}) && $options{$field}
1029 } elsif ( $href->{$field}{'type'} eq 'select-rate' ) {
1031 $html .= include('/elements/select-rate.html',
1032 'field' => $layer.'__'.$field,
1033 'curr_value' => $options{$field},
1034 map { $_ => $href->{$field}{$_} }
1035 grep { $_ !~ /^(name|type)$/ }
1036 keys %{ $href->{$field} }
1039 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
1042 $html .= ' MULTIPLE'
1043 if $href->{$field}{'type'} eq 'select_multiple';
1044 $html .= qq! NAME="${layer}__$field">!;
1046 $html .= '<OPTION VALUE="">'. $href->{$field}{'empty_label'}
1047 if exists($href->{$field}{'disable_empty'})
1048 && ! $href->{$field}{'disable_empty'};
1050 if ( $href->{$field}{'select_table'} ) {
1051 foreach my $record (
1052 qsearch( $href->{$field}{'select_table'},
1053 $href->{$field}{'select_hash'} )
1055 my $value = $record->getfield($href->{$field}{'select_key'});
1056 $html .= qq!<OPTION VALUE="$value"!.
1057 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
1061 '>'. $record->getfield($href->{$field}{'select_label'});
1063 } elsif ( $href->{$field}{'select_options'} ) {
1064 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
1065 my $label = $href->{$field}{'select_options'}{$key};
1066 $html .= qq!<OPTION VALUE="$key"!.
1067 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
1075 $html .= '<font color="#ff0000">warning: '.
1076 "don't know how to retreive options for $field select field".
1079 $html .= '</SELECT>';
1081 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
1084 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
1086 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
1087 my $label = $href->{$field}{'options'}{$key};
1088 $html .= qq!$radio VALUE="$key"!.
1089 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
1098 $html .= '</TD></TR>';
1100 $html .= '</TABLE>';
1102 $html .= qq(<INPUT TYPE="hidden" NAME="${layer}__OPTIONS" VALUE=").
1103 join(',', keys %{ $href } ). '">';
1109 my %selectlayers = (
1111 options => [ keys %plan_labels ],
1112 labels => \%plan_labels,
1113 curr_value => $object->plan,
1114 layer_callback => $layer_callback,
1115 onchange => 'aux_planchanged(what);',
1119 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
1120 '<SCRIPT TYPE="text/javascript">'.
1121 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
1124 "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
1127 $return .= '</SCRIPT>';
1133 my %usage_class = map { ($_->classnum => $_->classname) }
1134 qsearch('usage_class', {});
1135 $usage_class{setup} = 'Setup';
1136 $usage_class{recur} = 'Recurring';
1138 my %taxproduct_fields = ();
1139 my $end_callback = sub {
1140 my( $cgi, $object, $fields, $opt ) = @_;
1142 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
1144 if ( $object->pkgpart ) {
1145 foreach my $usage_class ( '', @taxproductnums ) {
1146 $tax_override{$usage_class} =
1147 join (",", map $_->taxclassnum,
1148 qsearch( 'part_pkg_taxoverride', {
1149 'pkgpart' => $object->pkgpart,
1150 'usage_class' => $usage_class,
1156 %taxproduct_fields =
1157 map { $_ => [ "taxproductnum_$_",
1158 { type => 'select-taxproduct',
1159 #label => "$usage_class{$_} tax product",
1162 { type => 'select-taxoverride' }
1167 $taxproduct_fields{'(default)'} =
1168 [ 'taxproductnum', { type => 'select-taxproduct',
1169 #label => 'Default tax product',
1171 'tax_override', { type => 'select-taxoverride' },
1175 my $taxproduct_values = sub {
1176 my ($cgi, $object, $flags) = @_;
1178 sub { my $layer = shift;
1179 my @fields = @{$taxproduct_fields{$layer}};
1182 my $field = shift @fields;
1184 $field =~ /^taxproductnum_\w+$/ &&
1185 push @values, ( $field => $options{"usage_$field"} );
1186 $field =~ /^tax_override_(\w+)$/ &&
1187 push @values, ( $field => $tax_override{$1} );
1188 $field =~ /^taxproductnum$/ &&
1189 push @values, ( $field => $object->taxproductnum );
1190 $field =~ /^tax_override$/ &&
1191 push @values, ( $field => $tax_override{''} );
1197 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
1198 return({ @result });
1202 my $field_callback = sub {
1203 my ($cgi, $object, $fieldref) = @_;
1205 my $field = $fieldref->{field};
1206 if ($field eq 'taxproductnums') {
1207 $fieldref->{value} = join(',', @taxproductnums);
1208 } elsif ($field eq 'taxproduct_select') {
1209 $fieldref->{options} = [ '(default)', @taxproductnums ];
1210 $fieldref->{labels} = { ( '(default)' => '(default)' ),
1211 map {( $_ => ($usage_class{$_} || $_) )}
1214 $fieldref->{layer_fields} = \%taxproduct_fields;
1215 $fieldref->{layer_values_callback} = $taxproduct_values;
1219 my $discountnum_hashref = {
1221 'months' => { 'op' => '>', 'value' => 1 },