<%doc> Example: include( '/elements/tr-select-reason.html', #required 'field' => 'reasonnum', 'reason_class' => 'C', # currently 'C', 'R', 'F', or 'S' # for cancel, credit, refund, or suspend #recommended 'cgi' => $cgi, #easiest way for things to be properly "sticky" on errors #optional 'control_button' => 'element_name', #button to be enabled when a reason is #selected 'id' => 'element_id', #deprecated ways to keep things "sticky" on errors # (requires duplicate code in each using file to parse cgi params) 'curr_value' => $curr_value, 'curr_value' => { 'typenum' => $typenum, 'reason' => $reason, }, ) % # note style improvements. % # - no more conditionally included code here % # - callers are not expected to pass javascript fragments % # - no redundant checking of ACLs or parameters % # - form fields are grouped for easy management % # - use the standard select-table widget instead of ad hoc crap %# sadly can't just use add_inline here, as we have non-text fields <& tr-select-table.html, 'label' => 'Reason', 'field' => $name, 'id' => $id, 'table' => 'reason', 'records' => \@reasons, 'label_callback' => sub { my $reason = shift; $reason->type . ' : ' . $reason->reason }, 'disable_empty' => 1, 'pre_options' => [ 0 => 'Select reason...' ], 'post_options' => [ -1 => 'Add new reason' ], 'curr_value' => $init_reason, 'onchange' => $id.'_changed()', &> % # "add new reason" fields % # should be a
, but that doesn't fit well into the table <& tr-input-text.html, label => 'New reason', field => $id.'_new_reason' &> % my @types = qsearch( 'reason_type', { 'class' => $class } ); % if (scalar(@types) < 1) { # we should never reach this % } elsif (scalar(@types) == 1) { <& tr-fixed.html, label => 'Reason type', field => $id.'_new_reason_type', curr_value => $types[0]->typenum, formatted_value => $types[0]->type, &> % } else { # more than one type, the normal case <& tr-select-table.html, label => 'Reason type', field => $id.'_new_reason_type', table => 'reason_type', name_col => 'type', hashref => { 'class' => $class }, disable_empty => 1, &> % } # scalar(@types) % if ( $class eq 'S' ) { <& tr-checkbox.html, label => 'Credit the unused portion of service when suspending', field => $id.'_new_unused_credit', value => 'Y' &> <& tr-select-part_pkg.html, label => 'Charge this fee when unsuspending', field => $id.'_new_unsuspend_pkgpart', hashref => { disabled => '', freq => '0' }, empty_label => 'none', &> <& tr-checkbox.html, label => 'Hold unsuspension fee until the next bill', field => $id.'_new_unsuspend_hold', value => 'Y', &> % }

<% mt('No reason types. Please add some.') |h %>

% # container for hints <%init> my %opt = @_; my $name = $opt{'field'}; my $class = $opt{'reason_class'}; my $init_reason; if ( $opt{'cgi'} ) { $init_reason = $opt{'cgi'}->param($name); } else { $init_reason = $opt{'curr_value'}; } my $id = $opt{'id'} || $name; my $add_access_right; if ($class eq 'C') { $add_access_right = 'Add on-the-fly cancel reason'; } elsif ($class eq 'S') { $add_access_right = 'Add on-the-fly suspend reason'; } elsif ($class eq 'R') { $add_access_right = 'Add on-the-fly credit reason'; } elsif ($class eq 'F') { $add_access_right = 'Add on-the-fly refund reason'; } else { die "illegal class: $class"; } my @reasons = qsearch({ 'table' => 'reason', 'addl_from' => ' LEFT JOIN reason_type'. ' ON (reason.reason_type = reason_type.typenum)', 'hashref' => { disabled => '' }, 'extra_sql' => " AND reason_type.class = '$class'", 'order_by' => ' ORDER BY type, reason', }); my %all_hints; if ( $class eq 'S' ) { my $conf = FS::Conf->new; %all_hints = ( 0 => '', -1 => '' ); foreach my $reason (@reasons) { my @hints; if ( $reason->unsuspend_pkgpart ) { my $part_pkg = FS::part_pkg->by_key($reason->unsuspend_pkgpart); if ( $part_pkg ) { if ( $part_pkg->option('setup_fee',1) > 0 and $part_pkg->option('recur_fee',1) == 0 ) { # the usual case push @hints, mt('A [_1] unsuspension fee will apply.', ($conf->config('money_char') || '$') . sprintf('%.2f', $part_pkg->option('setup_fee')) ); } else { # oddball cases--not really supported push @hints, mt('An unsuspension package will apply: [_1]', $part_pkg->price_info ); } } else { #no $part_pkg push @hints, 'Unsuspend pkg #'.$reason->unsuspend_pkgpart. ' not found.'; } } if ( $reason->unused_credit ) { push @hints, mt('The customer will be credited for unused time.'); } $all_hints{ $reason->reasonnum } = join('
', @hints); } } my $curuser = $FS::CurrentUser::CurrentUser;