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