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