1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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,
29 #======================================================================
33 #======================================================================
40 use SL::Locale::String qw(t8);
48 require "bin/mozilla/common.pl";
53 call_sub($main::form->{display_nextsub});
57 call_sub($main::form->{save_nextsub});
61 call_sub($main::form->{edit_nextsub});
64 sub display_template {
65 $main::lxdebug->enter_sub();
67 my $form = $main::form;
69 $main::auth->assert('admin');
72 display_template_form();
74 $main::lxdebug->leave_sub();
78 $main::lxdebug->enter_sub();
80 my $form = $main::form;
82 $main::auth->assert('admin');
85 display_template_form();
87 $main::lxdebug->leave_sub();
91 $main::lxdebug->enter_sub();
93 my $form = $main::form;
94 my %myconfig = %main::myconfig;
95 my $locale = $main::locale;
97 $main::auth->assert('admin');
99 $form->isblank("formname", $locale->text("You're not editing a file."));
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));
107 display_template_form();
109 $main::lxdebug->leave_sub();
112 sub display_template_form {
113 $main::lxdebug->enter_sub();
115 my $form = $main::form;
116 my %myconfig = %main::myconfig;
117 my $locale = $main::locale;
119 $main::auth->assert('admin');
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;
124 if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
125 $form->{"formname"} =~ s|.*/||;
128 my $format = $form->{"format"} eq "html" ? "html" : "tex";
130 $form->{"title"} = $locale->text("Edit templates");
131 if ($form->{"format"}) {
132 $form->{"title"} = uc($form->{"format"}) . " - " . $form->{"title"};
137 my @hidden = qw(type format);
139 if (!$form->{"edit"}) {
140 $options{"SHOW_EDIT_OPTIONS"} = 1;
143 # Setup "formname" selection
146 $form->get_lists("printers" => "ALL_PRINTERS",
147 "languages" => "ALL_LANGUAGES",
148 "dunning_configs" => "ALL_DUNNING_CONFIGS");
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"),
177 my (@values, $file, $setup);
179 while (($file, $setup) = each(%formname_setup)) {
180 next if ref($setup) && !$setup->{$format};
184 "label" => ref($setup) ? $setup->{"translation"} : $setup });
187 # "zahlungserinnerung" => $locale->text('Payment Reminder'),
189 foreach my $ref (@{ $form->{"ALL_DUNNING_CONFIGS"} }) {
190 next if !$ref->{"template"};
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") . ')' });
199 @values = sort({ $a->{"label"} cmp $b->{"label"} } @values);
202 # at the end: others/includes for tex
204 if ($format eq "tex") {
205 # search all .tex-files in template dir (recursively)
206 my $template_dir = $defaults->templates;
210 next if (-l $_ || -d $_);
211 next unless (-f $_ && $_ =~ m/.*?\.tex$/);
213 my $fname = $File::Find::name;
214 # remove template dir from name
215 $fname =~ s|^\Q$template_dir\E/||;
216 # remove .tex from name
219 push(@all_files, $fname);
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;
226 # add other tex files
227 foreach my $o (@other_files) {
228 push(@values, { "value" => $o, "label" => $locale->text("Others")." ($o)" });
232 $options{FORMNAME} = [ @values ];
235 # Setup "language" selection
240 foreach my $item (@{ $form->{"ALL_LANGUAGES"} }) {
241 next unless ($item->{"template_code"});
244 { "value" => "$item->{id}--$item->{template_code}",
245 "label" => $item->{"description"} });
248 $options{LANGUAGE} = [ @values ];
251 # Setup "printer" selection
256 foreach my $item (@{ $form->{"ALL_PRINTERS"} }) {
257 next unless ($item->{"template_code"});
260 { "value" => "$item->{id}--$item->{template_code}",
261 "label" => $item->{"printer_description"} });
264 $options{PRINTER} = [ @values ];
267 push(@hidden, qw(formname language printer));
270 if ($form->{formname}) {
271 $options{"SHOW_CONTENT"} = 1;
273 ($options{"filename"}, $options{"display_filename"})
274 = AM->prepare_template_filename(\%myconfig, $form);
276 ($options{"content"}, $options{"lines"})
277 = AM->load_template($options{"filename"});
279 $options{"CAN_EDIT"} = $form->{"edit"};
281 if (!$form->{edit}) {
282 $options{"content"} = "\n\n" if (!$options{"content"});
286 $options{"HIDDEN"} = [ map(+{ "name" => $_, "value" => $form->{$_} }, @hidden) ];
288 setup_amtemplates_display_form_action_bar(
289 mode => $form->{edit} ? 'edit' : 'show',
290 template_selected => $options{SHOW_CONTENT},
294 print($form->parse_html_template("am/edit_templates", \%options));
296 $main::lxdebug->leave_sub();
299 sub setup_amtemplates_display_form_action_bar {
302 for my $bar ($::request->layout->get('actionbar')) {
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,
314 submit => [ '#form', { action => 'save_template' } ],
315 accesskey => 'enter',
316 only_if => $params{mode} eq 'edit',
321 call => [ 'kivi.history_back' ],
322 only_if => $params{mode} eq 'edit',