Shop: Vorgangsbezeichnung nicht bei Shopware6 implementiert
[kivitendo-erp.git] / SL / Controller / Buchungsgruppen.pm
index 0ad05c4..a608a6e 100644 (file)
@@ -8,7 +8,6 @@ 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 (
@@ -34,9 +33,10 @@ sub action_list {
       $chartlist{ $gruppe->id } = SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($gruppe->id);
   }
 
+  $self->setup_list_action_bar;
   $::form->header;
   $self->render('buchungsgruppen/list',
-                title           => t8('Buchungsgruppen'),
+                title           => t8('Booking groups'),
                 BUCHUNGSGRUPPEN => $buchungsgruppen,
                 CHARTLIST       => \%chartlist,
                 TAXZONES        => $taxzones);
@@ -46,16 +46,15 @@ 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->setup_show_form_action_bar;
   $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 {
@@ -66,7 +65,7 @@ sub action_edit {
   # orphaned method, where an IF-ELSE statement toggles between L.select_tag
   # and text.
 
-  $self->show_form(title     => t8('Edit Buchungsgruppe'),
+  $self->show_form(title     => t8('Edit booking group'),
                    CHARTLIST => SL::DB::TaxzoneChart->get_all_accounts_by_buchungsgruppen_id($self->config->id));
 }
 
@@ -88,13 +87,14 @@ sub action_delete {
   # 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 buchungsgruppe has been deleted.'));
-  }) || flash_later('error', $::locale->text('The buchungsgruppe is in use and cannot be deleted.'));
+  $self->{config}->db->with_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.'));
+
+    1;
+  }) || flash_later('error', $::locale->text('The booking group is in use and cannot be deleted.'));
 
   $self->redirect_to(action => 'list');
 
@@ -133,33 +133,54 @@ 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;
+  if (!$db->with_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;
+
+    # 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 }) {
 
-  $self->config->save;
+        my $income_accno_id    = $::form->{"income_accno_id_"  . $tz->id};
+        my $expense_accno_id   = $::form->{"expense_accno_id_" . $tz->id};
 
-  # 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();
+        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;
 
-    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;
+        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;
+      }
     }
+
+    1;
+  })) {
+    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');
 }
 
@@ -169,4 +190,53 @@ sub create_or_update {
 
 sub init_defaults        { SL::DB::Default->get }
 
+#
+# helpers
+#
+
+sub setup_show_form_action_bar {
+  my ($self) = @_;
+
+  my $is_new = !$self->config->id;
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      action => [
+        t8('Save'),
+        submit    => [ '#form', { action => 'Buchungsgruppen/' . ($is_new ? 'create' : 'update') } ],
+        checks    => [ 'kivi.validate_form' ],
+        accesskey => 'enter',
+      ],
+
+      action => [
+        t8('Delete'),
+        submit   => [ '#form', { action => 'Buchungsgruppen/delete' } ],
+        confirm  => t8('Do you really want to delete this object?'),
+        disabled => $is_new                  ? t8('This object has not been saved yet.')
+                  : !$self->config->orphaned ? t8('The object is in use and cannot be deleted.')
+                  :                            undef,
+      ],
+
+      link => [
+        t8('Abort'),
+        link => $self->url_for(action => 'list'),
+      ],
+    );
+  }
+  $::request->layout->add_javascripts('kivi.Validator.js');
+}
+
+sub setup_list_action_bar {
+  my ($self) = @_;
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      link => [
+        t8('Add'),
+        link => $self->url_for(action => 'new'),
+      ],
+    );
+  }
+}
+
 1;