epic-ts
[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 $template_dir = $defaults->templates;
205       my @all_files;
206       find(
207         sub {
208           next if (-l $_ || -d $_);
209           next unless (-f $_ && $_ =~ m/.*?\.tex$/);
210
211           my $fname = $File::Find::name;
212           # remove template dir from name
213           $fname =~ s|^\Q$template_dir\E/||;
214           # remove .tex from name
215           $fname =~ s|.tex$||;
216
217           push(@all_files, $fname);
218
219           }, $template_dir);
220
221       # filter all files already set up (i.e. not already in @values)
222       my @other_files = grep { my $a=$_; not grep {$a eq $_->{value}} @values } @all_files;
223
224       # add other tex files
225       foreach my $o (@other_files) {
226         push(@values, { "value" => $o, "label" => $locale->text("Others")." ($o)" });
227       }
228     }
229
230     $options{FORMNAME} = [ @values ];
231
232     #
233     # Setup "language" selection
234     #
235
236     @values = ();
237
238     foreach my $item (@{ $form->{"ALL_LANGUAGES"} }) {
239       next unless ($item->{"template_code"});
240
241       push(@values,
242            { "value" => "$item->{id}--$item->{template_code}",
243              "label" => $item->{"description"} });
244     }
245
246     $options{LANGUAGE} = [ @values ];
247
248     #
249     # Setup "printer" selection
250     #
251
252     @values = ();
253
254     foreach my $item (@{ $form->{"ALL_PRINTERS"} }) {
255       next unless ($item->{"template_code"});
256
257       push(@values,
258            { "value" => "$item->{id}--$item->{template_code}",
259              "label" => $item->{"printer_description"} });
260     }
261
262     $options{PRINTER} = [ @values ];
263
264   } else {
265     push(@hidden, qw(formname language printer));
266   }
267
268   if ($form->{formname} || ($form->{type} eq "stylesheet")) {
269     $options{"SHOW_CONTENT"} = 1;
270
271     ($options{"filename"}, $options{"display_filename"})
272       = AM->prepare_template_filename(\%myconfig, $form);
273
274     ($options{"content"}, $options{"lines"})
275       = AM->load_template($options{"filename"});
276
277     $options{"CAN_EDIT"} = $form->{"edit"};
278
279     if (!$form->{edit}) {
280       $options{"content"}                 = "\n\n" if (!$options{"content"});
281       $options{"SHOW_SECOND_EDIT_BUTTON"} = $options{"lines"} > 25;
282     }
283   }
284
285   $options{"HIDDEN"} = [ map(+{ "name" => $_, "value" => $form->{$_} }, @hidden) ];
286
287   $form->header;
288   print($form->parse_html_template("am/edit_templates", \%options));
289
290   $main::lxdebug->leave_sub();
291 }
292
293 1;