Waren und Lieferanten als benutzerdefinierte Variablen hinzugefĆ¼gt.
[kivitendo-erp.git] / bin / mozilla / amcvar.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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # administration
31 #
32 #======================================================================
33
34 use SL::AM;
35 use SL::CVar;
36 use SL::Form;
37
38 use Data::Dumper;
39 use List::MoreUtils qw(any);
40
41 require "bin/mozilla/common.pl";
42
43 use strict;
44
45 1;
46
47 # end of main
48
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'),
58                      'vendor'    => $locale->text('Vendor'),
59                      'part'      => $locale->text('Part'),
60                      );
61
62 our @types = qw(text textfield number date bool select customer vendor part); # timestamp
63
64 our @modules = ({ module => 'CT',       description => $locale->text('Customers and vendors')          },
65                 { module => 'Contacts', description => $locale->text('Contact persons')                },
66                 { module => 'IC',       description => $locale->text('Parts, services and assemblies') },
67                 { module => 'Projects', description => $locale->text('Projects')                       },
68                );
69
70 sub add {
71   add_cvar_config();
72 }
73
74 sub edit {
75   edit_cvar_config();
76 }
77
78 sub _is_valid_module {
79   my $module = shift;
80
81   return any { $_->{module} eq $module } @modules;
82 }
83
84 sub list_cvar_configs {
85   $main::lxdebug->enter_sub();
86
87   my $form     = $main::form;
88   my $locale   = $main::locale;
89
90   $main::auth->assert('config');
91
92   $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
93   $form->{module} = 'CT' unless _is_valid_module($form->{module});
94
95   my @configs = @{ CVar->get_configs(module => $form->{module}) };
96
97   foreach my $config (@configs) {
98     $config->{type_tr} = $translations{$config->{type}};
99   }
100
101   $form->{title} = $locale->text('List of custom variables');
102   $form->header();
103   print $form->parse_html_template('amcvar/list_cvar_configs', { CONFIGS => \@configs,
104                                                                  MODULES => \@modules });
105
106 #  $main::lxdebug->dump(0, "modules", \@modules);
107
108   $main::lxdebug->leave_sub();
109 }
110
111 sub add_cvar_config {
112   $main::lxdebug->enter_sub();
113
114   my $form     = $main::form;
115
116   $main::auth->assert('config');
117
118   $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
119
120   $form->{edit} = 0;
121   display_cvar_config_form();
122
123   $main::lxdebug->leave_sub();
124 }
125
126 sub edit_cvar_config {
127   $main::lxdebug->enter_sub();
128
129   my $form     = $main::form;
130
131   $main::auth->assert('config');
132
133   my $config = CVar->get_config('id' => $form->{id});
134
135   map { $form->{$_} = $config->{$_} } keys %{ $config };
136
137   $form->{edit} = 1;
138   display_cvar_config_form();
139
140   $main::lxdebug->leave_sub();
141 }
142
143 sub save {
144   $main::lxdebug->enter_sub();
145
146   my $form     = $main::form;
147   my %myconfig = %main::myconfig;
148   my $locale   = $main::locale;
149
150   $main::auth->assert('config');
151
152   $form->isblank('name',        $locale->text('The name is missing.'));
153   $form->isblank('description', $locale->text('The description is missing.'));
154   $form->isblank('options',     $locale->text('The option field is empty.')) if ($form->{type} eq 'select');
155
156   if ($form->{name} !~ /^[a-z][a-z0-9_]*$/i) {
157     $form->error($locale->text('The name must only consist of letters, numbers and underscores and start with a letter.'));
158   }
159
160   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
161     $form->{default_value} = $form->parse_amount(\%myconfig, $form->{default_value});
162   }
163
164   $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
165   $form->{includeable}         = $form->{inclusion} ne 'no';
166   $form->{flags}               = join ':', map { m/^flag_(.*)/; "${1}=" . $form->{$_} } grep { m/^flag_/ } keys %{ $form };
167
168   CVar->save_config('module' => $form->{module},
169                     'config' => $form);
170
171   $form->{MESSAGE} = $locale->text('The custom variable has been saved.');
172
173   list_cvar_configs();
174
175   $main::lxdebug->leave_sub();
176 }
177
178 sub delete {
179   $main::lxdebug->enter_sub();
180
181   my $form     = $main::form;
182   my $locale   = $main::locale;
183
184   CVar->delete_config('id' => $form->{id});
185
186   $form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
187
188   list_cvar_configs();
189
190   $main::lxdebug->leave_sub();
191 }
192
193 sub display_cvar_config_form {
194   $main::lxdebug->enter_sub();
195
196   my $form     = $main::form;
197   my %myconfig = %main::myconfig;
198   my $locale   = $main::locale;
199
200   $main::auth->assert('config');
201
202   my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
203
204   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
205     $form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
206   }
207
208   $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
209
210   $form->header();
211   print $form->parse_html_template("amcvar/display_cvar_config_form", { TYPES   => \@types,
212                                                                         MODULES => \@modules });
213
214   $main::lxdebug->leave_sub();
215 }
216
217 sub update {
218   $main::lxdebug->enter_sub();
219
220   my $form     = $main::form;
221
222   $main::auth->assert('config');
223
224   $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
225   $form->{includeable}         = $form->{inclusion} ne 'no';
226
227   display_cvar_config_form();
228
229   $main::lxdebug->leave_sub();
230 }
231
232
233 sub dispatcher {
234   my $form     = $main::form;
235   my $locale   = $main::locale;
236
237   foreach my $action (qw(list_cvar_configs add_cvar_config update)) {
238     if ($form->{"action_${action}"}) {
239       call_sub($action);
240       return;
241     }
242   }
243
244   $form->error($locale->text('No action defined.'));
245 }
246
247 1;
248