From 231d59ba9a98891344997e20e2c482a8beab06f1 Mon Sep 17 00:00:00 2001
From: miker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Fri, 30 Oct 2009 02:52:49 +0000
Subject: [PATCH] add a validator (for stacking) to optionally check the age of
 an event target

git-svn-id: svn://svn.open-ils.org/ILS/trunk@14694 dcc99617-32d9-48b4-a31d-7c20da2025e4
---
 .../OpenILS/Application/Trigger/Validator.pm       | 25 ++++++++++++++++++++++
 Open-ILS/src/sql/Pg/002.schema.config.sql          |  2 +-
 Open-ILS/src/sql/Pg/400.schema.action_trigger.sql  |  1 +
 .../upgrade/0063.data.min-target-age-validator.sql |  8 +++++++
 4 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 Open-ILS/src/sql/Pg/upgrade/0063.data.min-target-age-validator.sql

diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator.pm b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator.pm
index f0906b8f30..bf3585e3f6 100644
--- a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator.pm
+++ b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator.pm
@@ -16,6 +16,11 @@ sub CircIsOpen {
     return 0 if (defined($env->{target}->checkin_time));
     return 0 if ($env->{params}->{max_delay_age} && !$self->MaxPassiveDelayAge($env));
 
+    if ($env->{params}->{min_target_age}) {
+        $env->{params}->{target_age_field} = 'xact_start';
+        return 0 if (!$self->MinPassiveTargetAge($env));
+    }
+
     return 1;
 }
 
@@ -38,6 +43,21 @@ sub MaxPassiveDelayAge {
     return 0;
 }
 
+sub MinPassiveTargetAge {
+    my $self = shift;
+    my $env = shift;
+    my $target = $env->{target};
+    my $delay_field = $env->{params}->{target_age_field} || $env->{event}->event_def->delay_field;
+
+    my $delay_field_ts = DateTime::Format::ISO8601->new->parse_datetime(clense_ISO8601($target->$delay_field()));
+
+    # to get the minimum time that the target must have aged to, add the min age to the delay field
+    $delay_field_ts->add( seconds => interval_to_seconds( $env->{params}->{min_target_age} ) );
+
+    return 1 if $delay_field_ts <= DateTime->now;
+    return 0;
+}
+
 sub CircIsOverdue {
     my $self = shift;
     my $env = shift;
@@ -47,6 +67,11 @@ sub CircIsOverdue {
     return 0 if $circ->stop_fines and not $circ->stop_fines =~ /MAXFINES|LONGOVERDUE/;
     return 0 if ($env->{params}->{max_delay_age} && !$self->MaxPassiveDelayAge($env));
 
+    if ($env->{params}->{min_target_age}) {
+        $env->{params}->{target_age_field} = 'xact_start';
+        return 0 if (!$self->MinPassiveTargetAge($env));
+    }
+
     my $due_date = DateTime::Format::ISO8601->new->parse_datetime(clense_ISO8601($circ->due_date));
     return 0 if $due_date > DateTime->now;
 
diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql
index 4d5e424711..5fa4cd88c0 100644
--- a/Open-ILS/src/sql/Pg/002.schema.config.sql
+++ b/Open-ILS/src/sql/Pg/002.schema.config.sql
@@ -51,7 +51,7 @@ CREATE TABLE config.upgrade_log (
     install_date    TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
 );
 
-INSERT INTO config.upgrade_log (version) VALUES ('0061'); -- Scott McKellar
+INSERT INTO config.upgrade_log (version) VALUES ('0063'); -- miker
 
 CREATE TABLE config.bib_source (
 	id		SERIAL	PRIMARY KEY,
diff --git a/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql b/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql
index b5f71a9b2c..83b3c10699 100644
--- a/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql
+++ b/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql
@@ -77,6 +77,7 @@ INSERT INTO action_trigger.validator (module,description) VALUES ('CircIsOpen','
 INSERT INTO action_trigger.validator (module,description) VALUES ('HoldIsAvailable','Check that an item is on the hold shelf');
 INSERT INTO action_trigger.validator (module,description) VALUES ('CircIsOverdue','Check that the circulation is overdue');
 INSERT INTO action_trigger.validator (module,description) VALUES ('MaxPassiveDelayAge','Check that the event is not too far past the delay_field time -- requires a max_delay_age interval parameter');
+INSERT INTO action_trigger.validator (module,description) VALUES ('MinPassiveTargetAge','Check that the target is old enough to be used by this event -- requires a min_target_age interval parameter, and accepts an optional target_age_field to specify what time to use for offsetting');
 
 -- After an event passes validation (action_trigger.validator), the reactor processes it.
 CREATE TABLE action_trigger.reactor (
diff --git a/Open-ILS/src/sql/Pg/upgrade/0063.data.min-target-age-validator.sql b/Open-ILS/src/sql/Pg/upgrade/0063.data.min-target-age-validator.sql
new file mode 100644
index 0000000000..d8a936bbe7
--- /dev/null
+++ b/Open-ILS/src/sql/Pg/upgrade/0063.data.min-target-age-validator.sql
@@ -0,0 +1,8 @@
+BEGIN;
+
+INSERT INTO config.upgrade_log (version) VALUES ('0063');
+
+INSERT INTO action_trigger.validator (module,description) VALUES ('MinPassiveTargetAge','Check that the target is old enough to be used by this event -- requires a min_target_age interval parameter, and accepts an optional target_age_field to specify what time to use for offsetting');
+
+COMMIT;
+
-- 
2.11.0