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