Bug 17248 - Koha::AuthorisedValues - Remove GetKohaAuthorisedValueLib
[koha-equinox.git] / catalogue / getitem-ajax.pl
1 #!/usr/bin/perl
2
3 # Copyright BibLibre 2012
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use JSON;
23
24 use C4::Auth;
25 use C4::Biblio;
26 use C4::Items;
27 use C4::Koha;
28 use C4::Output;
29 use Koha::Libraries;
30
31 use Koha::AuthorisedValues;
32
33 my $cgi = new CGI;
34
35 my ( $status, $cookie, $sessionID ) = C4::Auth::check_api_auth( $cgi, { acquisition => 'order_receive' } );
36 unless ($status eq "ok") {
37     print $cgi->header(-type => 'application/json', -status => '403 Forbidden');
38     print to_json({ auth_status => $status });
39     exit 0;
40 }
41
42 my $item = {};
43 my $itemnumber = $cgi->param('itemnumber');
44
45 if($itemnumber) {
46     my $acq_fw = GetMarcStructure(1, 'ACQ');
47     my $fw = ($acq_fw) ? 'ACQ' : '';
48     $item = GetItem($itemnumber);
49
50     if($item->{homebranch}) {
51         $item->{homebranchname} = Koha::Libraries->find($item->{homebranch})->branchname;
52     }
53
54     if($item->{holdingbranch}) {
55         $item->{holdingbranchname} = Koha::Libraries->find($item->{holdingbranch})->branchname;
56     }
57
58     if(my $code = GetAuthValCode("items.notforloan", $fw)) {
59         my $av = Koha::AuthorisedValues->search({ category => $code, authorised_values => $item->{notforloan} });
60         $item->{notforloan} = $av->count ? $av->next->lib : '';
61     }
62
63     if(my $code = GetAuthValCode("items.restricted", $fw)) {
64         my $av = Koha::AuthorisedValues->search({ category => $code, authorised_values => $item->{restricted} });
65         $item->{restricted} = $av->count ? $av->next->lib : '';
66     }
67
68     if(my $code = GetAuthValCode("items.location", $fw)) {
69         my $av = Koha::AuthorisedValues->search({ category => $code, authorised_values => $item->{location} });
70         $item->{location} = $av->count ? $av->next->lib : '';
71     }
72
73     if(my $code = GetAuthValCode("items.ccode", $fw)) {
74         my $av = Koha::AuthorisedValues->search({ category => $code, authorised_values => $item->{collection} });
75         $item->{collection} = $av->count ? $av->next->lib : '';
76     }
77
78     if(my $code = GetAuthValCode("items.materials", $fw)) {
79         my $av = Koha::AuthorisedValues->search({ category => $code, authorised_values => $item->{materials} });
80         $item->{materials} = $av->count ? $av->next->lib : '';
81     }
82
83     my $itemtype = getitemtypeinfo($item->{itype});
84     $item->{itemtype} = $itemtype->{description};
85 }
86
87 my $json_text = to_json( $item, { utf8 => 1 } );
88
89 output_with_http_headers $cgi, undef, $json_text, 'json';