From 6e4577aa01f4e2c99fcd8b7d7d96f125cbf15a1f Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Wed, 18 May 2022 08:30:48 +0100
Subject: [PATCH] Bug 29560: Add option to create MARC Links when bundling
 items

This patch add a checkbox to the 'Add to bundle' modal allowing the user
to optionally add MARC links between the new child record and the
current biblio. For MARC21 this will result in a 773 pointing to the
host from the item record and for UNIMARC a 461 field will be added.
---
 Koha/REST/V1/Items.pm                                       | 6 +++++-
 api/v1/swagger/definitions/bundle_link.yaml                 | 3 +++
 koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt | 6 +++++-
 3 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/Koha/REST/V1/Items.pm b/Koha/REST/V1/Items.pm
index 2e5478770f..a2fca5aa5b 100644
--- a/Koha/REST/V1/Items.pm
+++ b/Koha/REST/V1/Items.pm
@@ -199,7 +199,6 @@ sub add_to_bundle {
         );
     }
 
-
     my $bundle_item_id = $c->validation->param('body')->{'external_id'};
     my $bundle_item = Koha::Items->find( { barcode => $bundle_item_id } );
 
@@ -210,8 +209,13 @@ sub add_to_bundle {
         );
     }
 
+    my $add_link = $c->validation->param('body')->{'marc_link'} // 0;
     return try {
         my $link = $item->add_to_bundle($bundle_item);
+        if ($add_link) {
+            $bundle_item->biblio->link_marc_host(
+                { biblionumber => $item->biblio->biblionumber } );
+        }
         return $c->render(
             status  => 201,
             openapi => $bundle_item
diff --git a/api/v1/swagger/definitions/bundle_link.yaml b/api/v1/swagger/definitions/bundle_link.yaml
index 572be83d8e..d30079b93e 100644
--- a/api/v1/swagger/definitions/bundle_link.yaml
+++ b/api/v1/swagger/definitions/bundle_link.yaml
@@ -11,4 +11,7 @@ properties:
       - string
       - "null"
     description: Item barcode
+  marc_link:
+    type: boolean
+    description: Is there a marc link for this item
 additionalProperties: false
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
index 24331d81ff..2e672f7518 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/detail.tt
@@ -1080,6 +1080,10 @@ Note that permanent location is a code, and location may be an authval.
                                     <input type="text" id="external_id" name="external_id" required="required">
                                     <span class="required">Required</span>
                                 </li>
+                                <li>
+                                    <label for="bundle_link">Add MARC link: </label>
+                                    <input type="checkbox" name="bundle_link"/>
+                                </li>
                             </ol>
                         </fieldset>
                     </div>
@@ -1599,7 +1603,7 @@ Note that permanent location is a code, and location may be an authval.
                   /* Send the data using post with external_id */
                   var posting = $.post({
                       url: url,
-                      data: JSON.stringify({ external_id: $('#external_id').val()}),
+                      data: JSON.stringify({ external_id: $('#external_id').val(), marc_link: $('#link').prop('checked') }),
                       contentType: "application/json; charset=utf-8",
                       dataType: "json"
                   });
-- 
2.20.1