Bug 12540: Display "Every" on editing a MMT action if previously selected
[koha.git] / svc / checkouts
blob1885bd739c0d7d063c02c2d89b0ba298f48e7938
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 strict;
21 use warnings;
23 use CGI;
24 use JSON qw(to_json);
26 use C4::Auth qw(check_cookie_auth);
27 use C4::Biblio qw(GetMarcBiblio GetFrameworkCode GetRecordValue );
28 use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
29 use C4::Koha qw(GetAuthorisedValueByCode);
30 use C4::Overdues qw(GetFine);
31 use C4::Context;
33 use Koha::DateUtils;
35 my $input = new CGI;
37 my ( $auth_status, $sessionID ) =
38 check_cookie_auth( $input->cookie('CGISESSID'),
39 { circulate => 'circulate_remaining_permissions' } );
41 if ( $auth_status ne "ok" ) {
42 exit 0;
45 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
47 my @borrowernumber = $input->param('borrowernumber');
48 my $offset = $input->param('iDisplayStart');
49 my $results_per_page = $input->param('iDisplayLength') || -1;
51 my $sorting_column = $input->param('iSortCol_0') || q{};
52 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
54 my $sorting_direction = $input->param('sSortDir_0') || q{};
55 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
57 $results_per_page = undef if ( $results_per_page == -1 );
59 binmode STDOUT, ":encoding(UTF-8)";
60 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
62 my @parameters;
63 my $sql = '
64 SELECT
65 issuedate,
66 date_due,
67 date_due < now() as date_due_overdue,
68 issues.timestamp,
70 onsite_checkout,
72 biblionumber,
73 biblio.title,
74 author,
76 itemnumber,
77 barcode,
78 itemnotes,
79 itemcallnumber,
80 replacementprice,
82 issues.branchcode,
83 branchname,
85 items.itype,
86 itemtype_item.description AS itype_description,
87 biblioitems.itemtype,
88 itemtype_bib.description AS itemtype_description,
90 borrowernumber,
91 surname,
92 firstname,
93 cardnumber,
95 itemlost,
96 damaged,
98 DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
99 FROM issues
100 LEFT JOIN items USING ( itemnumber )
101 LEFT JOIN biblio USING ( biblionumber )
102 LEFT JOIN biblioitems USING ( biblionumber )
103 LEFT JOIN borrowers USING ( borrowernumber )
104 LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
105 LEFT JOIN itemtypes itemtype_bib ON ( biblioitems.itemtype = itemtype_bib.itemtype )
106 LEFT JOIN itemtypes itemtype_item ON ( items.itype = itemtype_item.itemtype )
107 WHERE borrowernumber
110 if ( @borrowernumber == 1 ) {
111 $sql .= '= ?';
113 else {
114 $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
116 push( @parameters, @borrowernumber );
118 $sql .= " ORDER BY $sorting_column $sorting_direction ";
120 my $dbh = C4::Context->dbh();
121 my $sth = $dbh->prepare($sql);
122 $sth->execute(@parameters);
124 my $item_level_itypes = C4::Context->preference('item-level_itypes');
126 my @checkouts_today;
127 my @checkouts_previous;
128 while ( my $c = $sth->fetchrow_hashref() ) {
129 my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
130 my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
132 my ( $can_renew, $can_renew_error ) =
133 CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
134 my $can_renew_date =
135 $can_renew_error && $can_renew_error eq 'too_soon'
136 ? output_pref(
138 dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
139 as_due_date => 1
142 : undef;
144 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
145 GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
147 my $checkout = {
148 DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
149 title => $c->{title},
150 author => $c->{author},
151 barcode => $c->{barcode},
152 itemtype => $item_level_itypes ? $c->{itype} : $c->{itemtype},
153 itemtype_description => $item_level_itypes ? $c->{itype_description} : $c->{itemtype_description},
154 itemnotes => $c->{itemnotes},
155 branchcode => $c->{branchcode},
156 branchname => $c->{branchname},
157 itemcallnumber => $c->{itemcallnumber} || q{},
158 charge => $charge,
159 fine => $fine,
160 price => $c->{replacementprice} || q{},
161 can_renew => $can_renew,
162 can_renew_error => $can_renew_error,
163 can_renew_date => $can_renew_date,
164 itemnumber => $c->{itemnumber},
165 borrowernumber => $c->{borrowernumber},
166 biblionumber => $c->{biblionumber},
167 issuedate => $c->{issuedate},
168 date_due => $c->{date_due},
169 date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
170 timestamp => $c->{timestamp},
171 onsite_checkout => $c->{onsite_checkout},
172 renewals_count => $renewals_count,
173 renewals_allowed => $renewals_allowed,
174 renewals_remaining => $renewals_remaining,
175 issuedate_formatted => output_pref(
177 dt => dt_from_string( $c->{issuedate} ),
178 as_due_date => 1
181 date_due_formatted => output_pref(
183 dt => dt_from_string( $c->{date_due} ),
184 as_due_date => 1
187 subtitle => GetRecordValue(
188 'subtitle',
189 GetMarcBiblio( $c->{biblionumber} ),
190 GetFrameworkCode( $c->{biblionumber} )
192 lost => $c->{itemlost} ? GetAuthorisedValueByCode( 'LOST', $c->{itemlost} ) : undef,
193 damaged => $c->{damaged} ? GetAuthorisedValueByCode( 'DAMAGED', $c->{damaged} ) : undef,
194 borrower => {
195 surname => $c->{surname},
196 firstname => $c->{firstname},
197 cardnumber => $c->{cardnumber},
199 issued_today => !$c->{not_issued_today},
202 if ( $c->{not_issued_today} ) {
203 push( @checkouts_previous, $checkout );
205 else {
206 push( @checkouts_today, $checkout );
211 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
212 @checkouts_today = reverse(@checkouts_today)
213 unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
215 @checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
216 @checkouts_previous = reverse(@checkouts_previous)
217 if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
219 my @checkouts = ( @checkouts_today, @checkouts_previous );
221 my $i = 1;
222 map { $_->{sort_order} = $i++ } @checkouts;
225 my $data;
226 $data->{'iTotalRecords'} = scalar @checkouts;
227 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
228 $data->{'sEcho'} = $input->param('sEcho') || undef;
229 $data->{'aaData'} = \@checkouts;
231 print to_json($data);