]> git.koha-community.org Git - koha.git/blob - svc/holds
Bug 17843: Replace C4::Koha::getitemtypeinfo with Koha::ItemTypes
[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::Charset;
28 use C4::Circulation qw(GetTransfers);
29 use C4::Context;
30
31 use Koha::DateUtils;
32 use Koha::Holds;
33 use Koha::ItemTypes;
34 use Koha::Libraries;
35
36 my $input = new CGI;
37
38 my ( $auth_status, $sessionID ) =
39   check_cookie_auth( $input->cookie('CGISESSID'),
40     { circulate => 'circulate_remaining_permissions' } );
41
42 if ( $auth_status ne "ok" ) {
43     exit 0;
44 }
45
46 my $branch = C4::Context->userenv->{'branch'};
47
48 my $schema = Koha::Database->new()->schema();
49
50 my @sort_columns =
51   qw/reservedate title itemcallnumber barcode expirationdate priority/;
52
53 my $borrowernumber    = $input->param('borrowernumber');
54 my $offset            = $input->param('iDisplayStart');
55 my $results_per_page  = $input->param('iDisplayLength');
56 my $sorting_direction = $input->param('sSortDir_0') || 'desc';
57 my $sorting_column    = $sort_columns[ $input->param('iSortCol_0') ]
58   || 'reservedate';
59
60 binmode STDOUT, ":encoding(UTF-8)";
61 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
62
63 my $holds_rs = Koha::Holds->search(
64     { borrowernumber => $borrowernumber },
65     {
66         order_by => { "-$sorting_direction" => $sorting_column }
67     }
68 );
69
70 my $borrower;
71 my @holds;
72 while ( my $h = $holds_rs->next() ) {
73     my $item = $h->item();
74
75     my $biblionumber = $h->biblio()->biblionumber();
76
77     my $itemtype_limit;
78     if ( $h->itemtype ) {
79         my $itemtype = Koha::ItemTypes->find( $h->itemtype );
80         $itemtype_limit = $itemtype->translated_description;
81     }
82
83     my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
84     for my $library ( @$libraries ) {
85         $library->{selected} = 1 if $library->{branchcode} eq $h->branchcode();
86     }
87     my $hold = {
88         DT_RowId       => $h->reserve_id(),
89         biblionumber   => $biblionumber,
90         title          => $h->biblio()->title(),
91         author         => $h->biblio()->author(),
92         reserve_id     => $h->reserve_id(),
93         branchcode     => $h->branch()->branchname(),
94         branches       => $libraries,
95         reservedate    => $h->reservedate(),
96         expirationdate => $h->expirationdate(),
97         suspend        => $h->suspend(),
98         suspend_until  => $h->suspend_until(),
99         found          => $h->found(),
100         waiting        => $h->is_waiting(),
101         waiting_at     => $h->branch()->branchname(),
102         waiting_here   => $h->branch()->branchcode() eq $branch,
103         priority       => $h->priority(),
104         itemtype_limit => $itemtype_limit,
105         subtitle       => GetRecordValue(
106             'subtitle', GetMarcBiblio($biblionumber),
107             GetFrameworkCode($biblionumber)
108         ),
109         reservedate_formatted => $h->reservedate() ? output_pref(
110             { dt => dt_from_string( $h->reservedate() ), dateonly => 1 }
111           )
112         : q{},
113         suspend_until_formatted => $h->suspend_until() ? output_pref(
114             { dt => dt_from_string( $h->suspend_until() ), dateonly => 1 }
115           )
116         : q{},
117         expirationdate_formatted => $h->expirationdate() ? output_pref(
118             { dt => dt_from_string( $h->expirationdate() ), dateonly => 1 }
119           )
120         : q{},
121     };
122
123     $hold->{transfered}     = 0;
124     $hold->{not_transfered} = 0;
125
126     if ($item) {
127         $hold->{itemnumber}     = $item->itemnumber();
128         $hold->{barcode}        = $item->barcode();
129         $hold->{itemtype}       = $item->effective_itemtype();
130         $hold->{itemcallnumber} = $item->itemcallnumber() || q{};
131
132         my ( $transferred_when, $transferred_from, $transferred_to ) =
133           GetTransfers( $item->itemnumber() );
134         if ($transferred_when) {
135             $hold->{color}       = 'transferred';
136             $hold->{transferred} = 1;
137             $hold->{date_sent} = output_pref( dt_from_string($transferred_when) );
138             $hold->{from_branch} = Koha::Libraries->find($transferred_from)->branchname;
139         }
140         elsif ( $item->holding_branch() && $item->holding_branch()->branchcode() ne
141             $h->branch()->branchcode() )
142         {
143             $hold->{not_transferred}    = 1;
144             $hold->{not_transferred_by} = $h->item()->holding_branch()->branchname();
145         }
146     }
147
148     push( @holds, $hold );
149 }
150
151 my $data;
152 $data->{'iTotalRecords'}        = scalar @holds;
153 $data->{'iTotalDisplayRecords'} = scalar @holds;
154 $data->{'sEcho'}                = $input->param('sEcho') || undef;
155 $data->{'aaData'}               = \@holds;
156
157 print to_json($data);