remove some unneeded settings from invoice configurations, #24723
[freeside.git] / httemplate / edit / invoice_conf.html
1 <& elements/edit.html,
2     'body_etc'          => $body_etc,
3     'name_singular'     => 'invoice configuration',
4     'table'             => 'invoice_conf',
5     'viewall_dir'       => 'browse',
6     'fields'            => \@fields,
7     'labels'            => \%labels,
8     'new_callback'      => \&new_callback,
9     'edit_callback'     => \&edit_callback,
10     'error_callback'    => \&error_callback,
11     'html_init'         => \&html_init,
12     'html_table_bottom' => \&html_table_bottom,
13     'html_bottom'       => '</DIV>', # close tablebreak-tabs
14 &>
15 <%init>
16
17 my $curuser = $FS::CurrentUser::CurrentUser;
18
19 # ???
20 die "access denied"
21   unless $curuser->access_right([ 'Edit templates', 'Edit global templates' ]);
22
23 my $body_etc = '';
24 $body_etc = q!onload="document.getElementById('locale').onchange()"!
25   if $cgi->param('locale') eq 'new';
26
27 my $modenum = $cgi->param('modenum');
28 my $mode = $modenum ? qsearchs('invoice_mode', { modenum => $modenum }) : '';
29
30 my %textarea = (type => 'textarea', rows => 10, cols => 40);
31 my @fields = (
32       { field => 'modenum', type => 'hidden' },
33       { field => 'agentnum',
34         type  => 'select-agent',
35       },
36       { field => 'modename',  size=>60, },
37       { type  => 'tablebreak-tabs',
38         include_opt_callback => \&menubar_opt_callback,
39       },
40       { field => 'locale', type => 'hidden' },
41       { field => 'notice_name',   size=>60, },
42       { field => 'subject',       size=>60, },
43       { field => 'lpr',           size=>60, },
44
45       { type  => 'columnstart', aligned => 1 },
46       { type  => 'title', value => '<BR>' },
47       map ( { +{ type => 'justtitle', value => $_ } }
48         'Notes',
49         'Footer',
50         'Summary header',
51         'Return address',
52         'Small footer',
53         'Enable coupon',
54       ),
55
56       { type  => 'columnnext' },
57       { type  => 'title', value => 'LaTeX' },
58       { field => 'latexnotes',          %textarea },
59       { field => 'latexfooter',         %textarea },
60       { field => 'latexsummary',        %textarea },
61       { field => 'latexreturnaddress',  %textarea },
62       { field => 'latexsmallfooter',    %textarea },
63       { field => 'with_latexcoupon', type => 'checkbox', value => 'Y' },
64
65       { type  => 'columnnext' },
66       { type  => 'title', value => 'HTML' },
67       { field => 'htmlnotes',           %textarea }, #htmlarea?
68       { field => 'htmlfooter',          %textarea },
69       { field => 'htmlsummary',         %textarea },
70       { field => 'htmlreturnaddress',   %textarea },
71       # logo
72
73       { type  => 'columnend' },
74 );
75
76 my %labels = (
77   'confnum'             => 'Configuration',
78   'locale'              => 'Locale',
79   'agentnum'            => 'Agent',
80   'modename'            => 'Mode name',
81   'notice_name'         => 'Notice name',
82   'subject'             => 'Email Subject: header',
83   'lpr'                 => 'Alternate lpr command',
84
85   map { $_ => '' } (qw(
86     latexnotes
87     latexfooter
88     latexsummary
89     latexreturnaddress
90     with_latexcoupon
91     latexsmallfooter
92     htmlnotes
93     htmlfooter
94     htmlsummary
95     htmlreturnaddress
96   ) ),
97
98 ); 
99
100 sub get_invoice_mode { # because we can't quite use agent_virt here
101   my $modenum = shift;
102   qsearchs({
103     'table'     => 'invoice_mode',
104     'hashref'   => { 'modenum' => $modenum },
105     'extra_sql' => ' AND '.
106       $FS::CurrentUser::CurrentUser->agentnums_sql(
107         'null_right' => 'Edit global templates',
108         'viewall_right' => 'Edit global templates' ),
109   });
110 };
111
112 sub error_callback {
113   my ($cgi, $object) = @_;
114   foreach (qw(modename agentnum)) {
115     $object->set($_, $cgi->param($_));
116   }
117   if ($object->confnum) {
118     return edit_callback(@_);
119   } else {
120     return new_callback(@_);
121   }
122 }
123
124 sub new_callback {
125   my ($cgi, $object, $fields_arrayref, $opt_hashref) = @_;
126   my $modenum;
127   if ( $cgi->param('locale') =~ /^(\w+)$/ ) {
128     $object->set('locale' => $1);
129   }
130
131   if ( $cgi->param('modenum') =~ /^(\d+)$/ ) {
132     $modenum = $1; # we're adding a locale to an existing mode
133     $object->set('modenum' => $modenum);
134     my $invoice_mode = get_invoice_mode($modenum)
135       or die "invoice mode $modenum not found";
136
137     $object->set('modename', $invoice_mode->modename);
138     $object->set('agentnum', $invoice_mode->agentnum);
139
140     # also, need to select a locale
141     # make a list of available locales
142     my %existing_locales = map { $_->locale }
143                           qsearch('invoice_conf', { modenum => $modenum });
144
145     my @locales = grep { !exists($existing_locales{$_}) } 
146                          FS::Conf->new->config('available-locales');
147     my %labels;
148     foreach (@locales) {
149       my %info = FS::Locales->locale_info($_);
150       $labels{$_} = $info{'label'};
151     }
152     unshift @locales, 'new';
153     $labels{'new'} = 'Select language';
154
155     # insert a field def
156     my $i = 0;
157     $i++ until ( $fields_arrayref->[$i]->{'field'} eq 'locale' );
158     my $locale_field = $fields_arrayref->[$i];
159
160     my $onchange_locale = "document.getElementById('submit').disabled = 
161     (this.options[this.selectedIndex].value == 'new');";
162
163     %$locale_field = (
164       field   => 'locale',
165       type    => 'select',
166       options => \@locales,
167       labels  => \%labels,
168       curr_value  => 'new',
169       onchange    => $onchange_locale,
170     );
171
172   } # otherwise it's a completely new mode, so the locale is default
173
174 }
175
176 sub edit_callback {
177   # massive false laziness with msg_template UI
178   my ($cgi, $object, $fields_arrayref, $opt_hashref) = @_;
179
180   # a little different here in that we treat the content object
181   # as "primary" (this is edit/invoice_conf.html, etc.)
182   # so all we need from the invoice_mode is its name
183   # (and agent identity)
184   my $modenum = $object->modenum;
185   my $invoice_mode = get_invoice_mode($modenum)
186     or die "invoice mode $modenum not found";
187   $object->set('modename', $invoice_mode->modename);
188   $object->set('agentnum', $invoice_mode->agentnum);
189 }
190
191 sub menubar_opt_callback {
192   my $object = shift;
193   my $modenum = $object->modenum or return; 
194   my (@tabs, @options, %labels);
195   my $display_new = 0;
196   my $selected = '';
197   foreach my $l ('', FS::Conf->new->config('available-locales')) {
198     my $invoice_conf =
199       qsearchs('invoice_conf', { modenum => $modenum, locale => $l });
200     if ( $invoice_conf ) {
201       my %info = FS::Locales->locale_info($l) if $l;
202       my $label = $info{'label'} || mt('Default');
203       push @tabs, $label, $invoice_conf->confnum;
204       $selected = $label if $object->locale eq $l;
205     }
206     else {
207       $display_new = 1; # there is at least one unused locale left
208     }
209   }
210   push @tabs, mt('New'), "modenum=$modenum;locale=new" if $display_new;
211   $selected = mt('New') if $object->locale eq 'new';
212   $selected ||= mt('Default');
213   (
214     'url_base' => $cgi->url() . '?',
215     'selected' => $selected,
216     'tabs'     => \@tabs
217   );
218 }
219
220 sub html_init {
221 q!
222 <STYLE>
223 .fstabcontainer th { vertical-align: middle; text-align: center }
224 </STYLE>
225 !
226 }
227
228 sub html_table_bottom {
229   my $object = shift;
230   my $locale = '';
231   my $modenum = '';
232
233   if ($object->locale =~ /^(\w+)$/) {
234     $locale = $1;
235   }
236   if ($object->modenum =~ /^(\d+)$/) {
237     $modenum = $1;
238   }
239   my $html;
240   my $show_delete = 1;
241   # don't allow the default locale to be removed unless it's the last one
242   # in the mode
243   $show_delete = 0 if (
244     $locale eq 'new' or
245     $modenum eq '' or
246     ($locale eq '' and
247      FS::invoice_conf->count("modenum = $modenum and locale is not null") > 0
248     )
249   );
250
251   if ( $show_delete ) {    
252     # set up a delete link
253     my $confnum = $object->confnum;
254     my $url = $p."misc/delete-invoice_conf.html?$confnum";
255     my $link = qq!<A HREF="javascript:areyousure('$url','Really delete this configuration?')">! .
256       'Delete this configuration' .
257       '</A>';
258     $html = qq!<TR><TD></TD>
259       <TD STYLE="font-style: italic; font-size: small">$link</TD></TR>
260       <SCRIPT TYPE="text/javascript">
261       function areyousure(url, message) {
262           if (confirm(message)) window.location.href = url;
263       }
264       </SCRIPT>
265       !;
266   }
267   $html;
268 }
269
270 </%init>