From 91c5193cdc2154fd9bc0dc2884731c08366b2ae1 Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Fri, 23 Mar 2018 01:03:00 +0000
Subject: [PATCH] Bug 20467: Add ability to batch add items to a course

Some librarians have requested the ability to batch add items to a
course using the same settings for all items.

Test Plan:
1) Apply this patch
2) Create a new course
3) Click the new 'Batch add reserves' button
4) Enter some valid and invalid barcodes in the text box, add some settings
5) Submit the batch, note the results list added items and invalid barcodes
6) Note the valid items were added to the course
---
 course_reserves/batch_add_items.pl                 | 114 ++++++++++++++++
 .../en/modules/course_reserves/batch_add_items.tt  | 146 +++++++++++++++++++++
 .../en/modules/course_reserves/course-details.tt   |   1 +
 3 files changed, 261 insertions(+)
 create mode 100755 course_reserves/batch_add_items.pl
 create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/batch_add_items.tt

diff --git a/course_reserves/batch_add_items.pl b/course_reserves/batch_add_items.pl
new file mode 100755
index 0000000000..4fd582192c
--- /dev/null
+++ b/course_reserves/batch_add_items.pl
@@ -0,0 +1,114 @@
+#!/usr/bin/perl
+
+#
+# Copyright 2012 Bywater Solutions
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use CGI qw ( -utf8 );
+
+use C4::Auth;
+use C4::Output;
+use C4::Koha;
+use C4::Biblio;
+use Koha::Items;
+
+use C4::CourseReserves
+  qw(GetCourse GetCourseItem GetCourseReserve ModCourseItem ModCourseReserve);
+
+use Koha::Items;
+use Koha::ItemTypes;
+
+my $cgi = new CGI;
+
+my $action    = $cgi->param('action')    || q{};
+my $course_id = $cgi->param('course_id') || q{};
+my $barcodes  = $cgi->param('barcodes')  || q{};
+
+my $itype         = $cgi->param('itype');
+my $ccode         = $cgi->param('ccode');
+my $holdingbranch = $cgi->param('holdingbranch');
+my $location      = $cgi->param('location');
+my $staff_note    = $cgi->param('staff_note');
+my $public_note   = $cgi->param('public_note');
+
+my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
+    {
+        template_name   => "course_reserves/batch_add_items.tt",
+        query           => $cgi,
+        type            => "intranet",
+        authnotrequired => 0,
+        flagsrequired   => { coursereserves => 'add_reserves' },
+    }
+);
+
+$template->param( course => GetCourse($course_id) );
+
+if ( !$action ) {
+
+    my $itemtypes = Koha::ItemTypes->search;
+    $template->param(
+        action    => 'display_form',
+        ccodes    => GetAuthorisedValues('CCODE'),
+        locations => GetAuthorisedValues('LOC'),
+        itypes    => $itemtypes,
+    );
+
+}
+elsif ( $action eq 'add' ) {
+    my @barcodes = split( "\r\n", $barcodes );
+
+    my @items;
+    my @invalid_barcodes;
+    for my $b (@barcodes) {
+        my $item = Koha::Items->find( { barcode => $b } );
+
+        if ($item) {
+            push( @items, $item );
+        }
+        else {
+            push( @invalid_barcodes, $b );
+        }
+    }
+
+    foreach my $item (@items) {
+        my $ci_id = ModCourseItem(
+            itemnumber    => $item->id,
+            itype         => $itype,
+            ccode         => $ccode,
+            holdingbranch => $holdingbranch,
+            location      => $location,
+        );
+
+        my $cr_id = ModCourseReserve(
+            course_id   => $course_id,
+            ci_id       => $ci_id,
+            staff_note  => $staff_note,
+            public_note => $public_note,
+        );
+    }
+
+    $template->param(
+        action           => 'display_results',
+        items_added      => \@items,
+        invalid_barcodes => \@invalid_barcodes,
+        course_id        => $course_id,
+    );
+}
+
+output_html_with_http_headers $cgi, $cookie, $template->output;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/batch_add_items.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/batch_add_items.tt
new file mode 100644
index 0000000000..aa0520cb2e
--- /dev/null
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/batch_add_items.tt
@@ -0,0 +1,146 @@
+[% INCLUDE 'doc-head-open.inc' %]
+<title>Koha &rsaquo; Course reserves &rsaquo; Add items</title>
+[% INCLUDE 'doc-head-close.inc' %]
+</head>
+<body id="courses_add_items_step1" class="course">
+
+[% INCLUDE 'header.inc' %]
+[% INCLUDE 'cat-search.inc' %]
+
+<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/course_reserves/course-reserves.pl">Course reserves</a> &rsaquo; Add reserves for <i><a href="/cgi-bin/koha/course_reserves/course-details.pl?course_id=[% course.course_id %]">[% course.course_name %]</a></i></div>
+
+<div class="main container-fluid">
+    <div class="row">
+        <div class="col-md-8 col-md-offset-2">
+
+        [% IF action == 'display_form' %]
+            <form method="post" action="/cgi-bin/koha/course_reserves/batch_add_items.pl">
+                <input type="hidden" name="course_id" value="[% course.course_id %]" />
+                <input type="hidden" name="action" value="add" />
+
+                <fieldset class="rows">
+                    <legend>Add items: scan barcodes</legend>
+                    <ol>
+                        <li>
+                            <label class="required" for="barcodes">Item barcodes:</label>
+                            <textarea rows="20" cols="50" id="barcodes" name="barcodes" class="focus"></textarea>
+                        </li>
+                        [% IF item_level_itypes %]
+                        <li>
+                            <label class="required" for="itype">Item type:</label>
+                            <select id="itype" name="itype">
+                                <option value="">LEAVE UNCHANGED</option>
+
+                                [% FOREACH it IN itypes %]
+                                    [% IF course_item.itype && ( ( course.enabled == 'yes' && it.itemtype == item.itype ) || ( course.enabled == 'no' && it.itemtype == course_item.itype ) ) %]
+                                        <option value="[% it.itemtype %]" selected="selected">[% it.description %]</option>
+                                    [% ELSE %]
+                                        <option value="[% it.itemtype %]">[% it.description %]</option>
+                                    [% END %]
+                                [% END %]
+                            </select>
+                        </li>
+                        [% END %]
+
+                        <li>
+                            <label class="required" for="ccode">Collection code:</label>
+                            <select id="ccode" name="ccode">
+                                <option value="">LEAVE UNCHANGED</option>
+
+                                [% FOREACH c IN ccodes %]
+                                    [% IF course_item.ccode && ( ( course.enabled == 'yes' && c.authorised_value == item.ccode ) || ( course.enabled == 'no' && c.authorised_value == course_item.ccode ) ) %]
+                                        <option value="[% c.authorised_value %]" selected="selected">[% c.lib %]</option>
+                                    [% ELSE %]
+                                        <option value="[% c.authorised_value %]">[% c.lib %]</option>
+                                    [% END %]
+                                [% END %]
+                            </select>
+                        </li>
+
+                        <li>
+                            <label class="required" for="location">Shelving location:</label>
+                            <select id="location" name="location">
+                                <option value="">LEAVE UNCHANGED</option>
+
+                                [% FOREACH s IN locations %]
+                                    [% IF course_item.location && ( ( course.enabled == 'yes' && s.authorised_value == item.location ) || ( course.enabled == 'no' && s.authorised_value == course_item.location ) ) %]
+                                        <option value="[% s.authorised_value %]" selected="selected">[% s.lib %]</option>
+                                    [% ELSE %]
+                                        <option value="[% s.authorised_value %]">[% s.lib %]</option>
+                                    [% END %]
+                                [% END %]
+                            </select>
+                        </li>
+
+                        <li>
+                            <label class="required" for="holdingbranch">Holding library:</label>
+                            <select id="holdingbranch" name="holdingbranch">
+                                <option value="">LEAVE UNCHANGED</option>
+                                [% FOREACH b IN Branches.all() %]
+                                    [% IF course_item.holdingbranch && ( ( course.enabled == 'yes' && b.value == item.holdingbranch ) || ( course.enabled == 'no' && b.value == course_item.holdingbranch ) ) %]
+                                        <option value="[% b.branchcode %]" selected="selected">[% b.branchname %]</option>
+                                    [% ELSE %]
+                                        <option value="[% b.branchcode %]">[% b.branchname %]</option>
+                                    [% END %]
+                                [% END %]
+                            </select>
+                        </li>
+
+                        <li>
+                            <label for="staff_note">Staff note:</label>
+                            <textarea name="staff_note" id="staff_note">[% course_reserve.staff_note %]</textarea>
+                        </li>
+
+                        <li>
+                            <label for="public_note">Public note:</label>
+                            <textarea name="public_note" id="public_note">[% course_reserve.public_note %]</textarea>
+                        </li>
+                    </ol>
+                </fieldset>
+
+                <p>
+                    Any items with existing course reserves will have their <i>on reserve</i> values updated.
+                </p>
+
+                <fieldset class="action">
+                    <input type="submit" value="Submit" class="submit" />
+
+                    <a href="/cgi-bin/koha/course_reserves/course-details.pl?course_id=[% course.course_id %]" class="cancel">Cancel</a>
+                </fieldset>
+            </form>
+        [% END %]
+
+        [% IF action == 'display_results' %]
+            <h1>Results</h1>
+
+            <h3>Items added</h3>
+            [% IF items_added.size > 0 %]
+                <p>The following items were added or updated:</p>
+                <ul>
+                    [% FOREACH i IN items_added %]
+                        <li>[% i.biblio.title %] ( [% i.barcode %] )</li>
+                    [% END %]
+                </ul>
+            [% ELSE %]
+                No valid item barcodes found.
+            [% END %]
+            
+
+            [% IF invalid_barcodes.size > 0 %]
+                <h3>Invalid barcodes</h3>
+                <p>The following invalid barcodes were skipped:</p>
+                <ul>
+                    [% FOREACH b IN invalid_barcodes %]
+                        <li>[% b %]</li>
+                    [% END %]
+                </ul>
+            [% END %]
+
+            <p>
+                <a class='btn btn-default' href="/cgi-bin/koha/course_reserves/course-details.pl?course_id=[% course_id %]">View course</a>
+            </p>
+        [% END %]
+    </div>
+</div>
+
+[% INCLUDE 'intranet-bottom.inc' %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/course-details.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/course-details.tt
index dec2c5d24e..c4bd75d4b1 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/course-details.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/course-details.tt
@@ -24,6 +24,7 @@
             <div id="toolbar">
                 [% IF CAN_user_coursereserves_add_reserves %]
                     <a class="btn btn-default btn-sm" id="add_items" href="/cgi-bin/koha/course_reserves/add_items.pl?course_id=[% course.course_id %]"><i class="fa fa-plus"></i> Add reserves</a>
+                    <a class="btn btn-default btn-sm" id="add_items" href="/cgi-bin/koha/course_reserves/batch_add_items.pl?course_id=[% course.course_id %]"><i class="fa fa-plus"></i> Batch add reserves</a>
                 [% END %]
                 [% IF ( CAN_user_coursereserves_manage_courses ) %]
                     <a class="btn btn-default btn-sm" id="edit_course" href="/cgi-bin/koha/course_reserves/course.pl?course_id=[% course.course_id %]"><i class="fa fa-pencil"></i> Edit course</a>
-- 
2.14.3 (Apple Git-98)