From 850ba82b521647752529615b3d5d4383ea77b2e6 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= <bernd@kivitendo-premium.de>
Date: Fri, 9 Jul 2021 12:01:24 +0200
Subject: [PATCH] =?utf8?q?Kreditorenbuchung:=20Redirects=20nach=20Buchen?=
 =?utf8?q?=20aufger=C3=A4umt=20=E2=80=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

- kein redirect nach redirect_header. redirect schickt ruft auch
  redirect_header auf, prüft aber, ob schon ein header geschickt wurde
  und bricht dann ab.
- Verbesserte Info-Ausgabe mit Rechnungsnummer.

Hinweis: callback und redirect mit Fragment geht (noch) nicht, da bei
redirect die callback-Parameter recht simple geparsed werden, um diese
in der Session zu hinterlegen. Das berücksichtigt das Fragment nicht.
---
 bin/mozilla/ap.pl | 35 ++++++++++++++++++++++++-----------
 locale/de/all     |  2 +-
 locale/en/all     |  2 +-
 3 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/bin/mozilla/ap.pl b/bin/mozilla/ap.pl
index d6dd24ebe..dce921d3e 100644
--- a/bin/mozilla/ap.pl
+++ b/bin/mozilla/ap.pl
@@ -39,7 +39,7 @@ use List::UtilsBy qw(sort_by);
 use SL::AP;
 use SL::FU;
 use SL::GL;
-use SL::Helper::Flash qw(flash);
+use SL::Helper::Flash qw(flash flash_later);
 use SL::IR;
 use SL::IS;
 use SL::ReportGenerator;
@@ -857,18 +857,31 @@ sub post {
       $form->{what_done} = "invoice";
       $form->save_history;
     }
-    # no restore_from_session_id needed. we like to have a newly generated
-    # list of invoices for bank transactions
-    print $form->redirect_header($form->{callback}) if ($form->{callback} =~ /BankTransaction/);
-    # With version 3.5 we can add documents, but only after posting. there should be a flag in myconfig for the user
-    # $form->{callback} ||= 'ap.pl?action=edit&id=' . $form->{id} if $myconfig{no_reset_arap};
-    # or a client config setting
-    if ($::instance_conf->get_ap_add_doc && $::instance_conf->get_doc_storage) {
-      my $add_doc_url = build_std_url("script=ap.pl", 'action=edit', 'id=' . E($form->{id}), 'fragment=ui-tabs-docs');
-      print $form->redirect_header($add_doc_url);
+
+    if (!$inline) {
+      my $msg = $locale->text("AP transaction '#1' posted (ID: #2)", $form->{invnumber}, $form->{id});
+      if ($form->{callback} =~ /BankTransaction/) {
+        # no restore_from_session_id needed. we like to have a newly generated
+        # list of invoices for bank transactions
+        SL::Helper::Flash::flash_later('info', $msg);
+        print $form->redirect_header($form->{callback});
+        $::dispatcher->end_request;
+
+      } elsif ($::instance_conf->get_ap_add_doc && $::instance_conf->get_doc_storage) {
+        # Redirect with callback containing a fragment does not work (by now)
+        # because the callback info is stored in the session an parsing the
+        # callback parameters does not support fragments (see SL::Form::redirect).
+        # So use flash_later for the message and redirect_headers for redirecting.
+        my $add_doc_url = build_std_url("script=ap.pl", 'action=edit', 'id=' . E($form->{id}), 'fragment=ui-tabs-docs');
+        SL::Helper::Flash::flash_later('info', $msg);
+        print $form->redirect_header($add_doc_url);
+        $::dispatcher->end_request;
+
+      } else {
+        $form->redirect($msg);
+      }
     }
 
-    $form->redirect($locale->text('AP transaction posted.') . ' ' . $locale->text('ID') . ': ' . $form->{id}) unless $inline;
   } else {
     $form->error($locale->text('Cannot post transaction!'));
   }
diff --git a/locale/de/all b/locale/de/all
index 5dc036db0..74dfbda25 100755
--- a/locale/de/all
+++ b/locale/de/all
@@ -82,7 +82,7 @@ $self->{texts} = {
   'AP Transaction with Storno (abbreviation)' => 'K(S)',
   'AP Transactions'             => 'Kreditorenbuchungen',
   'AP template suggestions'     => 'Vorschlag Kreditorenbuchung',
-  'AP transaction posted.'      => 'Kreditorenbuchung verbucht.',
+  'AP transaction \'#1\' posted (ID: #2)' => 'Kreditorenbuchung \'#1\' verbucht (Buchungsnummer: #2)',
   'AP transactions changeable'  => 'Änderbarkeit von Kreditorenbuchungen',
   'AP transactions with sales taxkeys and/or AR transactions with input taxkeys' => 'Kreditorenbuchungen mit Umsatzsteuer-Steuerschlüsseln und/oder Debitorenbuchungen mit Vorsteuer-Steuerschlüsseln',
   'AP/AR Aging & Journal'       => 'Offene Forderungen/Verbindlichkeiten & Buchungsjournal',
diff --git a/locale/en/all b/locale/en/all
index 194ceac4e..b0fe87bbb 100644
--- a/locale/en/all
+++ b/locale/en/all
@@ -82,7 +82,7 @@ $self->{texts} = {
   'AP Transaction with Storno (abbreviation)' => '',
   'AP Transactions'             => 'Purchase Transactions',
   'AP template suggestions'     => '',
-  'AP transaction posted.'      => '',
+  'AP transaction \'#1\' posted (ID: #2)' => '',
   'AP transactions changeable'  => '',
   'AP transactions with sales taxkeys and/or AR transactions with input taxkeys' => '',
   'AP/AR Aging & Journal'       => '',
-- 
2.20.1