]> git.koha-community.org Git - koha.git/blob - svc/holds
Bug 11529: (follow-up) Fix QA issues
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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::Circulation qw(GetTransfers);
28 use C4::Context;
29
30 use Koha::DateUtils;
31 use Koha::Holds;
32 use Koha::ItemTypes;
33 use Koha::Libraries;
34
35 my $input = new CGI;
36
37 my ( $auth_status, $sessionID ) =
38   check_cookie_auth( $input->cookie('CGISESSID'),
39     { circulate => 'circulate_remaining_permissions' } );
40
41 if ( $auth_status ne "ok" ) {
42     exit 0;
43 }
44
45 my $branch = C4::Context->userenv->{'branch'};
46
47 my $schema = Koha::Database->new()->schema();
48
49 my @sort_columns =
50   qw/reservedate title itemcallnumber barcode expirationdate priority/;
51
52 my $borrowernumber    = $input->param('borrowernumber');
53 my $offset            = $input->param('iDisplayStart');
54 my $results_per_page  = $input->param('iDisplayLength');
55 my $sorting_direction = $input->param('sSortDir_0') || 'desc';
56 my $iSortCol          = $input->param('iSortCol_0') // 0;
57 my $sorting_column    = $sort_columns[$iSortCol] // 'reservedate';
58
59 binmode STDOUT, ":encoding(UTF-8)";
60 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
61
62 my $holds_rs = Koha::Holds->search(
63     { borrowernumber => $borrowernumber },
64     {
65         order_by => { "-$sorting_direction" => $sorting_column }
66     }
67 );
68
69 my $borrower;
70 my @holds;
71 while ( my $h = $holds_rs->next() ) {
72     my $item = $h->item();
73
74     my $biblionumber = $h->biblio()->biblionumber();
75
76     my $itemtype_limit;
77     if ( $h->itemtype ) {
78         my $itemtype = Koha::ItemTypes->find( $h->itemtype );
79         $itemtype_limit = $itemtype->translated_description;
80     }
81
82     my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
83     for my $library ( @$libraries ) {
84         $library->{selected} = 1 if $library->{branchcode} eq $h->branchcode();
85     }
86
87     my $biblio = $h->biblio();
88     my @subtitles = split(/ \| /, $biblio->subtitle() // '');
89     my $hold = {
90         DT_RowId       => $h->reserve_id(),
91         biblionumber   => $biblionumber,
92         title          => $biblio->title(),
93         subtitle       => \@subtitles,
94         medium         => $biblio->medium() // '',
95         part_number    => $biblio->part_number() // '',
96         part_name      => $biblio->part_name() // '',
97         author         => $biblio->author(),
98         reserve_id     => $h->reserve_id(),
99         branchcode     => $h->branch()->branchname(),
100         branches       => $libraries,
101         reservedate    => $h->reservedate(),
102         expirationdate => $h->expirationdate(),
103         suspend        => $h->suspend(),
104         suspend_until  => $h->suspend_until(),
105         found          => $h->found(),
106         waiting        => $h->is_waiting(),
107         waiting_at     => $h->branch()->branchname(),
108         waiting_here   => $h->branch()->branchcode() eq $branch,
109         priority       => $h->priority(),
110         itemtype_limit => $itemtype_limit,
111         reservedate_formatted => $h->reservedate() ? output_pref(
112             { dt => dt_from_string( $h->reservedate() ), dateonly => 1 }
113           )
114         : q{},
115         suspend_until_formatted => $h->suspend_until() ? output_pref(
116             { dt => dt_from_string( $h->suspend_until() ), dateonly => 1 }
117           )
118         : q{},
119         expirationdate_formatted => $h->expirationdate() ? output_pref(
120             { dt => dt_from_string( $h->expirationdate() ), dateonly => 1 }
121           )
122         : q{},
123     };
124
125     $hold->{transfered}     = 0;
126     $hold->{not_transfered} = 0;
127
128     if ($item) {
129         $hold->{itemnumber}     = $item->itemnumber();
130         $hold->{barcode}        = $item->barcode();
131         $hold->{itemtype}       = $item->effective_itemtype();
132         $hold->{itemcallnumber} = $item->itemcallnumber() || q{};
133
134         my ( $transferred_when, $transferred_from, $transferred_to ) =
135           GetTransfers( $item->itemnumber() );
136         if ($transferred_when) {
137             $hold->{color}       = 'transferred';
138             $hold->{transferred} = 1;
139             $hold->{date_sent} = output_pref( dt_from_string($transferred_when) );
140             $hold->{from_branch} = Koha::Libraries->find($transferred_from)->branchname;
141         }
142         elsif ( $item->holding_branch() && $item->holding_branch()->branchcode() ne
143             $h->branch()->branchcode() )
144         {
145             $hold->{not_transferred}    = 1;
146             $hold->{not_transferred_by} = $h->item()->holding_branch()->branchname();
147         }
148     }
149
150     push( @holds, $hold );
151 }
152
153 my $data;
154 $data->{'iTotalRecords'}        = scalar @holds;
155 $data->{'iTotalDisplayRecords'} = scalar @holds;
156 $data->{'sEcho'}                = $input->param('sEcho') || undef;
157 $data->{'aaData'}               = \@holds;
158
159 print to_json($data);