Bug 9580: DBRev 3.19.00.019
[koha.git] / svc / holds
blob43bcf2a54a89607e02acd93a75802578ebaa0f2b
1 #!/usr/bin/perl
3 # Copyright 2014 ByWater Solutions
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
22 use CGI;
23 use JSON qw(to_json);
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;
32 use Koha::Database;
33 use Koha::DateUtils;
35 my $input = new CGI;
37 my ( $auth_status, $sessionID ) =
38 check_cookie_auth( $input->cookie('CGISESSID'),
39 { circulate => 'circulate_remaining_permissions' } );
41 if ( $auth_status ne "ok" ) {
42 exit 0;
45 my $branch = C4::Context->userenv->{'branch'};
47 my $schema = Koha::Database->new()->schema();
49 my @sort_columns =
50 qw/reservedate title itemcallnumber barcode expirationdate priority/;
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';
59 binmode STDOUT, ":encoding(UTF-8)";
60 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
62 my $holds_rs = $schema->resultset('Reserve')->search(
63 { borrowernumber => $borrowernumber },
65 prefetch => { 'item' => 'biblio' },
66 order_by => { "-$sorting_direction" => $sorting_column }
70 my $borrower;
71 my @holds;
72 while ( my $h = $holds_rs->next() ) {
73 my $item = $h->item();
75 my $biblionumber = $h->biblio()->biblionumber();
77 my $hold = {
78 DT_RowId => $h->reserve_id(),
79 biblionumber => $biblionumber,
80 title => $h->biblio()->title(),
81 author => $h->biblio()->author(),
82 reserve_id => $h->reserve_id(),
83 reservedate => $h->reservedate(),
84 expirationdate => $h->expirationdate(),
85 suspend => $h->suspend(),
86 suspend_until => $h->suspend_until(),
87 found => $h->found(),
88 waiting => $h->found() eq 'W',
89 waiting_at => $h->branchcode()->branchname(),
90 waiting_here => $h->branchcode()->branchcode() eq $branch,
91 priority => $h->priority(),
92 subtitle => GetRecordValue(
93 'subtitle', GetMarcBiblio($biblionumber),
94 GetFrameworkCode($biblionumber)
96 reservedate_formatted => $h->reservedate() ? output_pref(
97 { dt => dt_from_string( $h->reservedate() ), dateonly => 1 }
99 : q{},
100 suspend_until_formatted => $h->suspend_until() ? output_pref(
101 { dt => dt_from_string( $h->suspend_until() ), dateonly => 1 }
103 : q{},
104 expirationdate_formatted => $h->expirationdate() ? output_pref(
105 { dt => dt_from_string( $h->expirationdate() ), dateonly => 1 }
107 : q{},
110 $hold->{transfered} = 0;
111 $hold->{not_transfered} = 0;
113 if ($item) {
114 $hold->{itemnumber} = $item->itemnumber();
115 $hold->{barcode} = $item->barcode();
116 $hold->{itemtype} = $item->effective_itemtype();
117 $hold->{itemcallnumber} = $item->itemcallnumber() || q{};
119 my ( $transferred_when, $transferred_from, $transferred_to ) =
120 GetTransfers( $item->itemnumber() );
121 if ($transferred_when) {
122 $hold->{color} = 'transferred';
123 $hold->{transferred} = 1;
124 $hold->{date_sent} = output_pref( dt_from_string($transferred_when) );
125 $hold->{from_branch} = GetBranchName($transferred_from);
127 elsif ( $item->holdingbranch()->branchcode() ne
128 $h->branchcode()->branchcode() )
130 $hold->{not_transferred} = 1;
131 $hold->{not_transferred_by} = $h->item()->holdingbranch()->branchname();
135 push( @holds, $hold );
138 my $data;
139 $data->{'iTotalRecords'} = scalar @holds;
140 $data->{'iTotalDisplayRecords'} = scalar @holds;
141 $data->{'sEcho'} = $input->param('sEcho') || undef;
142 $data->{'aaData'} = \@holds;
144 print to_json($data);