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 #======================================================================
 
  39 use List::MoreUtils qw(any);
 
  41 require "bin/mozilla/common.pl";
 
  49 my $locale   = $main::locale;
 
  50 our %translations = ('text'      => $locale->text('Free-form text'),
 
  51                      'textfield' => $locale->text('Text field'),
 
  52                      'number'    => $locale->text('Number'),
 
  53                      'date'      => $locale->text('Date'),
 
  54                      'timestamp' => $locale->text('Timestamp'),
 
  55                      'bool'      => $locale->text('Yes/No (Checkbox)'),
 
  56                      'select'    => $locale->text('Selection'),
 
  57                      'customer'  => $locale->text('Customer'),
 
  60 our @types = qw(text textfield number date bool select customer); # timestamp
 
  62 our @modules = ({ module => 'CT',       description => $locale->text('Customers and vendors')          },
 
  63                 { module => 'IC',       description => $locale->text('Parts, services and assemblies') },
 
  64                 { module => 'Projects', description => $locale->text('Projects')                       },
 
  75 sub _is_valid_module {
 
  78   return any { $_->{module} eq $module } @modules;
 
  81 sub list_cvar_configs {
 
  82   $main::lxdebug->enter_sub();
 
  84   my $form     = $main::form;
 
  85   my $locale   = $main::locale;
 
  87   $main::auth->assert('config');
 
  89   $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
 
  90   $form->{module} = 'CT' unless _is_valid_module($form->{module});
 
  92   my @configs = @{ CVar->get_configs(module => $form->{module}) };
 
  94   foreach my $config (@configs) {
 
  95     $config->{type_tr} = $translations{$config->{type}};
 
  98   $form->{title} = $locale->text('List of custom variables');
 
 100   print $form->parse_html_template('amcvar/list_cvar_configs', { CONFIGS => \@configs,
 
 101                                                                  MODULES => \@modules });
 
 103 #  $main::lxdebug->dump(0, "modules", \@modules);
 
 105   $main::lxdebug->leave_sub();
 
 108 sub add_cvar_config {
 
 109   $main::lxdebug->enter_sub();
 
 111   my $form     = $main::form;
 
 113   $main::auth->assert('config');
 
 115   $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
 
 118   display_cvar_config_form();
 
 120   $main::lxdebug->leave_sub();
 
 123 sub edit_cvar_config {
 
 124   $main::lxdebug->enter_sub();
 
 126   my $form     = $main::form;
 
 128   $main::auth->assert('config');
 
 130   my $config = CVar->get_config('id' => $form->{id});
 
 132   map { $form->{$_} = $config->{$_} } keys %{ $config };
 
 135   display_cvar_config_form();
 
 137   $main::lxdebug->leave_sub();
 
 141   $main::lxdebug->enter_sub();
 
 143   my $form     = $main::form;
 
 144   my %myconfig = %main::myconfig;
 
 145   my $locale   = $main::locale;
 
 147   $main::auth->assert('config');
 
 149   $form->isblank('name',        $locale->text('The name is missing.'));
 
 150   $form->isblank('description', $locale->text('The description is missing.'));
 
 151   $form->isblank('options',     $locale->text('The option field is empty.')) if ($form->{type} eq 'select');
 
 153   if ($form->{name} !~ /^[a-z][a-z0-9_]*$/i) {
 
 154     $form->error($locale->text('The name must only consist of letters, numbers and underscores and start with a letter.'));
 
 157   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
 
 158     $form->{default_value} = $form->parse_amount(\%myconfig, $form->{default_value});
 
 161   $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
 
 162   $form->{includeable}         = $form->{inclusion} ne 'no';
 
 163   $form->{flags}               = join ':', map { m/^flag_(.*)/; "${1}=" . $form->{$_} } grep { m/^flag_/ } keys %{ $form };
 
 165   CVar->save_config('module' => $form->{module},
 
 168   $form->{MESSAGE} = $locale->text('The custom variable has been saved.');
 
 172   $main::lxdebug->leave_sub();
 
 176   $main::lxdebug->enter_sub();
 
 178   my $form     = $main::form;
 
 179   my $locale   = $main::locale;
 
 181   CVar->delete_config('id' => $form->{id});
 
 183   $form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
 
 187   $main::lxdebug->leave_sub();
 
 190 sub display_cvar_config_form {
 
 191   $main::lxdebug->enter_sub();
 
 193   my $form     = $main::form;
 
 194   my %myconfig = %main::myconfig;
 
 195   my $locale   = $main::locale;
 
 197   $main::auth->assert('config');
 
 199   my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
 
 201   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
 
 202     $form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
 
 205   $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
 
 208   print $form->parse_html_template("amcvar/display_cvar_config_form", { TYPES   => \@types,
 
 209                                                                         MODULES => \@modules });
 
 211   $main::lxdebug->leave_sub();
 
 215   $main::lxdebug->enter_sub();
 
 217   my $form     = $main::form;
 
 219   $main::auth->assert('config');
 
 221   $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
 
 222   $form->{includeable}         = $form->{inclusion} ne 'no';
 
 224   display_cvar_config_form();
 
 226   $main::lxdebug->leave_sub();
 
 231   my $form     = $main::form;
 
 232   my $locale   = $main::locale;
 
 234   foreach my $action (qw(list_cvar_configs add_cvar_config update)) {
 
 235     if ($form->{"action_${action}"}) {
 
 241   $form->error($locale->text('No action defined.'));