Bug 23727: Editing course reserve items is broken
[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 use Koha::Items;
30
31 use C4::CourseReserves qw(GetCourse GetCourseItem GetCourseReserve ModCourseItem ModCourseReserve);
32
33 use Koha::Items;
34 use Koha::ItemTypes;
35
36 my $cgi = new CGI;
37
38 my $action       = $cgi->param('action')       || '';
39 my $course_id    = $cgi->param('course_id')    || '';
40 my $barcode      = $cgi->param('barcode')      || '';
41 my $return       = $cgi->param('return')       || '';
42 my $itemnumber   = $cgi->param('itemnumber')   || '';
43 my $is_edit      = $cgi->param('is_edit')      || '';
44
45 my $item = Koha::Items->find( { ( $itemnumber ? ( itemnumber => $itemnumber ) : ( barcode => $barcode ) ) } );
46 $itemnumber = $item->id if $item;
47
48 my $title = ($item) ? $item->biblio->title : undef;
49
50 my $step = ( $action eq 'lookup' && $item ) ? '2' : '1';
51
52 my $tmpl = ($course_id) ? "add_items-step$step.tt" : "invalid-course.tt";
53 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
54     {   template_name   => "course_reserves/$tmpl",
55         query           => $cgi,
56         type            => "intranet",
57         authnotrequired => 0,
58         flagsrequired   => { coursereserves => 'add_reserves' },
59     }
60 );
61
62 if ( !$item && $action eq 'lookup' ){
63     $template->param( ERROR_ITEM_NOT_FOUND => 1 );
64     $template->param( UNKNOWN_BARCODE => $barcode ) if $barcode;
65 }
66
67 $template->param( course => GetCourse($course_id) );
68
69 if ( $action eq 'lookup' and $item ) {
70     my $course_item = Koha::Course::Items->find({ itemnumber => $item->id });
71     my $course_reserve =
72       ($course_item)
73       ? GetCourseReserve(
74         course_id => $course_id,
75         ci_id     => $course_item->ci_id,
76       )
77       : undef;
78
79     my $itemtypes = Koha::ItemTypes->search;
80     $template->param(
81         item           => $item,
82         biblio         => $item->biblio,
83         course_item    => $course_item,
84         course_reserve => $course_reserve,
85         is_edit        => $is_edit,
86
87         ccodes    => GetAuthorisedValues('CCODE'),
88         locations => GetAuthorisedValues('LOC'),
89         itypes    => $itemtypes, # FIXME We certainly want to display the translated_description in the template
90         return    => $return,
91     );
92
93 } elsif ( $action eq 'add' ) {
94     my $itype         = scalar $cgi->param('itype');
95     my $ccode         = scalar $cgi->param('ccode');
96     my $holdingbranch = scalar $cgi->param('holdingbranch');
97     my $location      = scalar $cgi->param('location');
98
99     my $itype_enabled         = scalar $cgi->param('itype_enabled') ? 1 : 0;
100     my $ccode_enabled         = scalar $cgi->param('ccode_enabled') ? 1 : 0;
101     my $holdingbranch_enabled = scalar $cgi->param('holdingbranch_enabled') ? 1 : 0;
102     my $location_enabled      = scalar $cgi->param('location_enabled') ? 1 : 0;
103
104     my $ci_id = ModCourseItem(
105             itemnumber    => $itemnumber,
106             itype         => $itype,
107             ccode         => $ccode,
108             holdingbranch => $holdingbranch,
109             location      => $location,
110             itype_enabled         => $itype_enabled,
111             ccode_enabled         => $ccode_enabled,
112             holdingbranch_enabled => $holdingbranch_enabled,
113             location_enabled      => $location_enabled,
114     );
115
116     my $cr_id = ModCourseReserve(
117         course_id   => $course_id,
118         ci_id       => $ci_id,
119         staff_note  => scalar $cgi->param('staff_note'),
120         public_note => scalar $cgi->param('public_note'),
121     );
122
123     if ( $return ) {
124         print $cgi->redirect("/cgi-bin/koha/course_reserves/course-details.pl?course_id=$return");
125         exit;
126     }
127 }
128
129 output_html_with_http_headers $cgi, $cookie, $template->output;