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'),
 
  59 our @types = qw(text textfield number date bool select); # timestamp
 
  61 our @modules = ({ module => 'CT',       description => $locale->text('Customers and vendors')          },
 
  62                 { module => 'IC',       description => $locale->text('Parts, services and assemblies') },
 
  63                 { module => 'Projects', description => $locale->text('Projects')                       },
 
  74 sub _is_valid_module {
 
  77   return any { $_->{module} eq $module } @modules;
 
  80 sub list_cvar_configs {
 
  81   $main::lxdebug->enter_sub();
 
  83   my $form     = $main::form;
 
  84   my $locale   = $main::locale;
 
  86   $main::auth->assert('config');
 
  88   $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
 
  89   $form->{module} = 'CT' unless _is_valid_module($form->{module});
 
  91   my @configs = @{ CVar->get_configs(module => $form->{module}) };
 
  95   foreach my $config (@configs) {
 
  96     $config->{type_tr} = $translations{$config->{type}};
 
  98     if ($previous_config) {
 
  99       $previous_config->{next_id} = $config->{id};
 
 100       $config->{previous_id}      = $previous_config->{id};
 
 103     $previous_config = $config;
 
 106   $form->{title} = $locale->text('List of custom variables');
 
 108   print $form->parse_html_template('amcvar/list_cvar_configs', { CONFIGS => \@configs,
 
 109                                                                  MODULES => \@modules });
 
 111 #  $main::lxdebug->dump(0, "modules", \@modules);
 
 113   $main::lxdebug->leave_sub();
 
 116 sub add_cvar_config {
 
 117   $main::lxdebug->enter_sub();
 
 119   my $form     = $main::form;
 
 121   $main::auth->assert('config');
 
 123   $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
 
 126   display_cvar_config_form();
 
 128   $main::lxdebug->leave_sub();
 
 131 sub edit_cvar_config {
 
 132   $main::lxdebug->enter_sub();
 
 134   my $form     = $main::form;
 
 136   $main::auth->assert('config');
 
 138   my $config = CVar->get_config('id' => $form->{id});
 
 140   map { $form->{$_} = $config->{$_} } keys %{ $config };
 
 143   display_cvar_config_form();
 
 145   $main::lxdebug->leave_sub();
 
 149   $main::lxdebug->enter_sub();
 
 151   my $form     = $main::form;
 
 152   my %myconfig = %main::myconfig;
 
 153   my $locale   = $main::locale;
 
 155   $main::auth->assert('config');
 
 157   $form->isblank('name',        $locale->text('The name is missing.'));
 
 158   $form->isblank('description', $locale->text('The description is missing.'));
 
 159   $form->isblank('options',     $locale->text('The option field is empty.')) if ($form->{type} eq 'select');
 
 161   if ($form->{name} !~ /^[a-z][a-z0-9_]*$/i) {
 
 162     $form->error($locale->text('The name must only consist of letters, numbers and underscores and start with a letter.'));
 
 165   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
 
 166     $form->{default_value} = $form->parse_amount(\%myconfig, $form->{default_value});
 
 169   $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
 
 170   $form->{includeable}         = $form->{inclusion} ne 'no';
 
 171   $form->{flags}               = join ':', map { m/^flag_(.*)/; "${1}=" . $form->{$_} } grep { m/^flag_/ } keys %{ $form };
 
 173   CVar->save_config('module' => $form->{module},
 
 176   $form->{MESSAGE} = $locale->text('The custom variable has been saved.');
 
 180   $main::lxdebug->leave_sub();
 
 184   $main::lxdebug->enter_sub();
 
 186   my $form     = $main::form;
 
 187   my $locale   = $main::locale;
 
 189   CVar->delete_config('id' => $form->{id});
 
 191   $form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
 
 195   $main::lxdebug->leave_sub();
 
 198 sub display_cvar_config_form {
 
 199   $main::lxdebug->enter_sub();
 
 201   my $form     = $main::form;
 
 202   my %myconfig = %main::myconfig;
 
 203   my $locale   = $main::locale;
 
 205   $main::auth->assert('config');
 
 207   my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
 
 209   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
 
 210     $form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
 
 213   $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
 
 216   print $form->parse_html_template("amcvar/display_cvar_config_form", { TYPES   => \@types,
 
 217                                                                         MODULES => \@modules });
 
 219   $main::lxdebug->leave_sub();
 
 222 sub swap_cvar_configs {
 
 223   $main::lxdebug->enter_sub();
 
 225   my $form     = $main::form;
 
 226   my %myconfig = %main::myconfig;
 
 228   AM->swap_sortkeys(\%myconfig, $form, 'custom_variable_configs');
 
 232   $main::lxdebug->leave_sub();
 
 236   my $form     = $main::form;
 
 237   my $locale   = $main::locale;
 
 239   foreach my $action (qw(list_cvar_configs add_cvar_config)) {
 
 240     if ($form->{"action_${action}"}) {
 
 246   $form->error($locale->text('No action defined.'));