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
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.
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);
37 my ( $auth_status, $sessionID ) =
38 check_cookie_auth( $input->cookie('CGISESSID'));
40 my $session = get_session($sessionID);
41 my $userid = $session->param('id');
43 unless (haspermission($userid, { circulate => 'circulate_remaining_permissions' })
44 || haspermission($userid, { borrowers => '*' })) {
48 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
50 my @borrowernumber = $input->multi_param('borrowernumber');
51 my $offset = $input->param('iDisplayStart');
52 my $results_per_page = $input->param('iDisplayLength') || -1;
54 my $sorting_column = $input->param('iSortCol_0') || q{};
55 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
57 my $sorting_direction = $input->param('sSortDir_0') || q{};
58 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
60 $results_per_page = undef if ( $results_per_page == -1 );
62 binmode STDOUT, ":encoding(UTF-8)";
63 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
70 date_due < now() as date_due_overdue,
102 DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
104 LEFT JOIN items USING ( itemnumber )
105 LEFT JOIN biblio USING ( biblionumber )
106 LEFT JOIN biblioitems USING ( biblionumber )
107 LEFT JOIN borrowers USING ( borrowernumber )
108 LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
112 if ( @borrowernumber == 1 ) {
116 $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
118 push( @parameters, @borrowernumber );
120 $sql .= " ORDER BY $sorting_column $sorting_direction ";
122 my $dbh = C4::Context->dbh();
123 my $sth = $dbh->prepare($sql);
124 $sth->execute(@parameters);
126 my $item_level_itypes = C4::Context->preference('item-level_itypes');
129 my @checkouts_previous;
130 while ( my $c = $sth->fetchrow_hashref() ) {
131 my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
132 my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
134 my ( $can_renew, $can_renew_error ) =
135 CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
137 $can_renew_error && $can_renew_error eq 'too_soon'
140 dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
146 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
147 GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
149 my $itemtype = C4::Koha::getitemtypeinfo( $item_level_itypes ? $c->{itype} : $c->{itemtype} );
151 DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
152 title => $c->{title},
153 author => $c->{author},
154 barcode => $c->{barcode},
155 itemtype => $item_level_itypes ? $c->{itype} : $c->{itemtype},
156 itemtype_description => $itemtype->{translated_description},
157 location => $c->{location} ? GetAuthorisedValueByCode( 'LOC', $c->{location} ) : q{},
158 itemnotes => $c->{itemnotes},
159 itemnotes_nonpublic => $c->{itemnotes_nonpublic},
160 branchcode => $c->{branchcode},
161 branchname => $c->{branchname},
162 itemcallnumber => $c->{itemcallnumber} || q{},
165 price => $c->{replacementprice} || q{},
166 can_renew => $can_renew,
167 can_renew_error => $can_renew_error,
168 can_renew_date => $can_renew_date,
169 itemnumber => $c->{itemnumber},
170 borrowernumber => $c->{borrowernumber},
171 biblionumber => $c->{biblionumber},
172 issuedate => $c->{issuedate},
173 date_due => $c->{date_due},
174 date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
175 timestamp => $c->{timestamp},
176 onsite_checkout => $c->{onsite_checkout},
177 enumchron => $c->{enumchron},
178 renewals_count => $renewals_count,
179 renewals_allowed => $renewals_allowed,
180 renewals_remaining => $renewals_remaining,
181 issuedate_formatted => output_pref(
183 dt => dt_from_string( $c->{issuedate} ),
187 date_due_formatted => output_pref(
189 dt => dt_from_string( $c->{date_due} ),
194 GetRecordValue( 'subtitle', GetMarcBiblio( $c->{biblionumber} ), GetFrameworkCode( $c->{biblionumber} ) ),
195 lost => $c->{itemlost} ? GetAuthorisedValueByCode( 'LOST', $c->{itemlost} ) : undef,
196 damaged => $c->{damaged} ? GetAuthorisedValueByCode( 'DAMAGED', $c->{damaged} ) : undef,
198 surname => $c->{surname},
199 firstname => $c->{firstname},
200 cardnumber => $c->{cardnumber},
202 issued_today => !$c->{not_issued_today},
205 if ( $c->{not_issued_today} ) {
206 push( @checkouts_previous, $checkout );
209 push( @checkouts_today, $checkout );
214 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
215 @checkouts_today = reverse(@checkouts_today)
216 unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
218 @checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
219 @checkouts_previous = reverse(@checkouts_previous)
220 if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
222 my @checkouts = ( @checkouts_today, @checkouts_previous );
225 map { $_->{sort_order} = $i++ } @checkouts;
229 $data->{'iTotalRecords'} = scalar @checkouts;
230 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
231 $data->{'sEcho'} = $input->param('sEcho') || undef;
232 $data->{'aaData'} = \@checkouts;
234 print to_json($data);