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