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::Output qw( output_html_with_http_headers );
28 use C4::CourseReserves qw( GetCourse ModCourse ModCourseItem ModCourseReserve );
34 my $action = $cgi->param('action') || q{};
35 my $course_id = $cgi->param('course_id') || q{};
36 my $barcodes = $cgi->param('barcodes') || q{};
37 my $biblionumbers = $cgi->param('biblionumbers') || q{};
39 my $itype = $cgi->param('itype');
40 my $ccode = $cgi->param('ccode');
41 my $homebranch = $cgi->param('homebranch');
42 my $holdingbranch = $cgi->param('holdingbranch');
43 my $location = $cgi->param('location');
45 my $itype_enabled = scalar $cgi->param('itype_enabled') ? 1 : 0;
46 my $ccode_enabled = scalar $cgi->param('ccode_enabled') ? 1 : 0;
47 my $homebranch_enabled = scalar $cgi->param('homebranch_enabled') ? 1 : 0;
48 my $holdingbranch_enabled = scalar $cgi->param('holdingbranch_enabled') ? 1 : 0;
49 my $location_enabled = scalar $cgi->param('location_enabled') ? 1 : 0;
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53 template_name => "course_reserves/batch_add_items.tt",
56 flagsrequired => { coursereserves => 'add_reserves' },
60 my $course = GetCourse($course_id);
62 if ( $course_id && $course ) {
63 $template->param( course => $course );
66 $template->param( action => 'display_form' );
68 elsif ( $action eq 'add' ) {
69 my @barcodes = uniq( split( /\s\n/, $barcodes ) );
70 my @biblionumbers = uniq( split( /\s\n/, $biblionumbers ) );
75 for my $b (@barcodes) {
76 my $item = Koha::Items->find( { barcode => $b } );
79 push( @items, $item );
82 push( @invalid_barcodes, $b );
86 foreach my $item (@items) {
87 my $ci_id = ModCourseItem(
88 itemnumber => $item->id,
89 biblionumber => undef,
92 holdingbranch => $holdingbranch,
93 homebranch => $homebranch,
94 location => $location,
95 itype_enabled => $itype_enabled,
96 ccode_enabled => $ccode_enabled,
97 holdingbranch_enabled => $holdingbranch_enabled,
98 homebranch_enabled => $homebranch_enabled,
99 location_enabled => $location_enabled,
102 my $staff_note = $cgi->param('item_staff_note');
103 my $public_note = $cgi->param('item_public_note');
104 my $cr_id = ModCourseReserve(
105 course_id => $course_id,
107 staff_note => $staff_note,
108 public_note => $public_note,
113 action => 'display_results',
114 items_added => \@items,
115 invalid_barcodes => \@invalid_barcodes,
116 course_id => $course_id,
120 } elsif (@biblionumbers > 0) {
122 my @invalid_biblionumbers;
123 for my $b (@biblionumbers) {
124 my $biblio = Koha::Biblios->find( $b );
127 push( @biblios, $biblio );
130 push( @invalid_biblionumbers, $b );
134 foreach my $biblio (@biblios) {
135 my $ci_id = ModCourseItem(
137 biblionumber => $biblio->id,
140 holdingbranch => $holdingbranch,
141 homebranch => $homebranch,
142 location => $location,
143 itype_enabled => $itype_enabled,
144 ccode_enabled => $ccode_enabled,
145 holdingbranch_enabled => $holdingbranch_enabled,
146 homebranch_enabled => $homebranch_enabled,
147 location_enabled => $location_enabled,
150 my $staff_note = $cgi->param('biblio_staff_note');
151 my $public_note = $cgi->param('biblio_public_note');
152 my $cr_id = ModCourseReserve(
153 course_id => $course_id,
155 staff_note => $staff_note,
156 public_note => $public_note,
161 action => 'display_results',
162 biblios_added => \@biblios,
163 invalid_biblionumbers => \@invalid_biblionumbers,
164 course_id => $course_id,
170 $template->param( action => 'invalid_course' );
173 output_html_with_http_headers $cgi, $cookie, $template->output;