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)',
72 'adjourn_months' => 'Suspend the package after ',
73 'contract_end_months' => 'Contract ends after ',
74 'expire_months' => 'Cancel the package after ',
75 'change_to_pkgpart'=> 'and replace it with ',
79 { field=>'clone', type=>'hidden',
80 curr_value_callback =>
81 sub { shift->param('clone') },
83 { field=>'pkgnum', type=>'hidden',
84 curr_value_callback =>
85 sub { shift->param('pkgnum') },
88 { field=>'custom', type=>'hidden' },
89 { field=>'family_pkgpart', type=>'hidden' },
90 { field=>'successor', type=>'hidden' },
92 { type => 'columnstart' },
100 {field=>'comment', type=>'text', size=>40 }, #32
101 { field => 'agentnum',
102 type => 'select-agent',
103 disable_empty => ! $acl_edit_global,
104 empty_label => '(global)',
105 onchange => 'agent_changed',
107 {field=>'classnum', type=>'select-pkg_class' },
108 ( $conf->exists('pkg-addon_classnum')
109 ? ( { field=>'addon_classnum',
110 type =>'select-pkg_class',
115 {field=>'disabled', type=>$disabled_type, value=>'Y'},
116 {field=>'disable_line_item_date_ranges', type=>$disabled_type, value=>'Y'},
117 { field => 'start_on_hold',
122 { type => 'tablebreak-tr-title',
123 value => 'Pricing', #better name?
126 type => 'selectlayers-select',
127 options => [ keys %plan_labels ],
128 labels => \%plan_labels,
129 onchange => 'aux_planchanged(what);',
131 { field => 'setup_fee',
133 onchange => 'setup_changed',
135 { field => 'setup_show_zero',
138 disabled => sub { $setup_show_zero_disabled },
140 ( map { +{ field => "setup_fee_$_",
142 prefix=> currency_symbol($_, SYM_HTML),
146 sort $conf->config('currencies')
149 type => 'part_pkg_freq',
150 onchange => 'freq_changed',
152 { field => 'recur_fee',
154 disabled => sub { $recur_disabled },
155 onchange => 'recur_changed',
157 { field => 'recur_show_zero',
160 disabled => sub { $recur_show_zero_disabled },
162 ( map { +{ field => "recur_fee_$_",
164 prefix=> currency_symbol($_, SYM_HTML),
168 sort $conf->config('currencies')
171 ( $conf->exists('part_pkg-delay_start')
172 ? ( { type => 'tablebreak-tr-title',
173 value => 'Delayed start',
175 { field => 'delay_start',
176 type => 'text', size => 6 },
181 { type => 'tablebreak-tr-title',
182 value => 'Limited duration',
184 { field => 'adjourn_months',
185 type => 'select-months',
187 { field => 'contract_end_months',
188 type => 'select-months',
190 { field => 'expire_months',
191 type => 'select-expire_months',
193 { field => 'change_to_pkgpart',
194 type => 'select-part_pkg',
195 extra_sql => sub { $pkgpart
196 ? "AND pkgpart != $pkgpart"
199 empty_label => 'no package',
205 #recurring fee (auto-disable)
207 { type => 'columnnext' },
209 {type=>'justtitle', value=>'Taxation' },
210 {field=>'setuptax', type=>'checkbox', value=>'Y'},
211 {field=>'recurtax', type=>'checkbox', value=>'Y'},
212 {field=>'taxclass', type=>'select-taxclass' },
213 { field => 'taxproductnums',
215 value => join(',', @taxproductnums),
217 #{ field => 'taxproduct_select',
218 # type => 'selectlayers',
219 # options => [ '(default)', @taxproductnums ],
220 # curr_value => '(default)',
221 # labels => { ( '(default)' => '(default)' ),
222 # map {($_=>$usage_class{$_})}
225 # layer_fields => \%taxproduct_fields,
226 # layer_values_callback => $taxproduct_values,
227 # layers_only => !$taxproducts,
228 # cell_style => ( !$taxproducts
233 { field => 'taxproductnum',
234 type => 'part_pkg-taxproducts',
235 include_opt_callback =>
236 sub { pkgpart => $_[0]->pkgpart },
239 { type => 'tablebreak-tr-title',
240 value => 'Promotions', #better name?
242 { field=>'promo_code', type=>'text', size=>15 },
244 { type => 'tablebreak-tr-title',
245 value => 'Cost tracking', #better name?
248 ( $curuser->access_right('Edit package definition costs')
249 ? ( { field=>'setup_cost', type=>'money', },
250 { field=>'recur_cost', type=>'money', },
252 : ( { field=>'setup_cost', type=>'fixed', },
253 { field=>'recur_cost', type=>'fixed', },
257 { type => 'columnnext' },
259 {type=>'justtitle', value=>'Agent (reseller) types' },
261 { field => 'agent_type',
262 type => 'select-agent_type',
263 disabled => ! $acl_edit_global,
264 element_etc => 'size="10"',
265 multiple => '1', #cause edit.html is dum
266 curr_value_callback => sub {
267 my($cgi, $object, $field) = @_;
268 #in the other callbacks..? hmm.
274 { type => 'tablebreak-tr-title',
275 value => 'FCC Form 477 information',
277 { field => 'fcc_options_string',
278 type => 'input-fcc_options',
279 curr_value_callback => sub {
280 my ($cgi, $part_pkg, $fref) = @_;
281 if ( $cgi->param('fcc_options_string') ) {
283 return $cgi->param('fcc_options_string');
286 %hash = $part_pkg->fcc_options
287 if ($part_pkg->pkgpart);
288 return encode_json(\%hash);
294 { type => 'tablebreak-tr-title',
295 value => 'External Links', #better name?
297 { field=>'agent_pkgpartid', type=>'text', size=>21 },
299 { type => 'tablebreak-tr-title',
300 value => 'Line-item revenue recogition', #better name?
302 { field=>'pay_weight', type=>'text', size=>6 },
303 { field=>'credit_weight', type=>'text', size=>6 },
305 { type => 'columnend' },
307 { type => 'tablebreak-tr-title',
308 value => 'Usage pricing add-ons', #better name? just 'Usage pricing' ? there's also CDR usage pricing, RADIUS usage pricing, etc :/
310 { 'field' => 'usagepricepart',
311 'type' => 'part_pkg_usageprice',
312 'o2m_table' => 'part_pkg_usageprice',
314 'm2_error_callback' => $usageprice_error_callback,
317 { 'type' => $report_option ? 'tablebreak-tr-title'
319 'value' => 'Optional report classes',
320 'field' => 'census_title',
322 { 'field' => 'report_option',
323 'type' => $report_option ? 'select-table'
325 'table' => 'part_pkg_report_option',
326 'name_col' => 'name',
327 'hashref' => { 'disabled' => '' },
331 { 'type' => 'tablebreak-tr-title',
332 'value' => 'Term discounts',
334 { 'field' => 'discountnum',
335 'type' => 'select-table',
336 'table' => 'discount',
337 'name_col' => 'name',
338 'hashref' => { %$discountnum_hashref },
339 #'extra_sql' => 'AND (months IS NOT NULL OR months != 0)',
340 'empty_label'=> 'Select discount',
341 'm2_label' => 'Offer discounts for longer terms',
342 'm2m_method' => 'part_pkg_discount',
343 'm2m_dstcol' => 'discountnum',
344 'm2_error_callback' => $discount_error_callback,
347 { 'type' => 'tablebreak-tr-title',
348 'value' => 'Pricing add-ons',
351 { 'field' => 'bill_dst_pkgpart',
352 'type' => 'select-part_pkg',
353 'extra_sql' => sub { $pkgpart
354 ? "AND pkgpart != $pkgpart"
357 'label_callback' => sub { shift->pkg_comment_only },
358 'm2_label' => 'Include line item(s) from package',
359 'm2m_method' => 'bill_part_pkg_link',
360 'm2m_dstcol' => 'dst_pkgpart',
361 'm2_error_callback' =>
362 &{$m2_error_callback_maker}('bill'),
363 'm2_fields' => [ { 'field' => 'hidden',
364 'type' => 'checkbox',
372 { type => 'tablebreak-tr-title',
375 { type => 'pkg_svc', },
377 { 'field' => 'svc_dst_pkgpart',
378 'label' => 'Also include services from package: ',
379 'type' => 'select-part_pkg',
380 'extra_sql' => sub { $pkgpart
381 ? "AND pkgpart != $pkgpart"
384 'label_callback' => sub { shift->pkg_comment_only },
385 'm2_label' => 'Include services of package: ',
386 'm2m_method' => 'svc_part_pkg_link',
387 'm2m_dstcol' => 'dst_pkgpart',
388 'm2_error_callback' =>
389 &{$m2_error_callback_maker}('svc'),
392 { 'type' => 'tablebreak-tr-title',
393 'value' => 'Supplemental packages',
395 'include_opt_callback' => sub {
396 'id' => 'show_supp_pkgs',
399 { 'field' => 'supp_dst_pkgpart',
400 'type' => 'select-part_pkg',
401 'label_callback' => sub { shift->pkg_comment_only },
402 'm2_label' => 'When ordering package, also order',
403 'm2m_method' => 'supp_part_pkg_link',
404 'm2m_dstcol' => 'dst_pkgpart',
405 'm2_error_callback' =>
406 &{$m2_error_callback_maker}('supp'),
409 { type => 'tablebreak-tr-title',
410 value => 'Price plan options',
417 my $curuser = $FS::CurrentUser::CurrentUser;
419 my $edit_global = 'Edit global package definitions';
420 my $acl_edit = $curuser->access_right('Edit package definitions');
421 my $acl_edit_global = $curuser->access_right($edit_global);
423 my $acl_edit_either = $acl_edit || $acl_edit_global;
425 my $begin_callback = sub {
426 my( $cgi, $fields, $opt ) = @_;
428 unless $acl_edit_either
429 || ( $cgi->param('pkgnum')
430 && $curuser->access_right('Customize customer package')
434 my $disabled_type = $acl_edit_either ? 'checkbox' : 'hidden';
436 #arg. access rights for cloning are Hard.
437 # on the one hand we don't really want cloning (customizing a package) to fail
438 # for want of finding the source package in normal usage
439 # on the other hand, we don't want people using the clone link to be able to
441 my $agent_clone_extra_sql =
442 ' ( '. FS::part_pkg->curuser_pkgs_sql.
443 " OR ( part_pkg.custom = 'Y' ) ".
446 my $conf = new FS::Conf;
447 my $taxproducts = $conf->config('tax_data_vendor') ne '';
449 my $fcc_opts = $conf->exists('part_pkg-show_fcc_options');
451 my @locales = grep { ! /^en_/i } $conf->config('available-locales'); #should filter from the default locale lang instead of en_
452 my %locale_labels = map {
453 ( $_ => 'Package -- '. FS::Locales->description($_) )
456 sort { $locale_labels{$a} cmp $locale_labels{$b} }
460 my %locale_field_labels = (
462 ( 'pkgpartmsgnum'. $n++. '_pkg' => $locale_labels{$_} );
467 my $sth = dbh->prepare("SELECT COUNT(*) FROM part_pkg_report_option".
468 " WHERE disabled IS NULL OR disabled = '' ")
470 $sth->execute or die $sth->errstr;
471 my $report_option = $sth->fetchrow_arrayref->[0];
474 # - tr-part_pkg_freq: month_increments_only (from price plans)
476 # - test errors cloning
477 # - test custom pricing
478 # - move the selectlayer divs away from lame layer_callback
480 #my ($query) = $cgi->keywords;
485 my %tax_override = ();
487 my %taxproductnums = map { ($_->classnum => 1) }
488 qsearch('usage_class', { 'disabled' => '' });
489 my @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
492 my $recur_disabled = 1;
493 my $setup_show_zero_disabled = 0;
494 my $recur_show_zero_disabled = 1;
498 my $splice_locale_fields = sub {
499 my( $fields, $pkey_value_callback, $pkg_value_callback ) = @_;
502 my @locale_fields = (
504 my $pkey_value= $pkey_value_callback ? &$pkey_value_callback($_) : '';
505 my $pkg_value = $pkg_value_callback
506 ? $pkg_value_callback eq 'cgiparam'
507 ? $cgi->param('pkgpartmsgnum'. $n. '_pkg')
508 : &$pkg_value_callback($_)
511 { field => 'pkgpartmsgnum'. $n,
513 value => $pkey_value,
515 { field => 'pkgpartmsgnum'. $n. '_locale',
519 { field => 'pkgpartmsgnum'. $n++. '_pkg',
530 splice(@$fields, 7, 0, @locale_fields); #XXX 7 is arbitrary above
534 my $error_callback = sub {
535 my($cgi, $object, $fields, $opt ) = @_;
537 (@agent_type) = $cgi->param('agent_type');
539 $opt->{action} = 'Custom' if $cgi->param('pkgnum');
541 $setup_show_zero_disabled = ($cgi->param('setup_fee') > 0) ? 1 : 0;
543 $recur_disabled = $cgi->param('freq') ? 0 : 1;
544 $recur_show_zero_disabled =
546 ? $cgi->param('recur_fee') > 0 ? 1 : 0
549 foreach ($cgi->param) {
550 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
552 $tax_override{''} = $cgi->param('tax_override');
553 $tax_override{$_} = $cgi->param('tax_override_$_')
554 foreach(grep { /^tax_override_(\w+)$/ } $cgi->param);
556 #some false laziness w/process
557 $cgi->param('plan') =~ /^(\w+)$/ or die 'unparsable plan';
559 my $options = $cgi->param($plan."__OPTIONS");
560 my @options = split(',', $options);
562 map { my $optionname = $_;
563 my $param = $plan."__$optionname";
564 my $value = join(', ', $cgi->param($param));
565 ( $optionname => $value );
569 $object->set($_ => scalar($cgi->param($_)) )
570 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
572 foreach my $currency ( $conf->config('currencies') ) {
573 my %part_pkg_currency = $object->part_pkg_currency_options($currency);
574 foreach (qw( setup_fee recur_fee )) {
575 my $param = $_.'_'.$currency;
576 $object->set( $param, $cgi->param($param) );
580 $pkgpart = $object->pkgpart;
582 &$splice_locale_fields(
586 my $part_pkg_msgcat = $object->part_pkg_msgcat($locale);
587 $part_pkg_msgcat ? $part_pkg_msgcat->pkgpartmsgnum : '';
592 if ( $cgi->param('error') =~ / is suggested with / ) {
593 #yeah, detection is a shitty kludge, but we don't have exception objects
594 $opt->{form_init} = '<INPUT TYPE="checkbox" NAME="part_pkg_restrict_soft_override" VALUE="Y"> Override suggestion<BR><BR>';
599 my $new_hashref_callback = sub { { 'plan' => 'flat' }; };
601 my $new_object_callback = sub {
602 my( $cgi, $hashref, $fields, $opt ) = @_;
604 my $part_pkg = FS::part_pkg->new( $hashref );
605 $part_pkg->set($_ => '0')
606 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
612 sub set_report_option {
613 my($cgi, $object, $fields ) = @_; #, $opt
615 my @report_option = ();
616 foreach ($object->options) {
617 /^usage_taxproductnum_(\d+)$/ && ($taxproductnums{$1} = 1);
618 /^report_option_(\d+)$/ && (push @report_option, $1);
620 foreach ($object->part_pkg_taxoverride) {
621 $taxproductnums{$_->usage_class} = 1
625 $cgi->param('report_option', join(',', @report_option));
626 foreach my $field ( @$fields ) {
628 ref($field) eq 'HASH' &&
630 $field->{field} eq 'report_option'
632 #$field->{curr_value} = join(',', @report_option);
633 $field->{value} = join(',', @report_option);
638 my $edit_callback = sub {
639 my( $cgi, $object, $fields, $opt ) = @_;
641 $setup_show_zero_disabled = ($object->option('setup_fee') > 0) ? 1 : 0;
643 $recur_disabled = $object->freq ? 0 : 1;
645 $recur_show_zero_disabled =
647 ? $object->option('recur_fee') > 0 ? 1 : 0
651 map {$_->typenum} qsearch('type_pkgs', { 'pkgpart' => $object->pkgpart } );
653 set_report_option( $cgi, $object, $fields);
655 %options = $object->options;
657 $object->set($_ => $object->option($_, 1))
658 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
660 foreach my $currency ( $conf->config('currencies') ) {
661 my %part_pkg_currency = $object->part_pkg_currency_options($currency);
662 $object->set( $_.'_'.$currency, $part_pkg_currency{$_} )
663 foreach keys %part_pkg_currency;
666 $pkgpart = $object->pkgpart;
668 &$splice_locale_fields(
672 my $part_pkg_msgcat = $object->part_pkg_msgcat($locale);
673 $part_pkg_msgcat ? $part_pkg_msgcat->pkgpartmsgnum : '';
677 my $part_pkg_msgcat = $object->part_pkg_msgcat($locale);
678 $part_pkg_msgcat ? $part_pkg_msgcat->pkg : '';
684 my $new_callback = sub {
685 my( $cgi, $object, $fields ) = @_;
687 my $conf = new FS::Conf;
689 if ( $conf->exists('agent_defaultpkg') ) {
690 @agent_type = map {$_->typenum} qsearch('agent_type', { 'disabled'=>'' });
693 $options{'suspend_bill'}=1 if $conf->exists('part_pkg-default_suspend_bill');
695 &$splice_locale_fields($fields, '', '');
699 my $clone_callback = sub {
700 my( $cgi, $object, $fields, $opt ) = @_;
702 if ( $cgi->param('pkgnum') ) {
704 my $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $cgi->param('pkgnum') } );
705 $object->agentnum( $cust_pkg->cust_main->agentnum );
707 $opt->{action} = 'Custom';
709 #my $part_pkg = $clone_part_pkg->clone;
710 #this is all clone does anyway
711 $object->custom('Y');
713 $object->disabled('Y');
715 } else { #when explicitly cloning, not customizing
718 map {$_->typenum} qsearch('type_pkgs',{ 'pkgpart' => $object->pkgpart } );
722 set_report_option( $cgi, $object, $fields);
724 %options = $object->options;
726 $object->set($_ => $options{$_})
727 foreach (qw( setup_fee recur_fee disable_line_item_date_ranges ));
729 foreach my $currency ( $conf->config('currencies') ) {
730 my %part_pkg_currency = $object->part_pkg_currency_options($currency);
731 $object->set( $_.'_'.$currency, $part_pkg_currency{$_} )
732 foreach keys %part_pkg_currency;
735 $recur_disabled = $object->freq ? 0 : 1;
737 &$splice_locale_fields(
742 my $part_pkg_msgcat = $object->part_pkg_msgcat($locale);
743 $part_pkg_msgcat ? $part_pkg_msgcat->pkg : '';
748 my $discount_error_callback = sub {
749 my( $cgi, $object ) = @_;
751 if ( /^discountnum(\d+)$/ &&
752 ( my $discountnum = $cgi->param("discountnum$1") ) )
754 new FS::part_pkg_discount {
755 'pkgpart' => $object->pkgpart,
756 'discountnum' => $discountnum,
765 my $usageprice_error_callback = sub {
766 my( $cgi, $object ) = @_;
768 if ( /^usagepricepart(\d+)_price$/
769 && $cgi->param("usagepricepart$1_price") )
771 new FS::part_pkg_usageprice {
772 'usagepricepart' => $cgi->param("usagepricepart$1"),
773 'pkgpart' => $object->pkgpart,
774 'price' => scalar($cgi->param("usagepricepart$1_price")),
776 'action' => scalar($cgi->param("usagepricepart$1_action")),
777 'target' => scalar($cgi->param("usagepricepart$1_target")),
778 'amount' => scalar($cgi->param("usagepricepart$1_amount")),
787 my $m2_error_callback_maker = sub {
788 my $link_type = shift; #yay closures
790 my( $cgi, $object ) = @_;
793 if ( /^${link_type}_dst_pkgpart(\d+)$/ &&
794 ( my $dst = $cgi->param("${link_type}_dst_pkgpart$1") ) )
797 my $hidden = $cgi->param("${link_type}_dst_pkgpart__hidden$1")
799 new FS::part_pkg_link {
800 'link_type' => $link_type,
801 'src_pkgpart' => $object->pkgpart,
802 'dst_pkgpart' => $dst,
813 my $javascript = <<'END';
814 <SCRIPT TYPE="text/javascript">
816 function freq_changed(what) {
817 var freq = what.options[what.selectedIndex].value;
820 what.form.recur_fee.disabled = true;
821 what.form.recur_fee.style.backgroundColor = '#dddddd';
822 what.form.recur_show_zero.disabled = true;
823 //what.form.recur_show_zero.style.backgroundColor= '#dddddd';
825 what.form.recur_fee.disabled = false;
826 what.form.recur_fee.style.backgroundColor = '#ffffff';
827 recur_changed( what.form.recur_fee );
828 //what.form.recur_show_zero.style.backgroundColor= '#ffffff';
833 function setup_changed(what) {
834 var setup = what.value;
835 if ( parseFloat(setup) == 0 ) {
836 what.form.setup_show_zero.disabled = false;
838 what.form.setup_show_zero.disabled = true;
842 function recur_changed(what) {
843 var recur = what.value;
844 if ( parseFloat(recur) == 0 ) {
845 what.form.recur_show_zero.disabled = false;
847 what.form.recur_show_zero.disabled = true;
851 function agent_changed(what) {
854 if ( what.type == 'select-one' ) {
855 agentnum = what.options[what.selectedIndex].value;
857 agentnum = what.value;
860 if ( agentnum == 0 ) {
861 what.form.agent_type.disabled = false;
862 //what.form.agent_type.style.backgroundColor = '#ffffff';
863 what.form.agent_type.style.visibility = '';
865 what.form.agent_type.disabled = true;
866 //what.form.agent_type.style.backgroundColor = '#dddddd';
867 what.form.agent_type.style.visibility = 'hidden';
872 function aux_planchanged(what) { //?
874 var plan = what.options[what.selectedIndex].value;
876 var term_table = document.getElementById('TableNumber8') // XXX NOT ROBUST
877 if ( plan == 'flat' || plan == 'prorate' || plan == 'subscription' ) {
878 //term_table.disabled = false;
879 //term_table.style.visibility = '';
880 term_table.style.display = '';
882 //term_table.disabled = true;
883 //term_table.style.visibility = 'hidden';
884 term_table.style.display = 'none';
887 var currency_regex = /^(setup|recur)_fee_[A-Z]{3}$/;
890 for ( var i=0; i < form.length; i++ ) {
891 if ( currency_regex.test(form[i].name) ) {
892 if ( plan == 'currency_fixed' ) {
893 form[i].disabled = false;
895 form[i].disabled = true;
902 // some magic to make "supplemental packages" less obvious
903 var supp_pkg_rows = [];
904 function show_supp_pkgs_click() {
905 supp_pkg_rows[0].style.display = '';
907 this.style.backgroundColor = '';
908 this.style.border = '';
909 this.style.padding = '';
912 function hide_supp_pkgs() {
913 var all_selects = document.getElementsByTagName('select');
914 for (var i=0; i < all_selects.length; i++) {
915 if ( all_selects[i].id.match(/^supp_dst_pkgpart/) ) {
916 supp_pkg_rows.push( all_selects[i].parentNode.parentNode );
919 if ( supp_pkg_rows.length == 1 ) {
920 // there are none configured, so hide the row to create a new one
921 supp_pkg_rows[0].style.display = 'none';
922 var button = document.getElementById('show_supp_pkgs');
923 button.onclick = show_supp_pkgs_click;
924 button.style.backgroundColor = '#cccccc';
925 button.style.border = '1px solid #7e0079';
926 button.style.padding = '1px';
930 function finish_edit_fcc(id) {
932 show_fcc_options(id); // refresh the display
938 'Changing the setup or recurring fee will create a new package definition. '.
941 $javascript .= "function confirm_submit(f) {";
942 if ( $conf->exists('part_pkg-lineage') ) {
945 var fields = Array('setup_fee','recur_fee');
946 for(var i=0; i < fields.length; i++) {
947 if ( f[fields[i]].value != f[fields[i]].defaultValue ) {
948 return confirm('$warning');
958 tie my %plans, 'Tie::IxHash', %{ FS::part_pkg::plan_info() };
960 tie my %plan_labels, 'Tie::IxHash',
961 map { $_ => ( $plans{$_}->{'shortname'} || $plans{$_}->{'name'} ) }
964 my $html_bottom = sub {
967 #warn join("\n", map { "$_: $options{$_}" } keys %options ). "\n";
969 my $layer_callback = sub {
972 my $html = ntable("#cccccc",2);
976 # <TD ALIGN="right">Recurring fee frequency </TD>
977 # <TD><SELECT NAME="freq">
980 #my @freq = keys %freq;
981 #@freq = grep { /^\d+$/ } @freq
982 #XXX this bit# # if exists($plans{$layer}->{'freq'}) && $plans{$layer}->{'freq'} eq 'm';
983 #foreach my $freq ( @freq ) {
984 # $html .= qq(<OPTION VALUE="$freq");
985 # $html .= ' SELECTED' if $freq eq $part_pkg->freq;
986 # $html .= ">$freq{$freq}";
989 #$html .= '</SELECT></TD></TR>';
991 my $href = $plans{$layer}->{'fields'};
993 if ( $plans{$layer}->{'fieldorder'} ) {
994 @fields = @{ $plans{$layer}->{'fieldorder'} };
996 warn "FS::part_pkg::$layer has no fieldorder.\n";
997 @fields = keys %$href;
1000 # hash of dependencies for each of the Pricing Plan fields.
1001 # make sure NOT to use double-quotes inside the 'msg' value.
1002 my $dependencies = {
1003 'unused_credit_suspend' => {
1004 'msg' => q|You must set the 'suspend_credit_type' option in Configuration->Settings to gain access to this option.|,
1006 my $conf = new FS::conf;
1007 my @conf_info = qsearch('conf', { 'name' => 'suspend_credit_type' } );
1008 return 1 if (exists($conf_info[0]) && $conf_info[0]->{Hash}{value});
1012 'unused_credit_cancel' => {
1013 'msg' => q|You must set the 'cancel_credit_type' option in Configuration->Settings to gain access to this option.|,
1015 my $conf = new FS::conf;
1016 my @conf_info = qsearch('conf', { 'name' => 'cancel_credit_type' } );
1017 return 1 if (exists($conf_info[0]) && $conf_info[0]->{Hash}{value});
1023 foreach my $field ( grep $_ !~ /^(setup|recur)_fee$/, @fields ) {
1025 if(!exists($href->{$field})) {
1027 warn "nonexistent part_pkg option: '$field'\n";
1030 if ( exists($href->{$field}->{display_if}) ) {
1031 my %args = ( 'plan' => $layer ); # anything else?
1032 my $display = &{ $href->{$field}->{display_if} }(%args);
1036 $html .= '<TR><TD ALIGN="right">'. $href->{$field}{'name'}. '</TD><TD>
1039 my $format = sub { shift };
1040 $format = $href->{$field}{'format'} if exists($href->{$field}{'format'});
1042 #XXX these should use elements/ fields... (or this whole thing should
1043 #just use layer_fields instead of layer_callback)
1045 if (exists($dependencies->{$field}) && !$dependencies->{$field}{'are_met'}()) {
1046 $html .= q!<span title="!.$dependencies->{$field}{'msg'}.q!">N/A</span>!;
1048 } elsif ( ! exists($href->{$field}{'type'}) ) {
1050 $html .= qq!<INPUT TYPE="text" NAME="${layer}__$field" VALUE="!.
1051 ( exists($options{$field})
1052 ? &$format($options{$field})
1053 : $href->{$field}{'default'} ).
1056 } elsif ( $href->{$field}{'type'} eq 'checkbox' ) {
1058 $html .= qq!<INPUT TYPE="checkbox" NAME="${layer}__$field" VALUE=1 !.
1059 ( exists($options{$field}) && $options{$field}
1064 } elsif ( $href->{$field}{'type'} eq 'select-rate' ) {
1066 $html .= include('/elements/select-rate.html',
1067 'field' => $layer.'__'.$field,
1068 'curr_value' => $options{$field},
1069 map { $_ => $href->{$field}{$_} }
1070 grep { $_ !~ /^(name|type)$/ }
1071 keys %{ $href->{$field} }
1074 } elsif ( $href->{$field}{'type'} =~ /^select/ ) {
1077 $html .= ' MULTIPLE'
1078 if $href->{$field}{'type'} eq 'select_multiple';
1079 $html .= qq! NAME="${layer}__$field">!;
1081 $html .= '<OPTION VALUE="">'. $href->{$field}{'empty_label'}
1082 if exists($href->{$field}{'disable_empty'})
1083 && ! $href->{$field}{'disable_empty'};
1085 if ( $href->{$field}{'select_table'} ) {
1086 foreach my $record (
1087 qsearch( $href->{$field}{'select_table'},
1088 $href->{$field}{'select_hash'} )
1090 my $value = $record->getfield($href->{$field}{'select_key'});
1091 $html .= qq!<OPTION VALUE="$value"!.
1092 ( $options{$field} =~ /(^|, *)$value *(,|$)/ #?
1096 '>'. $record->getfield($href->{$field}{'select_label'});
1098 } elsif ( $href->{$field}{'select_options'} ) {
1099 foreach my $key ( keys %{ $href->{$field}{'select_options'} } ) {
1100 my $label = $href->{$field}{'select_options'}{$key};
1101 $html .= qq!<OPTION VALUE="$key"!.
1102 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
1110 $html .= '<font color="#ff0000">warning: '.
1111 "don't know how to retreive options for $field select field".
1114 $html .= '</SELECT>';
1116 } elsif ( $href->{$field}{'type'} eq 'radio' ) {
1119 qq!<INPUT TYPE="radio" NAME="${layer}__$field"!;
1121 foreach my $key ( keys %{ $href->{$field}{'options'} } ) {
1122 my $label = $href->{$field}{'options'}{$key};
1123 $html .= qq!$radio VALUE="$key"!.
1124 ( $options{$field} =~ /(^|, *)$key *(,|$)/ #?
1133 $html .= '</TD></TR>';
1135 $html .= '</TABLE>';
1137 $html .= include('/elements/hidden.html',
1138 field => $layer.'__OPTIONS',
1139 value => join(',', @fields)
1146 my %selectlayers = (
1148 options => [ keys %plan_labels ],
1149 labels => \%plan_labels,
1150 curr_value => $object->plan,
1151 layer_callback => $layer_callback,
1152 onchange => 'aux_planchanged(what);',
1156 include('/elements/selectlayers.html', %selectlayers, 'layers_only'=>1 ).
1157 '<SCRIPT TYPE="text/javascript">'.
1158 include('/elements/selectlayers.html', %selectlayers, 'js_only'=>1 );
1161 # "taxproduct_selectchanged(document.getElementById('taxproduct_select'));\n"
1164 $return .= '</SCRIPT>';
1170 my %usage_class = map { ($_->classnum => $_->classname) }
1171 qsearch('usage_class', {});
1172 $usage_class{setup} = 'Setup';
1173 $usage_class{recur} = 'Recurring';
1175 my %taxproduct_fields = ();
1176 my $end_callback = sub {
1177 my( $cgi, $object, $fields, $opt ) = @_;
1179 @taxproductnums = ( qw( setup recur ), sort (keys %taxproductnums) );
1181 if ( $object->pkgpart ) {
1182 foreach my $usage_class ( '', @taxproductnums ) {
1183 $tax_override{$usage_class} =
1184 join (",", map $_->taxclassnum,
1185 qsearch( 'part_pkg_taxoverride', {
1186 'pkgpart' => $object->pkgpart,
1187 'usage_class' => $usage_class,
1193 %taxproduct_fields =
1194 map { $_ => [ "taxproductnum_$_",
1195 { type => 'select-taxproduct',
1196 #label => "$usage_class{$_} tax product",
1199 { type => 'select-taxoverride' }
1204 $taxproduct_fields{'(default)'} =
1205 [ 'taxproductnum', { type => 'select-taxproduct',
1206 #label => 'Default tax product',
1208 'tax_override', { type => 'select-taxoverride' },
1212 my $taxproduct_values = sub {
1213 my ($cgi, $object, $flags) = @_;
1215 sub { my $layer = shift;
1216 my @fields = @{$taxproduct_fields{$layer}};
1219 my $field = shift @fields;
1221 $field =~ /^taxproductnum_\w+$/ &&
1222 push @values, ( $field => $options{"usage_$field"} );
1223 $field =~ /^tax_override_(\w+)$/ &&
1224 push @values, ( $field => $tax_override{$1} );
1225 $field =~ /^taxproductnum$/ &&
1226 push @values, ( $field => $object->taxproductnum );
1227 $field =~ /^tax_override$/ &&
1228 push @values, ( $field => $tax_override{''} );
1234 map { ( $_ => { &{$routine}($_) } ) } ( '(default)', @taxproductnums );
1235 return({ @result });
1239 my $field_callback = sub {
1240 my ($cgi, $object, $fieldref) = @_;
1242 my $field = $fieldref->{field};
1243 if ($field eq 'taxproductnums') {
1244 $fieldref->{value} = join(',', @taxproductnums);
1245 } elsif ($field eq 'taxproduct_select') {
1246 $fieldref->{options} = [ '(default)', @taxproductnums ];
1247 $fieldref->{labels} = { ( '(default)' => '(default)' ),
1248 map {( $_ => ($usage_class{$_} || $_) )}
1251 $fieldref->{layer_fields} = \%taxproduct_fields;
1252 $fieldref->{layer_values_callback} = $taxproduct_values;
1253 } elsif ($field eq 'taxproductnum') { # part_pkg-taxproduct, new style
1254 if ( !$taxproducts ) {
1255 # then make the widget go away
1256 $fieldref->{type} = 'hidden';
1261 my $discountnum_hashref = {
1263 'months' => { 'op' => '>', 'value' => 1 },