Bug 11529: Add templates for biblio title display. Unify display.
[koha.git] / svc / checkouts
blob3b442307d7ab517c17bffccf0ac3030c9d0618f5
1 #!/usr/bin/perl
3 # Copyright 2014 ByWater Solutions
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
22 use CGI;
23 use JSON qw(to_json);
25 use C4::Auth qw(check_cookie_auth haspermission get_session);
26 use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
27 use C4::Overdues qw(GetFine);
28 use C4::Context;
30 use Koha::AuthorisedValues;
31 use Koha::DateUtils;
32 use Koha::ItemTypes;
34 my $input = new CGI;
36 my ( $auth_status, $sessionID ) =
37 check_cookie_auth( $input->cookie('CGISESSID'));
39 my $session = get_session($sessionID);
40 my $userid = $session->param('id');
42 unless (haspermission($userid, { circulate => 'circulate_remaining_permissions' })
43 || haspermission($userid, { borrowers => 'edit_borrowers' })) {
44 exit 0;
47 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
49 my @borrowernumber = $input->multi_param('borrowernumber');
50 my $offset = $input->param('iDisplayStart');
51 my $results_per_page = $input->param('iDisplayLength') || -1;
53 my $sorting_column = $input->param('iSortCol_0') || q{};
54 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
56 my $sorting_direction = $input->param('sSortDir_0') || q{};
57 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
59 $results_per_page = undef if ( $results_per_page == -1 );
61 binmode STDOUT, ":encoding(UTF-8)";
62 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
64 my @parameters;
65 my $sql = '
66 SELECT
67 issuedate,
68 date_due,
69 date_due < now() as date_due_overdue,
70 issues.timestamp,
72 onsite_checkout,
74 biblionumber,
75 biblio.title,
76 biblio.subtitle,
77 biblio.medium,
78 biblio.part_number,
79 biblio.part_name,
80 author,
82 itemnumber,
83 barcode,
84 branches2.branchname AS homebranch,
85 itemnotes,
86 itemnotes_nonpublic,
87 itemcallnumber,
88 replacementprice,
90 issues.branchcode,
91 branches.branchname,
93 items.itype,
94 biblioitems.itemtype,
96 items.ccode AS collection,
98 borrowernumber,
99 surname,
100 firstname,
101 cardnumber,
103 itemlost,
104 damaged,
105 location,
106 items.enumchron,
108 DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
109 FROM issues
110 LEFT JOIN items USING ( itemnumber )
111 LEFT JOIN biblio USING ( biblionumber )
112 LEFT JOIN biblioitems USING ( biblionumber )
113 LEFT JOIN borrowers USING ( borrowernumber )
114 LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
115 LEFT JOIN branches branches2 ON ( items.homebranch = branches2.branchcode )
116 WHERE borrowernumber
119 if ( @borrowernumber == 1 ) {
120 $sql .= '= ?';
122 else {
123 $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
125 push( @parameters, @borrowernumber );
127 $sql .= " ORDER BY $sorting_column $sorting_direction ";
129 my $dbh = C4::Context->dbh();
130 my $sth = $dbh->prepare($sql);
131 $sth->execute(@parameters);
133 my $item_level_itypes = C4::Context->preference('item-level_itypes');
135 my @checkouts_today;
136 my @checkouts_previous;
137 while ( my $c = $sth->fetchrow_hashref() ) {
138 my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
139 my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
141 my ( $can_renew, $can_renew_error ) =
142 CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
143 my $can_renew_date =
144 $can_renew_error && $can_renew_error eq 'too_soon'
145 ? output_pref(
147 dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
148 as_due_date => 1
151 : undef;
153 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
154 GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
156 my $itemtype = Koha::ItemTypes->find( $item_level_itypes ? $c->{itype} : $c->{itemtype} );
157 my $location;
158 if ( $c->{location} ) {
159 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $c->{location} });
160 $location = $av->count ? $av->next->lib : '';
162 my $collection;
163 if ( $c->{collection} ) {
164 my $av = Koha::AuthorisedValues->search({ category => 'CCODE', authorised_value => $c->{collection} });
165 $collection = $av->count ? $av->next->lib : '';
167 my $lost;
168 if ( $c->{itemlost} ) {
169 my $av = Koha::AuthorisedValues->search({ category => 'LOST', authorised_value => $c->{itemlost} });
170 $lost = $av->count ? $av->next->lib : '';
172 my $damaged;
173 if ( $c->{damaged} ) {
174 my $av = Koha::AuthorisedValues->search({ category => 'DAMAGED', authorised_value => $c->{damaged} });
175 $damaged = $av->count ? $av->next->lib : '';
177 my $checkout = {
178 DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
179 title => $c->{title},
180 subtitle => C4::Biblio::SplitKohaField($c->{'subtitle'}),
181 medium => $c->{medium} // '',
182 part_number => $c->{part_number} // '',
183 part_name => $c->{part_name} // '',
184 author => $c->{author},
185 barcode => $c->{barcode},
186 itemtype => $item_level_itypes ? $c->{itype} : $c->{itemtype},
187 itemtype_description => $itemtype ? $itemtype->translated_description : q{},
188 collection => $collection,
189 location => $location,
190 homebranch => $c->{homebranch},
191 itemnotes => $c->{itemnotes},
192 itemnotes_nonpublic => $c->{itemnotes_nonpublic},
193 branchcode => $c->{branchcode},
194 branchname => $c->{branchname},
195 itemcallnumber => $c->{itemcallnumber} || q{},
196 charge => $charge,
197 fine => $fine,
198 price => $c->{replacementprice} || q{},
199 can_renew => $can_renew,
200 can_renew_error => $can_renew_error,
201 can_renew_date => $can_renew_date,
202 itemnumber => $c->{itemnumber},
203 borrowernumber => $c->{borrowernumber},
204 biblionumber => $c->{biblionumber},
205 issuedate => $c->{issuedate},
206 date_due => $c->{date_due},
207 date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
208 timestamp => $c->{timestamp},
209 onsite_checkout => $c->{onsite_checkout},
210 enumchron => $c->{enumchron},
211 renewals_count => $renewals_count,
212 renewals_allowed => $renewals_allowed,
213 renewals_remaining => $renewals_remaining,
214 issuedate_formatted => output_pref(
216 dt => dt_from_string( $c->{issuedate} ),
217 as_due_date => 1
220 date_due_formatted => output_pref(
222 dt => dt_from_string( $c->{date_due} ),
223 as_due_date => 1
226 lost => $lost,
227 damaged => $damaged,
228 borrower => {
229 surname => $c->{surname},
230 firstname => $c->{firstname},
231 cardnumber => $c->{cardnumber},
233 issued_today => !$c->{not_issued_today},
236 if ( $c->{not_issued_today} ) {
237 push( @checkouts_previous, $checkout );
239 else {
240 push( @checkouts_today, $checkout );
245 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
246 @checkouts_today = reverse(@checkouts_today)
247 unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
249 @checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
250 @checkouts_previous = reverse(@checkouts_previous)
251 if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
253 my @checkouts = ( @checkouts_today, @checkouts_previous );
255 my $i = 1;
256 map { $_->{sort_order} = $i++ } @checkouts;
259 my $data;
260 $data->{'iTotalRecords'} = scalar @checkouts;
261 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
262 $data->{'sEcho'} = $input->param('sEcho') || undef;
263 $data->{'aaData'} = \@checkouts;
265 print to_json($data);