Bug 22762: Fix items.collection display on receiving
[koha.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 use Koha::Items;
33 use Koha::ItemTypes;
34
35 my $cgi = new CGI;
36
37 my ( $status, $cookie, $sessionID ) = C4::Auth::check_api_auth( $cgi, { acquisition => 'order_receive' } );
38 unless ($status eq "ok") {
39     print $cgi->header(-type => 'application/json', -status => '403 Forbidden');
40     print to_json({ auth_status => $status });
41     exit 0;
42 }
43
44 my $item = {};
45 my $itemnumber = $cgi->param('itemnumber');
46
47 my $item_unblessed = {};
48 if($itemnumber) {
49     my $acq_fw = GetMarcStructure(1, 'ACQ');
50     my $fw = ($acq_fw) ? 'ACQ' : '';
51     $item = Koha::Items->find($itemnumber);
52     $item_unblessed = $item->unblessed; # FIXME Not needed, call home_branch and holding_branch in the templates instead
53
54     if($item->homebranch) { # This test should not be needed, homebranch and holdingbranch are mandatory
55         $item_unblessed->{homebranchname} = $item->home_branch->branchname;
56     }
57
58     if($item->holdingbranch) {
59         $item_unblessed->{holdingbranchname} = $item->holding_branch->branchname;
60     }
61
62     my $descriptions;
63     $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({ frameworkcode => $fw, kohafield => 'items.notforloan', authorised_value => $item->notforloan });
64     $item_unblessed->{notforloan} = $descriptions->{lib} // '';
65
66     $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({ frameworkcode => $fw, kohafield => 'items.restricted', authorised_value => $item->restricted });
67     $item_unblessed->{restricted} = $descriptions->{lib} // '';
68
69     $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({ frameworkcode => $fw, kohafield => 'items.location', authorised_value => $item->location });
70     $item_unblessed->{location} = $descriptions->{lib} // '';
71
72     $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({ frameworkcode => $fw, kohafield => 'items.ccode', authorised_value => $item->ccode });
73     $item_unblessed->{collection} = $descriptions->{lib} // '';
74
75     $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({ frameworkcode => $fw, kohafield => 'items.materials', authorised_value => $item->materials });
76     $item_unblessed->{materials} = $descriptions->{lib} // '';
77
78     my $itemtype = Koha::ItemTypes->find( $item->effective_itemtype );
79     # We should not do that here, but call ->itemtype->description when needed instea
80     $item_unblessed->{itemtype} = $itemtype->description; # FIXME Should not it be translated_description?
81 }
82
83 my $json_text = to_json( $item_unblessed, { utf8 => 1 } );
84
85 output_with_http_headers $cgi, undef, $json_text, 'json';