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