1 package SL::Controller::ClientConfig;
4 use parent qw(SL::Controller::Base);
6 use List::Util qw(first);
13 use SL::Helper::Flash;
14 use SL::Locale::String qw(t8);
16 __PACKAGE__->run_before('check_auth');
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) ],
23 my ($self, %params) = @_;
28 my ($self, %params) = @_;
30 my $defaults = delete($::form->{defaults}) || {};
31 my $entered_currencies = delete($::form->{currencies}) || [];
32 my $original_currency_id = $self->defaults->currency_id;
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->{$_};
39 $self->defaults->assign_attributes(%{ $defaults });
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;
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.');
57 $new_currency_names{$new_name} = 1;
58 $existing_currency->name($new_name);
62 if ($::form->{new_currency} && $new_currency_names{ $::form->{new_currency} }) {
63 $errors_idx{1} = t8('Currency names must be unique.');
66 my @errors = map { $errors_idx{$_} } sort keys %errors_idx;
69 flash('error', @errors);
70 return $self->edit_form;
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 };
80 # Create new currency if required
82 if ($::form->{new_currency}) {
83 $new_currency = SL::DB::Currency->new(name => $::form->{new_currency});
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);
94 $self->defaults->save;
96 flash_later('info', t8('Client Configuration saved!'));
98 $self->redirect_to(action => 'edit');
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 }
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 }, ]
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 }, ]
123 sub init_accounting_options {
124 [ { title => t8("Accrual"), value => "accrual" },
125 { title => t8("cash"), value => "cash" }, ]
128 sub init_inventory_options {
129 [ { title => t8("perpetual"), value => "perpetual" },
130 { title => t8("periodic"), value => "periodic" }, ]
133 sub init_profit_options {
134 [ { title => t8("balance"), value => "balance" },
135 { title => t8("income"), value => "income" }, ]
141 foreach my $chart (@{ SL::DB::Manager::Chart->get_all(where => [ link => { like => '%IC%' } ], sort_by => 'accno ASC') }) {
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};
149 $accounts{$key} ||= [];
150 push @{ $accounts{$key} }, $chart;
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');
166 $::auth->assert('admin');
175 $self->render('client_config/form', title => t8('Client Configuration'),
176 make_chart_title => sub { $_[0]->accno . '--' . $_[0]->description });