ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / offline_circ / download.pl
index 2b6b5af..2b6c67e 100755 (executable)
@@ -19,7 +19,7 @@
 #
 
 use Modern::Perl;
-use CGI;
+use CGI qw ( -utf8 );
 use JSON;
 use C4::Auth;
 use C4::Output;
@@ -27,28 +27,29 @@ use C4::Context;
 use C4::Koha;
 
 my $query = new CGI;
-my ( $template, $loggedinuser, $cookie, $flags ) =
-  checkauth( $query, undef, { circulate => "circulate_remaining_permissions" },
+checkauth( $query, undef, { circulate => "circulate_remaining_permissions" },
     "intranet" );
 
 my $page     = $query->param('page') || 0;
 my $startrec = int($page) * 5000;
 my $req_data = $query->param('data') || '';
 
-my $patrons_query = qq{SELECT
+my $patrons_query = q{SELECT
     borrowers.borrowernumber, cardnumber, surname, firstname, title,
     othernames, initials, streetnumber, streettype, address, address2, city,
     state, zipcode, country, email, phone, mobile, fax, dateofbirth, branchcode,
-    categorycode, dateenrolled, dateexpiry, gonenoaddress, lost, debarred,
+    categorycode, dateenrolled, dateexpiry, COALESCE(gonenoaddress, 0) AS gonenoaddress,
+    COALESCE(lost, 0) AS lost, debarred,
     debarredcomment, SUM(accountlines.amountoutstanding) AS fine
     FROM borrowers
     LEFT JOIN accountlines ON borrowers.borrowernumber=accountlines.borrowernumber
+    WHERE cardnumber IS NOT NULL
     GROUP BY borrowers.borrowernumber
-    LIMIT $startrec, 5000;
+    LIMIT ?, 5000;
     };
 
 # NOTE: we can't fit very long titles on the interface so there isn't really any point in transferring them
-my $items_query = qq{SELECT
+my $items_query = q{SELECT
     items.barcode AS barcode, items.itemnumber AS itemnumber,
     items.itemcallnumber AS callnumber, items.homebranch AS homebranch,
     items.holdingbranch AS holdingbranch, items.itype AS itemtype,
@@ -56,10 +57,11 @@ my $items_query = qq{SELECT
     biblio.author AS author, biblio.biblionumber AS biblionumber
     FROM items
     JOIN biblio ON biblio.biblionumber = items.biblionumber
-    LIMIT $startrec, 5000;
+    WHERE barcode IS NOT NULL
+    LIMIT ?, 5000;
     };
 
-my $issues_query = qq{SELECT
+my $issues_query = q{SELECT
     biblio.title AS title,
     items.barcode AS barcode,
     items.itemcallnumber AS callnumber,
@@ -72,36 +74,35 @@ my $issues_query = qq{SELECT
     JOIN items ON items.itemnumber = issues.itemnumber
     JOIN biblio ON biblio.biblionumber = items.biblionumber
     JOIN borrowers ON borrowers.borrowernumber = issues.borrowernumber
-    LIMIT $startrec, 5000;
+    WHERE barcode IS NOT NULL
+    LIMIT ?, 5000;
     };
 
-if ( $req_data eq 'all' ) {
-    print $query->header( -type => 'application/json', -charset => 'utf-8' );
-    print to_json(
-        {
-            'patrons' => get_data( $patrons_query, 'cardnumber' ),
-            'items'   => get_data( $items_query,   'barcode' ),
-            'issues'  => get_data( $issues_query,  'barcode' ),
-        }
-    );
+my %results;
+my $finished = 1;
+if ( $req_data eq 'patrons' || $req_data eq 'all' ) {
+    $results{'patrons'} = get_data( $patrons_query, 'cardnumber', $startrec );
 }
-elsif ( $req_data eq 'patrons' ) {
-    print $query->header( -type => 'application/json', -charset => 'utf-8' );
-    print to_json( { 'patrons' => get_data( $patrons_query, 'cardnumber' ), } );
+if ( $req_data eq 'items' || $req_data eq 'all' ) {
+    $results{'items'} = get_data( $items_query, 'barcode', $startrec );
 }
-elsif ( $req_data eq 'items' ) {
-    print $query->header( -type => 'application/json', -charset => 'utf-8' );
-    print to_json( { 'items' => get_data( $items_query, 'barcode' ), } );
+if ( $req_data eq 'issues' || $req_data eq 'all' ) {
+    $results{'issues'} = get_data( $issues_query, 'barcode', $startrec );
 }
-elsif ( $req_data eq 'issues' ) {
-    print $query->header( -type => 'application/json', -charset => 'utf-8' );
-    print to_json( { 'issues' => get_data( $issues_query, 'barcode' ), } );
+
+foreach my $key ( keys %results ) {
+    $finished = 0 if keys %{ $results{$key} } == 5000;
 }
+$results{'finished'} = $finished;
+
+print $query->header( -type => 'application/json', -charset => 'utf-8' );
+print to_json( \%results );
 
 sub get_data {
-    my ( $sql, $key ) = @_;
+    my ( $sql, $key, $start ) = @_;
+    $start ||= 0;
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare($sql);
-    $sth->execute();
+    $sth->execute($start);
     return $sth->fetchall_hashref($key);
 }