'templates' nicht aus %::myconfig, sondern aus Defaults-Tabelle holen
[kivitendo-erp.git] / bin / mozilla / amtemplates.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # administration
31 #
32 #======================================================================
33
34 use File::Find;
35
36 use SL::DB::Default;
37 use SL::AM;
38 use SL::Form;
39
40 use Data::Dumper;
41
42 use strict;
43
44 1;
45
46 require "bin/mozilla/common.pl";
47
48 # end of main
49
50 sub display {
51   call_sub($main::form->{display_nextsub});
52 }
53
54 sub save {
55   call_sub($main::form->{save_nextsub});
56 }
57
58 sub edit {
59   call_sub($main::form->{edit_nextsub});
60 }
61
62 sub display_template {
63   $main::lxdebug->enter_sub();
64
65   my $form     = $main::form;
66
67   $main::auth->assert('admin');
68
69   $form->{edit} = 0;
70   display_template_form();
71
72   $main::lxdebug->leave_sub();
73 }
74
75 sub edit_template {
76   $main::lxdebug->enter_sub();
77
78   my $form     = $main::form;
79
80   $main::auth->assert('admin');
81
82   $form->{edit} = 1;
83   display_template_form();
84
85   $main::lxdebug->leave_sub();
86 }
87
88 sub save_template {
89   $main::lxdebug->enter_sub();
90
91   my $form     = $main::form;
92   my %myconfig = %main::myconfig;
93   my $locale   = $main::locale;
94
95   $main::auth->assert('admin');
96
97   $form->isblank("formname", $locale->text("You're not editing a file.")) unless ($form->{type} eq "stylesheet");
98
99   my ($filename) = AM->prepare_template_filename(\%myconfig, $form);
100   if (my $error = AM->save_template($filename, $form->{content})) {
101     $form->error(sprintf($locale->text("Saving the file '%s' failed. OS error message: %s"), $filename, $error));
102   }
103
104   $form->{edit} = 0;
105   display_template_form();
106
107   $main::lxdebug->leave_sub();
108 }
109
110 sub display_template_form {
111   $main::lxdebug->enter_sub();
112
113   my $form     = $main::form;
114   my %myconfig = %main::myconfig;
115   my $locale   = $main::locale;
116
117   $main::auth->assert('admin');
118
119   my $defaults = SL::DB::Default->get;
120   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
121
122   if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
123     $form->{"formname"} =~ s|.*/||;
124   }
125
126   my $format = $form->{"format"} eq "html" ? "html" : "tex";
127
128   $form->{"title"} = $form->{"type"} eq "stylesheet" ? $locale->text("Edit the stylesheet") : $locale->text("Edit templates");
129   if ($form->{"format"}) {
130       $form->{"title"} = uc($form->{"format"}) . " - " . $form->{"title"};
131   }
132
133   my %options;
134
135   my @hidden = qw(type format);
136
137   if (($form->{"type"} ne "stylesheet") && !$form->{"edit"}) {
138     $options{"SHOW_EDIT_OPTIONS"} = 1;
139
140     #
141     # Setup "formname" selection
142     #
143
144     $form->get_lists("printers" => "ALL_PRINTERS",
145                      "languages" => "ALL_LANGUAGES",
146                      "dunning_configs" => "ALL_DUNNING_CONFIGS");
147
148     my %formname_setup =
149       (
150         # balance_sheet           => { translation => $locale->text('Balance Sheet'),             html => 1 },
151         bin_list                => $locale->text('Bin List'),
152         bwa                     => { translation => $locale->text('BWA'),                       html => 1 },
153         check                   => { translation => $locale->text('Check'),                     html => 1 },
154         credit_note             => $locale->text('Credit Note'),
155         income_statement        => { translation => $locale->text('Income Statement'),          html => 1 },
156         invoice                 => $locale->text('Invoice'),
157         pick_list               => $locale->text('Pick List'),
158         proforma                => $locale->text('Proforma Invoice'),
159         purchase_delivery_order => { translation => $::locale->text('Purchase delivery order'), tex => 1 },
160         purchase_order          => $locale->text('Purchase Order'),
161         receipt                 => { translation => $locale->text('Receipt'),                   tex => 1 },
162         request_quotation       => $locale->text('RFQ'),
163         sales_delivery_order    => { translation => $::locale->text('Sales delivery order'),    tex => 1 },
164         sales_order             => $locale->text('Confirmation'),
165         sales_quotation         => $locale->text('Quotation'),
166         statement               => $locale->text('Statement'),
167         storno_invoice          => $locale->text('Storno Invoice'),
168         "ustva-2004"            => { translation => $locale->text("USTVA 2004"),                tex => 1 },
169         "ustva-2005"            => { translation => $locale->text("USTVA 2005"),                tex => 1 },
170         "ustva-2006"            => { translation => $locale->text("USTVA 2006"),                tex => 1 },
171         "ustva-2007"            => { translation => $locale->text("USTVA 2007"),                tex => 1 },
172         ustva                   => $locale->text("USTVA"),
173       );
174
175     my (@values, $file, $setup);
176
177     while (($file, $setup) = each(%formname_setup)) {
178       next if ref($setup) && !$setup->{$format};
179
180       push(@values,
181            { "value"   => $file,
182              "label"   => ref($setup) ? $setup->{"translation"} : $setup });
183     }
184
185     # "zahlungserinnerung" => $locale->text('Payment Reminder'),
186
187     foreach my $ref (@{ $form->{"ALL_DUNNING_CONFIGS"} }) {
188       next if !$ref->{"template"};
189
190       push(@values,
191            { "value" => $ref->{"template"},
192              "label" => $locale->text('Payment Reminder') . ": " . $ref->{"dunning_description"} },
193            { "value" => $ref->{"template"} . "_invoice",
194              "label" => $locale->text('Payment Reminder') . ": " . $ref->{"dunning_description"} . ' (' . $locale->text("Invoice for fees") . ')' });
195     }
196
197     @values = sort({ $a->{"label"} cmp $b->{"label"} } @values);
198
199     #
200     # at the end: others/includes for tex
201     #
202     if ($format eq "tex") {
203       # search all .tex-files in template dir (recursively)
204       my @all_files;
205       find(
206         sub {
207           next if (-l $_ || -d $_);
208           next unless (-f $_ && $_ =~ m/.*?\.tex$/);
209
210           my $fname = $File::Find::name;
211           # remove template dir from name
212           $fname =~ s|^templates/[^/+]/||;
213           # remove .tex from name
214           $fname =~ s|.tex$||;
215
216           push(@all_files, $fname);
217
218           }, $defaults->templates);
219
220       # filter all files already set up (i.e. not already in @values)
221       my @other_files = grep { my $a=$_; not grep {$a eq $_->{value}} @values } @all_files;
222
223       # add other tex files
224       foreach my $o (@other_files) {
225         push(@values, { "value" => $o, "label" => $locale->text("Others")." ($o)" });
226       }
227     }
228
229     $options{FORMNAME} = [ @values ];
230
231     #
232     # Setup "language" selection
233     #
234
235     @values = ();
236
237     foreach my $item (@{ $form->{"ALL_LANGUAGES"} }) {
238       next unless ($item->{"template_code"});
239
240       push(@values,
241            { "value" => "$item->{id}--$item->{template_code}",
242              "label" => $item->{"description"} });
243     }
244
245     $options{LANGUAGE} = [ @values ];
246
247     #
248     # Setup "printer" selection
249     #
250
251     @values = ();
252
253     foreach my $item (@{ $form->{"ALL_PRINTERS"} }) {
254       next unless ($item->{"template_code"});
255
256       push(@values,
257            { "value" => "$item->{id}--$item->{template_code}",
258              "label" => $item->{"printer_description"} });
259     }
260
261     $options{PRINTER} = [ @values ];
262
263   } else {
264     push(@hidden, qw(formname language printer));
265   }
266
267   if ($form->{formname} || ($form->{type} eq "stylesheet")) {
268     $options{"SHOW_CONTENT"} = 1;
269
270     ($options{"filename"}, $options{"display_filename"})
271       = AM->prepare_template_filename(\%myconfig, $form);
272
273     ($options{"content"}, $options{"lines"})
274       = AM->load_template($options{"filename"});
275
276     $options{"CAN_EDIT"} = $form->{"edit"};
277
278     if ($form->{edit}) {
279       $::request->{layout}->focus("#edit_content");
280
281     } else {
282       $options{"content"}                 = "\n\n" if (!$options{"content"});
283       $options{"SHOW_SECOND_EDIT_BUTTON"} = $options{"lines"} > 25;
284     }
285   }
286
287   $options{"HIDDEN"} = [ map(+{ "name" => $_, "value" => $form->{$_} }, @hidden) ];
288
289   $form->header;
290   print($form->parse_html_template("am/edit_templates", \%options));
291
292   $main::lxdebug->leave_sub();
293 }
294
295 1;