Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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         'Coupon',
53         'Small footer',
54         'Top margin',
55         'Header separation',
56         'Address separation',
57         'Text height',
58         'Coupon height',
59         'Footer separation',
60       ),
61
62       { type  => 'columnnext' },
63       { type  => 'title', value => 'LaTeX' },
64       { field => 'latexnotes',          %textarea },
65       { field => 'latexfooter',         %textarea },
66       { field => 'latexsummary',        %textarea },
67       { field => 'latexreturnaddress',  %textarea },
68       { field => 'latexcoupon',         %textarea },
69       { field => 'latexsmallfooter',    %textarea },
70       { field => 'latextopmargin',                size => 16 },
71       { field => 'latexheadsep',                  size => 16 },
72       { field => 'latexaddresssep',               size => 16 },
73       { field => 'latextextheight',               size => 16 },
74       { field => 'latexextracouponspace',         size => 16 },
75       { field => 'latexcouponfootsep',            size => 16 },
76       # are these still used?
77       #{ field => 'latexcouponamountenclosedsep',  size => 16 },
78       #{ field => 'latexverticalreturnaddress',    type => 'checkbox' },
79       #{ field => 'latexcouponaddcompanytoaddress',type => 'checkbox' },
80       # logo -- implement if someone really needs it...
81
82       { type  => 'columnnext' },
83       { type  => 'title', value => 'HTML' },
84       { field => 'htmlnotes',           %textarea }, #htmlarea?
85       { field => 'htmlfooter',          %textarea },
86       { field => 'htmlsummary',         %textarea },
87       { field => 'htmlreturnaddress',   %textarea },
88       # logo
89
90       { type  => 'columnend' },
91 );
92
93 my %labels = (
94   'confnum'             => 'Configuration',
95   'locale'              => 'Locale',
96   'agentnum'            => 'Agent',
97   'modename'            => 'Mode name',
98   'notice_name'         => 'Notice name',
99   'subject'             => 'Email Subject: header',
100   'lpr'                 => 'Alternate lpr command',
101
102   map { $_ => '' } (qw(
103     latexnotes
104     latexfooter
105     latexsummary
106     latexreturnaddress
107     latexcoupon
108     latexsmallfooter
109     latextopmargin
110     latexheadsep
111     latexaddresssep
112     latextextheight
113     latexextracouponspace
114     latexcouponfootsep
115     htmlnotes
116     htmlfooter
117     htmlsummary
118     htmlreturnaddress
119     logo_png
120     logo_eps
121   ) ),
122
123 ); 
124
125 sub get_invoice_mode { # because we can't quite use agent_virt here
126   my $modenum = shift;
127   qsearchs({
128     'table'     => 'invoice_mode',
129     'hashref'   => { 'modenum' => $modenum },
130     'extra_sql' => ' AND '.
131       $FS::CurrentUser::CurrentUser->agentnums_sql(
132         'null_right' => 'Edit global templates',
133         'viewall_right' => 'Edit global templates' ),
134   });
135 };
136
137 sub error_callback {
138   my ($cgi, $object) = @_;
139   foreach (qw(modename agentnum)) {
140     $object->set($_, $cgi->param($_));
141   }
142   if ($object->confnum) {
143     return edit_callback(@_);
144   } else {
145     return new_callback(@_);
146   }
147 }
148
149 sub new_callback {
150   my ($cgi, $object, $fields_arrayref, $opt_hashref) = @_;
151   my $modenum;
152   if ( $cgi->param('locale') =~ /^(\w+)$/ ) {
153     $object->set('locale' => $1);
154   }
155
156   if ( $cgi->param('modenum') =~ /^(\d+)$/ ) {
157     $modenum = $1; # we're adding a locale to an existing mode
158     $object->set('modenum' => $modenum);
159     my $invoice_mode = get_invoice_mode($modenum)
160       or die "invoice mode $modenum not found";
161
162     $object->set('modename', $invoice_mode->modename);
163     $object->set('agentnum', $invoice_mode->agentnum);
164
165     # also, need to select a locale
166     # make a list of available locales
167     my %existing_locales = map { $_->locale }
168                           qsearch('invoice_conf', { modenum => $modenum });
169
170     my @locales = grep { !exists($existing_locales{$_}) } 
171                          FS::Conf->new->config('available-locales');
172     my %labels;
173     foreach (@locales) {
174       my %info = FS::Locales->locale_info($_);
175       $labels{$_} = $info{'label'};
176     }
177     unshift @locales, 'new';
178     $labels{'new'} = 'Select language';
179
180     # insert a field def
181     my $i = 0;
182     $i++ until ( $fields_arrayref->[$i]->{'field'} eq 'locale' );
183     my $locale_field = $fields_arrayref->[$i];
184
185     my $onchange_locale = "document.getElementById('submit').disabled = 
186     (this.options[this.selectedIndex].value == 'new');";
187
188     %$locale_field = (
189       field   => 'locale',
190       type    => 'select',
191       options => \@locales,
192       labels  => \%labels,
193       curr_value  => 'new',
194       onchange    => $onchange_locale,
195     );
196
197   } # otherwise it's a completely new mode, so the locale is default
198
199 }
200
201 sub edit_callback {
202   # massive false laziness with msg_template UI
203   my ($cgi, $object, $fields_arrayref, $opt_hashref) = @_;
204
205   # a little different here in that we treat the content object
206   # as "primary" (this is edit/invoice_conf.html, etc.)
207   # so all we need from the invoice_mode is its name
208   # (and agent identity)
209   my $modenum = $object->modenum;
210   my $invoice_mode = get_invoice_mode($modenum)
211     or die "invoice mode $modenum not found";
212   $object->set('modename', $invoice_mode->modename);
213   $object->set('agentnum', $invoice_mode->agentnum);
214
215 }
216
217 sub menubar_opt_callback {
218   my $object = shift;
219   my $modenum = $object->modenum or return; 
220   my (@tabs, @options, %labels);
221   my $display_new = 0;
222   my $selected = '';
223   foreach my $l ('', FS::Conf->new->config('available-locales')) {
224     my $invoice_conf =
225       qsearchs('invoice_conf', { modenum => $modenum, locale => $l });
226     if ( $invoice_conf ) {
227       my %info = FS::Locales->locale_info($l) if $l;
228       my $label = $info{'label'} || mt('Default');
229       push @tabs, $label, $invoice_conf->confnum;
230       $selected = $label if $object->locale eq $l;
231     }
232     else {
233       $display_new = 1; # there is at least one unused locale left
234     }
235   }
236   push @tabs, mt('New'), "modenum=$modenum;locale=new" if $display_new;
237   $selected = mt('New') if $object->locale eq 'new';
238   $selected ||= mt('Default');
239   (
240     'url_base' => $cgi->url() . '?',
241     'selected' => $selected,
242     'tabs'     => \@tabs
243   );
244 }
245
246 sub html_init {
247 q!
248 <STYLE>
249 .fstabcontainer th { vertical-align: middle; text-align: center }
250 </STYLE>
251 !
252 }
253
254 sub html_table_bottom {
255   my $object = shift;
256   my $locale = '';
257   my $modenum = '';
258
259   if ($object->locale =~ /^(\w+)$/) {
260     $locale = $1;
261   }
262   if ($object->modenum =~ /^(\d+)$/) {
263     $modenum = $1;
264   }
265   my $html;
266   my $show_delete = 1;
267   # don't allow the default locale to be removed unless it's the last one
268   # in the mode
269   $show_delete = 0 if (
270     $locale eq 'new' or
271     $modenum eq '' or
272     ($locale eq '' and
273      FS::invoice_conf->count("modenum = $modenum and locale is not null") > 0
274     )
275   );
276
277   if ( $show_delete ) {    
278     # set up a delete link
279     my $confnum = $object->confnum;
280     my $url = $p."misc/delete-invoice_conf.html?$confnum";
281     my $link = qq!<A HREF="javascript:areyousure('$url','Really delete this configuration?')">! .
282       'Delete this configuration' .
283       '</A>';
284     $html = qq!<TR><TD></TD>
285       <TD STYLE="font-style: italic; font-size: small">$link</TD></TR>
286       <SCRIPT TYPE="text/javascript">
287       function areyousure(url, message) {
288           if (confirm(message)) window.location.href = url;
289       }
290       </SCRIPT>
291       !;
292   }
293   $html;
294 }
295
296 </%init>