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