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::Controller::TopQuickSearch;
21 use SL::DB::Helper::AccountingPeriod qw(get_balance_startdate_method_options);
22 use SL::Helper::ShippedQty;
24 __PACKAGE__->run_before('check_auth');
26 use Rose::Object::MakeMethods::Generic (
27 '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 available_shipped_qty_item_identity_fields
28 all_project_statuses all_project_types
29 posting_options payment_options accounting_options inventory_options profit_options balance_startdate_method_options
30 displayable_name_specs_by_module) ],
34 my ($self, %params) = @_;
36 $::form->{use_templates} = $self->defaults->templates ? 'existing' : 'new';
37 $::form->{feature_datev} = $self->defaults->feature_datev;
42 my ($self, %params) = @_;
44 my $defaults = delete($::form->{defaults}) || {};
45 my $entered_currencies = delete($::form->{currencies}) || [];
46 my $original_currency_id = $self->defaults->currency_id;
47 $defaults->{disabled_price_sources} ||= [];
49 # undef several fields if an empty value has been selected.
50 foreach (qw(warehouse_id bin_id warehouse_id_ignore_onhand bin_id_ignore_onhand)) {
51 undef $defaults->{$_} if !$defaults->{$_};
54 $defaults->{$_} = $::form->parse_amount(\%::myconfig, $defaults->{$_}) for qw(customer_hourly_rate);
56 $self->defaults->assign_attributes(%{ $defaults });
61 my (%new_currency_names);
62 foreach my $existing_currency (@{ $self->all_currencies }) {
63 my $new_name = $existing_currency->name;
64 my $new_currency = first { $_->{id} == $existing_currency->id } @{ $entered_currencies };
65 $new_name = $new_currency->{name} if $new_currency;
68 $errors_idx{0} = t8('Currency names must not be empty.');
69 } elsif ($new_currency_names{$new_name}) {
70 $errors_idx{1} = t8('Currency names must be unique.');
74 $new_currency_names{$new_name} = 1;
75 $existing_currency->name($new_name);
79 if ($::form->{new_currency} && $new_currency_names{ $::form->{new_currency} }) {
80 $errors_idx{1} = t8('Currency names must be unique.');
83 my @errors = map { $errors_idx{$_} } sort keys %errors_idx;
86 $::form->{new_templates} =~ s:/::g;
87 $::form->{new_master_templates} =~ s:/::g;
89 if (($::form->{use_templates} eq 'existing') && ($self->defaults->templates !~ m:^templates/[^/]+$:)) {
90 push @errors, t8('You must select existing print templates or create a new set.');
92 } elsif ($::form->{use_templates} eq 'new') {
93 if (!$::form->{new_templates}) {
94 push @errors, t8('You must enter a name for your new print templates.');
95 } elsif (-d "templates/" . $::form->{new_templates}) {
96 push @errors, t8('A directory with the name for the new print templates exists already.');
97 } elsif (! -d "templates/print/" . $::form->{new_master_templates}) {
98 push @errors, t8('The master templates where not found.');
102 # Show form again if there were any errors. Nothing's been changed
103 # yet in the database.
105 flash('error', @errors);
106 return $self->edit_form;
109 # Save currencies. As the names must be unique we cannot simply save
110 # them as they are -- the user might want to swap to names. So make
111 # them unique first and assign the actual names in a second step.
112 my %currency_names_by_id = map { ($_->id => $_->name) } @{ $self->all_currencies };
113 $_->update_attributes(name => '__039519735__' . $_->{id}) for @{ $self->all_currencies };
114 $_->update_attributes(name => $currency_names_by_id{ $_->{id} }) for @{ $self->all_currencies };
116 # Create new currency if required
118 if ($::form->{new_currency}) {
119 $new_currency = SL::DB::Currency->new(name => $::form->{new_currency});
123 # If the user wants the new currency to be the default then replace
124 # the ID placeholder with the proper value. However, if no new
125 # currency has been created then don't change the value at all.
126 if (-1 == $self->defaults->currency_id) {
127 $self->defaults->currency_id($new_currency ? $new_currency->id : $original_currency_id);
130 # Create new templates if requested.
131 if ($::form->{use_templates} eq 'new') {
132 local $File::Copy::Recursive::SkipFlop = 1;
133 File::Copy::Recursive::dircopy('templates/print/' . $::form->{new_master_templates}, 'templates/' . $::form->{new_templates});
134 $self->defaults->templates('templates/' . $::form->{new_templates});
137 # Displayable name preferences
138 foreach my $specs (@{ $::form->{displayable_name_specs} }) {
139 $self->displayable_name_specs_by_module->{$specs->{module}}->{prefs}->store_default($specs->{default});
142 # Finally save defaults.
143 $self->defaults->save;
145 flash_later('info', t8('Client Configuration saved!'));
147 $self->redirect_to(action => 'edit');
154 sub init_defaults { SL::DB::Default->get }
155 sub init_all_warehouses { SL::DB::Manager::Warehouse->get_all_sorted }
156 sub init_all_languages { SL::DB::Manager::Language->get_all_sorted }
157 sub init_all_currencies { SL::DB::Manager::Currency->get_all_sorted }
158 sub init_all_weightunits { my $unit = SL::DB::Manager::Unit->find_by(name => 'kg'); $unit ? $unit->convertible_units : [] }
159 sub init_all_templates { +{ SL::Template->available_templates } }
160 sub init_h_unit_name { first { SL::DB::Manager::Unit->find_by(name => $_) } qw(Std h Stunde) }
161 sub init_all_project_types { SL::DB::Manager::ProjectType->get_all_sorted }
162 sub init_all_project_statuses { SL::DB::Manager::ProjectStatus->get_all_sorted }
164 sub init_posting_options {
165 [ { title => t8("never"), value => 0 },
166 { title => t8("every time"), value => 1 },
167 { title => t8("on the same day"), value => 2 }, ]
170 sub init_payment_options {
171 [ { title => t8("never"), value => 0 },
172 { title => t8("every time"), value => 1 },
173 { title => t8("on the same day"), value => 2 }, ]
176 sub init_accounting_options {
177 [ { title => t8("Accrual"), value => "accrual" },
178 { title => t8("cash"), value => "cash" }, ]
181 sub init_inventory_options {
182 [ { title => t8("perpetual"), value => "perpetual" },
183 { title => t8("periodic"), value => "periodic" }, ]
186 sub init_profit_options {
187 [ { title => t8("balance"), value => "balance" },
188 { title => t8("income"), value => "income" }, ]
191 sub init_balance_startdate_method_options {
192 return SL::DB::Helper::AccountingPeriod::get_balance_startdate_method_options;
195 sub init_all_price_sources {
196 my @classes = SL::PriceSource::ALL->all_price_sources;
198 [ map { [ $_->name, $_->description ] } @classes ];
201 sub init_available_quick_search_modules {
202 [ SL::Controller::TopQuickSearch->new->available_modules ];
205 sub init_available_shipped_qty_item_identity_fields {
206 [ SL::Helper::ShippedQty->new->available_item_identity_fields ];
209 sub init_displayable_name_specs_by_module {
211 'SL::DB::Customer' => {
212 specs => SL::DB::Customer->displayable_name_specs,
213 prefs => SL::DB::Customer->displayable_name_prefs,
215 'SL::DB::Vendor' => {
216 specs => SL::DB::Vendor->displayable_name_specs,
217 prefs => SL::DB::Vendor->displayable_name_prefs,
220 specs => SL::DB::Part->displayable_name_specs,
221 prefs => SL::DB::Part->displayable_name_prefs,
231 $::auth->assert('admin');
241 $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side kivi.File);
243 $self->setup_edit_form_action_bar;
244 $self->render('client_config/form', title => t8('Client Configuration'),
245 make_chart_title => sub { $_[0]->accno . '--' . $_[0]->description },
246 make_templates_value => sub { 'templates/' . $_[0] },
250 sub setup_edit_form_action_bar {
253 for my $bar ($::request->layout->get('actionbar')) {
257 submit => [ '#form', { action => 'ClientConfig/save' } ],
258 accesskey => 'enter',