Bug 26190: Allow to close a basket with only cancelled order lines
[koha.git] / svc / checkouts
blob2dce8f3daf634db95929d9091a34ce22473cc5f9
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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 = CGI->new;
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 issues.issuedate,
68 issues.date_due,
69 issues.date_due < now() as date_due_overdue,
70 issues.timestamp,
72 issues.onsite_checkout,
74 biblio.biblionumber,
75 biblio.title,
76 biblio.subtitle,
77 biblio.medium,
78 biblio.part_number,
79 biblio.part_name,
80 biblio.author,
82 items.itemnumber,
83 items.barcode,
84 branches2.branchname AS homebranch,
85 items.itemnotes,
86 items.itemnotes_nonpublic,
87 items.itemcallnumber,
88 items.replacementprice,
90 issues.branchcode,
91 branches.branchname,
93 items.itype,
94 biblioitems.itemtype,
96 items.ccode AS collection,
98 borrowers.borrowernumber,
99 borrowers.surname,
100 borrowers.firstname,
101 borrowers.cardnumber,
103 items.itemlost,
104 items.damaged,
105 items.location,
106 items.enumchron,
107 items.materials,
109 DATEDIFF( issues.issuedate, CURRENT_DATE() ) AS not_issued_today,
111 return_claims.id AS return_claim_id,
112 return_claims.notes AS return_claim_notes,
113 return_claims.created_on AS return_claim_created_on,
114 return_claims.updated_on AS return_claim_updated_on
116 FROM issues
117 LEFT JOIN items USING ( itemnumber )
118 LEFT JOIN biblio USING ( biblionumber )
119 LEFT JOIN biblioitems USING ( biblionumber )
120 LEFT JOIN borrowers USING ( borrowernumber )
121 LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
122 LEFT JOIN branches branches2 ON ( items.homebranch = branches2.branchcode )
123 LEFT JOIN return_claims USING ( issue_id )
124 WHERE issues.borrowernumber
127 if ( @borrowernumber == 1 ) {
128 $sql .= '= ?';
130 else {
131 $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
133 push( @parameters, @borrowernumber );
135 $sql .= " ORDER BY $sorting_column $sorting_direction ";
137 my $dbh = C4::Context->dbh();
138 my $sth = $dbh->prepare($sql);
139 $sth->execute(@parameters);
141 my $item_level_itypes = C4::Context->preference('item-level_itypes');
142 my $claims_returned_lost_value = C4::Context->preference('ClaimReturnedLostValue');
143 my $confirm_parts_required = C4::Context->preference("CircConfirmItemParts");
145 my $itemtypes = { map { $_->{itemtype} => $_->{translated_description} } @{ Koha::ItemTypes->search_with_localization->unblessed } };
147 my @checkouts_today;
148 my @checkouts_previous;
149 while ( my $c = $sth->fetchrow_hashref() ) {
150 my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
151 my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
153 my ( $can_renew, $can_renew_error ) =
154 CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
155 my $can_renew_date =
156 $can_renew_error && $can_renew_error eq 'too_soon'
157 ? output_pref(
159 dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
160 as_due_date => 1
163 : undef;
165 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
166 GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
168 my ( $itemtype, $recordtype, $type_for_stat );
169 $itemtype = $itemtypes->{ $c->{itype} } if $c->{itype};
170 $recordtype = $itemtypes->{ $c->{itemtype} } if $c->{itemtype};
171 $type_for_stat = $item_level_itypes ? $itemtype : $recordtype;
173 my $location;
174 if ( $c->{location} ) {
175 my $av = Koha::AuthorisedValues->get_description_by_koha_field(
176 { kohafield => 'items.location', authorised_value => $c->{location} } );
177 $location = $av->{lib} ? $av->{lib} : '';
179 my $collection;
180 if ( $c->{collection} ) {
181 my $av = Koha::AuthorisedValues->get_description_by_koha_field(
182 { kohafield => 'items.ccode', authorised_value => $c->{collection} } );
183 $collection = $av->{lib} ? $av->{lib} : '';
185 my $lost;
186 my $claims_returned;
187 if ( $c->{itemlost} ) {
188 my $av = Koha::AuthorisedValues->get_description_by_koha_field(
189 { kohafield => 'items.itemlost', authorised_value => $c->{itemlost} } );
190 $lost = $av->{lib} ? $av->{lib} : '';
191 $claims_returned = $c->{itemlost} eq $claims_returned_lost_value;
193 my $damaged;
194 if ( $c->{damaged} ) {
195 my $av = Koha::AuthorisedValues->get_description_by_koha_field(
196 { kohafield => 'items.damaged', authorised_value => $c->{damaged} } );
197 $damaged = $av->{lib} ? $av->{lib} : '';
199 my $materials;
200 if ( $c->{materials} && $confirm_parts_required ) {
201 my $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => '', kohafield =>'items.materials', authorised_value => $c->{materials} });
202 $materials = $descriptions->{lib} // $c->{materials};
204 my @subtitles = split(/ \| /, $c->{'subtitle'} // '' );
205 my $checkout = {
206 DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
207 title => $c->{title},
208 subtitle => \@subtitles,
209 medium => $c->{medium} // '',
210 part_number => $c->{part_number} // '',
211 part_name => $c->{part_name} // '',
212 author => $c->{author},
213 barcode => $c->{barcode},
214 type_for_stat => $type_for_stat || q{},
215 itemtype_description => $itemtype || q{},
216 recordtype_description => $recordtype || q{},
217 collection => $collection,
218 location => $location,
219 homebranch => $c->{homebranch},
220 itemnotes => $c->{itemnotes},
221 itemnotes_nonpublic => $c->{itemnotes_nonpublic},
222 branchcode => $c->{branchcode},
223 branchname => $c->{branchname},
224 itemcallnumber => $c->{itemcallnumber} || q{},
225 charge => $charge,
226 fine => $fine,
227 price => $c->{replacementprice} || q{},
228 can_renew => $can_renew,
229 can_renew_error => $can_renew_error,
230 can_renew_date => $can_renew_date,
231 itemnumber => $c->{itemnumber},
232 borrowernumber => $c->{borrowernumber},
233 biblionumber => $c->{biblionumber},
234 issuedate => $c->{issuedate},
235 date_due => $c->{date_due},
236 date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
237 timestamp => $c->{timestamp},
238 onsite_checkout => $c->{onsite_checkout},
239 enumchron => $c->{enumchron},
240 renewals_count => $renewals_count,
241 renewals_allowed => $renewals_allowed,
242 renewals_remaining => $renewals_remaining,
244 return_claim_id => $c->{return_claim_id},
245 return_claim_notes => $c->{return_claim_notes},
246 return_claim_created_on => $c->{return_claim_created_on},
247 return_claim_updated_on => $c->{return_claim_updated_on},
248 return_claim_created_on_formatted => $c->{return_claim_created_on} ? output_pref({ dt => dt_from_string( $c->{return_claim_created_on} ) }) : undef,
249 return_claim_updated_on_formatted => $c->{return_claim_updated_on} ? output_pref({ dt => dt_from_string( $c->{return_claim_updated_on} ) }) : undef,
251 issuedate_formatted => output_pref(
253 dt => dt_from_string( $c->{issuedate} ),
254 as_due_date => 1
257 date_due_formatted => output_pref(
259 dt => dt_from_string( $c->{date_due} ),
260 as_due_date => 1
263 lost => $lost,
264 claims_returned => $claims_returned,
265 damaged => $damaged,
266 materials => $materials,
267 borrower => {
268 surname => $c->{surname},
269 firstname => $c->{firstname},
270 cardnumber => $c->{cardnumber},
272 issued_today => !$c->{not_issued_today},
275 if ( $c->{not_issued_today} ) {
276 push( @checkouts_previous, $checkout );
278 else {
279 push( @checkouts_today, $checkout );
284 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today; # latest to earliest
285 @checkouts_today = reverse(@checkouts_today)
286 if ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' ); # earliest to latest
288 @checkouts_previous =
289 sort { $a->{date_due} cmp $b->{date_due} || $a->{timestamp} cmp $b->{timestamp} }
290 @checkouts_previous; # latest to earliest
291 @checkouts_previous = reverse(@checkouts_previous)
292 if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' ); # earliest to latest
294 my @checkouts = ( @checkouts_today, @checkouts_previous );
296 my $i = 1;
297 map { $_->{sort_order} = $i++ } @checkouts;
300 my $data;
301 $data->{'iTotalRecords'} = scalar @checkouts;
302 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
303 $data->{'sEcho'} = $input->param('sEcho') || undef;
304 $data->{'aaData'} = \@checkouts;
306 print to_json($data);