Bug 22107: Make tests use a simpler class, with no FK
[koha.git] / svc / checkouts
blob9a1b3a5648c23df43c2237a871847d111090e1c0
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::Biblio qw(GetMarcBiblio GetFrameworkCode GetRecordValue );
27 use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
28 use C4::Overdues qw(GetFine);
29 use C4::Context;
31 use Koha::AuthorisedValues;
32 use Koha::DateUtils;
33 use Koha::ItemTypes;
35 my $input = new CGI;
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 => 'edit_borrowers' })) {
45 exit 0;
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' );
65 my @parameters;
66 my $sql = '
67 SELECT
68 issuedate,
69 date_due,
70 date_due < now() as date_due_overdue,
71 issues.timestamp,
73 onsite_checkout,
75 biblionumber,
76 biblio.title,
77 author,
79 itemnumber,
80 barcode,
81 branches2.branchname AS homebranch,
82 itemnotes,
83 itemnotes_nonpublic,
84 itemcallnumber,
85 replacementprice,
87 issues.branchcode,
88 branches.branchname,
90 items.itype,
91 biblioitems.itemtype,
93 items.ccode AS collection,
95 borrowernumber,
96 surname,
97 firstname,
98 cardnumber,
100 itemlost,
101 damaged,
102 location,
103 items.enumchron,
105 DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
106 FROM issues
107 LEFT JOIN items USING ( itemnumber )
108 LEFT JOIN biblio USING ( biblionumber )
109 LEFT JOIN biblioitems USING ( biblionumber )
110 LEFT JOIN borrowers USING ( borrowernumber )
111 LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
112 LEFT JOIN branches branches2 ON ( items.homebranch = branches2.branchcode )
113 WHERE borrowernumber
116 if ( @borrowernumber == 1 ) {
117 $sql .= '= ?';
119 else {
120 $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
122 push( @parameters, @borrowernumber );
124 $sql .= " ORDER BY $sorting_column $sorting_direction ";
126 my $dbh = C4::Context->dbh();
127 my $sth = $dbh->prepare($sql);
128 $sth->execute(@parameters);
130 my $item_level_itypes = C4::Context->preference('item-level_itypes');
132 my @checkouts_today;
133 my @checkouts_previous;
134 while ( my $c = $sth->fetchrow_hashref() ) {
135 my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
136 my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
138 my ( $can_renew, $can_renew_error ) =
139 CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
140 my $can_renew_date =
141 $can_renew_error && $can_renew_error eq 'too_soon'
142 ? output_pref(
144 dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
145 as_due_date => 1
148 : undef;
150 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
151 GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
153 my $itemtype = Koha::ItemTypes->find( $item_level_itypes ? $c->{itype} : $c->{itemtype} );
154 my $location;
155 if ( $c->{location} ) {
156 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $c->{location} });
157 $location = $av->count ? $av->next->lib : '';
159 my $collection;
160 if ( $c->{collection} ) {
161 my $av = Koha::AuthorisedValues->search({ category => 'CCODE', authorised_value => $c->{collection} });
162 $collection = $av->count ? $av->next->lib : '';
164 my $lost;
165 if ( $c->{itemlost} ) {
166 my $av = Koha::AuthorisedValues->search({ category => 'LOST', authorised_value => $c->{itemlost} });
167 $lost = $av->count ? $av->next->lib : '';
169 my $damaged;
170 if ( $c->{damaged} ) {
171 my $av = Koha::AuthorisedValues->search({ category => 'DAMAGED', authorised_value => $c->{damaged} });
172 $damaged = $av->count ? $av->next->lib : '';
174 my $checkout = {
175 DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
176 title => $c->{title},
177 author => $c->{author},
178 barcode => $c->{barcode},
179 itemtype => $item_level_itypes ? $c->{itype} : $c->{itemtype},
180 itemtype_description => $itemtype ? $itemtype->translated_description : q{},
181 collection => $collection,
182 location => $location,
183 homebranch => $c->{homebranch},
184 itemnotes => $c->{itemnotes},
185 itemnotes_nonpublic => $c->{itemnotes_nonpublic},
186 branchcode => $c->{branchcode},
187 branchname => $c->{branchname},
188 itemcallnumber => $c->{itemcallnumber} || q{},
189 charge => $charge,
190 fine => $fine,
191 price => $c->{replacementprice} || q{},
192 can_renew => $can_renew,
193 can_renew_error => $can_renew_error,
194 can_renew_date => $can_renew_date,
195 itemnumber => $c->{itemnumber},
196 borrowernumber => $c->{borrowernumber},
197 biblionumber => $c->{biblionumber},
198 issuedate => $c->{issuedate},
199 date_due => $c->{date_due},
200 date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
201 timestamp => $c->{timestamp},
202 onsite_checkout => $c->{onsite_checkout},
203 enumchron => $c->{enumchron},
204 renewals_count => $renewals_count,
205 renewals_allowed => $renewals_allowed,
206 renewals_remaining => $renewals_remaining,
207 issuedate_formatted => output_pref(
209 dt => dt_from_string( $c->{issuedate} ),
210 as_due_date => 1
213 date_due_formatted => output_pref(
215 dt => dt_from_string( $c->{date_due} ),
216 as_due_date => 1
219 subtitle => GetRecordValue(
220 'subtitle',
221 GetMarcBiblio({ biblionumber => $c->{biblionumber} }),
222 GetFrameworkCode( $c->{biblionumber} ) ),
223 lost => $lost,
224 damaged => $damaged,
225 borrower => {
226 surname => $c->{surname},
227 firstname => $c->{firstname},
228 cardnumber => $c->{cardnumber},
230 issued_today => !$c->{not_issued_today},
233 if ( $c->{not_issued_today} ) {
234 push( @checkouts_previous, $checkout );
236 else {
237 push( @checkouts_today, $checkout );
242 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
243 @checkouts_today = reverse(@checkouts_today)
244 unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
246 @checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
247 @checkouts_previous = reverse(@checkouts_previous)
248 if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
250 my @checkouts = ( @checkouts_today, @checkouts_previous );
252 my $i = 1;
253 map { $_->{sort_order} = $i++ } @checkouts;
256 my $data;
257 $data->{'iTotalRecords'} = scalar @checkouts;
258 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
259 $data->{'sEcho'} = $input->param('sEcho') || undef;
260 $data->{'aaData'} = \@checkouts;
262 print to_json($data);