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;
14 use Rose::Object::MakeMethods::Generic (
15 scalar => [ qw(config) ],
16 'scalar --get_set_init' => [ qw(defaults) ],
19 __PACKAGE__->run_before('check_auth');
20 __PACKAGE__->run_before('load_config', only => [ qw(edit update delete) ]);
29 my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
30 my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ]);
33 foreach my $gruppe (@{ $buchungsgruppen }) {
34 $chartlist{ $gruppe->id } = SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($gruppe->id);
38 $self->render('buchungsgruppen/list',
39 title => t8('Buchungsgruppen'),
40 BUCHUNGSGRUPPEN => $buchungsgruppen,
41 CHARTLIST => \%chartlist,
42 TAXZONES => $taxzones);
48 $self->config(SL::DB::Buchungsgruppe->new());
49 $self->show_form(title => t8('Add Buchungsgruppe'));
53 my ($self, %params) = @_;
55 $self->render('buchungsgruppen/form', %params,
56 TAXZONES => SL::DB::Manager::TaxZone->get_all_sorted(),
57 ACCOUNTS => SL::Controller::ClientConfig->init_accounts(),
58 account_label => sub { "$_[0]{accno}--$_[0]{description}" });
64 # Allow editing of the charts of the Buchungsgruppe if it isn't assigned to
65 # any parts. This is checked inside the template via the Buchungsgruppen
66 # orphaned method, where an IF-ELSE statement toggles between L.select_tag
69 $self->show_form(title => t8('Edit Buchungsgruppe'),
70 CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($self->config->id));
76 $self->config(SL::DB::Buchungsgruppe->new());
77 $self->create_or_update;
82 $self->create_or_update;
88 # allow deletion of unused Buchungsgruppen. Will fail, due to database
89 # constraint, if Buchungsgruppe is connected to a part
91 my $db = $self->{config}->db;
92 $db->do_transaction(sub {
93 my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ buchungsgruppen_id => $self->config->id ]);
94 foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
95 $self->config->delete();
96 flash_later('info', $::locale->text('The buchungsgruppe has been deleted.'));
97 }) || flash_later('error', $::locale->text('The buchungsgruppe is in use and cannot be deleted.'));
99 $self->redirect_to(action => 'list');
106 SL::DB::Buchungsgruppe->reorder_list(@{ $::form->{bg_id} || [] });
108 $self->render(\'', { type => 'json' });
116 $::auth->assert('config');
122 $self->config(SL::DB::Buchungsgruppe->new(id => $::form->{id})->load);
129 sub create_or_update {
131 my $is_new = !$self->config->id;
133 my $params = delete($::form->{config}) || { };
134 delete $params->{id};
136 $self->config->assign_attributes(%{ $params });
138 my @errors = $self->config->validate;
141 flash('error', @errors);
142 $self->show_form(title => $is_new ? t8('Add taxzone') : t8('Edit taxzone'));
148 # Save or update taxzone_charts for new or unused Buchungsgruppen
149 if ($is_new or $self->config->orphaned) {
150 my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
152 foreach my $tz (@{ $taxzones }) {
153 my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $self->config->id, taxzone_id => $tz->id);
154 $taxzone_chart->taxzone_id($tz->id);
155 $taxzone_chart->buchungsgruppen_id($self->config->id);
156 $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $tz->id});
157 $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $tz->id});
158 $taxzone_chart->save;
162 flash_later('info', $is_new ? t8('The Buchungsgruppe has been created.') : t8('The Buchungsgruppe has been saved.'));
163 $self->redirect_to(action => 'list');
170 sub init_defaults { SL::DB::Default->get }