webstaff: Teach serials perl to predict for "current" issue, not just future
authorMike Rylander <mrylander@gmail.com>
Mon, 15 May 2017 20:27:47 +0000 (16:27 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Tue, 30 May 2017 16:06:44 +0000 (12:06 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/perlmods/lib/OpenILS/Application/Serial.pm
Open-ILS/src/perlmods/lib/OpenILS/Utils/MFHD.pm

index 75f915a..d83e191 100644 (file)
@@ -891,17 +891,49 @@ __PACKAGE__->register_method(
     }
 );
 
+__PACKAGE__->register_method(
+    method    => 'make_prediction_values',
+    api_name  => 'open-ils.serial.make_prediction_values',
+    api_level => 1,
+    argc      => 1,
+    signature => {
+        desc     => 'Receives an ssub id and returns objects that can be used to populate the issuance and item tables',
+        'params' => [ {
+                 name => 'ssub_id',
+                 desc => 'Serial Subscription ID',
+                 type => 'int'
+            }
+        ]
+    }
+);
+
 sub make_predictions {
     my ($self, $conn, $authtoken, $args) = @_;
 
-    my $editor = OpenILS::Utils::CStoreEditor->new();
     my $ssub_id = $args->{ssub_id};
-    my $mfhd = MFHD->new(MARC::Record->new());
 
+    my $editor = OpenILS::Utils::CStoreEditor->new();
     my $ssub = $editor->retrieve_serial_subscription([$ssub_id]);
-    my $scaps = $editor->search_serial_caption_and_pattern({ subscription => $ssub_id, active => 't'});
     my $sdists = $editor->search_serial_distribution( [{ subscription => $ssub->id }, { flesh => 1, flesh_fields => {sdist => [ qw/ streams / ]} }] ); #TODO: 'deleted' support?
 
+    return store_predictions(
+        $self, $conn, $authtoken, $args, $ssub, $sdists,
+        make_prediction_values($self, $conn, $authtoken, $args, $ssub, $sdists, $editor)
+    );
+}
+
+sub make_prediction_values {
+    my ($self, $conn, $authtoken, $args, $ssub, $sdists, $editor) = @_;
+
+    my $ssub_id = $args->{ssub_id};
+
+    $editor ||= OpenILS::Utils::CStoreEditor->new();
+    $ssub ||= $editor->retrieve_serial_subscription([$ssub_id]);
+    $sdists ||= $editor->search_serial_distribution( [{ subscription => $ssub->id }, { flesh => 1, flesh_fields => {sdist => [ qw/ streams / ]} }] ); #TODO: 'deleted' support?
+
+    my $scaps = $editor->search_serial_caption_and_pattern({ subscription => $ssub_id, active => 't'});
+    my $mfhd = MFHD->new(MARC::Record->new());
+
     my $total_streams = 0;
     foreach (@$sdists) {
         $total_streams += scalar(@{$_->streams});
@@ -942,6 +974,7 @@ sub make_predictions {
         my $options = {
                 'caption' => $caption_field,
                 'scap_id' => $scap->id,
+                'include_base_issuance' => $args->{include_base_issuance},
                 'num_to_predict' => $args->{num_to_predict},
                 'end_date' => defined $args->{end_date} ?
                     $_strp_date->parse_datetime($args->{end_date}) : undef
@@ -981,6 +1014,12 @@ sub make_predictions {
         $link_id++;
     }
 
+    return @predictions;
+}
+
+sub store_predictions {
+    my ($self, $conn, $authtoken, $args, $ssub, $sdists, @predictions) = @_;
+
     my @issuances;
     foreach my $prediction (@predictions) {
         my $issuance = new Fieldmapper::serial::issuance;
@@ -1038,6 +1077,7 @@ sub _generate_issuance_values {
     my ($mfhd, $options) = @_;
     my $caption = $options->{caption};
     my $scap_id = $options->{scap_id};
+    my $include_base_issuance = $options->{include_base_issuance};
     my $num_to_predict = $options->{num_to_predict};
     my $end_date = $options->{end_date};
     my $predict_from = $options->{predict_from};   # MFHD::Holding to predict from
@@ -1084,7 +1124,12 @@ sub _generate_issuance_values {
         $predict_from = new MFHD::Holding($predict_from->seqno, new MARC::Field($predict_from->tag, $predict_from->indicator(1), $predict_from->indicator(2), $predict_from->subfields_list), $faked_caption);
     }
 
-    my @predictions = $mfhd->generate_predictions({'base_holding' => $predict_from, 'num_to_predict' => $num_to_predict, 'end_date' => $end_date});
+    my @predictions = $mfhd->generate_predictions({
+        'include_base_issuance' => $include_base_issuance,
+        'base_holding' => $predict_from,
+        'num_to_predict' => $num_to_predict,
+        'end_date' => $end_date
+    });
 
     my $pub_date;
     my @issuance_values;
index 03975cf..ba46c31 100644 (file)
@@ -273,6 +273,7 @@ sub _holding_date {
 # generate_predictions()
 # Accepts a hash ref of options initially defined as:
 # base_holding : reference to the holding field to predict from
+# include_base_issuance : whether to "predict" the startting holding, so as to generate a label for it
 # num_to_predict : the number of issues you wish to predict
 # OR
 # end_holding : holding field ref, keep predicting until you meet or exceed it
@@ -293,6 +294,7 @@ sub generate_predictions {
     my $end_holding    = $options->{end_holding};
     my $end_date       = $options->{end_date};
     my $max_to_predict = $options->{max_to_predict} || 10000; # fail-safe
+    my $include_base_issuance   = $options->{include_base_issuance};
 
     if (!defined($base_holding)) {
         carp("Base holding not defined in generate_predictions, returning empty set");
@@ -305,6 +307,7 @@ sub generate_predictions {
     my $curr_holding = $base_holding->clone; # prevent side-effects
     
     my @predictions;
+    push(@predictions, $curr_holding) if ($include_base_issuance);
         
     if ($num_to_predict) {
         for (my $i = 0; $i < $num_to_predict; $i++) {