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(query => [ obsolete => 0 ]);
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 # check whether buchungsgruppe is assigned to any parts
63 my $number_of_parts_with_buchungsgruppe = SL::DB::Manager::Part->get_objects_count(where => [ buchungsgruppen_id => $self->config->id]);
65 $self->show_form(title => t8('Edit Buchungsgruppe'),
66 linked_parts => $number_of_parts_with_buchungsgruppe,
67 CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($self->config->id));
73 $self->config(SL::DB::Buchungsgruppe->new());
74 $self->create_or_update;
79 $self->create_or_update;
85 SL::DB::Buchungsgruppe->reorder_list(@{ $::form->{bg_id} || [] });
87 $self->render(\'', { type => 'json' });
95 $::auth->assert('config');
101 $self->config(SL::DB::Buchungsgruppe->new(id => $::form->{id})->load);
108 sub create_or_update {
110 my $is_new = !$self->config->id;
112 my $params = delete($::form->{config}) || { };
113 delete $params->{id};
115 $self->config->assign_attributes(%{ $params });
117 my @errors = $self->config->validate;
120 flash('error', @errors);
121 $self->show_form(title => $is_new ? t8('Add taxzone') : t8('Edit taxzone'));
127 # check whether there are any assigned parts
128 my $number_of_parts_with_buchungsgruppe = SL::DB::Manager::Part->get_objects_count(where => [ buchungsgruppen_id => $self->config->id]);
130 # Save or update taxzone_charts:
131 if ($is_new or $number_of_parts_with_buchungsgruppe == 0) {
132 my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
134 foreach my $tz (@{ $taxzones }) {
135 my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $self->config->id, taxzone_id => $tz->id);
136 $taxzone_chart->taxzone_id($tz->id);
137 $taxzone_chart->buchungsgruppen_id($self->config->id);
138 $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $tz->id});
139 $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $tz->id});
140 $taxzone_chart->save;
144 flash_later('info', $is_new ? t8('The Buchungsgruppe has been created.') : t8('The Buchungsgruppe has been saved.'));
145 $self->redirect_to(action => 'list');