Bug 13195 - Regression: Circulation checkouts table no longer shows item type description
[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 qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
29 use C4::Koha qw(GetAuthorisedValueByCode);
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
50 my $sorting_column = $input->param('iSortCol_0') || q{};
51 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
52
53 my $sorting_direction = $input->param('sSortDir_0') || q{};
54 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
55
56 $results_per_page = undef if ( $results_per_page == -1 );
57
58 binmode STDOUT, ":encoding(UTF-8)";
59 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
60
61 my @parameters;
62 my $sql = '
63     SELECT
64         issuedate,
65         date_due,
66         date_due < now() as date_due_overdue,
67         issues.timestamp,
68
69         biblionumber,
70         biblio.title,
71         author,
72
73         itemnumber,
74         barcode,
75         itemnotes,
76         itemcallnumber,
77         replacementprice,
78
79         issues.branchcode,
80         branchname,
81
82         items.itype,
83         itemtype_item.description AS itype_description,
84         biblioitems.itemtype,
85         itemtype_bib.description AS itemtype_description,
86
87         borrowernumber,
88         surname,
89         firstname,
90         cardnumber,
91
92         itemlost,
93         damaged,
94
95         DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
96     FROM issues
97         LEFT JOIN items USING ( itemnumber )
98         LEFT JOIN biblio USING ( biblionumber )
99         LEFT JOIN biblioitems USING ( biblionumber )
100         LEFT JOIN borrowers USING ( borrowernumber )
101         LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
102         LEFT JOIN itemtypes itemtype_bib ON ( biblioitems.itemtype = itemtype_bib.itemtype )
103         LEFT JOIN itemtypes itemtype_item ON ( items.itype = itemtype_item.itemtype )
104     WHERE borrowernumber
105 ';
106
107 if ( @borrowernumber == 1 ) {
108     $sql .= '= ?';
109 }
110 else {
111     $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
112 }
113 push( @parameters, @borrowernumber );
114
115 $sql .= " ORDER BY $sorting_column $sorting_direction ";
116
117 my $dbh = C4::Context->dbh();
118 my $sth = $dbh->prepare($sql);
119 $sth->execute(@parameters);
120
121 my $item_level_itypes = C4::Context->preference('item-level_itypes');
122
123 my @checkouts_today;
124 my @checkouts_previous;
125 while ( my $c = $sth->fetchrow_hashref() ) {
126     my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
127
128     my ( $can_renew, $can_renew_error ) =
129       CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
130     my $can_renew_date =
131       $can_renew_error && $can_renew_error eq 'too_soon'
132       ? output_pref(
133         {
134             dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
135             as_due_date => 1
136         }
137       )
138       : undef;
139
140     my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
141       GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
142
143     my $checkout = {
144         DT_RowId   => $c->{itemnumber} . '-' . $c->{borrowernumber},
145         title      => $c->{title},
146         author     => $c->{author},
147         barcode    => $c->{barcode},
148         itemtype   => $item_level_itypes ? $c->{itype} : $c->{itemtype},
149         itemtype_description => $item_level_itypes ? $c->{itype_description} : $c->{itemtype_description},
150         itemnotes  => $c->{itemnotes},
151         branchcode => $c->{branchcode},
152         branchname => $c->{branchname},
153         itemcallnumber => $c->{itemcallnumber}   || q{},
154         charge         => $charge,
155         price          => $c->{replacementprice} || q{},
156         can_renew      => $can_renew,
157         can_renew_error     => $can_renew_error,
158         can_renew_date      => $can_renew_date,
159         itemnumber          => $c->{itemnumber},
160         borrowernumber      => $c->{borrowernumber},
161         biblionumber        => $c->{biblionumber},
162         issuedate           => $c->{issuedate},
163         date_due            => $c->{date_due},
164         date_due_overdue    => $c->{date_due_overdue} ? JSON::true : JSON::false,
165         timestamp           => $c->{timestamp},
166         renewals_count      => $renewals_count,
167         renewals_allowed    => $renewals_allowed,
168         renewals_remaining  => $renewals_remaining,
169         issuedate_formatted => output_pref(
170             {
171                 dt          => dt_from_string( $c->{issuedate} ),
172                 as_due_date => 1
173             }
174         ),
175         date_due_formatted => output_pref(
176             {
177                 dt          => dt_from_string( $c->{date_due} ),
178                 as_due_date => 1
179             }
180         ),
181         subtitle => GetRecordValue(
182             'subtitle',
183             GetMarcBiblio( $c->{biblionumber} ),
184             GetFrameworkCode( $c->{biblionumber} )
185         ),
186         lost => $c->{itemlost} ? GetAuthorisedValueByCode( 'LOST', $c->{itemlost} ) : undef,
187         damaged => $c->{damaged} ? GetAuthorisedValueByCode( 'DAMAGED', $c->{damaged} ) : undef,
188         borrower => {
189             surname    => $c->{surname},
190             firstname  => $c->{firstname},
191             cardnumber => $c->{cardnumber},
192         },
193         issued_today => !$c->{not_issued_today},
194     };
195
196     if ( $c->{not_issued_today} ) {
197         push( @checkouts_previous, $checkout );
198     }
199     else {
200         push( @checkouts_today, $checkout );
201     }
202 }
203
204 @checkouts_today = reverse(@checkouts_today)
205   if ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
206 @checkouts_previous = reverse(@checkouts_previous)
207   if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
208
209 my @checkouts = ( @checkouts_today, @checkouts_previous );
210
211 my $data;
212 $data->{'iTotalRecords'}        = scalar @checkouts;
213 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
214 $data->{'sEcho'}                = $input->param('sEcho') || undef;
215 $data->{'aaData'}               = \@checkouts;
216
217 print to_json($data);