Bug 20894: Add barcode size parameters to /svc/barcode
[koha.git] / svc / checkouts
1 #!/usr/bin/perl
2
3 # Copyright 2014 ByWater Solutions
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use CGI;
23 use JSON qw(to_json);
24
25 use C4::Auth qw(check_cookie_auth haspermission);
26 use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
27 use C4::Overdues qw(GetFine);
28 use C4::Context;
29
30 use Koha::AuthorisedValues;
31 use Koha::DateUtils qw( dt_from_string output_pref );
32 use Koha::ItemTypes;
33
34 my $input = CGI->new;
35
36 my ( $auth_status, $session ) =
37   check_cookie_auth( $input->cookie('CGISESSID'));
38
39 my $userid   = $session->param('id');
40
41 unless (haspermission($userid, { circulate => 'circulate_remaining_permissions' })
42     || haspermission($userid, { borrowers => 'edit_borrowers' })) {
43     exit 0;
44 }
45
46 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
47
48 my @borrowernumber   = $input->multi_param('borrowernumber');
49 my $offset           = $input->param('iDisplayStart');
50 my $results_per_page = $input->param('iDisplayLength') || -1;
51
52 my $sorting_column = $input->param('iSortCol_0') || q{};
53 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
54
55 my $sorting_direction = $input->param('sSortDir_0') || q{};
56 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
57
58 $results_per_page = undef if ( $results_per_page == -1 );
59
60 binmode STDOUT, ":encoding(UTF-8)";
61 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
62
63 my @parameters;
64 my $sql = '
65     SELECT
66         issues.issuedate,
67         issues.date_due,
68         issues.date_due < now() as date_due_overdue,
69         issues.timestamp,
70
71         issues.onsite_checkout,
72
73         biblio.biblionumber,
74         biblio.title,
75         biblio.subtitle,
76         biblio.medium,
77         biblio.part_number,
78         biblio.part_name,
79         biblio.author,
80
81         items.itemnumber,
82         items.barcode,
83         branches2.branchname AS homebranch,
84         items.itemnotes,
85         items.itemnotes_nonpublic,
86         items.itemcallnumber,
87         items.copynumber,
88         items.replacementprice,
89
90         issues.branchcode,
91         branches.branchname,
92
93         items.itype,
94         biblioitems.itemtype,
95
96         items.ccode AS collection,
97
98         borrowers.borrowernumber,
99         borrowers.surname,
100         borrowers.firstname,
101         borrowers.cardnumber,
102
103         items.itemlost,
104         items.damaged,
105         items.location,
106         items.enumchron,
107         items.materials,
108
109         DATEDIFF( issues.issuedate, CURRENT_DATE() ) AS not_issued_today,
110
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
115
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
125 ';
126
127 if ( @borrowernumber == 1 ) {
128     $sql .= '= ?';
129 }
130 else {
131     $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
132 }
133 push( @parameters, @borrowernumber );
134
135 $sql .= " ORDER BY $sorting_column $sorting_direction ";
136
137 my $dbh = C4::Context->dbh();
138 my $sth = $dbh->prepare($sql);
139 $sth->execute(@parameters);
140
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");
144
145 my $itemtypes = { map { $_->{itemtype} => $_->{translated_description} } @{ Koha::ItemTypes->search_with_localization->unblessed } };
146
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} );
152
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(
158         {
159             dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
160             as_due_date => 1
161         }
162       )
163       : undef;
164
165     my (
166         $renewals_count,
167         $renewals_allowed,
168         $renewals_remaining,
169         $unseen_count,
170         $unseen_allowed,
171         $unseen_remaining
172     ) =
173       GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
174
175     my ( $itemtype, $recordtype, $type_for_stat );
176     $itemtype      = $itemtypes->{ $c->{itype} }    if $c->{itype};
177     $recordtype    = $itemtypes->{ $c->{itemtype} } if $c->{itemtype};
178     $type_for_stat = $item_level_itypes ? $itemtype : $recordtype;
179
180     my $location;
181     if ( $c->{location} ) {
182         my $av = Koha::AuthorisedValues->get_description_by_koha_field(
183             { kohafield => 'items.location', authorised_value => $c->{location} } );
184         $location = $av->{lib} ? $av->{lib} : '';
185     }
186     my $collection;
187     if ( $c->{collection} ) {
188         my $av = Koha::AuthorisedValues->get_description_by_koha_field(
189             { kohafield => 'items.ccode', authorised_value => $c->{collection} } );
190         $collection = $av->{lib} ? $av->{lib} : '';
191     }
192     my $lost;
193     my $claims_returned;
194     if ( $c->{itemlost} ) {
195         my $av = Koha::AuthorisedValues->get_description_by_koha_field(
196             { kohafield => 'items.itemlost', authorised_value => $c->{itemlost} } );
197         $lost            = $av->{lib} ? $av->{lib} : '';
198         $claims_returned = $c->{itemlost} eq $claims_returned_lost_value;
199     }
200     my $damaged;
201     if ( $c->{damaged} ) {
202         my $av = Koha::AuthorisedValues->get_description_by_koha_field(
203             { kohafield => 'items.damaged', authorised_value => $c->{damaged} } );
204         $damaged = $av->{lib} ? $av->{lib} : '';
205     }
206     my $materials;
207     if ( $c->{materials} && $confirm_parts_required ) {
208         my $descriptions = Koha::AuthorisedValues->get_description_by_koha_field({frameworkcode => '', kohafield =>'items.materials', authorised_value => $c->{materials} });
209         $materials = $descriptions->{lib} // $c->{materials};
210     }
211     my @subtitles = split(/ \| /, $c->{'subtitle'} // '' );
212     my $checkout = {
213         DT_RowId             => $c->{itemnumber} . '-' . $c->{borrowernumber},
214         title                => $c->{title},
215         subtitle             => \@subtitles,
216         medium               => $c->{medium} // '',
217         part_number          => $c->{part_number} // '',
218         part_name            => $c->{part_name} // '',
219         author               => $c->{author},
220         barcode              => $c->{barcode},
221         type_for_stat          => $type_for_stat || q{},
222         itemtype_description   => $itemtype || q{},
223         recordtype_description => $recordtype || q{},
224         collection           => $collection,
225         location             => $location,
226         homebranch           => $c->{homebranch},
227         itemnotes            => $c->{itemnotes},
228         itemnotes_nonpublic  => $c->{itemnotes_nonpublic},
229         branchcode           => $c->{branchcode},
230         branchname           => $c->{branchname},
231         itemcallnumber       => $c->{itemcallnumber} || q{},
232         copynumber           => $c->{copynumber} || q{},
233         charge         => $charge,
234         fine           => $fine,
235         price          => $c->{replacementprice} || q{},
236         can_renew      => $can_renew,
237         can_renew_error     => $can_renew_error,
238         can_renew_date      => $can_renew_date,
239         itemnumber          => $c->{itemnumber},
240         borrowernumber      => $c->{borrowernumber},
241         biblionumber        => $c->{biblionumber},
242         issuedate           => $c->{issuedate},
243         date_due            => $c->{date_due},
244         date_due_overdue    => $c->{date_due_overdue} ? JSON::true : JSON::false,
245         timestamp           => $c->{timestamp},
246         onsite_checkout     => $c->{onsite_checkout},
247         enumchron           => $c->{enumchron},
248         renewals_count      => $renewals_count,
249         renewals_allowed    => $renewals_allowed || 0,
250         renewals_remaining  => $renewals_remaining,
251         unseen_count        => $unseen_count,
252         unseen_allowed      => $unseen_allowed,
253         unseen_remaining    => $unseen_remaining,
254
255         return_claim_id         => $c->{return_claim_id},
256         return_claim_notes      => $c->{return_claim_notes},
257         return_claim_created_on => $c->{return_claim_created_on},
258         return_claim_updated_on => $c->{return_claim_updated_on},
259         return_claim_created_on_formatted => $c->{return_claim_created_on} ? output_pref({ dt => dt_from_string( $c->{return_claim_created_on} ) }) : undef,
260         return_claim_updated_on_formatted => $c->{return_claim_updated_on} ? output_pref({ dt => dt_from_string( $c->{return_claim_updated_on} ) }) : undef,
261
262         issuedate_formatted => output_pref(
263             {
264                 dt          => dt_from_string( $c->{issuedate} ),
265                 as_due_date => 1
266             }
267         ),
268         date_due_formatted => output_pref(
269             {
270                 dt          => dt_from_string( $c->{date_due} ),
271                 as_due_date => 1
272             }
273         ),
274         lost    => $lost,
275         claims_returned => $claims_returned,
276         damaged => $damaged,
277         materials => $materials,
278         borrower => {
279             surname    => $c->{surname},
280             firstname  => $c->{firstname},
281             cardnumber => $c->{cardnumber},
282         },
283         issued_today => !$c->{not_issued_today},
284     };
285
286     if ( $c->{not_issued_today} ) {
287         push( @checkouts_previous, $checkout );
288     }
289     else {
290         push( @checkouts_today, $checkout );
291     }
292 }
293
294
295 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;    # latest to earliest
296 @checkouts_today = reverse(@checkouts_today)
297   if ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );      # earliest to latest
298
299 @checkouts_previous =
300   sort { $a->{date_due} cmp $b->{date_due} || $a->{timestamp} cmp $b->{timestamp} }
301   @checkouts_previous;                                                               # latest to earliest
302 @checkouts_previous = reverse(@checkouts_previous)
303   if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );    # earliest to latest
304
305 my @checkouts = ( @checkouts_today, @checkouts_previous );
306
307 my $i = 1;
308 map { $_->{sort_order} = $i++ } @checkouts;
309
310
311 my $data;
312 $data->{'iTotalRecords'}        = scalar @checkouts;
313 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
314 $data->{'sEcho'}                = $input->param('sEcho') || undef;
315 $data->{'aaData'}               = \@checkouts;
316
317 print to_json($data);