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