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