updated xact retrieval to do a ranged retrieval by org and not just org id
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 30 Jun 2006 19:22:45 +0000 (19:22 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 30 Jun 2006 19:22:45 +0000 (19:22 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@4869 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm
Open-ILS/src/perlmods/OpenILS/Application/Collections.pm

index 498fd90..7ccd503 100644 (file)
@@ -2,13 +2,12 @@ package OpenILS::Application::AppUtils;
 use strict; use warnings;
 use base qw/OpenSRF::Application/;
 use OpenSRF::Utils::Cache;
-use OpenSRF::EX qw(:try);
-use OpenILS::Perm;
-use OpenSRF::Utils::Logger;
+use OpenSRF::Utils::Logger qw/$logger/;
 use OpenILS::Utils::ModsParser;
+use OpenSRF::EX qw(:try);
 use OpenILS::Event;
 use Data::Dumper;
-my $logger = "OpenSRF::Utils::Logger";
+use OpenILS::Utils::CStoreEditor;
 
 
 my $cache_client = "OpenSRF::Utils::Cache";
@@ -59,7 +58,6 @@ sub check_user_perms {
 }
 
 # checks the list of user perms.  The first one that fails returns a new
-# OpenILS::Perm object of that type.  Returns undef if all perms are allowed
 sub check_perms {
        my( $self, $user_id, $org_id, @perm_types ) = @_;
        my $t = $self->check_user_perms( $user_id, $org_id, @perm_types );
@@ -1014,6 +1012,33 @@ sub fetch_bill {
 
 
 
+my $ORG_TREE;
+sub fetch_org_tree {
+       my $self = shift;
+       return $ORG_TREE if $ORG_TREE;
+       return $ORG_TREE = OpenILS::Utils::CStoreEditor->new->search_actor_org_unit( 
+               [
+                       {"parent_ou" => undef },
+                       {
+                               flesh                           => 2,
+                               flesh_fields    => { aou =>  ['children'] },
+                               order_by       => { aou => 'name'}
+                       }
+               ]
+       )->[0];
+}
+
+sub walk_org_tree {
+       my( $self, $node, $callback ) = @_;
+       return unless $node;
+       $callback->($node);
+       if( $node->children ) {
+               $self->walk_org_tree($_, $callback) for @{$node->children};
+       }
+}
+
+
+
 
 1;
 
index 87272f0..3972941 100644 (file)
@@ -5,10 +5,10 @@ use OpenILS::Application::AppUtils;
 use OpenSRF::Utils::Logger qw(:logger);
 use OpenSRF::Application;
 use base 'OpenSRF::Application';
-my $U = "OpenILS::Application::AppUtils";
 use OpenILS::Utils::CStoreEditor qw/:funcs/;
 use OpenILS::Utils::Fieldmapper;
 use OpenILS::Event;
+my $U = "OpenILS::Application::AppUtils";
 
 
 # --------------------------------------------------------------
@@ -17,6 +17,7 @@ use OpenILS::Event;
 sub initialize { return 1; }
 
 
+
 __PACKAGE__->register_method(
        method          => 'users_of_interest',
        api_name                => 'open-ils.collections.users_of_interest.retrieve',
@@ -204,6 +205,9 @@ sub transaction_details {
        my $org = $e->search_actor_org_unit({shortname => $location})
                or return $e->event; $org = $org->[0];
 
+       # get a reference to the org inside of the tree
+       $org = $U->find_org($U->fetch_org_tree(), $org->id);
+
        my @data;
        for my $uid (@$user_list) {
                my $blob = {};
@@ -230,9 +234,9 @@ sub transaction_details {
 
                $blob->{transactions} = {
                        circulations    => 
-                               fetch_circ_xacts($e, $uid, $org->id, $start_date, $end_date),
+                               fetch_circ_xacts($e, $uid, $org, $start_date, $end_date),
                        grocery                 => 
-                               fetch_grocery_xacts($e, $uid, $org->id, $start_date, $end_date)
+                               fetch_grocery_xacts($e, $uid, $org, $start_date, $end_date)
                };
 
                push( @data, $blob );
@@ -250,22 +254,36 @@ sub transaction_details {
 sub fetch_circ_xacts {
        my $e                           = shift;
        my $uid                 = shift;
-       my $orgid               = shift;
+       my $org                 = shift;
        my $start_date = shift;
        my $end_date    = shift;
 
-       # first get all open circs for this user
-       my $circs = $e->search_action_circulation(
-               {
-                       usr                     => $uid, 
-                       circ_lib                => $orgid, 
-                       xact_finish     => undef, 
-               }, 
-               {idlist => 1}
+       my @circs;
+
+       # at the specified org and each descendent org, 
+       # fetch the open circs for this user
+       $U->walk_org_tree( $org, 
+               sub {
+                       my $n = shift;
+                       $logger->debug("collect: searching for open circs at " . $n->shortname);
+                       push( @circs, 
+                               @{
+                                       $e->search_action_circulation(
+                                               {
+                                                       usr                     => $uid, 
+                                                       circ_lib                => $n->id,
+                                                       xact_finish     => undef, 
+                                               }, 
+                                               {idlist => 1}
+                                       )
+                               }
+                       );
+               }
        );
 
+
        my @data;
-       my $active_ids = fetch_active($e, $circs, $start_date, $end_date);
+       my $active_ids = fetch_active($e, \@circs, $start_date, $end_date);
 
        for my $cid (@$active_ids) {
                push( @data, 
@@ -290,21 +308,32 @@ sub fetch_circ_xacts {
 sub fetch_grocery_xacts {
        my $e                           = shift;
        my $uid                 = shift;
-       my $orgid               = shift;
+       my $org                 = shift;
        my $start_date = shift;
        my $end_date    = shift;
 
-       my $xacts = $e->search_money_grocery(
-               {
-                       usr                     => $uid, 
-                       circ_lib                => $orgid, 
-                       xact_finish     => undef, 
-               }, 
-               {idlist => 1}
+       my @xacts;
+       $U->walk_org_tree( $org, 
+               sub {
+                       my $n = shift;
+                       $logger->debug("collect: searching for open grocery xacts at " . $n->shortname);
+                       push( @xacts, 
+                               @{
+                                       $e->search_money_grocery(
+                                               {
+                                                       usr                                     => $uid, 
+                                                       billing_location        => $n->id,
+                                                       xact_finish                     => undef, 
+                                               }, 
+                                               {idlist => 1}
+                                       )
+                               }
+                       );
+               }
        );
 
        my @data;
-       my $active_ids = fetch_active($e, $xacts, $start_date, $end_date);
+       my $active_ids = fetch_active($e, \@xacts, $start_date, $end_date);
 
        for my $id (@$active_ids) {
                push( @data, 
@@ -333,6 +362,9 @@ sub fetch_grocery_xacts {
 sub fetch_active {
        my( $e, $ids, $start_date, $end_date ) = @_;
 
+       # use this..
+       # { payment_ts => { between => [ $start, $end ] } } ' ;) 
+
        my @active;
        for my $id (@$ids) {
 
@@ -340,8 +372,7 @@ sub fetch_active {
                my $bills = $e->search_money_billing (
                        {
                                xact                    => $id,
-                               billing_ts      => { ">=" => $start_date },
-                               billing_ts      => { "<=" => $end_date },
+                               billing_ts      => { between => [ $start_date, $end_date ] },
                        },
                        {idlist =>1}
                );
@@ -354,8 +385,7 @@ sub fetch_active {
                        $payments = $e->search_money_payment (
                                {
                                        xact                    => $id,
-                                       payment_ts      => { ">=" => $start_date },
-                                       payment_ts      => { "<=" => $end_date },
+                                       payment_ts      => { between => [ $start_date, $end_date ] },
                                },
                                {idlist =>1}
                        );