X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FBuchungsgruppen.pm;h=daaf94f5c38a7266dd13d4bdcf02074d577d7323;hb=44fb4fe8d98ced50689764dab64b4633016c9fe0;hp=f3f07d58ad44fc1ff33333e8747179c903be49e2;hpb=6e6038682b1a2b6e6bc74f1eee40eba21afcb7e9;p=kivitendo-erp.git diff --git a/SL/Controller/Buchungsgruppen.pm b/SL/Controller/Buchungsgruppen.pm index f3f07d58a..daaf94f5c 100644 --- a/SL/Controller/Buchungsgruppen.pm +++ b/SL/Controller/Buchungsgruppen.pm @@ -8,14 +8,15 @@ use SL::DB::TaxZone; use SL::Helper::Flash; use SL::Locale::String; use SL::DB::TaxzoneChart; -use SL::Controller::ClientConfig; +use SL::DB::Default; use Rose::Object::MakeMethods::Generic ( scalar => [ qw(config) ], + 'scalar --get_set_init' => [ qw(defaults) ], ); __PACKAGE__->run_before('check_auth'); -__PACKAGE__->run_before('load_config', only => [ qw(edit update) ]); #destroy +__PACKAGE__->run_before('load_config', only => [ qw(edit update delete) ]); # # actions @@ -34,7 +35,7 @@ sub action_list { $::form->header; $self->render('buchungsgruppen/list', - title => t8('Buchungsgruppen'), + title => t8('Booking groups'), BUCHUNGSGRUPPEN => $buchungsgruppen, CHARTLIST => \%chartlist, TAXZONES => $taxzones); @@ -44,26 +45,25 @@ sub action_new { my ($self) = @_; $self->config(SL::DB::Buchungsgruppe->new()); - $self->show_form(title => t8('Add Buchungsgruppe')); + $self->show_form(title => t8('Add booking group')); } sub show_form { my ($self, %params) = @_; $self->render('buchungsgruppen/form', %params, - TAXZONES => SL::DB::Manager::TaxZone->get_all_sorted(), - ACCOUNTS => SL::Controller::ClientConfig->init_accounts(), - account_label => sub { "$_[0]{accno}--$_[0]{description}" }); + TAXZONES => SL::DB::Manager::TaxZone->get_all_sorted()); } sub action_edit { my ($self) = @_; - # check whether buchungsgruppe is assigned to any parts - my $number_of_parts_with_buchungsgruppe = SL::DB::Manager::Part->get_objects_count(where => [ buchungsgruppen_id => $self->config->id]); + # Allow editing of the charts of the Buchungsgruppe if it isn't assigned to + # any parts. This is checked inside the template via the Buchungsgruppen + # orphaned method, where an IF-ELSE statement toggles between L.select_tag + # and text. - $self->show_form(title => t8('Edit Buchungsgruppe'), - linked_parts => $number_of_parts_with_buchungsgruppe, + $self->show_form(title => t8('Edit booking group'), CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($self->config->id)); } @@ -79,6 +79,24 @@ sub action_update { $self->create_or_update; } +sub action_delete { + my ($self) = @_; + + # allow deletion of unused Buchungsgruppen. Will fail, due to database + # constraint, if Buchungsgruppe is connected to a part + + my $db = $self->{config}->db; + $db->do_transaction(sub { + my $taxzone_charts = SL::DB::Manager::TaxzoneChart->get_all(where => [ buchungsgruppen_id => $self->config->id ]); + foreach my $taxzonechart ( @{$taxzone_charts} ) { $taxzonechart->delete }; + $self->config->delete(); + flash_later('info', $::locale->text('The booking group has been deleted.')); + }) || flash_later('error', $::locale->text('The booking group is in use and cannot be deleted.')); + + $self->redirect_to(action => 'list'); + +} + sub action_reorder { my ($self) = @_; @@ -112,37 +130,57 @@ sub create_or_update { my $params = delete($::form->{config}) || { }; delete $params->{id}; - $self->config->assign_attributes(%{ $params }); + my @errors; - my @errors = $self->config->validate; + my $db = $self->config->db; + $db->do_transaction( sub { - if (@errors) { - flash('error', @errors); - $self->show_form(title => $is_new ? t8('Add taxzone') : t8('Edit taxzone')); - return; - } + $self->config->assign_attributes(%{ $params }); # assign description and inventory_accno_id + + @errors = $self->config->validate; # check for description and inventory_accno_id - $self->config->save; + if (@errors) { + die "foo" . @errors . "\n"; + }; - # check whether there are any assigned parts - my $number_of_parts_with_buchungsgruppe = SL::DB::Manager::Part->get_objects_count(where => [ buchungsgruppen_id => $self->config->id]); + $self->config->save; - # Save or update taxzone_charts: - if ($is_new or $number_of_parts_with_buchungsgruppe == 0) { - my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted(); + # Save or update taxzone_charts for new or unused Buchungsgruppen + if ($is_new or $self->config->orphaned) { + my $taxzones = SL::DB::Manager::TaxZone->get_all_sorted(); - foreach my $tz (@{ $taxzones }) { - my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $self->config->id, taxzone_id => $tz->id); - $taxzone_chart->taxzone_id($tz->id); - $taxzone_chart->buchungsgruppen_id($self->config->id); - $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $tz->id}); - $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $tz->id}); - $taxzone_chart->save; + foreach my $tz (@{ $taxzones }) { + + my $income_accno_id = $::form->{"income_accno_id_" . $tz->id}; + my $expense_accno_id = $::form->{"expense_accno_id_" . $tz->id}; + + my ($income_accno, $expense_accno); + $income_accno = SL::DB::Manager::Chart->find_by( id => $income_accno_id ) if $income_accno_id; + $expense_accno = SL::DB::Manager::Chart->find_by( id => $expense_accno_id ) if $expense_accno_id; + + push(@errors, t8('Tax zone #1 needs a valid income account' , $tz->description)) unless $income_accno; + push(@errors, t8('Tax zone #1 needs a valid expense account' , $tz->description)) unless $expense_accno; + + my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $self->config->id, taxzone_id => $tz->id); + $taxzone_chart->taxzone_id($tz->id); + $taxzone_chart->buchungsgruppen_id($self->config->id); + $taxzone_chart->income_accno_id($income_accno->id); + $taxzone_chart->expense_accno_id($expense_accno->id); + $taxzone_chart->save; + } } - } + } ) || die @errors ? join("\n", @errors) . "\n" : $db->error . "\n"; + # die with rollback of taxzone save if saving of any of the taxzone_charts fails + # only show the $db->error if we haven't already identified the likely error ourselves - flash_later('info', $is_new ? t8('The Buchungsgruppe has been created.') : t8('The Buchungsgruppe has been saved.')); + flash_later('info', $is_new ? t8('The booking group has been created.') : t8('The booking group has been saved.')); $self->redirect_to(action => 'list'); } +# +# initializers +# + +sub init_defaults { SL::DB::Default->get } + 1;