]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/ScriptBuilder.pm
re-organizing, testing, adding functions, adding group configs
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / ScriptBuilder.pm
1 package OpenILS::Application::Circ::ScriptBuilder;
2 use strict; use warnings;
3 use OpenILS::Utils::ScriptRunner;
4 use OpenILS::Utils::CStoreEditor qw/:funcs/;
5 use OpenILS::Application::AppUtils;
6 use OpenILS::Application::Actor;
7 use OpenSRF::Utils::Logger qw/$logger/;
8 my $U = "OpenILS::Application::AppUtils";
9 use Data::Dumper;
10
11 my $evt = "environment";
12 my @COPY_STATUSES;
13 my @COPY_LOCATIONS;
14 my %GROUP_SET;
15 my $GROUP_TREE;
16
17
18 # -----------------------------------------------------------------------
19 # Possible Args:
20 #  copy
21 #  copy_id
22 #  copy_barcode
23 #
24 #  patron
25 #  patron_id
26 #  patron_barcode
27 #
28 #  fetch_patron_circ_info - load info on items out, overdues, and fines.
29 #
30 #  _direct - this is a hash of key/value pairs to shove directly into the 
31 #  script runner.  Use this to cover data not covered by this module
32 # -----------------------------------------------------------------------
33 sub build {
34         my( $class, $args ) = @_;
35
36         my $evt;
37         my @evts;
38
39         my $editor = $$args{editor} || new_editor();
40
41         $args->{_direct} = {} unless $args->{_direct};
42         
43         $evt = fetch_bib_data($editor, $args);
44         push(@evts, $evt) if $evt;
45         $evt = fetch_user_data($editor, $args);
46         push(@evts, $evt) if $evt;
47         $args->{_event} = \@evts;
48         return build_runner($editor, $args);
49 }
50
51
52 sub build_runner {
53         my $editor      = shift;
54         my $ctx         = shift;
55         my $runner      = OpenILS::Utils::ScriptRunner->new;
56
57         $runner->insert( "$evt.groupTree",      $GROUP_TREE, 1);
58
59         $runner->insert( "$evt.patron",         $ctx->{patron}, 1);
60         $runner->insert( "$evt.copy",                   $ctx->{copy}, 1);
61         $runner->insert( "$evt.volume",         $ctx->{volume}, 1);
62         $runner->insert( "$evt.title",          $ctx->{title}, 1);
63         $runner->insert( "$evt.requestor",      $ctx->{requestor}, 1);
64         $runner->insert( "$evt.titleDescriptor", $ctx->{titleDescriptor}, 1);
65
66         $runner->insert( "$evt.patronItemsOut", $ctx->{patronItemsOut}, 1 );
67         $runner->insert( "$evt.patronOverdueCount", $ctx->{patronOverdue}, 1 );
68         $runner->insert( "$evt.patronFines", $ctx->{patronFines}, 1 );
69
70         $runner->insert("$evt.$_", $ctx->{_direct}->{$_}) for keys %{$ctx->{_direct}};
71
72         $ctx->{runner} = $runner;
73         return $runner;
74 }
75
76 sub fetch_bib_data {
77         my $e = shift;
78         my $ctx = shift;
79
80         if(!$ctx->{copy}) {
81
82                 if($ctx->{copy_id}) {
83                         $ctx->{copy} = $e->retrieve_asset_copy($ctx->{copy_id})
84                                 or return $e->event;
85
86                 } elsif( $ctx->{copy_barcode} ) {
87
88                         $ctx->{copy} = $e->search_asset_copy(
89                                 {barcode => $ctx->{copy_barcode}}) or return $e->event;
90                         $ctx->{copy} = $ctx->{copy}->[0];
91                 }
92         }
93
94         return undef unless my $copy = $ctx->{copy};
95
96         # --------------------------------------------------------------------
97         # Fetch/Cache the copy status and location objects
98         # --------------------------------------------------------------------
99         if(!@COPY_STATUSES) {
100                 my $s = $e->retrieve_all_config_copy_status();
101                 @COPY_STATUSES = @$s;
102                 $s = $e->retrieve_all_asset_copy_location();
103                 @COPY_LOCATIONS = @$s;
104         }
105
106         # Flesh the status and location
107         $copy->status( 
108                 grep { $_->id == $copy->status } @COPY_STATUSES ) 
109                 unless ref $copy->status;
110
111         $copy->location( 
112                 grep { $_->id == $copy->location } @COPY_LOCATIONS ) 
113                 unless ref $copy->location;
114
115         $copy->circ_lib( 
116                 $e->retrieve_actor_org_unit($copy->circ_lib)) 
117                 unless ref $copy->circ_lib;
118
119         $ctx->{volume} = $e->retrieve_asset_call_number(
120                 $ctx->{copy}->call_number) or return $e->event;
121
122         $ctx->{title} = $e->retrieve_biblio_record_entry(
123                 $ctx->{volume}->record) or return $e->event;
124
125         if(!$ctx->{titleDescriptor}) {
126                 $ctx->{titleDescriptor} = $e->search_metabib_record_descriptor( 
127                         { record => $ctx->{title}->id }) or return $e->event;
128
129                 $ctx->{titleDescriptor} = $ctx->{titleDescriptor}->[0];
130         }
131
132         return undef;
133 }
134
135
136
137 sub fetch_user_data {
138         my( $e, $ctx ) = @_;
139         
140         if(!$ctx->{patron}) {
141
142                 if( $ctx->{patron_id} ) {
143                         $ctx->{patron} = $e->retrieve_actor_user($ctx->{patron_id});
144
145                 } elsif( $ctx->{patron_barcode} ) {
146
147                         my $card = $e->search_actor_card( 
148                                 { barcode => $ctx->{patron_barcode} } ) or return $e->event;
149
150                         $ctx->{patron} = $e->search_actor_user( 
151                                 { card => $card->[0]->id }) or return $e->event;
152                         $ctx->{patron} = $ctx->{patron}->[0];
153                 }
154         }
155
156         return undef unless my $patron = $ctx->{patron};
157
158         $patron->home_ou( 
159                 $e->retrieve_actor_org_unit($patron->home_ou) ) 
160                 unless ref $patron->home_ou;
161
162
163         if(!%GROUP_SET) {
164                 $GROUP_TREE = $e->search_permission_grp_tree(
165                         [
166                                 { parent => undef }, 
167                                 { 
168                                         flesh => 100,
169                                         flesh_fields => { pgt => ['children'] }
170                                 } 
171                         ]
172                 )->[0];
173
174                 _flatten_groups($GROUP_TREE);
175         }
176
177         $patron->profile( $GROUP_SET{$patron->profile} )
178                 unless ref $patron->profile;
179
180 #       $patron->profile( 
181 #               grep { $_->id == $patron->profile } @GROUP_LIST ) 
182 #               unless ref $patron->profile;
183
184         $patron->card($e->retrieve_actor_card($patron->card));
185
186         $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
187
188         # this could alter the requestor object within the editor..
189         #if( my $req = $ctx->{requestor} ) {
190         #       $req->home_ou( $e->retrieve_actor_org_unit($requestor->home_ou) );      
191         #       $req->ws_ou( $e->retrieve_actor_org_unit($requestor->ws_ou) );  
192         #}
193
194         if( $ctx->{fetch_patron_circ_info} ) {
195
196                 my $circ_counts = 
197                         OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
198
199                 $ctx->{patronOverdue} = $circ_counts->{overdue} || 0;
200                 $ctx->{patronItemsOut} = $ctx->{patronOverdue} + $circ_counts->{out};
201
202                 # Grab the fines
203                 my $fxacts = $e->search_money_open_billable_transaction_summary(
204                         { usr => $patron->id, balance_owed => { ">" => 0 } });
205
206                 my $fines = 0;
207                 $fines += $_->balance_owed for @$fxacts;
208                 $ctx->{patronFines} = $fines;
209
210                 $logger->debug("script_builder: patron fines determined to be $fines");
211                 $logger->debug("script_builder: patron overdue count is " . $ctx->{patronOverdue});
212         }
213
214         return undef;
215 }
216
217
218 sub _flatten_groups {
219         my $tree = shift;
220         return undef unless $tree;
221         $GROUP_SET{$tree->id} = $tree;
222         if( $tree->children ) {
223                 _flatten_groups($_) for @{$tree->children};
224         }
225 }
226
227
228 1;
229
230