1 package OpenILS::Application::Trigger::Validator;
2 use strict; use warnings;
4 use DateTime::Format::ISO8601;
5 use OpenSRF::Utils qw/:datetime/;
6 use OpenSRF::Utils::Logger qw/:logger/;
7 use OpenILS::Const qw/:const/;
8 use OpenILS::Application::AppUtils;
9 use OpenILS::Utils::CStoreEditor qw/:funcs/;
10 sub fourty_two { return 42 }
11 sub NOOP_True { return 1 }
12 sub NOOP_False { return 0 }
14 my $U = 'OpenILS::Application::AppUtils';
20 return 0 if (defined($env->{target}->checkin_time));
22 if ($env->{params}->{min_target_age}) {
23 $env->{params}->{target_age_field} = 'xact_start';
24 return 0 if (!$self->MinPassiveTargetAge($env));
30 sub MinPassiveTargetAge {
33 my $target = $env->{target};
34 my $delay_field = $env->{params}->{target_age_field} || $env->{event}->event_def->delay_field;
36 unless($env->{params}->{min_target_age}) {
37 $logger->warn("'min_target_age' parameter required for MinPassiveTargetAge validator");
38 return 0; # no-op false
41 unless($delay_field) {
42 $logger->warn("'target_age_field' parameter or delay_field required for MinPassiveTargetAge validator");
43 return 0; # no-op false
46 my $delay_field_ts = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($target->$delay_field()));
48 # to get the minimum time that the target must have aged to, add the min age to the delay field
49 $delay_field_ts->add( seconds => interval_to_seconds( $env->{params}->{min_target_age} ) );
51 return 1 if $delay_field_ts <= DateTime->now;
58 my $circ = $env->{target};
60 return 0 if $circ->checkin_time;
61 return 0 if $circ->stop_fines and not $circ->stop_fines =~ /MAXFINES|LONGOVERDUE/;
63 if ($env->{params}->{min_target_age}) {
64 $env->{params}->{target_age_field} = 'xact_start';
65 return 0 if (!$self->MinPassiveTargetAge($env));
68 my $due_date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($circ->due_date));
69 return 0 if $due_date > DateTime->now;
78 my $hold = $env->{target};
80 if ($env->{params}->{check_email_notify}) {
81 return 0 unless $U->is_true($hold->email_notify);
83 if ($env->{params}->{check_sms_notify}) {
84 return 0 unless $hold->sms_notify;
86 if ($env->{params}->{check_phone_notify}) {
87 return 0 unless $hold->phone_notify;
91 !$hold->cancel_time and
92 !$hold->fulfillment_time and
93 $hold->current_shelf_lib and
94 (ref $hold->current_shelf_lib ? $hold->current_shelf_lib->id : $hold->current_shelf_lib)
95 eq (ref $hold->pickup_lib ? $hold->pickup_lib->id : $hold->pickup_lib) and
96 $hold->capture_time and # redundant
97 $hold->current_copy and # redundant
98 $hold->shelf_time and # redundant
99 $hold->current_copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF; # redundant
104 sub ReservationIsAvailable {
107 my $reservation = $env->{target};
110 !$reservation->cancel_time and
111 $reservation->capture_time and
112 $reservation->current_resource;
117 sub HoldIsCancelled {
121 my $hold = $env->{target};
123 if ($env->{params}->{check_email_notify}) {
124 return 0 unless $U->is_true($hold->email_notify);
126 if ($env->{params}->{check_sms_notify}) {
127 return 0 unless $hold->sms_notify;
129 if ($env->{params}->{check_phone_notify}) {
130 return 0 unless $hold->phone_notify;
133 return ($hold->cancel_time) ? 1 : 0;
136 sub HoldNotifyCheck {
140 my $hold = $env->{target};
142 if ($env->{params}->{check_email_notify}) {
143 return 0 unless $U->is_true($hold->email_notify);
145 if ($env->{params}->{check_sms_notify}) {
146 return 0 unless $hold->sms_notify;
148 if ($env->{params}->{check_phone_notify}) {
149 return 0 unless $hold->phone_notify;
157 my ($self, $env) = @_;
158 return $U->is_true($env->{target}->barred);
161 sub PatronNotBarred {
162 return !PatronBarred(@_);
166 # Being "In Collections" means having the PATRON_IN_COLLECTIONS penalty
167 # applied to the user at or above the circ_lib of the target circ.
168 sub PatronNotInCollections {
169 my ($self, $env) = @_;
170 my $user = $env->{target}->usr;
171 my $org = $env->{target}->circ_lib;
173 # beware environment fleshing
174 $user = $user->id if ref $user;
175 $org = $org->id if ref $org;
177 my $existing = new_editor()->search_actor_user_standing_penalty({
179 org_unit => $U->get_org_ancestors($org, 1),
180 standing_penalty => 30, # PATRON_IN_COLLECTIONS
182 {stop_date => undef},
183 {stop_date => {'>' => 'now'}}
187 return @$existing ? 0 : 1;