Bug 18264 - Course reserves - use itemnumber for editing existing reserve items
[koha.git] / course_reserves / add_items.pl
1 #!/usr/bin/perl
2
3 #
4 # Copyright 2012 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::Biblio;
29
30 use C4::CourseReserves qw(GetCourse GetCourseItem GetCourseReserve ModCourseItem ModCourseReserve);
31
32 my $cgi = new CGI;
33
34 my $action       = $cgi->param('action')       || '';
35 my $course_id    = $cgi->param('course_id')    || '';
36 my $barcode      = $cgi->param('barcode')      || '';
37 my $return       = $cgi->param('return')       || '';
38 my $itemnumber   = ($cgi->param('itemnumber') && $action eq 'lookup') ? $cgi->param('itemnumber') : '';
39
40 my $item = GetBiblioFromItemNumber( $itemnumber, $barcode );
41
42 my $step = ( $action eq 'lookup' && $item ) ? '2' : '1';
43
44 my $tmpl = ($course_id) ? "add_items-step$step.tt" : "invalid-course.tt";
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46     {   template_name   => "course_reserves/$tmpl",
47         query           => $cgi,
48         type            => "intranet",
49         authnotrequired => 0,
50         flagsrequired   => { coursereserves => 'add_reserves' },
51     }
52 );
53 my $inumber = $itemnumber ? "<blank> (itemnumber:$itemnumber)" : "";
54 $template->param( ERROR_BARCODE_NOT_FOUND => $barcode . $inumber )
55   unless ( $barcode && !$itemnumber && $item && $action eq 'lookup' );
56
57 $template->param( course => GetCourse($course_id) );
58
59 if ( $action eq 'lookup' ) {
60     my $course_item = GetCourseItem( itemnumber => $item->{'itemnumber'} );
61     my $course_reserve =
62       ($course_item)
63       ? GetCourseReserve(
64         course_id => $course_id,
65         ci_id     => $course_item->{'ci_id'}
66       )
67       : undef;
68
69     $template->param(
70         item           => $item,
71         course_item    => $course_item,
72         course_reserve => $course_reserve,
73
74         ccodes    => GetAuthorisedValues('CCODE'),
75         locations => GetAuthorisedValues('LOC'),
76         itypes    => GetItemTypes( style => 'array' ),
77         return    => $return,
78     );
79
80 } elsif ( $action eq 'add' ) {
81     my $ci_id = ModCourseItem(
82         itemnumber    => scalar $cgi->param('itemnumber'),
83         itype         => scalar $cgi->param('itype'),
84         ccode         => scalar $cgi->param('ccode'),
85         holdingbranch => scalar $cgi->param('holdingbranch'),
86         location      => scalar $cgi->param('location'),
87     );
88
89     my $cr_id = ModCourseReserve(
90         course_id   => $course_id,
91         ci_id       => $ci_id,
92         staff_note  => scalar $cgi->param('staff_note'),
93         public_note => scalar $cgi->param('public_note'),
94     );
95
96     if ( $return ) {
97         print $cgi->redirect("/cgi-bin/koha/course_reserves/course-details.pl?course_id=$return");
98         exit;
99     }
100 }
101
102 output_html_with_http_headers $cgi, $cookie, $template->output;