Bug 20467: Add ability to batch add items to a course
[koha.git] / course_reserves / batch_add_items.pl
1 #!/usr/bin/perl
2
3 #
4 # Copyright 2018 Bywater Solutions
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24
25 use C4::Auth;
26 use C4::Output;
27 use C4::Koha;
28 use C4::CourseReserves qw(ModCourseItem ModCourseReserve GetCourse);
29
30 use Koha::Items;
31 use Koha::ItemTypes;
32
33 my $cgi = new CGI;
34
35 my $action    = $cgi->param('action')    || q{};
36 my $course_id = $cgi->param('course_id') || q{};
37 my $barcodes  = $cgi->param('barcodes')  || q{};
38
39 my $itype         = $cgi->param('itype');
40 my $ccode         = $cgi->param('ccode');
41 my $holdingbranch = $cgi->param('holdingbranch');
42 my $location      = $cgi->param('location');
43 my $staff_note    = $cgi->param('staff_note');
44 my $public_note   = $cgi->param('public_note');
45
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47     {
48         template_name   => "course_reserves/batch_add_items.tt",
49         query           => $cgi,
50         type            => "intranet",
51         authnotrequired => 0,
52         flagsrequired   => { coursereserves => 'add_reserves' },
53     }
54 );
55
56 $template->param( course => GetCourse($course_id) );
57
58 if ( !$action ) {
59
60     my $itemtypes = Koha::ItemTypes->search;
61     $template->param(
62         action    => 'display_form',
63         ccodes    => GetAuthorisedValues('CCODE'),
64         locations => GetAuthorisedValues('LOC'),
65         itypes    => $itemtypes,
66     );
67
68 }
69 elsif ( $action eq 'add' ) {
70     my @barcodes = split( "\r\n", $barcodes );
71
72     my @items;
73     my @invalid_barcodes;
74     for my $b (@barcodes) {
75         my $item = Koha::Items->find( { barcode => $b } );
76
77         if ($item) {
78             push( @items, $item );
79         }
80         else {
81             push( @invalid_barcodes, $b );
82         }
83     }
84
85     foreach my $item (@items) {
86         my $ci_id = ModCourseItem(
87             itemnumber    => $item->id,
88             itype         => $itype,
89             ccode         => $ccode,
90             holdingbranch => $holdingbranch,
91             location      => $location,
92         );
93
94         my $cr_id = ModCourseReserve(
95             course_id   => $course_id,
96             ci_id       => $ci_id,
97             staff_note  => $staff_note,
98             public_note => $public_note,
99         );
100     }
101
102     $template->param(
103         action           => 'display_results',
104         items_added      => \@items,
105         invalid_barcodes => \@invalid_barcodes,
106         course_id        => $course_id,
107     );
108 }
109
110 output_html_with_http_headers $cgi, $cookie, $template->output;