1 package SL::Controller::ClientConfig;
4 use parent qw(SL::Controller::Base);
6 use File::Copy::Recursive ();
7 use List::Util qw(first);
16 use SL::Helper::Flash;
17 use SL::Locale::String qw(t8);
18 use SL::PriceSource::ALL;
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);
28 __PACKAGE__->run_before('check_auth');
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) ],
38 my ($self, %params) = @_;
40 $::form->{use_templates} = $self->defaults->templates ? 'existing' : 'new';
41 $::form->{feature_datev} = $self->defaults->feature_datev;
46 my ($self, %params) = @_;
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} ||= [];
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->{$_};
58 $self->defaults->assign_attributes(%{ $defaults });
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;
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.');
76 $new_currency_names{$new_name} = 1;
77 $existing_currency->name($new_name);
80 if ($::form->{new_currency} && $new_currency_names{ $::form->{new_currency} }) {
81 $errors_idx{1} = t8('Currency names must be unique.');
84 my @errors = map { $errors_idx{$_} } sort keys %errors_idx;
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
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', $_);
96 $::form->{new_templates} =~ s:/::g;
97 $::form->{new_master_templates} =~ s:/::g;
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.');
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.');
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});
117 # Show form again if there were any errors. Nothing's been changed
118 # yet in the database.
120 flash('error', @errors);
121 return $self->edit_form;
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 };
131 # Create new currency if required
133 if ($::form->{new_currency}) {
134 $new_currency = SL::DB::Currency->new(name => $::form->{new_currency});
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);
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});
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});
157 # Finally save defaults.
158 $self->defaults->save;
160 flash_later('info', t8('Client Configuration saved!'));
162 $self->redirect_to(action => 'edit');
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 }
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 }, ]
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 }, ]
192 sub init_accounting_options {
193 [ { title => t8("Accrual"), value => "accrual" },
194 { title => t8("cash"), value => "cash" }, ]
197 sub init_inventory_options {
198 [ { title => t8("perpetual"), value => "perpetual" },
199 { title => t8("periodic"), value => "periodic" }, ]
202 sub init_profit_options {
203 [ { title => t8("balance"), value => "balance" },
204 { title => t8("income"), value => "income" }, ]
207 sub init_balance_startdate_method_options {
208 return SL::DB::Helper::AccountingPeriod::get_balance_startdate_method_options;
211 sub init_yearend_options {
212 [ { title => t8("default"), value => "default" },
213 { title => t8("simple"), value => "simple" }, ]
216 sub init_all_price_sources {
217 my @classes = SL::PriceSource::ALL->all_price_sources;
219 [ map { [ $_->name, $_->description ] } @classes ];
222 sub init_available_quick_search_modules {
223 [ SL::Controller::TopQuickSearch->new->available_modules ];
226 sub init_displayable_name_specs_by_module {
228 'SL::DB::Customer' => {
229 specs => SL::DB::Customer->displayable_name_specs,
230 prefs => SL::DB::Customer->displayable_name_prefs,
232 'SL::DB::Vendor' => {
233 specs => SL::DB::Vendor->displayable_name_specs,
234 prefs => SL::DB::Vendor->displayable_name_prefs,
237 specs => SL::DB::Part->displayable_name_specs,
238 prefs => SL::DB::Part->displayable_name_prefs,
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()} );
248 my @available_docs = map { {name => $_, description => $::form->get_formname_translation($_)} } @docs;
250 return \@available_docs;
258 $::auth->assert('admin');
268 $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side kivi.File);
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] },
277 sub setup_edit_form_action_bar {
280 for my $bar ($::request->layout->get('actionbar')) {
284 submit => [ '#form', { action => 'ClientConfig/save' } ],
285 accesskey => 'enter',