fix UI nit for late fee definitions, RT#25899
[freeside.git] / httemplate / edit / part_fee.html
1 <& elements/edit.html,
2   'name_singular' => 'fee definition',
3   'table'         => 'part_fee',
4   'labels'        => {
5     'feepart'       => 'Fee definition',
6     'itemdesc'      => 'Description',
7     'comment'       => 'Comment (customer-hidden)',
8     'classnum'      => 'Package class',
9     'taxable'       => 'This fee is taxable',
10     'disabled'      => 'Disable this fee',
11     'taxclass'      => 'Tax class name',
12     'taxproductnum' => 'Tax product',
13     'pay_weight'    => 'Payment weight',
14     'credit_weight' => 'Credit weight',
15     'agentnum'      => 'Agent',
16     'amount'        => 'Flat fee amount',
17     'basis'         => 'Based on',
18     'setuprecur'    => 'Report this fee as',
19     'minimum'       => 'Minimum fee',
20     'maximum'       => 'Maximum fee',
21     'limit_credit'  => 'Limit to customer credit balance',
22     %locale_labels
23   },
24   'fields'        => \@fields,
25   'edit_callback'   => $edit_callback,
26   'error_callback'  => $error_callback,
27 &>
28 <%init>
29 my $curuser = $FS::CurrentUser::CurrentUser;
30 my $acl_edit = $curuser->access_right('Edit fee definitions');
31 my $acl_edit_global = $curuser->access_right('Edit global fee definitions');
32 die "access denied"
33   unless $acl_edit or $acl_edit_global;
34
35 my $conf = FS::Conf->new;
36 my @tax_fields;
37 if ( $conf->exists('enable_taxproducts') ) {
38   @tax_fields = (
39     { field => 'taxproductnum', type => 'select-taxproduct' }
40   );
41 } else {
42   @tax_fields = (
43     { field => 'taxable', type => 'checkbox', value => 'Y' },
44   );
45   push @tax_fields,
46   (
47     { field => 'taxclass', type => 'select-taxclass' },
48   ) if $conf->exists('enable_taxclasses');
49 }
50
51 my $default_locale = $conf->config('locale') || 'en_US';
52 my @locales = grep {$_ ne $default_locale} $conf->config('available-locales');
53 # duplicates edit/part_pkg.cgi, yuck
54 my $n = 0;
55 my (@locale_fields, %locale_labels);
56 foreach (@locales) {
57   push @locale_fields,
58     { field => 'feepartmsgnum'. $n,            type => 'hidden' },
59     { field => 'feepartmsgnum'. $n. '_locale', type => 'hidden', value => $_ },
60     { field => 'feepartmsgnum'. $n. '_itemdesc',  type => 'text', size => 40 },
61   ;
62   $locale_labels{ 'feepartmsgnum'.$n.'_itemdesc' } =
63     'Description&mdash;' . FS::Locales->description($_);
64   $n++;
65 }
66
67 $n = 0;
68 my %layer_fields = (
69   'charged' => [
70     'charged_percent' => { label => 'Fraction of invoice total', type    => 'percentage', },
71   ],
72   'owed' => [
73     'owed_percent' => { label => 'Fraction of balance', type    => 'percentage', },
74   ],
75   'usage' => [
76     'usage'   => { type => 'part_fee_usage' }
77   ],
78 );
79
80 my @fields = (
81
82   { field   => 'itemdesc',  type    => 'text', size    => 40, },
83   @locale_fields,
84
85   { field   => 'comment',   type    => 'text', size    => 40, },
86
87   { field   => 'agentnum',
88     type    => 'select-agent',
89     disable_empty => !$acl_edit_global,
90     empty_label   => '(global)',
91   },
92
93   { field   => 'classnum',
94     type    => 'select-pkg_class',
95   },
96
97   { field   => 'disabled',
98     type    => 'checkbox',
99     value   => 'Y',
100   },
101
102   { field   => 'setuprecur',
103     type    => 'select',
104     options => [ 'setup', 'recur' ],
105     labels  => { 'setup'  => 'a setup fee',
106                  'recur'  => 'a recurring charge' },
107   },
108
109   { type => 'tablebreak-tr-title', value => 'Fee calculation' },
110   { field   => 'amount',  type    => 'money', },
111
112   { field   => 'basis',
113     type    => 'selectlayers',
114     options => [ 'charged', 'owed', 'usage' ],
115     labels  => { 'charged'  => 'amount charged',
116                  'owed'     => 'balance due',
117                  'usage'    => 'usage charges' },
118     layer_fields => \%layer_fields,
119     layer_values_callback => sub {
120       my ($cgi, $obj) = @_;
121       {
122         'charged' => { charged_percent => $obj->percent },
123         'owed'    => { owed_percent => $obj->percent },
124         'usage'   => { usage => [ $obj->part_fee_usage ] },
125       }
126     },
127   },
128   { field   => 'minimum', type    => 'money', },
129   { field   => 'maximum', type    => 'money', },
130   { field   => 'limit_credit',
131     type    => 'checkbox',
132     value   => 'Y' },
133
134   { type => 'tablebreak-tr-title', value => 'Taxation' },
135
136   @tax_fields,
137 );
138
139 my $edit_callback = sub {
140   my ($cgi, $obj, $fields, $opt) = @_;
141   my %existing_locales;
142   if ( $obj->feepart ) {
143     %existing_locales = map { $_->locale => $_ } $obj->part_fee_msgcat;
144   }
145   my $n = 0;
146   foreach (@locales) {
147     $obj->set('feepartmsgnum'.$n.'_locale', $_);
148     # load the existing itemdescs
149     if ( my $msgcat = $existing_locales{$_} ) {
150       $obj->set('feepartmsgnum'.$n, $msgcat->feepartmsgnum);
151       $obj->set('feepartmsgnum'.$n.'_itemdesc', $msgcat->itemdesc);
152     } 
153     # then override that with the CGI param if there is one
154     if ( my $itemdesc = $cgi->param('feepartmsgnum'.$n.'_itemdesc') ) {
155       $obj->set('feepartmsgnum'.$n.'_itemdesc', $itemdesc);
156     }
157     $n++;
158   }
159 };
160
161 my $error_callback = $edit_callback;
162 </%init>