From cef9764f794fc2d60e89633e28fe463983d1d4fe Mon Sep 17 00:00:00 2001
From: Emmi Takkinen <emmi.takkinen@koha-suomi.fi>
Date: Thu, 28 Jul 2022 15:22:20 +0300
Subject: [PATCH] Bug 24616: Allow copying notice to a branch even if it exists
 in that branch

If you try to copy notice or slip to a branch which already
has that notice or slip Koha won't let you save it. Instead
pop-up saying branch already has that notice is displayed.

This patch allows users to copy and override already existing
notice or slip.

To test:
1. Find notice which exists e.g. in all branches and branch A.
2. Copy that notice, add notice screen is displayed.
3. Press save.
=> Pop-up informing that notice already exists in branch A
is displayed.
4. Apply this patch.
5. Repeat steps 2. and 3.
6. Pop-up to confirm overwrite of notice is displayed.
=> Confirm that if you press OK, notice is saved succesfully.
=> Otherwise, changes to notice should not be saved.

Also try to save the notice you tested with as a new (same code)
to branch A. Confirm that pop-up is displayed as it should be.

Sponsored-by: Koha-Suomi Oy

Signed-off-by: Nick <nick@bywatersolutions.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
---
 koha-tmpl/intranet-tmpl/prog/js/letter.js | 30 ++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/js/letter.js b/koha-tmpl/intranet-tmpl/prog/js/letter.js
index 53ac7b120e..da044f5dd4 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/letter.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/letter.js
@@ -4,7 +4,7 @@
 var modal_loading = "<div id=\"loading\"><img src=\"" + interface + "/" + theme + "/img/spinner-small.gif\" alt=\"\" /> "+ __('Loading...') +"</div>";
 
 var editing = 0;
-if( add_form == 1 && code !== '' ){
+if( add_form == 1 && code !== '' || copy_form == 1 && code !== ''){
     editing = 1;
 }
 
@@ -30,6 +30,24 @@ function checkCodes( new_lettercode, new_branchcode ){
     });
 }
 
+function confirmOverwrite( new_lettercode, new_branchcode ){
+    var letter_exists;
+    $.ajax({
+        data: { code: new_lettercode, branchcode: new_branchcode },
+        type: 'GET',
+        url: '/cgi-bin/koha/svc/letters/get/',
+        async: !1,
+        success: function (data) {
+            if ( data.letters.length > 0 ) {
+                letter_exists = 1;
+            }
+        }
+    });
+    if(letter_exists){
+        return confirm(__("A letter with the code '%s' already exists for '%s'. Overwrite this letter?").format(new_lettercode, new_branchcode));
+    }
+}
+
 var Sticky;
 
 $(document).ready(function() {
@@ -103,6 +121,16 @@ $(document).ready(function() {
             // Test if code already exists in DB
             if( editing == 1 ){ // This is an edit operation
                 // We don't need to check for an existing Code
+                // However if we're copying, provide confirm
+                // pop up of overwriting existing notice or slip
+                if(copy_form == 1){
+                    var new_lettercode = $("#code").val();
+                    var new_branchcode = $("#branch").val();
+                    var confirm = confirmOverwrite( new_lettercode, new_branchcode );
+                    if( confirm && confirm === false){
+                        return false;
+                    }
+                }
             } else {
                 var new_lettercode = $("#code").val();
                 var new_branchcode = $("#branch").val();
-- 
2.30.2