Überarbeitung Speichern von Buchungsgruppen
authorG. Richardson <information@kivitendo-premium.de>
Thu, 30 Jul 2015 11:52:36 +0000 (13:52 +0200)
committerG. Richardson <information@kivitendo-premium.de>
Thu, 30 Jul 2015 12:01:07 +0000 (14:01 +0200)
analog zum Verhalten von Steuerzonen: beim Speichern bessere Prüfung und
gegebenenfalls Fehlermeldungen und Rollback, wenn Speichern fehlschlägt.
Verhindert, daß "unfertige" Buchungsgruppen gespeichert werden, wo die
TaxzoneCharts fehlen.

SL/Controller/Buchungsgruppen.pm
SL/DB/Buchungsgruppe.pm
locale/de/all

index 0de9d0c..3f93582 100644 (file)
@@ -130,31 +130,48 @@ 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
+
+    if (@errors) {
+      die "foo" . @errors . "\n";
+    };
+
+    $self->config->save;
 
-  $self->config->save;
+    # 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();
 
-  # 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 }) {
 
-    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;
+        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.'));
   $self->redirect_to(action => 'list');
index eb467c5..9074fb9 100644 (file)
@@ -21,6 +21,13 @@ sub validate {
 
   my @errors;
   push @errors, $::locale->text('The description is missing.') if !$self->description;
+  if( $self->inventory_accno_id ) {
+    require SL::DB::Chart;
+    my $inventory_accno = SL::DB::Manager::Chart->find_by( id => $self->inventory_accno_id );
+    push(@errors, $::locale->text('Buchungsgruppe #1 needs a valid inventory account', $self->description)) unless $inventory_accno;
+  } else {
+    push @errors, $::locale->text('The Buchungsgruppe needs an inventory account.');
+  };
 
   return @errors;
 }
index 50018d8..3735aae 100755 (executable)
@@ -418,6 +418,7 @@ $self->{texts} = {
   'Buchungsgruppe'              => 'Buchungsgruppe',
   'Buchungsgruppe #1 needs a valid expense account' => 'Buchungsgruppe #1 braucht ein gültiges Aufwandskonto',
   'Buchungsgruppe #1 needs a valid income account' => 'Buchungsgruppe #1 braucht ein gültiges Erfolgskonto',
+  'Buchungsgruppe #1 needs a valid inventory account' => 'Buchungsgruppe #1 braucht ein gültiges Warenbestandskonto',
   'Buchungsgruppe (database ID)' => 'Buchungsgruppe (Datenbank-ID)',
   'Buchungsgruppe (name)'       => 'Buchungsgruppe (Name)',
   'Buchungsgruppen'             => 'Buchungsgruppen',
@@ -2524,6 +2525,8 @@ $self->{texts} = {
   'Tax paid'                    => 'Vorsteuer',
   'Tax rate'                    => 'Steuersatz',
   'Tax saved!'                  => 'Steuer gespeichert!',
+  'Tax zone #1 needs a valid expense account' => 'Steuerzone #1 braucht ein gültiges Aufwandskonto',
+  'Tax zone #1 needs a valid income account' => 'Steuerzone #1 braucht ein gültiges Ertragskonto',
   'Tax zone (database ID)'      => 'Steuerzone ((Datenbank-ID)',
   'Tax zone (description)'      => 'Steuerzone (Beschreibung)',
   'Tax-O-Matic'                 => 'Steuer',