Bug 32170: Add page-section to csv-profiles
[koha-ffzg.git] / misc / cronjobs / update_totalissues.pl
index 71da3aa..eee8fde 100755 (executable)
@@ -4,38 +4,34 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
 
-BEGIN {
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
 
-    # find Koha's Perl modules
-    # test carefully before changing this
-    use FindBin;
-    eval { require "$FindBin::Bin/../kohalib.pl" };
-}
-
-use Getopt::Long;
-use Pod::Usage;
+use Koha::Script -cron;
+use Koha::DateUtils qw( dt_from_string );
 use C4::Context;
-use C4::Biblio;
+use C4::Biblio qw( UpdateTotalIssues );
+use C4::Log qw( cronlogaction );
 use DateTime;
 use DateTime::Format::MySQL;
-use Time::HiRes qw/time/;
-use POSIX qw/strftime ceil/;
+use Time::HiRes qw( time );
+use POSIX qw( ceil strftime );
 
 sub usage {
     pod2usage( -verbose => 2 );
@@ -56,6 +52,8 @@ my $incremental = 0;
 my $commit      = 100;
 my $unit;
 
+my $command_line_options = join(" ",@ARGV);
+
 my $result = GetOptions(
     'v|verbose'    => \$verbose,
     't|test'       => \$test_only,
@@ -68,7 +66,7 @@ my $result = GetOptions(
     'h|help'       => \$want_help
 );
 
-binmode( STDOUT, ":utf8" );
+binmode( STDOUT, ":encoding(UTF-8)" );
 
 if ( defined $since && defined $interval ) {
     print "The --since and --interval options are mutually exclusive.\n\n";
@@ -94,10 +92,13 @@ if ( not $result or $want_help ) {
     usage();
 }
 
+cronlogaction({ info => $command_line_options });
+
 my $dbh = C4::Context->dbh;
 $dbh->{AutoCommit} = 0;
 
 my $num_bibs_processed = 0;
+my $num_bibs_error = 0;
 
 my $starttime = time();
 
@@ -106,6 +107,8 @@ process_stats() if $usestats;
 
 report();
 
+cronlogaction({ action => 'End', info => "COMPLETED" });
+
 exit 0;
 
 sub process_items {
@@ -116,7 +119,7 @@ sub process_items {
 
 sub process_stats {
     if ($interval) {
-        my $dt = DateTime->now;
+        my $dt = dt_from_string();
 
         my %units = (
             h => 'hours',
@@ -132,22 +135,16 @@ sub process_stats {
             $dt->subtract( $units{$unit} => $1 ) );
     }
     my $limit = '';
-    $limit = " AND statistics.datetime >= ?" if ( $interval || $since );
+    $limit = " WHERE statistics.datetime >= ?" if ( $interval || $since );
 
     my $query =
-"SELECT biblio.biblionumber, COUNT(statistics.itemnumber) FROM biblio LEFT JOIN items ON (biblio.biblionumber=items.biblionumber) LEFT JOIN statistics ON (items.itemnumber=statistics.itemnumber) WHERE statistics.type = 'issue' $limit GROUP BY biblio.biblionumber;";
+"SELECT biblio.biblionumber, COUNT(statistics.itemnumber) FROM biblio\
+ LEFT JOIN items ON (biblio.biblionumber=items.biblionumber)\
+ LEFT JOIN statistics ON (items.itemnumber=statistics.itemnumber AND statistics.type = 'issue')
+ $limit\
+ GROUP BY biblio.biblionumber";
     process_query( $query, $limit );
 
-    unless ($incremental) {
-        $query =
-"SELECT biblio.biblionumber, 0 FROM biblio LEFT JOIN items ON (biblio.biblionumber=items.biblionumber) LEFT JOIN statistics ON (items.itemnumber=statistics.itemnumber) WHERE statistics.itemnumber IS NULL GROUP BY biblio.biblionumber;";
-        process_query( $query, '' );
-
-        $query =
-"SELECT biblio.biblionumber, 0 FROM biblio LEFT JOIN items ON (biblio.biblionumber=items.biblionumber) WHERE items.itemnumber IS NULL GROUP BY biblio.biblionumber;";
-        process_query( $query, '' );
-    }
-
     $dbh->commit();
 }
 
@@ -169,11 +166,16 @@ sub process_query {
         print "Processing bib $biblionumber ($totalissues issues)\n"
           if $verbose;
         if ( not $test_only ) {
+            my $ret;
             if ( $incremental && $totalissues > 0 ) {
-                UpdateTotalIssues( $biblionumber, $totalissues );
+                $ret = UpdateTotalIssues( $biblionumber, $totalissues );
             }
             else {
-                UpdateTotalIssues( $biblionumber, 0, $totalissues );
+                $ret = UpdateTotalIssues( $biblionumber, 0, $totalissues );
+            }
+            unless ($ret) {
+                print "Error while processing bib $biblionumber\n" if $verbose;
+                $num_bibs_error++;
             }
         }
         if ( not $test_only and ( $num_bibs_processed % $commit ) == 0 ) {
@@ -198,6 +200,7 @@ Run started at:                         $starttime
 Run ended at:                           $endtime
 Total run time:                         $totaltime ms
 Number of bibs modified:                $num_bibs_processed
+Number of bibs with error:              $num_bibs_error
 _SUMMARY_
     $summary .= "\n****  Ran in test mode only  ****\n" if $test_only;
     print $summary;