Bug 14237: Add individual bibliographic records to course reserves
[srvgit] / 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 = CGI->new;
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 my $biblionumber = $cgi->param('biblionumber') || '';
45
46 $barcode =~ s/^\s*|\s*$//g; #remove leading/trailing whitespace
47 $biblionumber =~ s/^\s*|\s*$//g; #remove leading/trailing whitespace
48
49 my ( $item, $biblio );
50
51 if ( $barcode || $itemnumber ) {
52     # adding an item to course items
53     $item = $itemnumber ? Koha::Items->find( $itemnumber ) : Koha::Items->find({ barcode => $barcode });
54     if ( $item ) {
55         $itemnumber = $item->id;
56         $biblio = $item->biblio;
57         $biblionumber = $biblio->biblionumber;
58     }
59 } else {
60     # adding a biblio to course items
61     $biblio = Koha::Biblios->find( $biblionumber );
62 }
63
64 my $title = $biblio->title if $biblio;
65
66 my $step = ( $action eq 'lookup' && ( $item or $biblio ) ) ? '2' : '1';
67
68 my $tmpl = ($course_id) ? "add_items-step$step.tt" : "invalid-course.tt";
69 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
70     {   template_name   => "course_reserves/$tmpl",
71         query           => $cgi,
72         type            => "intranet",
73         flagsrequired   => { coursereserves => 'add_reserves' },
74     }
75 );
76
77 if ( !$item && !$biblio && $action eq 'lookup' ){
78     $template->param( ERROR_ITEM_NOT_FOUND => 1 );
79     $template->param( UNKNOWN_BARCODE => $barcode ) if $barcode;
80     $template->param( UNKNOWN_BIBLIONUMBER => $biblionumber ) if $biblionumber;
81 }
82
83 $template->param( course => GetCourse($course_id) );
84
85 if ( $action eq 'lookup' and $item ) {
86     my $course_item = Koha::Course::Items->find({ itemnumber => $item->id });
87     my $course_reserve =
88       ($course_item)
89       ? GetCourseReserve(
90         course_id => $course_id,
91         ci_id     => $course_item->ci_id,
92       )
93       : undef;
94
95     my $itemtypes = Koha::ItemTypes->search;
96     $template->param(
97         item           => $item,
98         biblio         => $biblio,
99         course_item    => $course_item,
100         course_reserve => $course_reserve,
101         is_edit        => $is_edit,
102
103         ccodes    => GetAuthorisedValues('CCODE'),
104         locations => GetAuthorisedValues('LOC'),
105         itypes    => $itemtypes, # FIXME We certainly want to display the translated_description in the template
106         return    => $return,
107     );
108
109 } elsif ( $action eq 'lookup' and $biblio ) {
110     my $course_item = Koha::Course::Items->find({ biblionumber => $biblio->biblionumber });
111     my $course_reserve =
112       ($course_item)
113       ? GetCourseReserve(
114         course_id => $course_id,
115         ci_id     => $course_item->ci_id,
116       )
117       : undef;
118
119     my $itemtypes = Koha::ItemTypes->search;
120     $template->param(
121         biblio         => $biblio,
122         course_item    => $course_item,
123         course_reserve => $course_reserve,
124         is_edit        => $is_edit,
125
126         return    => $return,
127     );
128
129 } elsif ( $action eq 'add' ) {
130     my $itype         = scalar $cgi->param('itype');
131     my $ccode         = scalar $cgi->param('ccode');
132     my $homebranch    = $cgi->param('homebranch');
133     my $holdingbranch = scalar $cgi->param('holdingbranch');
134     my $location      = scalar $cgi->param('location');
135
136     my $itype_enabled         = scalar $cgi->param('itype_enabled') ? 1 : 0;
137     my $ccode_enabled         = scalar $cgi->param('ccode_enabled') ? 1 : 0;
138     my $homebranch_enabled    = $cgi->param('homebranch_enabled') ? 1 : 0;
139     my $holdingbranch_enabled = scalar $cgi->param('holdingbranch_enabled') ? 1 : 0;
140     my $location_enabled      = scalar $cgi->param('location_enabled') ? 1 : 0;
141
142     my $ci_id = ModCourseItem(
143             itemnumber    => $itemnumber,
144             biblionumber  => $biblionumber,
145             itype         => $itype,
146             ccode         => $ccode,
147             homebranch    => $homebranch,
148             holdingbranch => $holdingbranch,
149             location      => $location,
150             itype_enabled         => $itype_enabled,
151             ccode_enabled         => $ccode_enabled,
152             homebranch_enabled    => $homebranch_enabled,
153             holdingbranch_enabled => $holdingbranch_enabled,
154             location_enabled      => $location_enabled,
155     );
156
157     my $cr_id = ModCourseReserve(
158         course_id   => $course_id,
159         ci_id       => $ci_id,
160         staff_note  => scalar $cgi->param('staff_note'),
161         public_note => scalar $cgi->param('public_note'),
162     );
163
164     if ( $return ) {
165         print $cgi->redirect("/cgi-bin/koha/course_reserves/course-details.pl?course_id=$return");
166         exit;
167     }
168 }
169
170 output_html_with_http_headers $cgi, $cookie, $template->output;