4 # Copyright 2018 Bywater Solutions
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use List::MoreUtils qw( uniq );
26 use C4::Auth qw( get_template_and_user );
27 use C4::Circulation qw( barcodedecode );
28 use C4::Output qw( output_html_with_http_headers );
29 use C4::CourseReserves qw( GetCourse ModCourse ModCourseItem ModCourseReserve );
35 my $action = $cgi->param('action') || q{};
36 my $course_id = $cgi->param('course_id') || q{};
37 my $barcodes = $cgi->param('barcodes') || q{};
38 my $biblionumbers = $cgi->param('biblionumbers') || q{};
40 my $itype = $cgi->param('itype');
41 my $ccode = $cgi->param('ccode');
42 my $homebranch = $cgi->param('homebranch');
43 my $holdingbranch = $cgi->param('holdingbranch');
44 my $location = $cgi->param('location');
46 my $itype_enabled = scalar $cgi->param('itype_enabled') ? 1 : 0;
47 my $ccode_enabled = scalar $cgi->param('ccode_enabled') ? 1 : 0;
48 my $homebranch_enabled = scalar $cgi->param('homebranch_enabled') ? 1 : 0;
49 my $holdingbranch_enabled = scalar $cgi->param('holdingbranch_enabled') ? 1 : 0;
50 my $location_enabled = scalar $cgi->param('location_enabled') ? 1 : 0;
52 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
54 template_name => "course_reserves/batch_add_items.tt",
57 flagsrequired => { coursereserves => 'add_reserves' },
61 my $course = GetCourse($course_id);
63 if ( $course_id && $course ) {
64 $template->param( course => $course );
67 $template->param( action => 'display_form' );
69 elsif ( $action eq 'add' ) {
70 my @barcodes = uniq( split( /\s\n/, $barcodes ) );
71 my @biblionumbers = uniq( split( /\s\n/, $biblionumbers ) );
76 for my $b (@barcodes) {
77 $b = barcodedecode($b) if $b;
78 my $item = Koha::Items->find( { barcode => $b } );
81 push( @items, $item );
84 push( @invalid_barcodes, $b );
88 foreach my $item (@items) {
89 my $ci_id = ModCourseItem(
90 itemnumber => $item->id,
91 biblionumber => undef,
94 holdingbranch => $holdingbranch,
95 homebranch => $homebranch,
96 location => $location,
97 itype_enabled => $itype_enabled,
98 ccode_enabled => $ccode_enabled,
99 holdingbranch_enabled => $holdingbranch_enabled,
100 homebranch_enabled => $homebranch_enabled,
101 location_enabled => $location_enabled,
104 my $staff_note = $cgi->param('item_staff_note');
105 my $public_note = $cgi->param('item_public_note');
106 my $cr_id = ModCourseReserve(
107 course_id => $course_id,
109 staff_note => $staff_note,
110 public_note => $public_note,
115 action => 'display_results',
116 items_added => \@items,
117 invalid_barcodes => \@invalid_barcodes,
118 course_id => $course_id,
122 } elsif (@biblionumbers > 0) {
124 my @invalid_biblionumbers;
125 for my $b (@biblionumbers) {
126 my $biblio = Koha::Biblios->find( $b );
129 push( @biblios, $biblio );
132 push( @invalid_biblionumbers, $b );
136 foreach my $biblio (@biblios) {
137 my $ci_id = ModCourseItem(
139 biblionumber => $biblio->id,
142 holdingbranch => $holdingbranch,
143 homebranch => $homebranch,
144 location => $location,
145 itype_enabled => $itype_enabled,
146 ccode_enabled => $ccode_enabled,
147 holdingbranch_enabled => $holdingbranch_enabled,
148 homebranch_enabled => $homebranch_enabled,
149 location_enabled => $location_enabled,
152 my $staff_note = $cgi->param('biblio_staff_note');
153 my $public_note = $cgi->param('biblio_public_note');
154 my $cr_id = ModCourseReserve(
155 course_id => $course_id,
157 staff_note => $staff_note,
158 public_note => $public_note,
163 action => 'display_results',
164 biblios_added => \@biblios,
165 invalid_biblionumbers => \@invalid_biblionumbers,
166 course_id => $course_id,
172 $template->param( action => 'invalid_course' );
175 output_html_with_http_headers $cgi, $cookie, $template->output;