Bug 18403: Update permissions - borrowers => 1|* becomes borrowers => 'edit_borrowers'
[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 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.
11 #
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.
15 #
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.
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 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;
30
31 use Koha::AuthorisedValues;
32 use Koha::DateUtils;
33 use Koha::ItemTypes;
34
35 my $input = new CGI;
36
37 my ( $auth_status, $sessionID ) =
38   check_cookie_auth( $input->cookie('CGISESSID'));
39
40 my $session   = get_session($sessionID);
41 my $userid   = $session->param('id');
42
43 unless (haspermission($userid, { circulate => 'circulate_remaining_permissions' })
44     || haspermission($userid, { borrowers => 'edit_borrowers' })) {
45     exit 0;
46 }
47
48 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
49
50 my @borrowernumber   = $input->multi_param('borrowernumber');
51 my $offset           = $input->param('iDisplayStart');
52 my $results_per_page = $input->param('iDisplayLength') || -1;
53
54 my $sorting_column = $input->param('iSortCol_0') || q{};
55 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
56
57 my $sorting_direction = $input->param('sSortDir_0') || q{};
58 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
59
60 $results_per_page = undef if ( $results_per_page == -1 );
61
62 binmode STDOUT, ":encoding(UTF-8)";
63 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
64
65 my @parameters;
66 my $sql = '
67     SELECT
68         issuedate,
69         date_due,
70         date_due < now() as date_due_overdue,
71         issues.timestamp,
72
73         onsite_checkout,
74
75         biblionumber,
76         biblio.title,
77         author,
78
79         itemnumber,
80         barcode,
81         branches2.branchname AS homebranch,
82         itemnotes,
83         itemnotes_nonpublic,
84         itemcallnumber,
85         replacementprice,
86
87         issues.branchcode,
88         branches.branchname,
89
90         items.itype,
91         biblioitems.itemtype,
92
93         borrowernumber,
94         surname,
95         firstname,
96         cardnumber,
97
98         itemlost,
99         damaged,
100         location,
101         items.enumchron,
102
103         DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
104     FROM issues
105         LEFT JOIN items USING ( itemnumber )
106         LEFT JOIN biblio USING ( biblionumber )
107         LEFT JOIN biblioitems USING ( biblionumber )
108         LEFT JOIN borrowers USING ( borrowernumber )
109         LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
110         LEFT JOIN branches branches2 ON ( items.homebranch = branches2.branchcode )
111     WHERE borrowernumber
112 ';
113
114 if ( @borrowernumber == 1 ) {
115     $sql .= '= ?';
116 }
117 else {
118     $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
119 }
120 push( @parameters, @borrowernumber );
121
122 $sql .= " ORDER BY $sorting_column $sorting_direction ";
123
124 my $dbh = C4::Context->dbh();
125 my $sth = $dbh->prepare($sql);
126 $sth->execute(@parameters);
127
128 my $item_level_itypes = C4::Context->preference('item-level_itypes');
129
130 my @checkouts_today;
131 my @checkouts_previous;
132 while ( my $c = $sth->fetchrow_hashref() ) {
133     my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
134     my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
135
136     my ( $can_renew, $can_renew_error ) =
137       CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
138     my $can_renew_date =
139       $can_renew_error && $can_renew_error eq 'too_soon'
140       ? output_pref(
141         {
142             dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
143             as_due_date => 1
144         }
145       )
146       : undef;
147
148     my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
149       GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
150
151     my $itemtype = Koha::ItemTypes->find( $item_level_itypes ? $c->{itype} : $c->{itemtype} );
152     my $location;
153     if ( $c->{location} ) {
154         my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $c->{location} });
155         $location = $av->count ? $av->next->lib : '';
156     }
157     my $lost;
158     if ( $c->{itemlost} ) {
159         my $av = Koha::AuthorisedValues->search({ category => 'LOST', authorised_value => $c->{itemlost} });
160         $lost = $av->count ? $av->next->lib : '';
161     }
162     my $damaged;
163     if ( $c->{damaged} ) {
164         my $av = Koha::AuthorisedValues->search({ category => 'DAMAGED', authorised_value => $c->{damaged} });
165         $damaged = $av->count ? $av->next->lib : '';
166     }
167     my $checkout = {
168         DT_RowId             => $c->{itemnumber} . '-' . $c->{borrowernumber},
169         title                => $c->{title},
170         author               => $c->{author},
171         barcode              => $c->{barcode},
172         itemtype             => $item_level_itypes ? $c->{itype} : $c->{itemtype},
173         itemtype_description => $itemtype ? $itemtype->translated_description : q{},
174         location             => $location,
175         homebranch           => $c->{homebranch},
176         itemnotes            => $c->{itemnotes},
177         itemnotes_nonpublic  => $c->{itemnotes_nonpublic},
178         branchcode           => $c->{branchcode},
179         branchname           => $c->{branchname},
180         itemcallnumber => $c->{itemcallnumber}   || q{},
181         charge         => $charge,
182         fine           => $fine,
183         price          => $c->{replacementprice} || q{},
184         can_renew      => $can_renew,
185         can_renew_error     => $can_renew_error,
186         can_renew_date      => $can_renew_date,
187         itemnumber          => $c->{itemnumber},
188         borrowernumber      => $c->{borrowernumber},
189         biblionumber        => $c->{biblionumber},
190         issuedate           => $c->{issuedate},
191         date_due            => $c->{date_due},
192         date_due_overdue    => $c->{date_due_overdue} ? JSON::true : JSON::false,
193         timestamp           => $c->{timestamp},
194         onsite_checkout     => $c->{onsite_checkout},
195         enumchron           => $c->{enumchron},
196         renewals_count      => $renewals_count,
197         renewals_allowed    => $renewals_allowed,
198         renewals_remaining  => $renewals_remaining,
199         issuedate_formatted => output_pref(
200             {
201                 dt          => dt_from_string( $c->{issuedate} ),
202                 as_due_date => 1
203             }
204         ),
205         date_due_formatted => output_pref(
206             {
207                 dt          => dt_from_string( $c->{date_due} ),
208                 as_due_date => 1
209             }
210         ),
211         subtitle => GetRecordValue(
212             'subtitle',
213             GetMarcBiblio({ biblionumber => $c->{biblionumber} }),
214             GetFrameworkCode( $c->{biblionumber} ) ),
215         lost    => $lost,
216         damaged => $damaged,
217         borrower => {
218             surname    => $c->{surname},
219             firstname  => $c->{firstname},
220             cardnumber => $c->{cardnumber},
221         },
222         issued_today => !$c->{not_issued_today},
223     };
224
225     if ( $c->{not_issued_today} ) {
226         push( @checkouts_previous, $checkout );
227     }
228     else {
229         push( @checkouts_today, $checkout );
230     }
231 }
232
233
234 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
235 @checkouts_today = reverse(@checkouts_today)
236   unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
237
238 @checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
239 @checkouts_previous = reverse(@checkouts_previous)
240   if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
241
242 my @checkouts = ( @checkouts_today, @checkouts_previous );
243
244 my $i = 1;
245 map { $_->{sort_order} = $i++ } @checkouts;
246
247
248 my $data;
249 $data->{'iTotalRecords'}        = scalar @checkouts;
250 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
251 $data->{'sEcho'}                = $input->param('sEcho') || undef;
252 $data->{'aaData'}               = \@checkouts;
253
254 print to_json($data);