[open-ils-commits] r18324 - branches/rel_2_0/Open-ILS/src/perlmods/OpenILS/Utils (erickson)

svn at svn.open-ils.org svn at svn.open-ils.org
Wed Oct 13 16:18:28 EDT 2010


Author: erickson
Date: 2010-10-13 16:18:23 -0400 (Wed, 13 Oct 2010)
New Revision: 18324

Modified:
   branches/rel_2_0/Open-ILS/src/perlmods/OpenILS/Utils/Penalty.pm
Log:
clean up duplicate system-controlled penalties during penalty calculation; perform penalty trigger event creation after the standalone commit occurs.  todo, handle non-standalone post-commit penalty trigger event creation

Modified: branches/rel_2_0/Open-ILS/src/perlmods/OpenILS/Utils/Penalty.pm
===================================================================
--- branches/rel_2_0/Open-ILS/src/perlmods/OpenILS/Utils/Penalty.pm	2010-10-13 20:16:58 UTC (rev 18323)
+++ branches/rel_2_0/Open-ILS/src/perlmods/OpenILS/Utils/Penalty.pm	2010-10-13 20:18:23 UTC (rev 18324)
@@ -26,41 +26,53 @@
     my $penalties = $e->json_query({from => ['actor.calculate_system_penalties',$user_id, $context_org]});
 
     my $user = $e->retrieve_actor_user( $user_id );
-    my $ses = OpenSRF::AppSession->create('open-ils.trigger') if (@$penalties);
+    my @existing_penalties = grep { defined $_->{id} } @$penalties;
+    my @wanted_penalties = grep { !defined $_->{id} } @$penalties;
+    my @trigger_events;
 
     my %csp;
-    for my $pen_obj (@$penalties) {
+    for my $pen_obj (@wanted_penalties) {
 
-        next if grep { # leave duplicate penalties in place
-            $_->{org_unit} == $pen_obj->{org_unit} and
-            $_->{standing_penalty} == $pen_obj->{standing_penalty} and
-            ($_->{id} || '') ne ($pen_obj->{id} || '') } @$penalties;
-
         my $pen = Fieldmapper::actor::user_standing_penalty->new;
         $pen->$_($pen_obj->{$_}) for keys %$pen_obj;
 
-        if(defined $pen_obj->{id}) {
-            $e->delete_actor_user_standing_penalty($pen) or return $e->die_event;
+        # let's see if this penalty is accounted for already
+        my ($existing) = grep { 
+                $_->{org_unit} == $pen_obj->{org_unit} and
+                $_->{standing_penalty} == $pen_obj->{standing_penalty}
+            } @existing_penalties;
 
+        if($existing) { 
+            # we have one of these already.  Leave it be, but remove it from the 
+            # existing set so it's not deleted in the subsequent loop
+            @existing_penalties = grep { $_->{id} ne $existing->{id} }  @existing_penalties;
+
         } else {
+
+            # this is a new penalty
             $e->create_actor_user_standing_penalty($pen) or return $e->die_event;
 
-            my $csp_obj = $csp{$pen->standing_penalty} ||
+            my $csp_obj = $csp{$pen->standing_penalty} || 
                 $e->retrieve_config_standing_penalty( $pen->standing_penalty );
 
             # cache for later
             $csp{$pen->standing_penalty} = $csp_obj;
 
-            $ses->request(
-                'open-ils.trigger.event.autocreate',
-                'penalty.' . $csp_obj->name,
-                $pen,
-                $pen->org_unit
-            );
+            push(@trigger_events, ['penalty.' . $csp_obj->name, $pen, $pen->org_unit]);
         }
     }
 
+    # at this point, any penalties remaining in the existing 
+    # penalty set are unaccounted for and should be removed
+    for my $pen_obj (@existing_penalties) {
+        my $pen = Fieldmapper::actor::user_standing_penalty->new;
+        $pen->$_($pen_obj->{$_}) for keys %$pen_obj;
+        $e->delete_actor_user_standing_penalty($pen) or return $e->die_event;
+    }
+
     $e->commit if $commit;
+
+    $U->create_events_for_hook($$_[0], $$_[1], $$_[2]) for @trigger_events;
     return undef;
 }
 



More information about the open-ils-commits mailing list