Bug 12164: (followup) keys expects a hash parameter
[koha.git] / svc / checkouts
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 strict;
21 use warnings;
22
23 use CGI;
24 use JSON qw(to_json);
25
26 use C4::Auth qw(check_cookie_auth);
27 use C4::Biblio qw(GetMarcBiblio GetFrameworkCode GetRecordValue );
28 use C4::Circulation
29   qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
30 use C4::Context;
31
32 use Koha::DateUtils;
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 @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
45
46 my @borrowernumber   = $input->param('borrowernumber');
47 my $offset           = $input->param('iDisplayStart');
48 my $results_per_page = $input->param('iDisplayLength') || -1;
49 my $sorting_column   = $sort_columns[ $input->param('iSortCol_0') ]
50   || 'issuedate';
51 my $sorting_direction = $input->param('sSortDir_0') eq 'asc' ? 'asc' : 'desc';
52
53 $results_per_page = undef if ( $results_per_page == -1 );
54
55 binmode STDOUT, ":encoding(UTF-8)";
56 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
57
58 my @parameters;
59 my $sql = '
60     SELECT
61         issuedate,
62         date_due,
63
64         biblionumber,
65         biblio.title,
66         author,
67
68         itemnumber,
69         barcode,
70         itemnotes,
71         itemcallnumber,
72         replacementprice,
73
74         issues.branchcode,
75         branchname,
76
77         itype,
78         itemtype,
79
80         borrowernumber,
81         surname,
82         firstname,
83         cardnumber,
84
85         DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
86     FROM issues
87         LEFT JOIN items USING ( itemnumber )
88         LEFT JOIN biblio USING ( biblionumber )
89         LEFT JOIN biblioitems USING ( biblionumber )
90         LEFT JOIN borrowers USING ( borrowernumber )
91         LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
92     WHERE borrowernumber
93 ';
94
95 if ( @borrowernumber == 1 ) {
96     $sql .= '= ?';
97 }
98 else {
99     $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
100 }
101 push( @parameters, @borrowernumber );
102
103 $sql .= " ORDER BY $sorting_column $sorting_direction ";
104
105 my $dbh = C4::Context->dbh();
106 my $sth = $dbh->prepare($sql);
107 $sth->execute(@parameters);
108
109 my $item_level_itypes = C4::Context->preference('item-level_itypes');
110
111 my @checkouts_today;
112 my @checkouts_previous;
113 while ( my $c = $sth->fetchrow_hashref() ) {
114     my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
115
116     my ( $can_renew, $can_renew_error ) =
117       CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
118     my $can_renew_date =
119       $can_renew_error eq 'too_soon'
120       ? output_pref(
121         {
122             dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
123             as_due_date => 1
124         }
125       )
126       : undef;
127
128     my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
129       GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
130
131     my $checkout = {
132         DT_RowId   => $c->{itemnumber} . '-' . $c->{borrowernumber},
133         title      => $c->{title},
134         author     => $c->{author},
135         barcode    => $c->{barcode},
136         itemtype   => $item_level_itypes ? $c->{itype} : $c->{itemtype},
137         itemnotes  => $c->{itemnotes},
138         branchcode => $c->{branchcode},
139         branchname => $c->{branchname},
140         itemcallnumber => $c->{itemcallnumber}   || q{},
141         charge         => $charge,
142         price          => $c->{replacementprice} || q{},
143         can_renew      => $can_renew,
144         can_renew_error     => $can_renew_error,
145         can_renew_date      => $can_renew_date,
146         itemnumber          => $c->{itemnumber},
147         borrowernumber      => $c->{borrowernumber},
148         biblionumber        => $c->{biblionumber},
149         issuedate           => $c->{issuedate},
150         date_due            => $c->{date_due},
151         renewals_count      => $renewals_count,
152         renewals_allowed    => $renewals_allowed,
153         renewals_remaining  => $renewals_remaining,
154         issuedate_formatted => output_pref(
155             {
156                 dt          => dt_from_string( $c->{issuedate} ),
157                 as_due_date => 1
158             }
159         ),
160         date_due_formatted => output_pref(
161             {
162                 dt          => dt_from_string( $c->{date_due} ),
163                 as_due_date => 1
164             }
165         ),
166         subtitle => GetRecordValue(
167             'subtitle',
168             GetMarcBiblio( $c->{biblionumber} ),
169             GetFrameworkCode( $c->{biblionumber} )
170         ),
171         borrower => {
172             surname    => $c->{surname},
173             firstname  => $c->{firstname},
174             cardnumber => $c->{cardnumber},
175         },
176         issued_today => !$c->{not_issued_today},
177     };
178
179     if ( $c->{not_issued_today} ) {
180         push( @checkouts_previous, $checkout );
181     }
182     else {
183         push( @checkouts_today, $checkout );
184     }
185 }
186
187 @checkouts_today = reverse(@checkouts_today)
188   if ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
189 @checkouts_previous = reverse(@checkouts_previous)
190   if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
191
192 my @checkouts = ( @checkouts_today, @checkouts_previous );
193
194 my $data;
195 $data->{'iTotalRecords'}        = scalar @checkouts;
196 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
197 $data->{'sEcho'}                = $input->param('sEcho') || undef;
198 $data->{'aaData'}               = \@checkouts;
199
200 print to_json($data);