1 package SL::Controller::Buchungsgruppen;
5 use parent qw(SL::Controller::Base);
9 use SL::Locale::String;
10 use SL::DB::TaxzoneChart;
11 use SL::Controller::ClientConfig;
13 use Rose::Object::MakeMethods::Generic (
14 scalar => [ qw(config) ],
17 __PACKAGE__->run_before('check_auth');
18 __PACKAGE__->run_before('load_config', only => [ qw(edit update) ]); #destroy
27 my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
28 my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
31 foreach my $gruppe (@{ $buchungsgruppen }) {
32 $chartlist{ $gruppe->id } = SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($gruppe->id);
36 $self->render('buchungsgruppen/list',
37 title => t8('Buchungsgruppen'),
38 BUCHUNGSGRUPPEN => $buchungsgruppen,
39 CHARTLIST => \%chartlist,
40 TAXZONES => $taxzones);
46 $self->config(SL::DB::Buchungsgruppe->new());
47 $self->show_form(title => t8('Add Buchungsgruppe'));
51 my ($self, %params) = @_;
53 $self->render('buchungsgruppen/form', %params,
54 TAXZONES => SL::DB::Manager::TaxZone->get_all_sorted(),
55 ACCOUNTS => SL::Controller::ClientConfig->init_accounts(),
56 account_label => sub { "$_[0]{accno}--$_[0]{description}" });
62 $self->show_form(title => t8('Edit Buchungsgruppe'),
63 CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($self->config->id));
69 $self->config(SL::DB::Buchungsgruppe->new());
70 $self->create_or_update;
75 $self->create_or_update;
81 SL::DB::Buchungsgruppe->reorder_list(@{ $::form->{bg_id} || [] });
83 $self->render(\'', { type => 'json' });
91 $::auth->assert('config');
97 $self->config(SL::DB::Buchungsgruppe->new(id => $::form->{id})->load);
104 sub create_or_update {
106 my $is_new = !$self->config->id;
108 my $params = delete($::form->{config}) || { };
109 delete $params->{id};
111 $self->config->assign_attributes(%{ $params });
113 my @errors = $self->config->validate;
116 flash('error', @errors);
117 $self->show_form(title => $is_new ? t8('Add taxzone') : t8('Edit taxzone'));
123 #Save taxzone_charts:
125 my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
127 foreach my $tz (@{ $taxzones }) {
128 my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $self->config->id, taxzone_id => $tz->id);
129 $taxzone_chart->taxzone_id($tz->id);
130 $taxzone_chart->buchungsgruppen_id($self->config->id);
131 $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $tz->id});
132 $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $tz->id});
133 $taxzone_chart->save;
137 flash_later('info', $is_new ? t8('The Buchungsgruppe has been created.') : t8('The Buchungsgruppe has been saved.'));
138 $self->redirect_to(action => 'list');