1 package OpenILS::Application::Trigger::Validator;
2 use strict; use warnings;
4 use DateTime::Format::ISO8601;
5 use OpenILS::Utils::DateTime 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/;
12 sub fourty_two { return 42 }
13 sub NOOP_True { return 1 }
14 sub NOOP_False { return 0 }
16 my $U = 'OpenILS::Application::AppUtils';
22 return 0 if (defined($env->{target}->checkin_time));
23 return 0 if (defined($env->{target}->xact_finish));
25 if ($env->{params}->{min_target_age}) {
26 $env->{params}->{target_age_field} = 'xact_start';
27 return 0 if (!$self->MinPassiveTargetAge($env));
30 $logger->info("AUTORENEW: CircIsOpen is TRUE!");
34 sub MinPassiveTargetAge {
37 my $target = $env->{target};
38 my $delay_field = $env->{params}->{target_age_field} || $env->{event}->event_def->delay_field;
40 unless($env->{params}->{min_target_age}) {
41 $logger->warn("'min_target_age' parameter required for MinPassiveTargetAge validator");
42 return 0; # no-op false
45 unless($delay_field) {
46 $logger->warn("'target_age_field' parameter or delay_field required for MinPassiveTargetAge validator");
47 return 0; # no-op false
50 my $delay_field_ts = DateTime::Format::ISO8601->new->parse_datetime(clean_ISO8601($target->$delay_field()));
52 # to get the minimum time that the target must have aged to, add the min age to the delay field
53 $delay_field_ts->add( seconds => interval_to_seconds( $env->{params}->{min_target_age} ) );
55 return 1 if $delay_field_ts <= DateTime->now;
62 my $circ = $env->{target};
64 return 0 if $circ->checkin_time;
65 return 0 if $circ->stop_fines and not $circ->stop_fines =~ /MAXFINES|LONGOVERDUE/;
67 if ($env->{params}->{min_target_age}) {
68 $env->{params}->{target_age_field} = 'xact_start';
69 return 0 if (!$self->MinPassiveTargetAge($env));
72 my $due_date = DateTime::Format::ISO8601->new->parse_datetime(clean_ISO8601($circ->due_date));
73 return 0 if $due_date > DateTime->now;
82 my $hold = $env->{target};
84 if ($env->{params}->{check_email_notify}) {
85 return 0 unless $U->is_true($hold->email_notify);
87 if ($env->{params}->{check_sms_notify}) {
88 return 0 unless $hold->sms_notify;
90 if ($env->{params}->{check_phone_notify}) {
91 return 0 unless $hold->phone_notify;
95 !$hold->cancel_time and
96 !$hold->fulfillment_time and
97 $hold->current_shelf_lib and
98 (ref $hold->current_shelf_lib ? $hold->current_shelf_lib->id : $hold->current_shelf_lib)
99 eq (ref $hold->pickup_lib ? $hold->pickup_lib->id : $hold->pickup_lib) and
100 $hold->capture_time and # redundant
101 $hold->current_copy and # redundant
102 $hold->shelf_time and # redundant
103 $hold->current_copy->status == OILS_COPY_STATUS_ON_HOLDS_SHELF; # redundant
108 sub ReservationIsAvailable {
111 my $reservation = $env->{target};
114 !$reservation->cancel_time and
115 $reservation->capture_time and
116 $reservation->current_resource;
121 sub HoldIsCancelled {
125 my $hold = $env->{target};
127 if ($env->{params}->{check_email_notify}) {
128 return 0 unless $U->is_true($hold->email_notify);
130 if ($env->{params}->{check_sms_notify}) {
131 return 0 unless $hold->sms_notify;
133 if ($env->{params}->{check_phone_notify}) {
134 return 0 unless $hold->phone_notify;
137 return ($hold->cancel_time) ? 1 : 0;
140 sub HoldNotifyCheck {
144 my $hold = $env->{target};
146 if ($env->{params}->{check_email_notify}) {
147 return 0 unless $U->is_true($hold->email_notify);
149 if ($env->{params}->{check_sms_notify}) {
150 return 0 unless $hold->sms_notify;
152 if ($env->{params}->{check_phone_notify}) {
153 return 0 unless $hold->phone_notify;
161 my ($self, $env) = @_;
162 return $U->is_true($env->{target}->barred);
165 sub PatronNotBarred {
166 return !PatronBarred(@_);
170 # Being "In Collections" means having the PATRON_IN_COLLECTIONS penalty
171 # applied to the user at or above the circ_lib of the target circ.
172 sub PatronNotInCollections {
173 my ($self, $env) = @_;
174 my $user = $env->{target}->usr;
175 my $org = $env->{target}->circ_lib;
177 # beware environment fleshing
178 $user = $user->id if ref $user;
179 $org = $org->id if ref $org;
181 my $existing = new_editor()->search_actor_user_standing_penalty({
183 org_unit => $U->get_org_ancestors($org, 1),
184 standing_penalty => 30, # PATRON_IN_COLLECTIONS
186 {stop_date => undef},
187 {stop_date => {'>' => 'now'}}
191 return @$existing ? 0 : 1;
194 # core type circ in $env->{target}
195 sub CircIsAutoRenewable {
199 my $circ = $env->{target};
200 my $userId = $env->{target}->usr;
201 # 1. check if circ is open
202 if (!$self->CircIsOpen($env)){
206 # 2. Check if patron is barred
208 my ($user, $res) = $U->fetch_user($userId);
209 if ( $U->is_true($user->barred()) ){
213 reason => 'Please contact your library about your account.',
216 $U->create_events_for_hook('autorenewal', $circ, $user->home_ou(), 'system_autorenewal', \%user_data);