Bug 14100: Generic solution for language overlay - Item types
[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         itemcallnumber,
80         replacementprice,
81
82         issues.branchcode,
83         branchname,
84
85         items.itype,
86         biblioitems.itemtype,
87
88         borrowernumber,
89         surname,
90         firstname,
91         cardnumber,
92
93         itemlost,
94         damaged,
95         location,
96
97         DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
98     FROM issues
99         LEFT JOIN items USING ( itemnumber )
100         LEFT JOIN biblio USING ( biblionumber )
101         LEFT JOIN biblioitems USING ( biblionumber )
102         LEFT JOIN borrowers USING ( borrowernumber )
103         LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
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     my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
128
129     my ( $can_renew, $can_renew_error ) =
130       CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
131     my $can_renew_date =
132       $can_renew_error && $can_renew_error eq 'too_soon'
133       ? output_pref(
134         {
135             dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
136             as_due_date => 1
137         }
138       )
139       : undef;
140
141     my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
142       GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
143
144     my $itemtype = C4::Koha::getitemtypeinfo( $item_level_itypes ? $c->{itype} : $c->{itemtype} );
145     my $checkout = {
146         DT_RowId   => $c->{itemnumber} . '-' . $c->{borrowernumber},
147         title      => $c->{title},
148         author     => $c->{author},
149         barcode    => $c->{barcode},
150         itemtype   => $item_level_itypes ? $c->{itype} : $c->{itemtype},
151         itemtype_description => $itemtype->{translated_description},
152         location   => $c->{location} ? GetAuthorisedValueByCode( 'LOC', $c->{location} ) : q{},
153         itemnotes  => $c->{itemnotes},
154         branchcode => $c->{branchcode},
155         branchname => $c->{branchname},
156         itemcallnumber => $c->{itemcallnumber}   || q{},
157         charge         => $charge,
158         fine           => $fine,
159         price          => $c->{replacementprice} || q{},
160         can_renew      => $can_renew,
161         can_renew_error     => $can_renew_error,
162         can_renew_date      => $can_renew_date,
163         itemnumber          => $c->{itemnumber},
164         borrowernumber      => $c->{borrowernumber},
165         biblionumber        => $c->{biblionumber},
166         issuedate           => $c->{issuedate},
167         date_due            => $c->{date_due},
168         date_due_overdue    => $c->{date_due_overdue} ? JSON::true : JSON::false,
169         timestamp           => $c->{timestamp},
170         onsite_checkout         => $c->{onsite_checkout},
171         renewals_count      => $renewals_count,
172         renewals_allowed    => $renewals_allowed,
173         renewals_remaining  => $renewals_remaining,
174         issuedate_formatted => output_pref(
175             {
176                 dt          => dt_from_string( $c->{issuedate} ),
177                 as_due_date => 1
178             }
179         ),
180         date_due_formatted => output_pref(
181             {
182                 dt          => dt_from_string( $c->{date_due} ),
183                 as_due_date => 1
184             }
185         ),
186         subtitle => GetRecordValue(
187             'subtitle',
188             GetMarcBiblio( $c->{biblionumber} ),
189             GetFrameworkCode( $c->{biblionumber} )
190         ),
191         lost => $c->{itemlost} ? GetAuthorisedValueByCode( 'LOST', $c->{itemlost} ) : undef,
192         damaged => $c->{damaged} ? GetAuthorisedValueByCode( 'DAMAGED', $c->{damaged} ) : undef,
193         borrower => {
194             surname    => $c->{surname},
195             firstname  => $c->{firstname},
196             cardnumber => $c->{cardnumber},
197         },
198         issued_today => !$c->{not_issued_today},
199     };
200
201     if ( $c->{not_issued_today} ) {
202         push( @checkouts_previous, $checkout );
203     }
204     else {
205         push( @checkouts_today, $checkout );
206     }
207 }
208
209
210 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
211 @checkouts_today = reverse(@checkouts_today)
212   unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
213
214 @checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
215 @checkouts_previous = reverse(@checkouts_previous)
216   if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
217
218 my @checkouts = ( @checkouts_today, @checkouts_previous );
219
220 my $i = 1;
221 map { $_->{sort_order} = $i++ } @checkouts;
222
223
224 my $data;
225 $data->{'iTotalRecords'}        = scalar @checkouts;
226 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
227 $data->{'sEcho'}                = $input->param('sEcho') || undef;
228 $data->{'aaData'}               = \@checkouts;
229
230 print to_json($data);