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