Nummernkreise/Standardkonten in Mandantenkonfigurationsdialog verschoben
[kivitendo-erp.git] / SL / Controller / ClientConfig.pm
1 package SL::Controller::ClientConfig;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
5
6 use List::Util qw(first);
7
8 use SL::DB::Chart;
9 use SL::DB::Currency;
10 use SL::DB::Default;
11 use SL::DB::Language;
12 use SL::DB::Unit;
13 use SL::Helper::Flash;
14 use SL::Locale::String qw(t8);
15
16 __PACKAGE__->run_before('check_auth');
17
18 use Rose::Object::MakeMethods::Generic (
19   'scalar --get_set_init' => [ qw(defaults all_warehouses all_weightunits all_languages all_currencies posting_options payment_options accounting_options inventory_options profit_options accounts) ],
20 );
21
22 sub action_edit {
23   my ($self, %params) = @_;
24   $self->edit_form;
25 }
26
27 sub action_save {
28   my ($self, %params)      = @_;
29
30   my $defaults             = delete($::form->{defaults}) || {};
31   my $entered_currencies   = delete($::form->{currencies}) || [];
32   my $original_currency_id = $self->defaults->currency_id;
33
34   # undef several fields if an empty value has been selected.
35   foreach (qw(warehouse_id bin_id warehouse_id_ignore_onhand bin_id_ignore_onhand)) {
36     undef $defaults->{$_} if !$defaults->{$_};
37   }
38
39   $self->defaults->assign_attributes(%{ $defaults });
40
41   my %errors_idx;
42
43   # Handle currencies
44   my (%new_currency_names);
45   foreach my $existing_currency (@{ $self->all_currencies }) {
46     my $new_name     = $existing_currency->name;
47     my $new_currency = first { $_->{id} == $existing_currency->id } @{ $entered_currencies };
48     $new_name        = $new_currency->{name} if $new_currency;
49
50     if (!$new_name) {
51       $errors_idx{0} = t8('Currency names must not be empty.');
52     } elsif ($new_currency_names{$new_name}) {
53       $errors_idx{1} = t8('Currency names must be unique.');
54     }
55
56     if ($new_name) {
57       $new_currency_names{$new_name} = 1;
58       $existing_currency->name($new_name);
59     }
60   }
61
62   if ($::form->{new_currency} && $new_currency_names{ $::form->{new_currency} }) {
63     $errors_idx{1} = t8('Currency names must be unique.');
64   }
65
66   my @errors = map { $errors_idx{$_} } sort keys %errors_idx;
67
68   if (@errors) {
69     flash('error', @errors);
70     return $self->edit_form;
71   }
72
73   # Save currencies. As the names must be unique we cannot simply save
74   # them as they are -- the user might want to swap to names. So make
75   # them unique first and assign the actual names in a second step.
76   my %currency_names_by_id = map { ($_->id => $_->name) } @{ $self->all_currencies };
77   $_->update_attributes(name => '__039519735__' . $_->{id})        for @{ $self->all_currencies };
78   $_->update_attributes(name => $currency_names_by_id{ $_->{id} }) for @{ $self->all_currencies };
79
80   # Create new currency if required
81   my $new_currency;
82   if ($::form->{new_currency}) {
83     $new_currency = SL::DB::Currency->new(name => $::form->{new_currency});
84     $new_currency->save;
85   }
86
87   # If the user wants the new currency to be the default then replace
88   # the ID placeholder with the proper value. However, if no new
89   # currency has been created then don't change the value at all.
90   if (-1 == $self->defaults->currency_id) {
91     $self->defaults->currency_id($new_currency ? $new_currency->id : $original_currency_id);
92   }
93
94   $self->defaults->save;
95
96   flash_later('info', t8('Client Configuration saved!'));
97
98   $self->redirect_to(action => 'edit');
99 }
100
101 #
102 # initializers
103 #
104
105 sub init_defaults        { SL::DB::Default->get                                           }
106 sub init_all_warehouses  { SL::DB::Manager::Warehouse->get_all_sorted                     }
107 sub init_all_languages   { SL::DB::Manager::Language->get_all_sorted                      }
108 sub init_all_currencies  { SL::DB::Manager::Currency->get_all_sorted                      }
109 sub init_all_weightunits { SL::DB::Manager::Unit->find_by(name => 'g')->convertible_units }
110
111 sub init_posting_options {
112   [ { title => t8("never"),           value => 0           },
113     { title => t8("every time"),      value => 1           },
114     { title => t8("on the same day"), value => 2           }, ]
115 }
116
117 sub init_payment_options {
118   [ { title => t8("never"),           value => 0           },
119     { title => t8("every time"),      value => 1           },
120     { title => t8("on the same day"), value => 2           }, ]
121 }
122
123 sub init_accounting_options {
124   [ { title => t8("Accrual"),         value => "accrual"   },
125     { title => t8("cash"),            value => "cash"      }, ]
126 }
127
128 sub init_inventory_options {
129   [ { title => t8("perpetual"),       value => "perpetual" },
130     { title => t8("periodic"),        value => "periodic"  }, ]
131 }
132
133 sub init_profit_options {
134   [ { title => t8("balance"),         value => "balance"   },
135     { title => t8("income"),          value => "income"    }, ]
136 }
137
138 sub init_accounts {
139   my %accounts;
140
141   foreach my $chart (@{ SL::DB::Manager::Chart->get_all(where => [ link => { like => '%IC%' } ], sort_by => 'accno ASC') }) {
142     my %added;
143
144     foreach my $link (split m/:/, $chart->link) {
145       my $key = lc($link =~ /cogs/ ? 'IC_expense' : $link =~ /sale/ ? 'IC_income' : $link);
146       next if $added{$key};
147
148       $added{$key}      = 1;
149       $accounts{$key} ||= [];
150       push @{ $accounts{$key} }, $chart;
151     }
152   }
153
154   $accounts{fx_gain} = SL::DB::Manager::Chart->get_all(where => [ category => 'I', charttype => 'A' ], sort_by => 'accno ASC');
155   $accounts{fx_loss} = SL::DB::Manager::Chart->get_all(where => [ category => 'E', charttype => 'A' ], sort_by => 'accno ASC');
156   $accounts{ar_paid} = SL::DB::Manager::Chart->get_all(where => [ link => { like => '%AR_paid%' }   ], sort_by => 'accno ASC');
157
158   return \%accounts;
159 }
160
161 #
162 # filters
163 #
164
165 sub check_auth {
166   $::auth->assert('admin');
167 }
168
169 #
170 # helpers
171 #
172
173 sub edit_form {
174   my ($self) = @_;
175   $self->render('client_config/form', title => t8('Client Configuration'),
176                 make_chart_title => sub { $_[0]->accno . '--' . $_[0]->description });
177 }
178
179 1;