263f8eca047ffe93c6aeb6824a65fcf689cbdcf2
[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::Biblio qw(GetMarcBiblio GetFrameworkCode GetRecordValue );
27 use C4::Branch qw(GetBranchName);
28 use C4::Charset;
29 use C4::Circulation qw(GetTransfers);
30 use C4::Context;
31
32 use Koha::Database;
33 use Koha::DateUtils;
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 $sorting_column    = $sort_columns[ $input->param('iSortCol_0') ]
57   || 'reservedate';
58
59 binmode STDOUT, ":encoding(UTF-8)";
60 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
61
62 my $holds_rs = $schema->resultset('Reserve')->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 $hold = {
77         DT_RowId       => $h->reserve_id(),
78         biblionumber   => $biblionumber,
79         title          => $h->biblio()->title(),
80         author         => $h->biblio()->author(),
81         reserve_id     => $h->reserve_id(),
82         reservedate    => $h->reservedate(),
83         expirationdate => $h->expirationdate(),
84         suspend        => $h->suspend(),
85         suspend_until  => $h->suspend_until(),
86         found          => $h->found(),
87         waiting        => $h->found() eq 'W',
88         waiting_at     => $h->branchcode()->branchname(),
89         waiting_here   => $h->branchcode()->branchcode() eq $branch,
90         priority       => $h->priority(),
91         subtitle       => GetRecordValue(
92             'subtitle', GetMarcBiblio($biblionumber),
93             GetFrameworkCode($biblionumber)
94         ),
95         reservedate_formatted => $h->reservedate() ? output_pref(
96             { dt => dt_from_string( $h->reservedate() ), dateonly => 1 }
97           )
98         : q{},
99         suspend_until_formatted => $h->suspend_until() ? output_pref(
100             { dt => dt_from_string( $h->suspend_until() ), dateonly => 1 }
101           )
102         : q{},
103         expirationdate_formatted => $h->expirationdate() ? output_pref(
104             { dt => dt_from_string( $h->expirationdate() ), dateonly => 1 }
105           )
106         : q{},
107     };
108
109     $hold->{transfered}     = 0;
110     $hold->{not_transfered} = 0;
111
112     if ($item) {
113         $hold->{itemnumber}     = $item->itemnumber();
114         $hold->{barcode}        = $item->barcode();
115         $hold->{itemtype}       = $item->effective_itemtype();
116         $hold->{itemcallnumber} = $item->itemcallnumber() || q{};
117
118         my ( $transferred_when, $transferred_from, $transferred_to ) =
119           GetTransfers( $item->itemnumber() );
120         if ($transferred_when) {
121             $hold->{color}       = 'transferred';
122             $hold->{transferred} = 1;
123             $hold->{date_sent} = output_pref( dt_from_string($transferred_when) );
124             $hold->{from_branch} = GetBranchName($transferred_from);
125         }
126         elsif ( $item->holdingbranch()->branchcode() ne
127             $h->branchcode()->branchcode() )
128         {
129             $hold->{not_transferred}    = 1;
130             $hold->{not_transferred_by} = $h->item()->holdingbranch()->branchname();
131         }
132     }
133
134     push( @holds, $hold );
135 }
136
137 my $data;
138 $data->{'iTotalRecords'}        = scalar @holds;
139 $data->{'iTotalDisplayRecords'} = scalar @holds;
140 $data->{'sEcho'}                = $input->param('sEcho') || undef;
141 $data->{'aaData'}               = \@holds;
142
143 print to_json($data);