- $taxzone_chart->taxzone_id($self->config->id);
- $taxzone_chart->buchungsgruppen_id($bg->id);
- $taxzone_chart->income_accno_id($::form->{"income_accno_id_" . $bg->id});
- $taxzone_chart->expense_accno_id($::form->{"expense_accno_id_" . $bg->id});
- $taxzone_chart->save;
+ foreach my $bg (@{ $buchungsgruppen }) {
+ my $income_accno_id = $::form->{"income_accno_id_" . $bg->id};
+ my $expense_accno_id = $::form->{"expense_accno_id_" . $bg->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('Booking group #1 needs a valid income account' , $bg->description)) unless $income_accno;
+ push(@errors, t8('Booking group #1 needs a valid expense account', $bg->description)) unless $expense_accno;
+
+ my $taxzone_chart = SL::DB::Manager::TaxzoneChart->find_by_or_create(buchungsgruppen_id => $bg->id, taxzone_id => $self->config->id);
+ # if taxzonechart doesn't exist an empty new TaxzoneChart object is
+ # created by find_by_or_create, so we have to assign buchungsgruppe and
+ # taxzone again for the new case to work
+ $taxzone_chart->taxzone_id($self->config->id);
+ $taxzone_chart->buchungsgruppen_id($bg->id);
+ $taxzone_chart->income_accno_id($income_accno->id);
+ $taxzone_chart->expense_accno_id($expense_accno->id);
+ $taxzone_chart->save;
+ }