]> wagnertech.de Git - mfinanz.git/blob - SL/Controller/ClientConfig.pm
Merge branch 'master' of http://wagnertech.de/git/mfinanz
[mfinanz.git] / SL / Controller / ClientConfig.pm
1 package SL::Controller::ClientConfig;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
5
6 use File::Copy::Recursive ();
7 use List::Util qw(first);
8
9 use SL::DB::Chart;
10 use SL::DB::Currency;
11 use SL::DB::Default;
12 use SL::DB::Language;
13 use SL::DB::Part;
14 use SL::DB::Unit;
15 use SL::DB::Customer;
16 use SL::Helper::Flash;
17 use SL::Locale::String qw(t8);
18 use SL::PriceSource::ALL;
19 use SL::Template;
20 use SL::DB::Order::TypeData;
21 use SL::DB::DeliveryOrder::TypeData;
22 use SL::DB::Reclamation::TypeData;
23 use SL::Controller::TopQuickSearch;
24 use SL::DB::Helper::AccountingPeriod qw(get_balance_startdate_method_options);
25 use SL::VATIDNr;
26 use SL::ZUGFeRD;
27
28 __PACKAGE__->run_before('check_auth');
29
30 use Rose::Object::MakeMethods::Generic (
31   'scalar --get_set_init' => [ qw(defaults all_warehouses all_weightunits all_languages all_currencies all_templates all_price_sources h_unit_name available_quick_search_modules
32                                   all_project_statuses all_project_types zugferd_settings
33                                   posting_options payment_options accounting_options inventory_options profit_options balance_startdate_method_options yearend_options
34                                   displayable_name_specs_by_module available_documents_with_no_positions) ],
35 );
36
37 sub action_edit {
38   my ($self, %params) = @_;
39
40   $::form->{use_templates} = $self->defaults->templates ? 'existing' : 'new';
41   $::form->{feature_datev} = $self->defaults->feature_datev;
42   $self->edit_form;
43 }
44
45 sub action_save {
46   my ($self, %params)      = @_;
47
48   my $defaults             = delete($::form->{defaults}) || {};
49   my $entered_currencies   = delete($::form->{currencies}) || [];
50   my $original_currency_id = $self->defaults->currency_id;
51   $defaults->{disabled_price_sources} ||= [];
52
53   # undef several fields if an empty value has been selected.
54   foreach (qw(warehouse_id bin_id warehouse_id_ignore_onhand bin_id_ignore_onhand)) {
55     undef $defaults->{$_} if !$defaults->{$_};
56   }
57
58   $self->defaults->assign_attributes(%{ $defaults });
59
60   my %errors_idx;
61
62   # Handle currencies
63   my (%new_currency_names);
64   foreach my $existing_currency (@{ $self->all_currencies }) {
65     my $new_name     = $existing_currency->name;
66     my $new_currency = first { $_->{id} == $existing_currency->id } @{ $entered_currencies };
67     $new_name        = $new_currency->{name} if $new_currency;
68
69     if (!$new_name) {
70       $errors_idx{0} = t8('Currency names must not be empty.');
71     } elsif ($new_currency_names{$new_name}) {
72       $errors_idx{1} = t8('Currency names must be unique.');
73     }
74
75     if ($new_name) {
76       $new_currency_names{$new_name} = 1;
77       $existing_currency->name($new_name);
78     }
79   }
80   if ($::form->{new_currency} && $new_currency_names{ $::form->{new_currency} }) {
81     $errors_idx{1} = t8('Currency names must be unique.');
82   }
83
84   my @errors = map { $errors_idx{$_} } sort keys %errors_idx;
85
86   # check valid mail adresses
87   foreach (qw(email_sender_sales_quotation email_sender_request_quotation email_sender_sales_order
88              email_sender_purchase_order email_sender_sales_delivery_order email_sender_purchase_delivery_order
89              email_sender_invoice email_sender_purchase_invoice email_sender_letter email_sender_dunning
90              global_bcc)) {
91     next unless $defaults->{$_};
92     next if     $defaults->{$_} =~ /^[a-z0-9.]+\@[a-z0-9.-]+$/i;
93     push @errors, t8('The email entry for #1 looks invalid', $_);
94   }
95   # Check templates
96   $::form->{new_templates}        =~ s:/::g;
97   $::form->{new_master_templates} =~ s:/::g;
98
99   if (($::form->{use_templates} eq 'existing') && ($self->defaults->templates !~ m:^templates/[^/]+$:)) {
100     push @errors, t8('You must select existing print templates or create a new set.');
101
102   } elsif ($::form->{use_templates} eq 'new') {
103     if (!$::form->{new_templates}) {
104       push @errors, t8('You must enter a name for your new print templates.');
105     } elsif (-d "templates/" . $::form->{new_templates}) {
106       push @errors, t8('A directory with the name for the new print templates exists already.');
107     } elsif (! -d "templates/print/" . $::form->{new_master_templates}) {
108       push @errors, t8('The master templates where not found.');
109     }
110   }
111
112   my $cleaned_ustid = SL::VATIDNr->clean($defaults->{co_ustid});
113   if ($cleaned_ustid && !SL::VATIDNr->validate($cleaned_ustid)) {
114     push @errors, t8("The VAT ID number '#1' is invalid.", $defaults->{co_ustid});
115   }
116
117   # Show form again if there were any errors. Nothing's been changed
118   # yet in the database.
119   if (@errors) {
120     flash('error', @errors);
121     return $self->edit_form;
122   }
123
124   # Save currencies. As the names must be unique we cannot simply save
125   # them as they are -- the user might want to swap to names. So make
126   # them unique first and assign the actual names in a second step.
127   my %currency_names_by_id = map { ($_->id => $_->name) } @{ $self->all_currencies };
128   $_->update_attributes(name => '__039519735__' . $_->{id})        for @{ $self->all_currencies };
129   $_->update_attributes(name => $currency_names_by_id{ $_->{id} }) for @{ $self->all_currencies };
130
131   # Create new currency if required
132   my $new_currency;
133   if ($::form->{new_currency}) {
134     $new_currency = SL::DB::Currency->new(name => $::form->{new_currency});
135     $new_currency->save;
136   }
137
138   # If the user wants the new currency to be the default then replace
139   # the ID placeholder with the proper value. However, if no new
140   # currency has been created then don't change the value at all.
141   if (-1 == $self->defaults->currency_id) {
142     $self->defaults->currency_id($new_currency ? $new_currency->id : $original_currency_id);
143   }
144
145   # Create new templates if requested.
146   if ($::form->{use_templates} eq 'new') {
147     local $File::Copy::Recursive::SkipFlop = 1;
148     File::Copy::Recursive::dircopy('templates/print/' . $::form->{new_master_templates}, 'templates/' . $::form->{new_templates});
149     $self->defaults->templates('templates/' . $::form->{new_templates});
150   }
151
152   # Displayable name preferences
153   foreach my $specs (@{ $::form->{displayable_name_specs} }) {
154     $self->displayable_name_specs_by_module->{$specs->{module}}->{prefs}->store_default($specs->{default});
155   }
156
157   # Finally save defaults.
158   $self->defaults->save;
159
160   flash_later('info', t8('Client Configuration saved!'));
161
162   $self->redirect_to(action => 'edit');
163 }
164
165 #
166 # initializers
167 #
168
169 sub init_defaults        { SL::DB::Default->get                                                                          }
170 sub init_all_warehouses  { SL::DB::Manager::Warehouse->get_all_sorted                                                    }
171 sub init_all_languages   { SL::DB::Manager::Language->get_all_sorted                                                     }
172 sub init_all_currencies  { SL::DB::Manager::Currency->get_all_sorted                                                     }
173 sub init_all_weightunits { my $unit = SL::DB::Manager::Unit->find_by(name => 'kg'); $unit ? $unit->convertible_units : [] }
174 sub init_all_templates   { +{ SL::Template->available_templates }                                                        }
175 sub init_h_unit_name     { first { SL::DB::Manager::Unit->find_by(name => $_) } qw(Std h Stunde)                         }
176 sub init_all_project_types    { SL::DB::Manager::ProjectType->get_all_sorted                                             }
177 sub init_all_project_statuses { SL::DB::Manager::ProjectStatus->get_all_sorted                                           }
178 sub init_zugferd_settings     { \@SL::ZUGFeRD::customer_settings                                                         }
179
180 sub init_posting_options {
181   [ { title => t8("never"),           value => 0           },
182     { title => t8("every time"),      value => 1           },
183     { title => t8("on the same day"), value => 2           }, ]
184 }
185
186 sub init_payment_options {
187   [ { title => t8("never"),           value => 0           },
188     { title => t8("every time"),      value => 1           },
189     { title => t8("on the same day"), value => 2           }, ]
190 }
191
192 sub init_accounting_options {
193   [ { title => t8("Accrual"),         value => "accrual"   },
194     { title => t8("cash"),            value => "cash"      }, ]
195 }
196
197 sub init_inventory_options {
198   [ { title => t8("perpetual"),       value => "perpetual" },
199     { title => t8("periodic"),        value => "periodic"  }, ]
200 }
201
202 sub init_profit_options {
203   [ { title => t8("balance"),         value => "balance"   },
204     { title => t8("income"),          value => "income"    }, ]
205 }
206
207 sub init_balance_startdate_method_options {
208   return SL::DB::Helper::AccountingPeriod::get_balance_startdate_method_options;
209 }
210
211 sub init_yearend_options {
212   [ { title => t8("default"),         value => "default"   },
213     { title => t8("simple"),          value => "simple"    }, ]
214 }
215
216 sub init_all_price_sources {
217   my @classes = SL::PriceSource::ALL->all_price_sources;
218
219   [ map { [ $_->name, $_->description ] } @classes ];
220 }
221
222 sub init_available_quick_search_modules {
223   [ SL::Controller::TopQuickSearch->new->available_modules ];
224 }
225
226 sub init_displayable_name_specs_by_module {
227   +{
228      'SL::DB::Customer' => {
229        specs => SL::DB::Customer->displayable_name_specs,
230        prefs => SL::DB::Customer->displayable_name_prefs,
231      },
232      'SL::DB::Vendor' => {
233        specs => SL::DB::Vendor->displayable_name_specs,
234        prefs => SL::DB::Vendor->displayable_name_prefs,
235      },
236      'SL::DB::Part' => {
237        specs => SL::DB::Part->displayable_name_specs,
238        prefs => SL::DB::Part->displayable_name_prefs,
239      },
240   };
241 }
242
243 sub init_available_documents_with_no_positions {
244   my @docs = ( @{SL::DB::Order::TypeData::valid_types()},
245                @{SL::DB::DeliveryOrder::TypeData::valid_types()},
246                @{SL::DB::Reclamation::TypeData::valid_types()} );
247
248   my @available_docs = map { {name => $_, description => $::form->get_formname_translation($_)} } @docs;
249
250   return \@available_docs;
251 }
252
253 #
254 # filters
255 #
256
257 sub check_auth {
258   $::auth->assert('admin');
259 }
260
261 #
262 # helpers
263 #
264
265 sub edit_form {
266   my ($self) = @_;
267
268   $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side kivi.File);
269
270   $self->setup_edit_form_action_bar;
271   $self->render('client_config/form', title => t8('Client Configuration'),
272                 make_chart_title     => sub { $_[0]->accno . '--' . $_[0]->description },
273                 make_templates_value => sub { 'templates/' . $_[0] },
274               );
275 }
276
277 sub setup_edit_form_action_bar {
278   my ($self) = @_;
279
280   for my $bar ($::request->layout->get('actionbar')) {
281     $bar->add(
282       action => [
283         t8('Save'),
284         submit    => [ '#form', { action => 'ClientConfig/save' } ],
285         accesskey => 'enter',
286       ],
287     );
288   }
289 }
290
291 1;