1 package SL::Controller::Buchungsgruppen;
5 use parent qw(SL::Controller::Base);
9 use SL::Locale::String;
10 use SL::DB::TaxzoneChart;
13 use Rose::Object::MakeMethods::Generic (
14 scalar => [ qw(config) ],
15 'scalar --get_set_init' => [ qw(defaults) ],
18 __PACKAGE__->run_before('check_auth');
19 __PACKAGE__->run_before('load_config', only => [ qw(edit update delete) ]);
28 my $buchungsgruppen = SL::DB::Manager::Buchungsgruppe->get_all_sorted();
29 my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ]);
32 foreach my $gruppe (@{ $buchungsgruppen }) {
33 $chartlist{ $gruppe->id } = SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($gruppe->id);
37 $self->render('buchungsgruppen/list',
38 title => t8('Booking groups'),
39 BUCHUNGSGRUPPEN => $buchungsgruppen,
40 CHARTLIST => \%chartlist,
41 TAXZONES => $taxzones);
47 $self->config(SL::DB::Buchungsgruppe->new());
48 $self->show_form(title => t8('Add booking group'));
52 my ($self, %params) = @_;
54 $self->render('buchungsgruppen/form', %params,
55 TAXZONES => SL::DB::Manager::TaxZone->get_all_sorted());
61 # Allow editing of the charts of the Buchungsgruppe if it isn't assigned to
62 # any parts. This is checked inside the template via the Buchungsgruppen
63 # orphaned method, where an IF-ELSE statement toggles between L.select_tag
66 $self->show_form(title => t8('Edit booking group'),
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 # allow deletion of unused Buchungsgruppen. Will fail, due to database
86 # constraint, if Buchungsgruppe is connected to a part
88 $self->{config}->db->with_transaction(sub {
89 my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ buchungsgruppen_id => $self->config->id ]);
90 foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete };
91 $self->config->delete();
92 flash_later('info', $::locale->text('The booking group has been deleted.'));
95 }) || flash_later('error', $::locale->text('The booking group is in use and cannot be deleted.'));
97 $self->redirect_to(action => 'list');
104 SL::DB::Buchungsgruppe->reorder_list(@{ $::form->{bg_id} || [] });
106 $self->render(\'', { type => 'json' });
114 $::auth->assert('config');
120 $self->config(SL::DB::Buchungsgruppe->new(id => $::form->{id})->load);
127 sub create_or_update {
129 my $is_new = !$self->config->id;
131 my $params = delete($::form->{config}) || { };
132 delete $params->{id};
136 my $db = $self->config->db;
137 if (!$db->with_transaction(sub {
139 $self->config->assign_attributes(%{ $params }); # assign description and inventory_accno_id
141 @errors = $self->config->validate; # check for description and inventory_accno_id
144 die "foo" . @errors . "\n";
149 # Save or update taxzone_charts for new or unused Buchungsgruppen
150 if ($is_new or $self->config->orphaned) {
151 my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted();
153 foreach my $tz (@{ $taxzones }) {
155 my $income_accno_id = $::form->{"income_accno_id_" . $tz->id};
156 my $expense_accno_id = $::form->{"expense_accno_id_" . $tz->id};
158 my ($income_accno, $expense_accno);
159 $income_accno = SL::DB::Manager::Chart->find_by( id => $income_accno_id ) if $income_accno_id;
160 $expense_accno = SL::DB::Manager::Chart->find_by( id => $expense_accno_id ) if $expense_accno_id;
162 push(@errors, t8('Tax zone #1 needs a valid income account' , $tz->description)) unless $income_accno;
163 push(@errors, t8('Tax zone #1 needs a valid expense account' , $tz->description)) unless $expense_accno;
165 my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $self->config->id, taxzone_id => $tz->id);
166 $taxzone_chart->taxzone_id($tz->id);
167 $taxzone_chart->buchungsgruppen_id($self->config->id);
168 $taxzone_chart->income_accno_id($income_accno->id);
169 $taxzone_chart->expense_accno_id($expense_accno->id);
170 $taxzone_chart->save;
176 die @errors ? join("\n", @errors) . "\n" : $db->error . "\n";
177 # die with rollback of taxzone save if saving of any of the taxzone_charts fails
178 # only show the $db->error if we haven't already identified the likely error ourselves
181 flash_later('info', $is_new ? t8('The booking group has been created.') : t8('The booking group has been saved.'));
182 $self->redirect_to(action => 'list');
189 sub init_defaults { SL::DB::Default->get }