Bug 10132: Simplify code, call the method only once
[koha.git] / offline_circ / download.pl
blob713057c805ea651f6cfe05007597403b007c748b
1 #!/usr/bin/perl
3 # Copyright 2013 C & P Bibliography Services
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use JSON;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Context;
27 use C4::Koha;
29 my $query = new CGI;
30 my ( $template, $loggedinuser, $cookie, $flags ) =
31 checkauth( $query, undef, { circulate => "circulate_remaining_permissions" },
32 "intranet" );
34 my $page = $query->param('page') || 0;
35 my $startrec = int($page) * 5000;
36 my $req_data = $query->param('data') || '';
38 my $patrons_query = q{SELECT
39 borrowers.borrowernumber, cardnumber, surname, firstname, title,
40 othernames, initials, streetnumber, streettype, address, address2, city,
41 state, zipcode, country, email, phone, mobile, fax, dateofbirth, branchcode,
42 categorycode, dateenrolled, dateexpiry, COALESCE(gonenoaddress, 0) AS gonenoaddress,
43 COALESCE(lost, 0) AS lost, debarred,
44 debarredcomment, SUM(accountlines.amountoutstanding) AS fine
45 FROM borrowers
46 LEFT JOIN accountlines ON borrowers.borrowernumber=accountlines.borrowernumber
47 WHERE cardnumber IS NOT NULL
48 GROUP BY borrowers.borrowernumber
49 LIMIT ?, 5000;
52 # NOTE: we can't fit very long titles on the interface so there isn't really any point in transferring them
53 my $items_query = q{SELECT
54 items.barcode AS barcode, items.itemnumber AS itemnumber,
55 items.itemcallnumber AS callnumber, items.homebranch AS homebranch,
56 items.holdingbranch AS holdingbranch, items.itype AS itemtype,
57 items.materials AS materials, LEFT(biblio.title, 60) AS title,
58 biblio.author AS author, biblio.biblionumber AS biblionumber
59 FROM items
60 JOIN biblio ON biblio.biblionumber = items.biblionumber
61 WHERE barcode IS NOT NULL
62 LIMIT ?, 5000;
65 my $issues_query = q{SELECT
66 biblio.title AS title,
67 items.barcode AS barcode,
68 items.itemcallnumber AS callnumber,
69 issues.date_due AS date_due,
70 issues.issuedate AS issuedate,
71 issues.renewals AS renewals,
72 borrowers.cardnumber AS cardnumber,
73 CONCAT(borrowers.surname, ', ', borrowers.firstname) AS borrower_name
74 FROM issues
75 JOIN items ON items.itemnumber = issues.itemnumber
76 JOIN biblio ON biblio.biblionumber = items.biblionumber
77 JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
78 WHERE barcode IS NOT NULL
79 LIMIT ?, 5000;
82 my %results;
83 my $finished = 1;
84 if ( $req_data eq 'patrons' || $req_data eq 'all' ) {
85 $results{'patrons'} = get_data( $patrons_query, 'cardnumber', $startrec );
87 if ( $req_data eq 'items' || $req_data eq 'all' ) {
88 $results{'items'} = get_data( $items_query, 'barcode', $startrec );
90 if ( $req_data eq 'issues' || $req_data eq 'all' ) {
91 $results{'issues'} = get_data( $issues_query, 'barcode', $startrec );
94 foreach my $key ( keys %results ) {
95 $finished = 0 if keys %{ $results{$key} } == 5000;
97 $results{'finished'} = $finished;
99 print $query->header( -type => 'application/json', -charset => 'utf-8' );
100 print to_json( \%results );
102 sub get_data {
103 my ( $sql, $key, $start ) = @_;
104 $start ||= 0;
105 my $dbh = C4::Context->dbh;
106 my $sth = $dbh->prepare($sql);
107 $sth->execute($start);
108 return $sth->fetchall_hashref($key);