--- /dev/null
+#!/usr/bin/perl
+
+require "/openils/bin/oils_header.pl";
+
+use strict;
+use warnings;
+use OpenSRF::Utils qw/cleanse_ISO8601/;
+use OpenSRF::Utils::Logger qw/$logger/;
+use OpenSRF::Utils::SettingsClient;
+use OpenILS::Application::AppUtils;
+use OpenILS::Application::Serial;
+
+use Getopt::Std;
+use Data::Dumper;
+
+my $U = 'OpenILS::Application::AppUtils';
+my $debug = 0;
+
+sub regenerate_summaries {
+ my ($rollback, $print, @dist_ids) = @_;
+ print "dist IDs ", join(",", @dist_ids), "\n" if $debug;
+
+ my $e = new OpenILS::Utils::CStoreEditor(xact => 1);
+
+ foreach my $sdist_id (@dist_ids) {
+ # get distribution
+ my $sdist = $e->retrieve_serial_distribution($sdist_id)
+ or return $e->die_event;
+
+ foreach my $type (qw/basic index supplement/) {
+ # get issuances
+ my $issuances = $e->search_serial_issuance([
+ {
+ "+sdist" => {"id"=> $sdist_id},
+ "+sitem" => {"status" => "Received"},
+ "+scap" => {"type" => $type}
+ },
+ {
+ "join" => {
+ "sitem" => {},
+ "scap" => {},
+ "ssub" => {
+ "join" => {"sdist" =>{}}
+ }
+ },
+ "order_by" => {
+ "siss" => "date_published"
+ }
+ }
+ ]) or return $e->die_event;
+
+ next unless @$issuances;
+
+ my $evt = OpenILS::Application::Serial::_prepare_summaries(
+ $e, $issuances, $sdist, $type
+ );
+ if ($U->event_code($evt)) {
+ $e->rollback;
+ return $evt;
+ }
+ }
+ }
+
+ if ($print) {
+ my $summaries =
+ $e->search_serial_any_summary({distribution => \@dist_ids}) or
+ return $e->die_event;
+
+ print(Dumper($_), "\n") for sort {
+ $a->{summary_type} cmp $b->{summary_type} || $a->{id} <=> $b->{id}
+ } map { $_->to_bare_hash } @$summaries;
+ }
+
+ if ($rollback) {
+ $e->rollback;
+ } else {
+ $e->commit;
+ }
+
+ return;
+}
+
+sub usage {
+ print STDERR qq<
+$0: regenerate serials summaries for given distributions
+
+ $0 -h | -d DISTID1,DISTID2,... [-c /openils/conf/opensrf_core.xml]
+ [-p] [-r] [-D]
+
+ -h this help message
+ -d comma-separated list of distribution IDs
+ -c path to opensrf config file telling us where Evergreen is
+ -p print regenerated summaries
+ -r rollback transaction instead of committing
+ -D give a little extra debugging output
+>;
+ exit 1;
+}
+
+#############################################################################
+### main
+
+my @dists;
+my $opts = {};
+
+getopts('Dc:d:hrp', $opts) or usage;
+
+usage if $opts->{h};
+$debug = $opts->{D};
+
+if ($opts->{d}) {
+ @dists = map(int, split ",", $opts->{d});
+ usage if not @dists;
+} else {
+ print STDERR "-d required\n";
+ usage;
+}
+
+osrf_connect($opts->{c} || $ENV{SRF_CORE} || "/openils/conf/opensrf_core.xml");
+
+my $evt = regenerate_summaries($opts->{r}, $opts->{p}, @dists);
+if (defined $evt) {
+ print STDERR "Failure: " . Dumper($evt) . "\n";
+ exit 2;
+} else {
+ print "OK\n" if $debug;
+}
+
+0;