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
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.
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.
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>.
25 use C4::Auth qw(check_cookie_auth);
29 use Koha::DateUtils qw( dt_from_string output_pref );
37 check_cookie_auth( $input->cookie('CGISESSID'),
38 { circulate => 'circulate_remaining_permissions' } );
40 if ( $auth_status ne "ok" ) {
44 my $branch = C4::Context->userenv->{'branch'};
46 my $schema = Koha::Database->new()->schema();
49 qw/reservedate title itemcallnumber barcode expirationdate priority/;
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';
58 binmode STDOUT, ":encoding(UTF-8)";
59 print $input->header( -type => 'application/json', -charset => 'UTF-8' );
61 my $holds_rs = Koha::Holds->search(
62 { borrowernumber => $borrowernumber },
64 order_by => { "-$sorting_direction" => $sorting_column }
69 while ( my $h = $holds_rs->next() ) {
70 my $item = $h->item();
71 my $item_group = $h->item_group();
73 my $biblionumber = $h->biblio()->biblionumber();
75 my $desk_id = $h->desk_id;
78 $desk_name = $h->desk()->desk_name();
83 my $itemtype = Koha::ItemTypes->find( $h->itemtype );
84 $itemtype_limit = $itemtype->translated_description;
87 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
88 for my $library ( @$libraries ) {
89 $library->{selected} = 1 if $library->{branchcode} eq $h->branchcode();
92 my $biblio = $h->biblio();
93 my @subtitles = split(/ \| /, $biblio->subtitle() // '');
95 DT_RowId => $h->reserve_id(),
96 biblionumber => $biblionumber,
97 title => $biblio->title(),
98 subtitle => \@subtitles,
99 medium => $biblio->medium() // '',
100 part_number => $biblio->part_number() // '',
101 part_name => $biblio->part_name() // '',
102 author => $biblio->author(),
103 reserve_id => $h->reserve_id(),
104 branchcode => $h->branch()->branchname(),
105 branches => $libraries,
106 desk_name => $desk_name,
107 reservedate => $h->reservedate(),
108 expirationdate => $h->expirationdate(),
109 suspend => $h->suspend(),
110 suspend_until => $h->suspend_until(),
111 found => $h->found(),
112 waiting => $h->is_waiting(),
113 waiting_at => $h->branch()->branchname(),
114 waiting_here => $h->branch()->branchcode() eq $branch,
115 priority => $h->priority(),
116 reservenotes => $h->reservenotes(),
117 itemtype_limit => $itemtype_limit,
118 reservedate_formatted => $h->reservedate()
119 ? output_pref( { dt => dt_from_string( $h->reservedate() ), dateonly => 1 } )
121 suspend_until_formatted => $h->suspend_until()
122 ? output_pref( { dt => dt_from_string( $h->suspend_until() ), dateonly => 1 } )
124 expirationdate_formatted => $h->expirationdate()
125 ? output_pref( { dt => dt_from_string( $h->expirationdate() ), dateonly => 1 } )
129 $hold->{transfered} = 0;
130 $hold->{not_transfered} = 0;
133 $hold->{itemnumber} = $item->itemnumber();
134 $hold->{barcode} = $item->barcode();
135 $hold->{itemtype} = $item->effective_itemtype();
136 $hold->{itemtype_description} = $item->itemtype->description;
137 $hold->{enumchron} = $item->enumchron();
138 $hold->{itemcallnumber} = $item->itemcallnumber() || q{};
140 my $transfer = $item->get_transfer;
141 if ( $transfer && $transfer->in_transit ) {
142 $hold->{color} = 'transferred';
143 $hold->{transferred} = 1;
144 $hold->{date_sent} = output_pref({ dt => dt_from_string($transfer->datesent) });
145 $hold->{from_branch} = Koha::Libraries->find($transfer->frombranch)->branchname;
147 elsif ( $item->holding_branch() && $item->holding_branch()->branchcode() ne
148 $h->branch()->branchcode() )
150 $hold->{not_transferred} = 1;
151 $hold->{not_transferred_by} = $h->item()->holding_branch()->branchname();
156 $hold->{item_group_id} = $item_group->id;
157 $hold->{item_group_description} = $item_group->description;
160 push( @holds, $hold );
164 $data->{'iTotalRecords'} = scalar @holds;
165 $data->{'iTotalDisplayRecords'} = scalar @holds;
166 $data->{'sEcho'} = $input->param('sEcho') || undef;
167 $data->{'aaData'} = \@holds;
169 print to_json($data);