summaryrefslogtreecommitdiff
path: root/httemplate/edit/invoice_conf.html
blob: 861114b1ca8eca4a15930340c2f9fb209df5461b (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<& elements/edit.html,
    'body_etc'          => $body_etc,
    'name_singular'     => 'invoice configuration',
    'table'             => 'invoice_conf',
    'viewall_dir'       => 'browse',
    'fields'            => \@fields,
    'labels'            => \%labels,
    'new_callback'      => \&new_callback,
    'edit_callback'     => \&edit_callback,
    'error_callback'    => \&error_callback,
    'html_init'         => \&html_init,
    'html_table_bottom' => \&html_table_bottom,
    'html_bottom'       => '</DIV>', # close tablebreak-tabs
&>
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;

# ???
die "access denied"
  unless $curuser->access_right([ 'Edit templates', 'Edit global templates' ]);

my $body_etc = '';
$body_etc = q!onload="document.getElementById('locale').onchange()"!
  if $cgi->param('locale') eq 'new';

my $modenum = $cgi->param('modenum');
my $mode = $modenum ? qsearchs('invoice_mode', { modenum => $modenum }) : '';

my %textarea = (type => 'textarea', rows => 10, cols => 40);
my @fields = (
      { field => 'modenum', type => 'hidden' },
      { field => 'agentnum',
        type  => 'select-agent',
      },
      { field => 'modename',  size=>60, },
      { type  => 'tablebreak-tabs',
        include_opt_callback => \&menubar_opt_callback,
      },
      { field => 'locale', type => 'hidden' },
      { field => 'notice_name',   size=>60, },
      { field => 'subject',       size=>60, },
      { field => 'lpr',           size=>60, },

      { type  => 'columnstart', aligned => 1 },
      { type  => 'title', value => '<BR>' },
      map ( { +{ type => 'justtitle', value => $_ } }
        'Notes',
        'Footer',
        'Summary header',
        'Return address',
        'Watermark',
        'Small footer',
        'Enable coupon',
      ),

      { type  => 'columnnext' },
      { type  => 'title', value => 'LaTeX' },
      { field => 'latexnotes',          %textarea },
      { field => 'latexfooter',         %textarea },
      { field => 'latexsummary',        %textarea },
      { field => 'latexreturnaddress',  %textarea },
      { field => 'latexwatermark',      %textarea },
      { field => 'latexsmallfooter',    %textarea },
      { field => 'with_latexcoupon', type => 'checkbox', value => 'Y' },

      { type  => 'columnnext' },
      { type  => 'title', value => 'HTML' },
      { field => 'htmlnotes',           %textarea }, #htmlarea?
      { field => 'htmlfooter',          %textarea },
      { field => 'htmlsummary',         %textarea },
      { field => 'htmlreturnaddress',   %textarea },
      { field => 'htmlwatermark',      %textarea },
      # logo

      { type  => 'columnend' },
);

my %labels = (
  'confnum'             => 'Configuration',
  'locale'              => 'Locale',
  'agentnum'            => 'Agent',
  'modename'            => 'Mode name',
  'notice_name'         => 'Notice name',
  'subject'             => 'Email Subject: header',
  'lpr'                 => 'Alternate lpr command',

  map { $_ => '' } (qw(
    latexnotes
    latexfooter
    latexsummary
    latexreturnaddress
    latexwatermark
    with_latexcoupon
    latexsmallfooter
    htmlnotes
    htmlfooter
    htmlsummary
    htmlreturnaddress
    htmlwatermark
  ) ),

); 

sub get_invoice_mode { # because we can't quite use agent_virt here
  my $modenum = shift;
  qsearchs({
    'table'     => 'invoice_mode',
    'hashref'   => { 'modenum' => $modenum },
    'extra_sql' => ' AND '.
      $FS::CurrentUser::CurrentUser->agentnums_sql(
        'null_right' => 'Edit global templates',
        'viewall_right' => 'Edit global templates' ),
  });
};

sub error_callback {
  my ($cgi, $object) = @_;
  foreach (qw(modename agentnum)) {
    $object->set($_, $cgi->param($_));
  }
  if ($object->confnum) {
    return edit_callback(@_);
  } else {
    return new_callback(@_);
  }
}

sub new_callback {
  my ($cgi, $object, $fields_arrayref, $opt_hashref) = @_;
  my $modenum;
  if ( $cgi->param('locale') =~ /^(\w+)$/ ) {
    $object->set('locale' => $1);
  }

  if ( $cgi->param('modenum') =~ /^(\d+)$/ ) {
    $modenum = $1; # we're adding a locale to an existing mode
    $object->set('modenum' => $modenum);
    my $invoice_mode = get_invoice_mode($modenum)
      or die "invoice mode $modenum not found";

    $object->set('modename', $invoice_mode->modename);
    $object->set('agentnum', $invoice_mode->agentnum);

    # also, need to select a locale
    # make a list of available locales
    my %existing_locales = map { $_->locale }
                          qsearch('invoice_conf', { modenum => $modenum });

    my @locales = grep { !exists($existing_locales{$_}) } 
                         FS::Conf->new->config('available-locales');
    my %labels;
    foreach (@locales) {
      my %info = FS::Locales->locale_info($_);
      $labels{$_} = $info{'label'};
    }
    unshift @locales, 'new';
    $labels{'new'} = 'Select language';

    # insert a field def
    my $i = 0;
    $i++ until ( $fields_arrayref->[$i]->{'field'} eq 'locale' );
    my $locale_field = $fields_arrayref->[$i];

    my $onchange_locale = "document.getElementById('submit').disabled = 
    (this.options[this.selectedIndex].value == 'new');";

    %$locale_field = (
      field   => 'locale',
      type    => 'select',
      options => \@locales,
      labels  => \%labels,
      curr_value  => 'new',
      onchange    => $onchange_locale,
    );

  } # otherwise it's a completely new mode, so the locale is default

}

sub edit_callback {
  # massive false laziness with msg_template UI
  my ($cgi, $object, $fields_arrayref, $opt_hashref) = @_;

  # a little different here in that we treat the content object
  # as "primary" (this is edit/invoice_conf.html, etc.)
  # so all we need from the invoice_mode is its name
  # (and agent identity)
  my $modenum = $object->modenum;
  my $invoice_mode = get_invoice_mode($modenum)
    or die "invoice mode $modenum not found";
  $object->set('modename', $invoice_mode->modename);
  $object->set('agentnum', $invoice_mode->agentnum);
}

sub menubar_opt_callback {
  my $object = shift;
  my $modenum = $object->modenum or return; 
  my (@tabs, @options, %labels);
  my $display_new = 0;
  my $selected = '';
  foreach my $l ('', FS::Conf->new->config('available-locales')) {
    my $invoice_conf =
      qsearchs('invoice_conf', { modenum => $modenum, locale => $l });
    if ( $invoice_conf ) {
      my %info = FS::Locales->locale_info($l) if $l;
      my $label = $info{'label'} || mt('Default');
      push @tabs, $label, $invoice_conf->confnum;
      $selected = $label if $object->locale eq $l;
    }
    else {
      $display_new = 1; # there is at least one unused locale left
    }
  }
  push @tabs, mt('New'), "modenum=$modenum;locale=new" if $display_new;
  $selected = mt('New') if $object->locale eq 'new';
  $selected ||= mt('Default');
  (
    'url_base' => $cgi->url() . '?',
    'selected' => $selected,
    'tabs'     => \@tabs
  );
}

sub html_init {
q!
<STYLE>
.fstabcontainer th { vertical-align: middle; text-align: center }
</STYLE>
!
}

sub html_table_bottom {
  my $object = shift;
  my $locale = '';
  my $modenum = '';

  if ($object->locale =~ /^(\w+)$/) {
    $locale = $1;
  }
  if ($object->modenum =~ /^(\d+)$/) {
    $modenum = $1;
  }
  my $html;
  my $show_delete = 1;
  # don't allow the default locale to be removed unless it's the last one
  # in the mode
  $show_delete = 0 if (
    $locale eq 'new' or
    $modenum eq '' or
    ($locale eq '' and
     FS::invoice_conf->count("modenum = $modenum and locale is not null") > 0
    )
  );

  if ( $show_delete ) {    
    # set up a delete link
    my $confnum = $object->confnum;
    my $url = $p."misc/delete-invoice_conf.html?$confnum";
    my $link = qq!<A HREF="javascript:areyousure('$url','Really delete this configuration?')">! .
      'Delete this configuration' .
      '</A>';
    $html = qq!<TR><TD></TD>
      <TD STYLE="font-style: italic; font-size: small">$link</TD></TR>
      <SCRIPT TYPE="text/javascript">
      function areyousure(url, message) {
          if (confirm(message)) window.location.href = url;
      }
      </SCRIPT>
      !;
  }
  $html;
}

</%init>