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.
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);
30 use Koha
::AuthorisedValues
;
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' })) {
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' );
69 date_due < now() as date_due_overdue,
84 branches2.branchname AS homebranch,
96 items.ccode AS collection,
108 DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
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 )
119 if ( @borrowernumber == 1 ) {
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');
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
} );
144 $can_renew_error && $can_renew_error eq 'too_soon'
147 dt
=> GetSoonestRenewDate
( $c->{borrowernumber
}, $c->{itemnumber
} ),
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
} );
158 if ( $c->{location
} ) {
159 my $av = Koha
::AuthorisedValues
->search({ category
=> 'LOC', authorised_value
=> $c->{location
} });
160 $location = $av->count ?
$av->next->lib : '';
163 if ( $c->{collection
} ) {
164 my $av = Koha
::AuthorisedValues
->search({ category
=> 'CCODE', authorised_value
=> $c->{collection
} });
165 $collection = $av->count ?
$av->next->lib : '';
168 if ( $c->{itemlost
} ) {
169 my $av = Koha
::AuthorisedValues
->search({ category
=> 'LOST', authorised_value
=> $c->{itemlost
} });
170 $lost = $av->count ?
$av->next->lib : '';
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 @subtitles = split(/ \| /, $c->{'subtitle'} // '' );
179 DT_RowId
=> $c->{itemnumber
} . '-' . $c->{borrowernumber
},
180 title
=> $c->{title
},
181 subtitle
=> \
@subtitles,
182 medium
=> $c->{medium
} // '',
183 part_number
=> $c->{part_number
} // '',
184 part_name
=> $c->{part_name
} // '',
185 author
=> $c->{author
},
186 barcode
=> $c->{barcode
},
187 itemtype
=> $item_level_itypes ?
$c->{itype
} : $c->{itemtype
},
188 itemtype_description
=> $itemtype ?
$itemtype->translated_description : q{},
189 collection
=> $collection,
190 location
=> $location,
191 homebranch
=> $c->{homebranch
},
192 itemnotes
=> $c->{itemnotes
},
193 itemnotes_nonpublic
=> $c->{itemnotes_nonpublic
},
194 branchcode
=> $c->{branchcode
},
195 branchname
=> $c->{branchname
},
196 itemcallnumber
=> $c->{itemcallnumber
} || q{},
199 price
=> $c->{replacementprice
} || q{},
200 can_renew
=> $can_renew,
201 can_renew_error
=> $can_renew_error,
202 can_renew_date
=> $can_renew_date,
203 itemnumber
=> $c->{itemnumber
},
204 borrowernumber
=> $c->{borrowernumber
},
205 biblionumber
=> $c->{biblionumber
},
206 issuedate
=> $c->{issuedate
},
207 date_due
=> $c->{date_due
},
208 date_due_overdue
=> $c->{date_due_overdue
} ? JSON
::true
: JSON
::false
,
209 timestamp
=> $c->{timestamp
},
210 onsite_checkout
=> $c->{onsite_checkout
},
211 enumchron
=> $c->{enumchron
},
212 renewals_count
=> $renewals_count,
213 renewals_allowed
=> $renewals_allowed,
214 renewals_remaining
=> $renewals_remaining,
215 issuedate_formatted
=> output_pref
(
217 dt
=> dt_from_string
( $c->{issuedate
} ),
221 date_due_formatted
=> output_pref
(
223 dt
=> dt_from_string
( $c->{date_due
} ),
230 surname
=> $c->{surname
},
231 firstname
=> $c->{firstname
},
232 cardnumber
=> $c->{cardnumber
},
234 issued_today
=> !$c->{not_issued_today
},
237 if ( $c->{not_issued_today
} ) {
238 push( @checkouts_previous, $checkout );
241 push( @checkouts_today, $checkout );
246 @checkouts_today = sort { $a->{timestamp
} cmp $b->{timestamp
} } @checkouts_today;
247 @checkouts_today = reverse(@checkouts_today)
248 unless ( C4
::Context
->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
250 @checkouts_previous = sort { $a->{date_due
} cmp $b->{date_due
} } @checkouts_previous;
251 @checkouts_previous = reverse(@checkouts_previous)
252 if ( C4
::Context
->preference('previousIssuesDefaultSortOrder') eq 'desc' );
254 my @checkouts = ( @checkouts_today, @checkouts_previous );
257 map { $_->{sort_order
} = $i++ } @checkouts;
261 $data->{'iTotalRecords'} = scalar @checkouts;
262 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
263 $data->{'sEcho'} = $input->param('sEcho') || undef;
264 $data->{'aaData'} = \
@checkouts;
266 print to_json
($data);