Bug 11703 [QA Followup]
[koha.git] / svc / holds.pl
1 #!/usr/bin/perl
2
3 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
4
5 # Copyright 2014 ByWater Solutions
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 use Modern::Perl;
23
24 use CGI;
25 use JSON qw(to_json);
26
27 use C4::Auth qw(check_cookie_auth);
28 use C4::Biblio qw(GetMarcBiblio GetFrameworkCode GetRecordValue );
29 use C4::Branch qw(GetBranchName);
30 use C4::Charset;
31 use C4::Circulation qw(GetTransfers);
32 use C4::Context;
33
34 use Koha::Database;
35 use Koha::DateUtils;
36
37 my $input = new CGI;
38
39 my ( $auth_status, $sessionID ) =
40   check_cookie_auth( $input->cookie('CGISESSID'),
41     { circulate => 'circulate_remaining_permissions' } );
42
43 if ( $auth_status ne "ok" ) {
44     exit 0;
45 }
46
47 my $branch = C4::Context->userenv->{'branch'};
48
49 my $schema = Koha::Database->new()->schema();
50
51 my @sort_columns =
52   qw/reservedate title itemcallnumber barcode expirationdate priority/;
53
54 my $borrowernumber    = $input->param('borrowernumber');
55 my $offset            = $input->param('iDisplayStart');
56 my $results_per_page  = $input->param('iDisplayLength');
57 my $sorting_direction = $input->param('sSortDir_0') || 'desc';
58 my $sorting_column    = $sort_columns[ $input->param('iSortCol_0') ]
59   || 'reservedate';
60
61 binmode STDOUT, ":encoding(UTF-8)";
62 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
63
64 my $holds_rs = $schema->resultset('Reserve')->search(
65     { borrowernumber => $borrowernumber },
66     {
67         prefetch => { 'item'                => 'biblio' },
68         order_by => { "-$sorting_direction" => $sorting_column }
69     }
70 );
71
72 my $borrower;
73 my @holds;
74 while ( my $h = $holds_rs->next() ) {
75     my $item = $h->item();
76
77     my $biblionumber = $h->biblio()->biblionumber();
78
79     my $hold = {
80         DT_RowId       => $h->reserve_id(),
81         biblionumber   => $biblionumber,
82         title          => $h->biblio()->title(),
83         author         => $h->biblio()->author(),
84         reserve_id     => $h->reserve_id(),
85         reservedate    => $h->reservedate(),
86         expirationdate => $h->expirationdate(),
87         suspend        => $h->suspend(),
88         suspend_until  => $h->suspend_until(),
89         found          => $h->found(),
90         waiting        => $h->found() eq 'W',
91         waiting_at     => $h->branchcode()->branchname(),
92         waiting_here   => $h->branchcode()->branchcode() eq $branch,
93         priority       => $h->priority(),
94         subtitle       => GetRecordValue(
95             'subtitle', GetMarcBiblio($biblionumber),
96             GetFrameworkCode($biblionumber)
97         ),
98         reservedate_formatted => $h->reservedate() ? output_pref(
99             { dt => dt_from_string( $h->reservedate() ), dateonly => 1 }
100           )
101         : q{},
102         suspend_until_formatted => $h->suspend_until() ? output_pref(
103             { dt => dt_from_string( $h->suspend_until() ), dateonly => 1 }
104           )
105         : q{},
106         expirationdate_formatted => $h->expirationdate() ? output_pref(
107             { dt => dt_from_string( $h->expirationdate() ), dateonly => 1 }
108           )
109         : q{},
110     };
111
112     $hold->{transfered}     = 0;
113     $hold->{not_transfered} = 0;
114
115     if ($item) {
116         $hold->{itemnumber}     = $item->itemnumber();
117         $hold->{barcode}        = $item->barcode();
118         $hold->{itemtype}       = $item->effective_itemtype();
119         $hold->{itemcallnumber} = $item->itemcallnumber() || q{};
120
121         my ( $transferred_when, $transferred_from, $transferred_to ) =
122           GetTransfers( $item->itemnumber() );
123         if ($transferred_when) {
124             $hold->{color}       = 'transferred';
125             $hold->{transferred} = 1;
126             $hold->{date_sent} = output_pref( dt_from_string($transferred_when) );
127             $hold->{from_branch} = GetBranchName($transferred_from);
128         }
129         elsif ( $item->holdingbranch()->branchcode() ne
130             $h->branchcode()->branchcode() )
131         {
132             $hold->{not_transferred}    = 1;
133             $hold->{not_transferred_by} = $h->branchcode()->branchname();
134         }
135     }
136
137     push( @holds, $hold );
138 }
139
140 my $data;
141 $data->{'iTotalRecords'}        = scalar @holds;
142 $data->{'iTotalDisplayRecords'} = scalar @holds;
143 $data->{'sEcho'}                = $input->param('sEcho') || undef;
144 $data->{'aaData'}               = \@holds;
145
146 print to_json($data);