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
::Overdues
qw(GetFine);
32 use Koha
::AuthorisedValues
;
38 my ( $auth_status, $sessionID ) =
39 check_cookie_auth
( $input->cookie('CGISESSID'));
41 my $session = get_session
($sessionID);
42 my $userid = $session->param('id');
44 unless (haspermission
($userid, { circulate
=> 'circulate_remaining_permissions' })
45 || haspermission
($userid, { borrowers
=> '*' })) {
49 my @sort_columns = qw
/date_due title itype issuedate branchcode itemcallnumber/;
51 my @borrowernumber = $input->multi_param('borrowernumber');
52 my $offset = $input->param('iDisplayStart');
53 my $results_per_page = $input->param('iDisplayLength') || -1;
55 my $sorting_column = $input->param('iSortCol_0') || q{};
56 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ?
$sort_columns[$sorting_column] : 'issuedate';
58 my $sorting_direction = $input->param('sSortDir_0') || q{};
59 $sorting_direction = $sorting_direction eq 'asc' ?
'asc' : 'desc';
61 $results_per_page = undef if ( $results_per_page == -1 );
63 binmode STDOUT
, ":encoding(UTF-8)";
64 print $input->header( -type
=> 'text/plain', -charset
=> 'UTF-8' );
71 date_due < now() as date_due_overdue,
82 branches2.branchname AS homebranch,
104 DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
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 )
115 if ( @borrowernumber == 1 ) {
119 $sql .= ' IN (' . join( ',', ('?') x
@borrowernumber ) . ') ';
121 push( @parameters, @borrowernumber );
123 $sql .= " ORDER BY $sorting_column $sorting_direction ";
125 my $dbh = C4
::Context
->dbh();
126 my $sth = $dbh->prepare($sql);
127 $sth->execute(@parameters);
129 my $item_level_itypes = C4
::Context
->preference('item-level_itypes');
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
} );
137 my ( $can_renew, $can_renew_error ) =
138 CanBookBeRenewed
( $c->{borrowernumber
}, $c->{itemnumber
} );
140 $can_renew_error && $can_renew_error eq 'too_soon'
143 dt
=> GetSoonestRenewDate
( $c->{borrowernumber
}, $c->{itemnumber
} ),
149 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
150 GetRenewCount
( $c->{borrowernumber
}, $c->{itemnumber
} );
152 my $itemtype = Koha
::ItemTypes
->find( $item_level_itypes ?
$c->{itype
} : $c->{itemtype
} );
154 if ( $c->{location
} ) {
155 my $av = Koha
::AuthorisedValues
->search({ category
=> 'LOC', authorised_value
=> $c->{location
} });
156 $location = $av->count ?
$av->next->lib : '';
159 if ( $c->{itemlost
} ) {
160 my $av = Koha
::AuthorisedValues
->search({ category
=> 'LOST', authorised_value
=> $c->{itemlost
} });
161 $lost = $av->count ?
$av->next->lib : '';
164 if ( $c->{damaged
} ) {
165 my $av = Koha
::AuthorisedValues
->search({ category
=> 'DAMAGED', authorised_value
=> $c->{damaged
} });
166 $damaged = $av->count ?
$av->next->lib : '';
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 ?
$itemtype->translated_description : q{},
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{},
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
(
202 dt
=> dt_from_string
( $c->{issuedate
} ),
206 date_due_formatted
=> output_pref
(
208 dt
=> dt_from_string
( $c->{date_due
} ),
212 subtitle
=> GetRecordValue
(
214 GetMarcBiblio
({ biblionumber
=> $c->{biblionumber
} }),
215 GetFrameworkCode
( $c->{biblionumber
} ) ),
219 surname
=> $c->{surname
},
220 firstname
=> $c->{firstname
},
221 cardnumber
=> $c->{cardnumber
},
223 issued_today
=> !$c->{not_issued_today
},
226 if ( $c->{not_issued_today
} ) {
227 push( @checkouts_previous, $checkout );
230 push( @checkouts_today, $checkout );
235 @checkouts_today = sort { $a->{timestamp
} cmp $b->{timestamp
} } @checkouts_today;
236 @checkouts_today = reverse(@checkouts_today)
237 unless ( C4
::Context
->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
239 @checkouts_previous = sort { $a->{date_due
} cmp $b->{date_due
} } @checkouts_previous;
240 @checkouts_previous = reverse(@checkouts_previous)
241 if ( C4
::Context
->preference('previousIssuesDefaultSortOrder') eq 'desc' );
243 my @checkouts = ( @checkouts_today, @checkouts_previous );
246 map { $_->{sort_order
} = $i++ } @checkouts;
250 $data->{'iTotalRecords'} = scalar @checkouts;
251 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
252 $data->{'sEcho'} = $input->param('sEcho') || undef;
253 $data->{'aaData'} = \
@checkouts;
255 print to_json
($data);