Bug 25716: (QA follow-up) Move additional options to etc/z3950/config.xml
[koha.git] / svc / holds
1 #!/usr/bin/perl
2
3 # Copyright 2014 ByWater Solutions
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
22 use CGI;
23 use JSON qw(to_json);
24
25 use C4::Auth qw(check_cookie_auth);
26 use C4::Charset;
27 use C4::Context;
28
29 use Koha::DateUtils qw( dt_from_string output_pref );
30 use Koha::Holds;
31 use Koha::ItemTypes;
32 use Koha::Libraries;
33
34 my $input = CGI->new;
35
36 my ( $auth_status ) =
37   check_cookie_auth( $input->cookie('CGISESSID'),
38     { circulate => 'circulate_remaining_permissions' } );
39
40 if ( $auth_status ne "ok" ) {
41     exit 0;
42 }
43
44 my $branch = C4::Context->userenv->{'branch'};
45
46 my $schema = Koha::Database->new()->schema();
47
48 my @sort_columns =
49   qw/reservedate title itemcallnumber barcode expirationdate priority/;
50
51 my $borrowernumber    = $input->param('borrowernumber');
52 my $offset            = $input->param('iDisplayStart');
53 my $results_per_page  = $input->param('iDisplayLength');
54 my $sorting_direction = $input->param('sSortDir_0') || 'desc';
55 my $iSortCol          = $input->param('iSortCol_0') // 0;
56 my $sorting_column    = $sort_columns[$iSortCol] // 'reservedate';
57
58 binmode STDOUT, ":encoding(UTF-8)";
59 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
60
61 my $holds_rs = Koha::Holds->search(
62     { borrowernumber => $borrowernumber },
63     {
64         order_by => { "-$sorting_direction" => $sorting_column }
65     }
66 );
67
68 my @holds;
69 while ( my $h = $holds_rs->next() ) {
70     my $item = $h->item();
71
72     my $biblionumber = $h->biblio()->biblionumber();
73
74     my $desk_id = $h->desk_id;
75     my $desk_name = '';
76     if ($desk_id) {
77         $desk_name = $h->desk()->desk_name();
78     }
79
80     my $itemtype_limit;
81     if ( $h->itemtype ) {
82         my $itemtype = Koha::ItemTypes->find( $h->itemtype );
83         $itemtype_limit = $itemtype->translated_description;
84     }
85
86     my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
87     for my $library ( @$libraries ) {
88         $library->{selected} = 1 if $library->{branchcode} eq $h->branchcode();
89     }
90
91     my $biblio = $h->biblio();
92     my @subtitles = split(/ \| /, $biblio->subtitle() // '');
93     my $hold = {
94         DT_RowId       => $h->reserve_id(),
95         biblionumber   => $biblionumber,
96         title          => $biblio->title(),
97         subtitle       => \@subtitles,
98         medium         => $biblio->medium() // '',
99         part_number    => $biblio->part_number() // '',
100         part_name      => $biblio->part_name() // '',
101         author         => $biblio->author(),
102         reserve_id     => $h->reserve_id(),
103         branchcode     => $h->branch()->branchname(),
104         branches       => $libraries,
105         desk_name      => $desk_name,
106         reservedate    => $h->reservedate(),
107         expirationdate => $h->expirationdate(),
108         suspend        => $h->suspend(),
109         suspend_until  => $h->suspend_until(),
110         found          => $h->found(),
111         waiting        => $h->is_waiting(),
112         waiting_at     => $h->branch()->branchname(),
113         waiting_here   => $h->branch()->branchcode() eq $branch,
114         priority       => $h->priority(),
115         itemtype_limit => $itemtype_limit,
116         reservedate_formatted => $h->reservedate() ? output_pref(
117             { dt => dt_from_string( $h->reservedate() ), dateonly => 1 }
118           )
119         : q{},
120         suspend_until_formatted => $h->suspend_until() ? output_pref(
121             { dt => dt_from_string( $h->suspend_until() ), dateonly => 1 }
122           )
123         : q{},
124         expirationdate_formatted => $h->expirationdate() ? output_pref(
125             { dt => dt_from_string( $h->expirationdate() ), dateonly => 1 }
126           )
127         : q{},
128     };
129
130     $hold->{transfered}     = 0;
131     $hold->{not_transfered} = 0;
132
133     if ($item) {
134         $hold->{itemnumber}     = $item->itemnumber();
135         $hold->{barcode}        = $item->barcode();
136         $hold->{itemtype}       = $item->effective_itemtype();
137         $hold->{itemtype_description} = $item->itemtype->description;
138         $hold->{enumchron}      = $item->enumchron();
139         $hold->{itemcallnumber} = $item->itemcallnumber() || q{};
140
141         my $transfer = $item->get_transfer;
142         if ( $transfer && $transfer->in_transit ) {
143             $hold->{color}       = 'transferred';
144             $hold->{transferred} = 1;
145             $hold->{date_sent} = output_pref({ dt => dt_from_string($transfer->datesent) });
146             $hold->{from_branch} = Koha::Libraries->find($transfer->frombranch)->branchname;
147         }
148         elsif ( $item->holding_branch() && $item->holding_branch()->branchcode() ne
149             $h->branch()->branchcode() )
150         {
151             $hold->{not_transferred}    = 1;
152             $hold->{not_transferred_by} = $h->item()->holding_branch()->branchname();
153         }
154     }
155
156     push( @holds, $hold );
157 }
158
159 my $data;
160 $data->{'iTotalRecords'}        = scalar @holds;
161 $data->{'iTotalDisplayRecords'} = scalar @holds;
162 $data->{'sEcho'}                = $input->param('sEcho') || undef;
163 $data->{'aaData'}               = \@holds;
164
165 print to_json($data);