From b03eee6d525e7e6347e48faa71a0eea2779166ef Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Wed, 26 Feb 2020 13:20:45 -0500
Subject: [PATCH] Bug 24857: Add ability to attach newly cataloged items to a
 volume

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>

Signed-off-by: Rebecca  Coert <rcoert@arlingtonva.us>
---
 cataloguing/additem.pl                             | 61 +++++++++++++++++-----
 .../prog/en/modules/cataloguing/additem.tt         | 30 +++++++++++
 .../intranet-tmpl/prog/js/cataloging_additem.js    | 21 ++++++++
 3 files changed, 99 insertions(+), 13 deletions(-)

diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl
index 130ff0a3f2..84c1587a2a 100755
--- a/cataloguing/additem.pl
+++ b/cataloguing/additem.pl
@@ -22,27 +22,29 @@
 use Modern::Perl;
 
 use CGI qw ( -utf8 );
+use List::MoreUtils qw/any/;
+use MARC::File::XML;
+use Storable qw(thaw freeze);
+use URI::Escape;
+
 use C4::Auth;
-use C4::Output;
 use C4::Biblio;
-use C4::Items;
-use C4::Context;
 use C4::Circulation;
-use C4::Koha;
 use C4::ClassSource;
+use C4::Context;
+use C4::Items;
+use C4::Koha;
+use C4::Members;
+use C4::Output;
+use C4::Search;
+
+use Koha::Biblio::Volume::Items;
+use Koha::Biblio::Volumes;
 use Koha::DateUtils;
-use Koha::Items;
 use Koha::ItemTypes;
+use Koha::Items;
 use Koha::Libraries;
 use Koha::Patrons;
-use List::MoreUtils qw/any/;
-use C4::Search;
-use Storable qw(thaw freeze);
-use URI::Escape;
-use C4::Members;
-
-use MARC::File::XML;
-use URI::Escape;
 
 our $dbh = C4::Context->dbh;
 
@@ -86,6 +88,34 @@ sub set_item_default_location {
     $item->store;
 }
 
+sub add_item_to_volume {
+    my ( $biblionumber, $itemnumber, $volume, $volume_description ) = @_;
+
+    return unless $volume;
+
+    my $volume_id;
+    if ( $volume eq 'create' ) {
+        my $volume = Koha::Biblio::Volume->new(
+            {
+                biblionumber => $biblionumber,
+                description  => $volume_description,
+            }
+        )->store();
+
+        $volume_id = $volume->id;
+    }
+    else {
+        $volume_id = $volume;
+    }
+
+    my $volume_item = Koha::Biblio::Volume::Item->new(
+        {
+            itemnumber => $itemnumber,
+            volume_id  => $volume_id,
+        }
+    )->store();
+}
+
 # NOTE: This code is subject to change in the future with the implemenation of ajax based autobarcode code
 # NOTE: 'incremental' is the ONLY autoBarcode option available to those not using javascript
 sub _increment_barcode {
@@ -390,6 +420,8 @@ my $fa_barcode            = $input->param('barcode');
 my $fa_branch             = $input->param('branch');
 my $fa_stickyduedate      = $input->param('stickyduedate');
 my $fa_duedatespec        = $input->param('duedatespec');
+my $volume                = $input->param('volume');
+my $volume_description    = $input->param('volume_description');
 
 my $frameworkcode = &GetFrameworkCode($biblionumber);
 
@@ -499,6 +531,7 @@ if ($op eq "additem") {
         unless ($exist_itemnumber) {
             my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) = AddItemFromMarc( $record, $biblionumber );
             set_item_default_location($oldbibitemnum);
+            add_item_to_volume( $oldbiblionumber, $oldbibitemnum, $volume, $volume_description );
 
             # Pushing the last created item cookie back
             if ($prefillitem && defined $record) {
@@ -599,6 +632,7 @@ if ($op eq "additem") {
             my ( $oldbiblionumber, $oldbibnum, $oldbibitemnum ) =
                 AddItemFromMarc( $record, $biblionumber, { skip_modzebra_update => 1 } );
             set_item_default_location($oldbibitemnum);
+            add_item_to_volume( $oldbiblionumber, $oldbibitemnum, $volume, $volume_description );
 
             # We count the item only if it was really added
             # That way, all items are added, even if there was some already existing barcodes
@@ -958,6 +992,7 @@ my $item = Koha::Items->find($itemnumber); # We certainly want to fetch it earli
 
 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
 $template->param(
+    volumes      => scalar Koha::Biblio::Volumes->search({ biblionumber => $biblionumber }),
     biblionumber => $biblionumber,
     title        => $oldrecord->{title},
     author       => $oldrecord->{author},
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
index 2d64bef70f..da825161da 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
@@ -16,6 +16,9 @@
 [% INCLUDE 'calendar.inc' %]
 [% INCLUDE 'str/cataloging_additem.inc' %]
 [% Asset.js("js/cataloging_additem.js") | $raw %]
+<script>
+  var has_volumes = [% volumes.count %];
+</script>
 </head>
 
 <body id="cat_additem" class="cat">
@@ -224,6 +227,33 @@
     <input type="hidden" name="indicator" value=" " />
     <input type="hidden" name="itemnumber" value="[% itemnumber | html %]" />
 
+[% IF volumes.count && op != 'saveitem' %]
+    <fieldset class="rows">
+        <legend><i class="fa fa-plus"></i> Add to volume</legend>
+
+        <p>
+            <label for="select_volume">Options: </label>
+            <select name="volume" id="volume-add-or-create-form-select">
+                <optgroup label="Use existing volume">
+                    [% FOREACH v IN volumes %]
+                        <option value="[% v.id | html %]">Use: [% v.description | html %]</option>
+                    [% END %]
+                </optgroup>
+                <optgroup label="Other options">
+                    <option id="volume-add-or-create-form-no-add" value="">Do not add to volume</option>
+                    <option value="create">Create new volume</option>
+                </optgroup>
+            </select>
+        </p>
+
+        <p id="volume-add-or-create-form-description-block">
+            <label for="volume_description" class="required">Name: </label>
+            <input name="volume_description" id="volume-add-or-create-form-description" type="text" size="30" class="required" />
+            <span class="required">Required</span>
+        </p>
+    </fieldset>
+[% END %]
+
 <fieldset class="action">    [% IF op != 'saveitem' %]
     <input type="submit" name="phony_submit" value="phony_submit" id="phony_submit" style="display:none;" onclick="return false;" />
     <!-- Note : We use here a false submit button because we have several submit buttons and we don't want the user to believe they validated the adding of multiple items
diff --git a/koha-tmpl/intranet-tmpl/prog/js/cataloging_additem.js b/koha-tmpl/intranet-tmpl/prog/js/cataloging_additem.js
index e3e746764a..64fd9e26a4 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/cataloging_additem.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/cataloging_additem.js
@@ -73,6 +73,27 @@ $(document).ready(function(){
     });
 
     $('.subfield_line select').select2();
+
+    // Add new item to volume
+    if ( has_volumes ) {
+        $('#volume-add-or-create-form-description-block').hide();
+        $('#volume-add-or-create-form-no-add').attr('selected', 'selected' );
+
+        $('#volume-add-or-create-form-select').on('change', function(){
+            if ( $(this).val() == 'create' ) {
+                $('#volume-add-or-create-form-description')
+                    .addClass('required')
+                    .attr( 'required', 'required' );
+                $('#volume-add-or-create-form-description-block').show();
+            } else {
+                $('#volume-add-or-create-form-description')
+                    .removeClass('required')
+                    .removeAttr('required');
+                $('#volume-add-or-create-form-description-block').hide();
+            }
+        });
+    }
+
 });
 
 function Check(f) {
-- 
2.11.0