summaryrefslogtreecommitdiff
path: root/httemplate/edit/part_fee.html
blob: 5f6dc3818c7f4292ff5b235224b08e667a1437d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<& elements/edit.html,
  'name_singular' => 'fee definition',
  'table'         => 'part_fee',
  'labels'        => {
    'feepart'       => 'Fee definition',
    'itemdesc'      => 'Description',
    'comment'       => 'Comment (customer-hidden)',
    'classnum'      => 'Package class',
    'taxable'       => 'This fee is taxable',
    'disabled'      => 'Disable this fee',
    'taxclass'      => 'Tax class name',
    'taxproductnum' => 'Tax product',
    'pay_weight'    => 'Payment weight',
    'credit_weight' => 'Credit weight',
    'agentnum'      => 'Agent',
    'amount'        => 'Flat fee amount',
    'basis'         => 'Based on',
    'setuprecur'    => 'Report this fee as',
    'minimum'       => 'Minimum fee',
    'maximum'       => 'Maximum fee',
    'limit_credit'  => 'Limit to customer credit balance',
    %locale_labels
  },
  'fields'        => \@fields,
  'viewall_dir'   => 'browse',
  'edit_callback'   => $edit_callback,
  'error_callback'  => $error_callback,
&>
<%init>
my $curuser = $FS::CurrentUser::CurrentUser;
my $acl_edit = $curuser->access_right('Edit fee definitions');
my $acl_edit_global = $curuser->access_right('Edit global fee definitions');
die "access denied"
  unless $acl_edit or $acl_edit_global;

my $conf = FS::Conf->new;
my @tax_fields;
if ( $conf->config('tax_data_vendor') ) {
  @tax_fields = (
    { field => 'taxproductnum', type => 'select-taxproduct' }
  );
} else {
  @tax_fields = (
    { field => 'taxable', type => 'checkbox', value => 'Y' },
  );
  push @tax_fields,
  (
    { field => 'taxclass', type => 'select-taxclass' },
  ) if $conf->exists('enable_taxclasses');
}

my $default_locale = $conf->config('locale') || 'en_US';
my @locales = grep {$_ ne $default_locale} $conf->config('available-locales');
# duplicates edit/part_pkg.cgi, yuck
my $n = 0;
my (@locale_fields, %locale_labels);
foreach (@locales) {
  push @locale_fields,
    { field => 'feepartmsgnum'. $n,            type => 'hidden' },
    { field => 'feepartmsgnum'. $n. '_locale', type => 'hidden', value => $_ },
    { field => 'feepartmsgnum'. $n. '_itemdesc',  type => 'text', size => 40 },
  ;
  $locale_labels{ 'feepartmsgnum'.$n.'_itemdesc' } =
    'Description&mdash;' . FS::Locales->description($_);
  $n++;
}

$n = 0;
my %layer_fields = (
  'charged' => [
    'charged_percent' => { label => 'Fraction of invoice total', type    => 'percentage', },
  ],
  'owed' => [
    'owed_percent' => { label => 'Fraction of balance', type    => 'percentage', },
  ],
  'usage' => [
    'usage'   => { type => 'part_fee_usage' }
  ],
);

my @fields = (

  { field   => 'itemdesc',  type    => 'text', size    => 40, },
  @locale_fields,

  { field   => 'comment',   type    => 'text', size    => 40, },

  { field   => 'agentnum',
    type    => 'select-agent',
    disable_empty => !$acl_edit_global,
    empty_label   => '(global)',
  },

  { field   => 'classnum',
    type    => 'select-pkg_class',
  },

  { field   => 'disabled',
    type    => 'checkbox',
    value   => 'Y',
  },

  { field   => 'setuprecur',
    type    => 'select',
    options => [ 'setup', 'recur' ],
    labels  => { 'setup'  => 'a setup fee',
                 'recur'  => 'a recurring charge' },
  },

  { type => 'tablebreak-tr-title', value => 'Fee calculation' },
  { field   => 'amount',  type    => 'money', },

  { field   => 'basis',
    type    => 'selectlayers',
    options => [ 'charged', 'owed', 'usage' ],
    labels  => { 'charged'  => 'amount charged',
                 'owed'     => 'balance due',
                 'usage'    => 'usage charges' },
    layer_fields => \%layer_fields,
    layer_values_callback => sub {
      my ($cgi, $obj) = @_;
      {
        'charged' => { charged_percent => $obj->percent },
        'owed'    => { owed_percent => $obj->percent },
        'usage'   => { usage => [ $obj->part_fee_usage ] },
      }
    },
  },
  { field   => 'minimum', type    => 'money', },
  { field   => 'maximum', type    => 'money', },
  { field   => 'limit_credit',
    type    => 'checkbox',
    value   => 'Y' },

  { type => 'tablebreak-tr-title', value => 'Taxation' },

  @tax_fields,
);

my $edit_callback = sub {
  my ($cgi, $obj, $fields, $opt) = @_;
  my %existing_locales;
  if ( $obj->feepart ) {
    %existing_locales = map { $_->locale => $_ } $obj->part_fee_msgcat;
  }
  my $n = 0;
  foreach (@locales) {
    $obj->set('feepartmsgnum'.$n.'_locale', $_);
    # load the existing itemdescs
    if ( my $msgcat = $existing_locales{$_} ) {
      $obj->set('feepartmsgnum'.$n, $msgcat->feepartmsgnum);
      $obj->set('feepartmsgnum'.$n.'_itemdesc', $msgcat->itemdesc);
    } 
    # then override that with the CGI param if there is one
    if ( my $itemdesc = $cgi->param('feepartmsgnum'.$n.'_itemdesc') ) {
      $obj->set('feepartmsgnum'.$n.'_itemdesc', $itemdesc);
    }
    $n++;
  }
};

my $error_callback = $edit_callback;
</%init>