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., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  28 #======================================================================
 
  32 #======================================================================
 
  46 require "bin/mozilla/common.pl";
 
  51   call_sub($main::form->{display_nextsub});
 
  55   call_sub($main::form->{save_nextsub});
 
  59   call_sub($main::form->{edit_nextsub});
 
  62 sub display_template {
 
  63   $main::lxdebug->enter_sub();
 
  65   my $form     = $main::form;
 
  67   $main::auth->assert('admin');
 
  70   display_template_form();
 
  72   $main::lxdebug->leave_sub();
 
  76   $main::lxdebug->enter_sub();
 
  78   my $form     = $main::form;
 
  80   $main::auth->assert('admin');
 
  83   display_template_form();
 
  85   $main::lxdebug->leave_sub();
 
  89   $main::lxdebug->enter_sub();
 
  91   my $form     = $main::form;
 
  92   my %myconfig = %main::myconfig;
 
  93   my $locale   = $main::locale;
 
  95   $main::auth->assert('admin');
 
  97   $form->isblank("formname", $locale->text("You're not editing a file.")) unless ($form->{type} eq "stylesheet");
 
  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));
 
 105   display_template_form();
 
 107   $main::lxdebug->leave_sub();
 
 110 sub display_template_form {
 
 111   $main::lxdebug->enter_sub();
 
 113   my $form     = $main::form;
 
 114   my %myconfig = %main::myconfig;
 
 115   my $locale   = $main::locale;
 
 117   $main::auth->assert('admin');
 
 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;
 
 122   if ($form->{"formname"} =~ m|\.\.| || $form->{"formname"} =~ m|^/|) {
 
 123     $form->{"formname"} =~ s|.*/||;
 
 126   my $format = $form->{"format"} eq "html" ? "html" : "tex";
 
 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"};
 
 135   my @hidden = qw(type format);
 
 137   if (($form->{"type"} ne "stylesheet") && !$form->{"edit"}) {
 
 138     $options{"SHOW_EDIT_OPTIONS"} = 1;
 
 141     # Setup "formname" selection
 
 144     $form->get_lists("printers" => "ALL_PRINTERS",
 
 145                      "languages" => "ALL_LANGUAGES",
 
 146                      "dunning_configs" => "ALL_DUNNING_CONFIGS");
 
 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"),
 
 175     my (@values, $file, $setup);
 
 177     while (($file, $setup) = each(%formname_setup)) {
 
 178       next if ref($setup) && !$setup->{$format};
 
 182              "label"   => ref($setup) ? $setup->{"translation"} : $setup });
 
 185     # "zahlungserinnerung" => $locale->text('Payment Reminder'),
 
 187     foreach my $ref (@{ $form->{"ALL_DUNNING_CONFIGS"} }) {
 
 188       next if !$ref->{"template"};
 
 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") . ')' });
 
 197     @values = sort({ $a->{"label"} cmp $b->{"label"} } @values);
 
 200     # at the end: others/includes for tex
 
 202     if ($format eq "tex") {
 
 203       # search all .tex-files in template dir (recursively)
 
 204       my $template_dir = $defaults->templates;
 
 208           next if (-l $_ || -d $_);
 
 209           next unless (-f $_ && $_ =~ m/.*?\.tex$/);
 
 211           my $fname = $File::Find::name;
 
 212           # remove template dir from name
 
 213           $fname =~ s|^\Q$template_dir\E/||;
 
 214           # remove .tex from name
 
 217           push(@all_files, $fname);
 
 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;
 
 224       # add other tex files
 
 225       foreach my $o (@other_files) {
 
 226         push(@values, { "value" => $o, "label" => $locale->text("Others")." ($o)" });
 
 230     $options{FORMNAME} = [ @values ];
 
 233     # Setup "language" selection
 
 238     foreach my $item (@{ $form->{"ALL_LANGUAGES"} }) {
 
 239       next unless ($item->{"template_code"});
 
 242            { "value" => "$item->{id}--$item->{template_code}",
 
 243              "label" => $item->{"description"} });
 
 246     $options{LANGUAGE} = [ @values ];
 
 249     # Setup "printer" selection
 
 254     foreach my $item (@{ $form->{"ALL_PRINTERS"} }) {
 
 255       next unless ($item->{"template_code"});
 
 258            { "value" => "$item->{id}--$item->{template_code}",
 
 259              "label" => $item->{"printer_description"} });
 
 262     $options{PRINTER} = [ @values ];
 
 265     push(@hidden, qw(formname language printer));
 
 268   if ($form->{formname} || ($form->{type} eq "stylesheet")) {
 
 269     $options{"SHOW_CONTENT"} = 1;
 
 271     ($options{"filename"}, $options{"display_filename"})
 
 272       = AM->prepare_template_filename(\%myconfig, $form);
 
 274     ($options{"content"}, $options{"lines"})
 
 275       = AM->load_template($options{"filename"});
 
 277     $options{"CAN_EDIT"} = $form->{"edit"};
 
 279     if (!$form->{edit}) {
 
 280       $options{"content"}                 = "\n\n" if (!$options{"content"});
 
 281       $options{"SHOW_SECOND_EDIT_BUTTON"} = $options{"lines"} > 25;
 
 285   $options{"HIDDEN"} = [ map(+{ "name" => $_, "value" => $form->{$_} }, @hidden) ];
 
 288   print($form->parse_html_template("am/edit_templates", \%options));
 
 290   $main::lxdebug->leave_sub();