From a301d22fb55c312a85225d7ce764b73c10104936 Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Wed, 3 Apr 2019 14:49:00 +0200
Subject: [PATCH] Bug 22630: Allow to change homebranch in course reserves

Test plan:
1. Create a course (disabled)
2. Add a reserve to this course for an item and set a homebranch
different from the item's homebranch
3. Enable the course
4. Verify that the item's homebranch has changed
5. Disable the course
6. Verify that the item's homebranch was reset to its initial value
7. prove t/db_dependent/CourseReserves/CourseItems.t

Signed-off-by: Liz Rea <wizzyrea@gmail.com>

Signed-off-by: Liz Rea <wizzyrea@gmail.com>
---
 C4/CourseReserves.pm                               |  2 +-
 course_reserves/add_items.pl                       |  1 +
 installer/data/mysql/atomicupdate/bug-22630.perl   | 13 ++++++++++++
 installer/data/mysql/kohastructure.sql             |  2 ++
 .../en/modules/course_reserves/add_items-step2.tt  | 14 +++++++++++++
 .../en/modules/course_reserves/course-details.tt   | 23 +++++++++++++++++++++-
 t/db_dependent/CourseReserves/CourseItems.t        | 11 ++++++++++-
 7 files changed, 63 insertions(+), 3 deletions(-)
 create mode 100644 installer/data/mysql/atomicupdate/bug-22630.perl

diff --git a/C4/CourseReserves.pm b/C4/CourseReserves.pm
index 428d7e9d33..0488dd40c0 100644
--- a/C4/CourseReserves.pm
+++ b/C4/CourseReserves.pm
@@ -52,7 +52,7 @@ BEGIN {
     %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
 
     $DEBUG = 0;
-    @FIELDS = ( 'itype', 'ccode', 'holdingbranch', 'location' );
+    @FIELDS = ( 'itype', 'ccode', 'homebranch', 'holdingbranch', 'location' );
 }
 
 =head1 NAME
diff --git a/course_reserves/add_items.pl b/course_reserves/add_items.pl
index 42416b3a63..f7133b3592 100755
--- a/course_reserves/add_items.pl
+++ b/course_reserves/add_items.pl
@@ -93,6 +93,7 @@ if ( $action eq 'lookup' and $item ) {
         itemnumber    => scalar $cgi->param('itemnumber'),
         itype         => scalar $cgi->param('itype'),
         ccode         => scalar $cgi->param('ccode'),
+        homebranch    => scalar $cgi->param('homebranch'),
         holdingbranch => scalar $cgi->param('holdingbranch'),
         location      => scalar $cgi->param('location'),
     );
diff --git a/installer/data/mysql/atomicupdate/bug-22630.perl b/installer/data/mysql/atomicupdate/bug-22630.perl
new file mode 100644
index 0000000000..02b7fab10b
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug-22630.perl
@@ -0,0 +1,13 @@
+$DBversion = 'XXX';
+if (CheckVersion($DBversion)) {
+    if (!column_exists('course_items', 'homebranch')) {
+        $dbh->do(q{
+            ALTER TABLE course_items
+            ADD COLUMN homebranch VARCHAR(10) NULL DEFAULT NULL AFTER ccode,
+            ADD CONSTRAINT course_items_ibfk_3 FOREIGN KEY (homebranch) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE
+        });
+    }
+
+    SetVersion($DBversion);
+    print "Upgrade to $DBversion done (Bug 22630 - Add course_items.homebranch)\n";
+}
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index 77224bcac3..d27a980253 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -3811,6 +3811,7 @@ CREATE TABLE `course_items` (
   `itemnumber` int(11) NOT NULL, -- items.itemnumber for the item on reserve
   `itype` varchar(10) DEFAULT NULL, -- new itemtype for the item to have while on reserve (optional)
   `ccode` varchar(80) DEFAULT NULL, -- new category code for the item to have while on reserve (optional)
+  `homebranch` varchar(10) DEFAULT NULL, -- new home branch for the item to have while on reserve (optional)
   `holdingbranch` varchar(10) DEFAULT NULL, -- new holding branch for the item to have while on reserve (optional)
   `location` varchar(80) DEFAULT NULL, -- new shelving location for the item to have while on reseve (optional)
   `enabled` enum('yes','no') NOT NULL DEFAULT 'no', -- if at least one enabled course has this item on reseve, this field will be 'yes', otherwise it will be 'no'
@@ -3824,6 +3825,7 @@ CREATE TABLE `course_items` (
 -- Constraints for table `course_items`
 --
 ALTER TABLE `course_items`
+  ADD CONSTRAINT `course_items_ibfk_3` FOREIGN KEY (`homebranch`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE,
   ADD CONSTRAINT `course_items_ibfk_2` FOREIGN KEY (`holdingbranch`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE,
   ADD CONSTRAINT `course_items_ibfk_1` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE CASCADE ON UPDATE CASCADE;
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/add_items-step2.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/add_items-step2.tt
index 919c7d57c6..b59af815dd 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/add_items-step2.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/course_reserves/add_items-step2.tt
@@ -84,6 +84,20 @@
                     </li>
 
                     <li>
+                        <label class="required" for="homebranch">Home library:</label>
+                        <select id="homebranch" name="homebranch">
+                            <option value="">LEAVE UNCHANGED</option>
+                            [% FOREACH b IN Branches.all() %]
+                                [% IF course_item.homebranch.defined && ( ( course.enabled == 'yes' && b.value == item.homebranch ) || ( course.enabled == 'no' && b.value == course_item.homebranch ) ) %]
+                                    <option value="[% b.branchcode | html %]" selected="selected">[% b.branchname | html %]</option>
+                                [% ELSE %]
+                                    <option value="[% b.branchcode | html %]">[% b.branchname | html %]</option>
+                                [% END %]
+                            [% END %]
+                        </select>
+                    </li>
+
+                    <li>
                         <label class="required" for="holdingbranch">Holding library:</label>
                         <select id="holdingbranch" name="holdingbranch">
                             <option value="">LEAVE UNCHANGED</option>
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 446192d0e5..fe2e6a0bf7 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
@@ -81,7 +81,8 @@
                         [% IF item_level_itypes %]<th>Item type</th>[% END %]
                         <th>Collection</th>
                         <th>Location</th>
-                        <th>Library</th>
+                        <th>Home Library</th>
+                        <th>Holding Library</th>
                         <th>Staff note</th>
                         <th>Public note</th>
                         <th>Link</th>
@@ -162,6 +163,26 @@
                                 [% END %]
                             </td>
                             <td>
+                                [% IF cr.course_item.homebranch.defined %]
+                                    [% IF cr.course_item.enabled == 'yes' %]
+                                        <strong>[% Branches.GetName( cr.item.homebranch ) | html %]</strong>
+                                        [% IF cr.item.homebranch %]
+                                            ([% Branches.GetName( cr.course_item.homebranch ) | html %])
+                                        [% END %]
+                                    [% ELSE %]
+                                        [% Branches.GetName( cr.course_item.homebranch ) | html %]
+                                        [% IF cr.item.homebranch %]
+                                            (<strong>[% Branches.GetName( cr.item.homebranch ) | html %]</strong>)
+                                        [% END %]
+                                    [% END %]
+                                [% ELSE %]
+                                    <i>Unchanged</i>
+                                    [% IF cr.item.homebranch %]
+                                        ([% Branches.GetName( cr.item.homebranch ) | html %])
+                                    [% END %]
+                                [% END %]
+                            </td>
+                            <td>
                                 [% IF cr.course_item.holdingbranch.defined %]
                                     [% IF cr.course_item.enabled == 'yes' %]
                                         <strong>[% Branches.GetName( cr.item.holdingbranch ) | html %]</strong>
diff --git a/t/db_dependent/CourseReserves/CourseItems.t b/t/db_dependent/CourseReserves/CourseItems.t
index 4d985f96d1..bf72061b07 100644
--- a/t/db_dependent/CourseReserves/CourseItems.t
+++ b/t/db_dependent/CourseReserves/CourseItems.t
@@ -23,7 +23,7 @@ use C4::CourseReserves qw/ModCourseItem ModCourseReserve DelCourseReserve GetCou
 use C4::Context;
 use Koha::Items;
 
-use Test::More tests => 27;
+use Test::More tests => 32;
 
 BEGIN {
     require_ok('C4::CourseReserves');
@@ -41,6 +41,7 @@ my $ci_id = ModCourseItem(
     itemnumber    => $itemnumber,
     itype         => 'BK_foo',
     ccode         => 'BOOK',
+    homebranch    => 'B2',
     holdingbranch => 'B2',
     location      => 'TH',
 );
@@ -62,12 +63,14 @@ my $cr_id = ModCourseReserve(
 my $course_item = GetCourseItem( ci_id => $ci_id );
 is($course_item->{itype}, 'CD_foo', 'Course item itype should be CD_foo');
 is($course_item->{ccode}, 'CD', 'Course item ccode should be CD');
+is($course_item->{homebranch}, 'B1', 'Course item home branch should be B1');
 is($course_item->{holdingbranch}, 'B1', 'Course item holding branch should be B1');
 is($course_item->{location}, 'HR', 'Course item location should be HR');
 
 my $item = Koha::Items->find($itemnumber);
 is($item->itype, 'BK_foo', 'Item type in course should be BK_foo');
 is($item->ccode, 'BOOK', 'Item ccode in course should be BOOK');
+is($item->homebranch, 'B2', 'Item home branch in course should be B2');
 is($item->holdingbranch, 'B2', 'Item holding branch in course should be B2');
 is($item->location, 'TH', 'Item location in course should be TH');
 
@@ -75,6 +78,7 @@ ModCourseItem(
     itemnumber    => $itemnumber,
     itype         => 'BK_foo',
     ccode         => 'DVD',
+    homebranch    => 'B3',
     holdingbranch => 'B3',
     location      => 'TH',
 );
@@ -89,12 +93,14 @@ ModCourseReserve(
 $course_item = GetCourseItem( ci_id => $ci_id );
 is($course_item->{itype}, 'CD_foo', 'Course item itype should be CD_foo');
 is($course_item->{ccode}, 'CD', 'Course item ccode should be CD');
+is($course_item->{homebranch}, 'B1', 'Course item home branch should be B1');
 is($course_item->{holdingbranch}, 'B1', 'Course item holding branch should be B1');
 is($course_item->{location}, 'HR', 'Course item location should be HR');
 
 $item = Koha::Items->find($itemnumber);
 is($item->itype, 'BK_foo', 'Item type in course should be BK_foo');
 is($item->ccode, 'DVD', 'Item ccode in course should be DVD');
+is($item->homebranch, 'B3', 'Item home branch in course should be B3');
 is($item->holdingbranch, 'B3', 'Item holding branch in course should be B3');
 is($item->location, 'TH', 'Item location in course should be TH');
 
@@ -102,6 +108,7 @@ DelCourseReserve( cr_id => $cr_id );
 $item = Koha::Items->find($itemnumber);
 is($item->itype, 'CD_foo', 'Item type removed from course should be set back to CD_foo');
 is($item->ccode, 'CD', 'Item ccode removed from course should be set back to CD');
+is($item->homebranch, 'B1', 'Item home branch removed from course should be set back B1');
 is($item->holdingbranch, 'B1', 'Item holding branch removed from course should be set back B1');
 is($item->location, 'HR', 'Item location removed from course should be TH');
 
@@ -115,6 +122,7 @@ my $ci_id2 = ModCourseItem(
     itemnumber    => $itemnumber,
     itype         => 'CD_foo',
     ccode         => 'BOOK',
+    homebranch    => 'B1',
     holdingbranch => 'B1',
     location      => 'HR',
 );
@@ -136,6 +144,7 @@ ModCourseItem(
     itemnumber    => $itemnumber,
     itype         => 'CD_foo',
     ccode         => 'DVD',
+    homebranch    => 'B1',
     holdingbranch => 'B1',
     location      => 'HR',
 );
-- 
2.11.0