9843f82b1eae32fb8e4906b2f5769f7fd0cbced4
[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 SL::AM;
35 use SL::Form;
36
37 use Data::Dumper;
38
39 1;
40
41 require "bin/mozilla/common.pl";
42
43 # end of main
44
45 sub display {
46   call_sub($form->{display_nextsub});
47 }
48
49 sub save {
50   call_sub($form->{save_nextsub});
51 }
52
53 sub edit {
54   call_sub($form->{edit_nextsub});
55 }
56
57 sub display_template {
58   $lxdebug->enter_sub();
59
60   $auth->assert('config');
61
62   $form->{edit} = 0;
63   display_template_form();
64
65   $lxdebug->leave_sub();
66 }
67
68 sub edit_template {
69   $lxdebug->enter_sub();
70
71   $auth->assert('config');
72
73   $form->{edit} = 1;
74   display_template_form();
75
76   $lxdebug->leave_sub();
77 }
78
79 sub save_template {
80   $lxdebug->enter_sub();
81
82   $auth->assert('config');
83
84   $form->isblank("formname", $locale->text("You're not editing a file.")) unless ($form->{type} eq "stylesheet");
85
86   my ($filename) = AM->prepare_template_filename(\%myconfig, $form);
87   if (my $error = AM->save_template($filename, $form->{content})) {
88     $form->error(sprintf($locale->text("Saving the file '%s' failed. OS error message: %s"), $filename, $error));
89   }
90
91   $form->{edit} = 0;
92   display_template_form();
93
94   $lxdebug->leave_sub();
95 }
96
97 sub display_template_form {
98   $lxdebug->enter_sub();
99
100   $auth->assert('config');
101
102   $form->{"formname"} =~ s|.*/||;
103   my $format = $form->{"format"} eq "html" ? "html" : "tex";
104
105   $form->{"title"} = $form->{"type"} eq "stylesheet" ? $locale->text("Edit the stylesheet") : $locale->text("Edit templates");
106
107   my %options;
108
109   my @hidden = qw(type format);
110
111   if (($form->{"type"} ne "stylesheet") && !$form->{"edit"}) {
112     $options{"SHOW_EDIT_OPTIONS"} = 1;
113
114     #
115     # Setup "formname" selection
116     #
117
118     $form->get_lists("printers" => "ALL_PRINTERS",
119                      "languages" => "ALL_LANGUAGES",
120                      "dunning_configs" => "ALL_DUNNING_CONFIGS");
121
122     my %formname_setup =
123       (
124         "balance_sheet" => { "translation" => $locale->text('Balance Sheet'), "html" => 1 },
125         "bin_list" => $locale->text('Bin List'),
126         "bwa" => { "translation" => $locale->text('BWA'), "html" => 1 },
127         "check" => { "translation" => $locale->text('Check'), "html" => 1 },
128         "credit_note" => $locale->text('Credit Note'),
129         "income_statement" => { "translation" => $locale->text('Income Statement'), "html" => 1 },
130         "invoice" => $locale->text('Invoice'),
131         "packing_list" => $locale->text('Packing List'),
132         "pick_list" => $locale->text('Pick List'),
133         "proforma" => $locale->text('Proforma Invoice'),
134         "purchase_order" => $locale->text('Purchase Order'),
135         "receipt" => { "translation" => $locale->text('Receipt'), "tex" => 1 },
136         "request_quotation" => $locale->text('RFQ'),
137         "sales_order" => $locale->text('Confirmation'),
138         "sales_quotation" => $locale->text('Quotation'),
139         "statement" => $locale->text('Statement'),
140         "storno_invoice" => $locale->text('Storno Invoice'),
141         "storno_packing_list" => $locale->text('Storno Packing List'),
142         "ustva-2004" => { "translation" => $locale->text("USTVA 2004"), "tex" => 1 },
143         "ustva-2005" => { "translation" => $locale->text("USTVA 2005"), "tex" => 1 },
144         "ustva-2006" => { "translation" => $locale->text("USTVA 2006"), "tex" => 1 },
145         "ustva-2007" => { "translation" => $locale->text("USTVA 2007"), "tex" => 1 },
146         "ustva" => $locale->text("USTVA"),
147       );
148
149     my (@values, $file, $setup);
150
151     while (($file, $setup) = each(%formname_setup)) {
152       next if ref($setup) && !$setup->{$format};
153
154       push(@values,
155            { "value"   => $file,
156              "label"   => ref($setup) ? $setup->{"translation"} : $setup });
157     }
158
159     # "zahlungserinnerung" => $locale->text('Payment Reminder'),
160
161     foreach my $ref (@{ $form->{"ALL_DUNNING_CONFIGS"} }) {
162       next if !$ref->{"template"};
163
164       push(@values,
165            { "value" => $ref->{"template"},
166              "label" => $locale->text('Payment Reminder') . ": " . $ref->{"dunning_description"} },
167            { "value" => $ref->{"template"} . "_invoice",
168              "label" => $locale->text('Payment Reminder') . ": " . $ref->{"dunning_description"} . ' (' . $locale->text("Invoice for fees") . ')' });
169     }
170
171     @values = sort({ $a->{"label"} cmp $b->{"label"} } @values);
172
173     $options{FORMNAME} = [ @values ];
174
175     #
176     # Setup "language" selection
177     #
178
179     @values = ();
180
181     foreach my $item (@{ $form->{"ALL_LANGUAGES"} }) {
182       next unless ($item->{"template_code"});
183
184       push(@values,
185            { "value" => "$item->{id}--$item->{template_code}",
186              "label" => $item->{"description"} });
187     }
188
189     $options{LANGUAGE} = [ @values ];
190
191     #
192     # Setup "printer" selection
193     #
194
195     @values = ();
196
197     foreach my $item (@{ $form->{"ALL_PRINTERS"} }) {
198       next unless ($item->{"template_code"});
199
200       push(@values,
201            { "value" => "$item->{id}--$item->{template_code}",
202              "label" => $item->{"printer_description"} });
203     }
204
205     $options{PRINTER} = [ @values ];
206
207   } else {
208     push(@hidden, qw(formname language printer));
209   }
210
211   if ($form->{formname} || ($form->{type} eq "stylesheet")) {
212     $options{"SHOW_CONTENT"} = 1;
213
214     ($options{"filename"}, $options{"display_filename"})
215       = AM->prepare_template_filename(\%myconfig, $form);
216
217     ($options{"content"}, $options{"lines"})
218       = AM->load_template($options{"filename"});
219
220     $options{"CAN_EDIT"} = $form->{"edit"};
221
222     if ($form->{edit}) {
223       $form->{fokus} = "Form.content";
224
225     } else {
226       $options{"content"}                 = "\n\n" if (!$options{"content"});
227       $options{"SHOW_SECOND_EDIT_BUTTON"} = $options{"lines"} > 25;
228     }
229   }
230
231   $options{"HIDDEN"} = [ map(+{ "name" => $_, "value" => $form->{$_} }, @hidden) ];
232
233   $form->header;
234   print($form->parse_html_template("am/edit_templates", \%options));
235
236   $lxdebug->leave_sub();
237 }
238
239 1;