Bug 15362: Translatability: Fix issue on Administration 'Did you mean?'
[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::Overdues qw(GetFine);
31 use C4::Context;
32
33 use Koha::DateUtils;
34
35 my $input = new CGI;
36
37 my ( $auth_status, $sessionID ) =
38   check_cookie_auth( $input->cookie('CGISESSID'),
39     { circulate => 'circulate_remaining_permissions' } );
40
41 if ( $auth_status ne "ok" ) {
42     exit 0;
43 }
44
45 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
46
47 my @borrowernumber   = $input->param('borrowernumber');
48 my $offset           = $input->param('iDisplayStart');
49 my $results_per_page = $input->param('iDisplayLength') || -1;
50
51 my $sorting_column = $input->param('iSortCol_0') || q{};
52 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
53
54 my $sorting_direction = $input->param('sSortDir_0') || q{};
55 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
56
57 $results_per_page = undef if ( $results_per_page == -1 );
58
59 binmode STDOUT, ":encoding(UTF-8)";
60 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
61
62 my @parameters;
63 my $sql = '
64     SELECT
65         issuedate,
66         date_due,
67         date_due < now() as date_due_overdue,
68         issues.timestamp,
69
70         onsite_checkout,
71
72         biblionumber,
73         biblio.title,
74         author,
75
76         itemnumber,
77         barcode,
78         itemnotes,
79         itemnotes_nonpublic,
80         itemcallnumber,
81         replacementprice,
82
83         issues.branchcode,
84         branchname,
85
86         items.itype,
87         biblioitems.itemtype,
88
89         borrowernumber,
90         surname,
91         firstname,
92         cardnumber,
93
94         itemlost,
95         damaged,
96         location,
97
98         DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
99     FROM issues
100         LEFT JOIN items USING ( itemnumber )
101         LEFT JOIN biblio USING ( biblionumber )
102         LEFT JOIN biblioitems USING ( biblionumber )
103         LEFT JOIN borrowers USING ( borrowernumber )
104         LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
105     WHERE borrowernumber
106 ';
107
108 if ( @borrowernumber == 1 ) {
109     $sql .= '= ?';
110 }
111 else {
112     $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
113 }
114 push( @parameters, @borrowernumber );
115
116 $sql .= " ORDER BY $sorting_column $sorting_direction ";
117
118 my $dbh = C4::Context->dbh();
119 my $sth = $dbh->prepare($sql);
120 $sth->execute(@parameters);
121
122 my $item_level_itypes = C4::Context->preference('item-level_itypes');
123
124 my @checkouts_today;
125 my @checkouts_previous;
126 while ( my $c = $sth->fetchrow_hashref() ) {
127     my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
128     my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
129
130     my ( $can_renew, $can_renew_error ) =
131       CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
132     my $can_renew_date =
133       $can_renew_error && $can_renew_error eq 'too_soon'
134       ? output_pref(
135         {
136             dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
137             as_due_date => 1
138         }
139       )
140       : undef;
141
142     my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
143       GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
144
145     my $itemtype = C4::Koha::getitemtypeinfo( $item_level_itypes ? $c->{itype} : $c->{itemtype} );
146     my $checkout = {
147         DT_RowId             => $c->{itemnumber} . '-' . $c->{borrowernumber},
148         title                => $c->{title},
149         author               => $c->{author},
150         barcode              => $c->{barcode},
151         itemtype             => $item_level_itypes ? $c->{itype} : $c->{itemtype},
152         itemtype_description => $itemtype->{translated_description},
153         location             => $c->{location} ? GetAuthorisedValueByCode( 'LOC', $c->{location} ) : q{},
154         itemnotes            => $c->{itemnotes},
155         itemnotes_nonpublic  => $c->{itemnotes_nonpublic},
156         branchcode           => $c->{branchcode},
157         branchname           => $c->{branchname},
158         itemcallnumber => $c->{itemcallnumber}   || q{},
159         charge         => $charge,
160         fine           => $fine,
161         price          => $c->{replacementprice} || q{},
162         can_renew      => $can_renew,
163         can_renew_error     => $can_renew_error,
164         can_renew_date      => $can_renew_date,
165         itemnumber          => $c->{itemnumber},
166         borrowernumber      => $c->{borrowernumber},
167         biblionumber        => $c->{biblionumber},
168         issuedate           => $c->{issuedate},
169         date_due            => $c->{date_due},
170         date_due_overdue    => $c->{date_due_overdue} ? JSON::true : JSON::false,
171         timestamp           => $c->{timestamp},
172         onsite_checkout     => $c->{onsite_checkout},
173         renewals_count      => $renewals_count,
174         renewals_allowed    => $renewals_allowed,
175         renewals_remaining  => $renewals_remaining,
176         issuedate_formatted => output_pref(
177             {
178                 dt          => dt_from_string( $c->{issuedate} ),
179                 as_due_date => 1
180             }
181         ),
182         date_due_formatted => output_pref(
183             {
184                 dt          => dt_from_string( $c->{date_due} ),
185                 as_due_date => 1
186             }
187         ),
188         subtitle =>
189           GetRecordValue( 'subtitle', GetMarcBiblio( $c->{biblionumber} ), GetFrameworkCode( $c->{biblionumber} ) ),
190         lost    => $c->{itemlost} ? GetAuthorisedValueByCode( 'LOST',    $c->{itemlost} ) : undef,
191         damaged => $c->{damaged}  ? GetAuthorisedValueByCode( 'DAMAGED', $c->{damaged} )  : undef,
192         borrower => {
193             surname    => $c->{surname},
194             firstname  => $c->{firstname},
195             cardnumber => $c->{cardnumber},
196         },
197         issued_today => !$c->{not_issued_today},
198     };
199
200     if ( $c->{not_issued_today} ) {
201         push( @checkouts_previous, $checkout );
202     }
203     else {
204         push( @checkouts_today, $checkout );
205     }
206 }
207
208
209 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
210 @checkouts_today = reverse(@checkouts_today)
211   unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
212
213 @checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
214 @checkouts_previous = reverse(@checkouts_previous)
215   if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
216
217 my @checkouts = ( @checkouts_today, @checkouts_previous );
218
219 my $i = 1;
220 map { $_->{sort_order} = $i++ } @checkouts;
221
222
223 my $data;
224 $data->{'iTotalRecords'}        = scalar @checkouts;
225 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
226 $data->{'sEcho'}                = $input->param('sEcho') || undef;
227 $data->{'aaData'}               = \@checkouts;
228
229 print to_json($data);