Merge branch 'master' of https://github.com/jgoodman/Freeside
[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 (
46     { field => 'taxclass', type => 'select-taxclass' },
47   ) if $conf->exists('enable_taxclasses');
48 }
49
50 my $default_locale = $conf->config('locale') || 'en_US';
51 my @locales = grep {$_ ne $default_locale} $conf->config('available-locales');
52 # duplicates edit/part_pkg.cgi, yuck
53 my $n = 0;
54 my (@locale_fields, %locale_labels);
55 foreach (@locales) {
56   push @locale_fields,
57     { field => 'feepartmsgnum'. $n,            type => 'hidden' },
58     { field => 'feepartmsgnum'. $n. '_locale', type => 'hidden', value => $_ },
59     { field => 'feepartmsgnum'. $n. '_itemdesc',  type => 'text', size => 40 },
60   ;
61   $locale_labels{ 'feepartmsgnum'.$n.'_itemdesc' } =
62     'Description&mdash;' . FS::Locales->description($_);
63   $n++;
64 }
65
66 $n = 0;
67 my %layer_fields = (
68   'charged' => [
69     'percent' => { label => 'Fraction of invoice total', type    => 'percentage', },
70   ],
71   'owed' => [
72     'percent' => { label => 'Fraction of balance', type    => 'percentage', },
73   ],
74   'usage' => [
75     'usage'   => { type => 'part_fee_usage' }
76   ],
77 );
78
79 my @fields = (
80
81   { field   => 'itemdesc',  type    => 'text', size    => 40, },
82   @locale_fields,
83
84   { field   => 'comment',   type    => 'text', size    => 40, },
85
86   { field   => 'agentnum',
87     type    => 'select-agent',
88     disable_empty => !$acl_edit_global,
89     empty_label   => '(global)',
90   },
91
92   { field   => 'classnum',
93     type    => 'select-pkg_class',
94   },
95
96   { field   => 'disabled',
97     type    => 'checkbox',
98     value   => 'Y',
99   },
100
101   { field   => 'setuprecur',
102     type    => 'select',
103     options => [ 'setup', 'recur' ],
104     labels  => { 'setup'  => 'a setup fee',
105                  'recur'  => 'a recurring charge' },
106   },
107
108   { type => 'justtitle', value => 'Fee calculation' },
109   { field   => 'amount',  type    => 'money', },
110
111   { field   => 'basis',
112     type    => 'selectlayers',
113     options => [ 'charged', 'owed', 'usage' ],
114     labels  => { 'charged'  => 'amount charged',
115                  'owed'     => 'balance due',
116                  'usage'    => 'usage charges' },
117     layer_fields => \%layer_fields,
118     layer_values_callback => sub {
119       my ($cgi, $obj) = @_;
120       {
121         'charged' => { percent => $obj->percent },
122         'owed'    => { percent => $obj->percent },
123         'usage'   => { usage => [ $obj->part_fee_usage ] },
124       }
125     },
126   },
127   { field   => 'minimum', type    => 'money', },
128   { field   => 'maximum', type    => 'money', },
129   { field   => 'limit_credit',
130     type    => 'checkbox',
131     value   => 'Y' },
132
133   { type => 'justtitle', value => 'Taxation' },
134
135   @tax_fields,
136 );
137
138 my $edit_callback = sub {
139   my ($cgi, $obj, $fields, $opt) = @_;
140   my %existing_locales;
141   if ( $obj->feepart ) {
142     %existing_locales = map { $_->locale => $_ } $obj->part_fee_msgcat;
143   }
144   my $n = 0;
145   foreach (@locales) {
146     $obj->set('feepartmsgnum'.$n.'_locale', $_);
147     # load the existing itemdescs
148     if ( my $msgcat = $existing_locales{$_} ) {
149       $obj->set('feepartmsgnum'.$n, $msgcat->feepartmsgnum);
150       $obj->set('feepartmsgnum'.$n.'_itemdesc', $msgcat->itemdesc);
151     } 
152     # then override that with the CGI param if there is one
153     if ( my $itemdesc = $cgi->param('feepartmsgnum'.$n.'_itemdesc') ) {
154       $obj->set('feepartmsgnum'.$n.'_itemdesc', $itemdesc);
155     }
156     $n++;
157   }
158 };
159
160 my $error_callback = $edit_callback;
161 </%init>