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