d32cf790a1b5602839ea4b6fd85cd6b2ca4c9d72
[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., 51 Franklin Street, Fifth Floor, Boston,
28 # MA 02110-1335, USA.
29 #======================================================================
30 #
31 # administration
32 #
33 #======================================================================
34
35 use File::Find;
36
37 use SL::DB::Default;
38 use SL::AM;
39 use SL::Form;
40
41 use Data::Dumper;
42
43 use strict;
44
45 1;
46
47 require "bin/mozilla/common.pl";
48
49 # end of main
50
51 sub display {
52   call_sub($main::form->{display_nextsub});
53 }
54
55 sub save {
56   call_sub($main::form->{save_nextsub});
57 }
58
59 sub edit {
60   call_sub($main::form->{edit_nextsub});
61 }
62
63 sub display_template {
64   $main::lxdebug->enter_sub();
65
66   my $form     = $main::form;
67
68   $main::auth->assert('admin');
69
70   $form->{edit} = 0;
71   display_template_form();
72
73   $main::lxdebug->leave_sub();
74 }
75
76 sub edit_template {
77   $main::lxdebug->enter_sub();
78
79   my $form     = $main::form;
80
81   $main::auth->assert('admin');
82
83   $form->{edit} = 1;
84   display_template_form();
85
86   $main::lxdebug->leave_sub();
87 }
88
89 sub save_template {
90   $main::lxdebug->enter_sub();
91
92   my $form     = $main::form;
93   my %myconfig = %main::myconfig;
94   my $locale   = $main::locale;
95
96   $main::auth->assert('admin');
97
98   $form->isblank("formname", $locale->text("You're not editing a file.")) unless ($form->{type} eq "stylesheet");
99
100   my ($filename) = AM->prepare_template_filename(\%myconfig, $form);
101   if (my $error = AM->save_template($filename, $form->{content})) {
102     $form->error(sprintf($locale->text("Saving the file '%s' failed. OS error message: %s"), $filename, $error));
103   }
104
105   $form->{edit} = 0;
106   display_template_form();
107
108   $main::lxdebug->leave_sub();
109 }
110
111 sub display_template_form {
112   $main::lxdebug->enter_sub();
113
114   my $form     = $main::form;
115   my %myconfig = %main::myconfig;
116   my $locale   = $main::locale;
117
118   $main::auth->assert('admin');
119
120   my $defaults = SL::DB::Default->get;
121   $form->error($::locale->text('No print templates have been created for this client yet. Please do so in the client configuration.')) if !$defaults->templates;
122
123   if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
124     $form->{"formname"} =~ s|.*/||;
125   }
126
127   my $format = $form->{"format"} eq "html" ? "html" : "tex";
128
129   $form->{"title"} = $form->{"type"} eq "stylesheet" ? $locale->text("Edit the stylesheet") : $locale->text("Edit templates");
130   if ($form->{"format"}) {
131       $form->{"title"} = uc($form->{"format"}) . " - " . $form->{"title"};
132   }
133
134   my %options;
135
136   my @hidden = qw(type format);
137
138   if (($form->{"type"} ne "stylesheet") && !$form->{"edit"}) {
139     $options{"SHOW_EDIT_OPTIONS"} = 1;
140
141     #
142     # Setup "formname" selection
143     #
144
145     $form->get_lists("printers" => "ALL_PRINTERS",
146                      "languages" => "ALL_LANGUAGES",
147                      "dunning_configs" => "ALL_DUNNING_CONFIGS");
148
149     my %formname_setup =
150       (
151         # balance_sheet           => { translation => $locale->text('Balance Sheet'),             html => 1 },
152         bin_list                => $locale->text('Bin List'),
153         bwa                     => { translation => $locale->text('BWA'),                       html => 1 },
154         check                   => { translation => $locale->text('Check'),                     html => 1 },
155         credit_note             => $locale->text('Credit Note'),
156         income_statement        => { translation => $locale->text('Income Statement'),          html => 1 },
157         invoice                 => $locale->text('Invoice'),
158         pick_list               => $locale->text('Pick List'),
159         proforma                => $locale->text('Proforma Invoice'),
160         purchase_delivery_order => { translation => $::locale->text('Purchase delivery order'), tex => 1 },
161         purchase_order          => $locale->text('Purchase Order'),
162         receipt                 => { translation => $locale->text('Receipt'),                   tex => 1 },
163         request_quotation       => $locale->text('RFQ'),
164         sales_delivery_order    => { translation => $::locale->text('Sales delivery order'),    tex => 1 },
165         sales_order             => $locale->text('Confirmation'),
166         sales_quotation         => $locale->text('Quotation'),
167         statement               => $locale->text('Statement'),
168         storno_invoice          => $locale->text('Storno Invoice'),
169         "ustva-2004"            => { translation => $locale->text("USTVA 2004"),                tex => 1 },
170         "ustva-2005"            => { translation => $locale->text("USTVA 2005"),                tex => 1 },
171         "ustva-2006"            => { translation => $locale->text("USTVA 2006"),                tex => 1 },
172         "ustva-2007"            => { translation => $locale->text("USTVA 2007"),                tex => 1 },
173         ustva                   => $locale->text("USTVA"),
174       );
175
176     my (@values, $file, $setup);
177
178     while (($file, $setup) = each(%formname_setup)) {
179       next if ref($setup) && !$setup->{$format};
180
181       push(@values,
182            { "value"   => $file,
183              "label"   => ref($setup) ? $setup->{"translation"} : $setup });
184     }
185
186     # "zahlungserinnerung" => $locale->text('Payment Reminder'),
187
188     foreach my $ref (@{ $form->{"ALL_DUNNING_CONFIGS"} }) {
189       next if !$ref->{"template"};
190
191       push(@values,
192            { "value" => $ref->{"template"},
193              "label" => $locale->text('Payment Reminder') . ": " . $ref->{"dunning_description"} },
194            { "value" => $ref->{"template"} . "_invoice",
195              "label" => $locale->text('Payment Reminder') . ": " . $ref->{"dunning_description"} . ' (' . $locale->text("Invoice for fees") . ')' });
196     }
197
198     @values = sort({ $a->{"label"} cmp $b->{"label"} } @values);
199
200     #
201     # at the end: others/includes for tex
202     #
203     if ($format eq "tex") {
204       # search all .tex-files in template dir (recursively)
205       my $template_dir = $defaults->templates;
206       my @all_files;
207       find(
208         sub {
209           next if (-l $_ || -d $_);
210           next unless (-f $_ && $_ =~ m/.*?\.tex$/);
211
212           my $fname = $File::Find::name;
213           # remove template dir from name
214           $fname =~ s|^\Q$template_dir\E/||;
215           # remove .tex from name
216           $fname =~ s|.tex$||;
217
218           push(@all_files, $fname);
219
220           }, $template_dir);
221
222       # filter all files already set up (i.e. not already in @values)
223       my @other_files = grep { my $a=$_; not grep {$a eq $_->{value}} @values } @all_files;
224
225       # add other tex files
226       foreach my $o (@other_files) {
227         push(@values, { "value" => $o, "label" => $locale->text("Others")." ($o)" });
228       }
229     }
230
231     $options{FORMNAME} = [ @values ];
232
233     #
234     # Setup "language" selection
235     #
236
237     @values = ();
238
239     foreach my $item (@{ $form->{"ALL_LANGUAGES"} }) {
240       next unless ($item->{"template_code"});
241
242       push(@values,
243            { "value" => "$item->{id}--$item->{template_code}",
244              "label" => $item->{"description"} });
245     }
246
247     $options{LANGUAGE} = [ @values ];
248
249     #
250     # Setup "printer" selection
251     #
252
253     @values = ();
254
255     foreach my $item (@{ $form->{"ALL_PRINTERS"} }) {
256       next unless ($item->{"template_code"});
257
258       push(@values,
259            { "value" => "$item->{id}--$item->{template_code}",
260              "label" => $item->{"printer_description"} });
261     }
262
263     $options{PRINTER} = [ @values ];
264
265   } else {
266     push(@hidden, qw(formname language printer));
267   }
268
269   if ($form->{formname} || ($form->{type} eq "stylesheet")) {
270     $options{"SHOW_CONTENT"} = 1;
271
272     ($options{"filename"}, $options{"display_filename"})
273       = AM->prepare_template_filename(\%myconfig, $form);
274
275     ($options{"content"}, $options{"lines"})
276       = AM->load_template($options{"filename"});
277
278     $options{"CAN_EDIT"} = $form->{"edit"};
279
280     if (!$form->{edit}) {
281       $options{"content"}                 = "\n\n" if (!$options{"content"});
282       $options{"SHOW_SECOND_EDIT_BUTTON"} = $options{"lines"} > 25;
283     }
284   }
285
286   $options{"HIDDEN"} = [ map(+{ "name" => $_, "value" => $form->{$_} }, @hidden) ];
287
288   $form->header;
289   print($form->parse_html_template("am/edit_templates", \%options));
290
291   $main::lxdebug->leave_sub();
292 }
293
294 1;